1 30 31 package rcm.util; 32 33 import java.util.StringTokenizer ; 34 35 38 public abstract class Str { 39 40 47 public static int indexOfAnyChar (String subject, String chars) { 48 return indexOfAnyChar (subject, chars, 0); 49 } 50 51 60 public static int indexOfAnyChar (String subject, String chars, int start) { 61 for (int i=start; i<subject.length(); ++i) 62 if (chars.indexOf (subject.charAt (i)) != -1) 63 return i; 64 return -1; 65 } 66 67 74 public static String replace (String subject, String original, String replacement) { 75 StringBuffer output = new StringBuffer (); 76 77 int p = 0; 78 int i; 79 while ((i = subject.indexOf (original, p)) != -1) { 80 output.append (subject.substring (p, i)); 81 output.append (replacement); 82 p = i + original.length(); 83 } 84 if (p < subject.length ()) 85 output.append (subject.substring(p)); 86 return output.toString (); 87 } 88 89 96 public static String escape (String subject, char escapeChar, String metachars) { 97 return escape (subject, metachars, escapeChar, metachars); 98 } 99 100 111 public static String escape (String subject, String chars, char escapeChar, String metachars) { 112 StringBuffer output = new StringBuffer (); 113 114 int p = 0; 115 int i; 116 while ((i = indexOfAnyChar (subject, chars, p)) != -1) { 117 output.append (subject.substring (p, i)); 118 119 char c = subject.charAt (i); int k = chars.indexOf (c); 121 char metac = metachars.charAt (k); output.append (escapeChar); 123 output.append (metac); 124 125 p = i + 1; 126 } 127 if (p < subject.length ()) 128 output.append (subject.substring(p)); 129 return output.toString (); 130 } 131 132 148 public static String unescape (String subject, char escapeChar, String metachars, String chars, boolean keepUntranslatedEscapes) { 149 StringBuffer output = new StringBuffer (); 150 151 int p = 0; 152 int i; 153 int len = subject.length (); 154 while ((i = subject.indexOf (escapeChar, p)) != -1) { 155 output.append (subject.substring (p, i)); 156 if (i + 1 == len) 157 break; 158 159 char metac = subject.charAt (i+1); int k = metachars.indexOf (metac); 161 if (k == -1) { 162 if (keepUntranslatedEscapes) 164 output.append (escapeChar); 165 output.append (metac); 166 } 167 else 168 output.append (chars.charAt (k)); 170 p = i + 2; } 172 173 if (p < len) 174 output.append (subject.substring(p)); 175 return output.toString (); 176 } 177 178 186 private static final int INT = 0; 187 private static final int FRAC = 1; 188 private static final int EXP = 2; 189 public static Number parseNumber (String s) throws NumberFormatException { 190 int p = 0; 191 for (int i=0; i<s.length(); ++i) { 192 char c = s.charAt (i); 193 if (Character.isDigit (c)) { 194 int start = i; 195 int end = ++i; 196 int state = INT; 197 198 if (start > 0 && s.charAt (start-1) == '.') { 199 --start; 200 state = FRAC; 201 } 202 if (start > 0 && s.charAt (start-1) == '-') 203 --start; 204 205 foundEnd: 206 while (i < s.length()) { 207 switch (s.charAt (i)) { 208 case '0': case '1': case '2': case '3': case '4': 209 case '5': case '6': case '7': case '8': case '9': 210 end = ++i; 211 break; 212 case '.': 213 if (state != INT) 214 break foundEnd; 215 state = FRAC; 216 ++i; 217 break; 218 case ',': ++i; 220 break; 221 case 'e': 222 case 'E': 223 state = EXP; 224 ++i; 225 if (i < s.length() && 226 ( (c = s.charAt (i)) == '+' || c == '-') ) 227 ++i; 228 break; 229 default: 230 break foundEnd; 231 } 232 } 233 234 String num = s.substring (start, end); 235 num = replace (num, ",", ""); 236 try { 237 if (state == INT) 238 return new Integer (num); 239 else 240 return new Float (num); 241 } catch (NumberFormatException e) { 242 throw new RuntimeException ("internal error: " + e); 243 } 244 } 245 } 246 throw new NumberFormatException (s); 247 } 248 256 257 258 264 public static String repeat (String s, int n) { 265 StringBuffer out = new StringBuffer (); 266 while (--n >= 0) 267 out.append (s); 268 return out.toString (); 269 } 270 271 277 public static String compressWhitespace (String s) { 278 StringBuffer output = new StringBuffer (); 279 int p = 0; 280 boolean inSpace = true; 281 for (int i = 0, len = s.length (); i < len; ++i) { 282 if (Character.isWhitespace (s.charAt (i))) { 283 if (!inSpace) { 284 output.append (s.substring (p, i)); 285 output.append (' '); 286 inSpace = true; 287 } 288 } 289 else { 290 if (inSpace) { 291 p = i; 292 inSpace = false; 293 } 294 } 295 } 296 if (!inSpace) 297 output.append (s.substring (p)); 298 return output.toString (); 299 } 300 301 307 public static boolean isWhitespace (String s) { 308 for (int i = 0, n = s.length (); i < n; ++i) 309 if (!Character.isWhitespace (s.charAt (i))) 310 return false; 311 return true; 312 } 313 314 320 public static String join (String [] list, String sep) { 321 StringBuffer result = new StringBuffer (); 322 for (int i=0; i < list.length; ++i) { 323 if (i > 0) 324 result.append (sep); 325 result.append (list[i]); 326 } 327 return result.toString (); 328 } 329 330 337 public static String abbreviate (String s, int max) { 338 s = compressWhitespace (s); 339 if (s.length() < max) 340 return s; 341 else { 342 max = Math.max (max-3, 2); int half = max/2; 344 return s.substring (0, half) + "..." + s.substring (s.length()-half); 345 } 346 } 347 348 358 public static String abbreviateLines (String s, int maxLines, String message) { 359 int nLines = countLines (s); 360 if (nLines < maxLines) 361 return s; 362 else { 363 maxLines = Math.max (maxLines-1, 2); int half = maxLines/2; 365 return s.substring (0, nthLine (s, half)) 366 + replace (message, "%d", String.valueOf (nLines - half*2)) 367 + s.substring (nthLine (s, -half)); 368 } 369 } 370 371 static int countLines (String s) { 372 int n = 1; 373 int i = -1; 374 while ((i = s.indexOf ('\n', i+1)) != -1) 375 ++n; 376 return n; 377 } 378 static int nthLine (String s, int n) { 379 if (n >= 0) { 380 int i = -1; 381 while (n > 0 && (i = s.indexOf ('\n', i+1)) != -1) 382 --n; 383 return i+1; 384 } else { 385 int i = s.length (); 386 while (n < 0 && (i = s.lastIndexOf ('\n', i-1)) != -1) 387 ++n; 388 return i+1; 389 } 390 } 391 392 399 public static String before (String s, String pat) { 400 int i = s.indexOf (pat); 401 return (i >= 0) ? s.substring(0, i) : s; 402 } 403 404 411 public static String after (String s, String pat) { 412 int i = s.indexOf (pat); 413 return (i >= 0) ? s.substring(i + pat.length ()) : ""; 414 } 415 416 417 420 public static boolean startsWithIgnoreCase (String s, String prefix) { 421 int sLen = s.length (); 422 int prefixLen = prefix.length (); 423 return (sLen >= prefixLen 424 && s.substring (0, prefixLen).equalsIgnoreCase (prefix)); 425 } 426 427 430 public static boolean endsWithIgnoreCase (String s, String suffix) { 431 int sLen = s.length (); 432 int suffixLen = suffix.length (); 433 return (sLen >= suffixLen 434 && s.substring (sLen - suffixLen).equalsIgnoreCase (suffix)); 435 } 436 437 440 public static String untabify (String s, int tabsize) { 441 if (s.indexOf ('\t') == -1) 442 return s; 444 int col = 0; 445 StringBuffer result = new StringBuffer (); 446 for (StringTokenizer tokenizer = new StringTokenizer (s, "\t\r\n", true); 447 tokenizer.hasMoreTokens (); ) { 448 String tok = tokenizer.nextToken (); 449 switch (tok.charAt (0)) { 450 case '\t': 451 { 452 int oldcol = col; 453 col = (col/tabsize + 1) * tabsize; 454 result.append (Str.repeat (" ", col - oldcol)); 455 } 456 break; 457 case '\r': 458 case '\n': 459 col = 0; 460 result.append (tok); 461 break; 462 default: 463 col += tok.length (); 464 result.append (tok); 465 break; 466 } 467 } 468 469 return result.toString (); 470 } 471 472 477 public static String reverse (String s) { 478 StringBuffer t = new StringBuffer (s.length ()); 479 for (int i = s.length () - 1; i >= 0; --i) 480 t.append (s.charAt(i)); 481 return t.toString (); 482 } 483 484 487 public static String longestCommonPrefix (String s, String t) { 488 return s.substring (0, longestCommonPrefixLength (s, t)); 489 } 490 491 public static int longestCommonPrefixLength (String s, String t) { 492 int m = Math.min (s.length (), t.length()); 493 for (int k = 0; k < m; ++k) 494 if (s.charAt (k) != t.charAt (k)) 495 return k; 496 return m; 497 } 498 499 502 public static String longestCommonSuffix (String s, String t) { 503 return s.substring (s.length () - longestCommonSuffixLength (s, t)); 504 } 505 506 public static int longestCommonSuffixLength (String s, String t) { 507 int i = s.length ()-1; 508 int j = t.length ()-1; 509 for (; i >= 0 && j >= 0; --i, --j) 510 if (s.charAt (i) != t.charAt (j)) 511 return s.length () - (i+1); 512 return s.length () - (i+1); 513 } 514 515 516 517 518 521 public static String longestCommonPrefixIgnoreCase (String s, String t) { 522 return s.substring (0, longestCommonPrefixLengthIgnoreCase (s, t)); 523 } 524 525 public static int longestCommonPrefixLengthIgnoreCase (String s, String t) { 526 int m = Math.min (s.length (), t.length()); 527 for (int k = 0; k < m; ++k) 528 if (Character.toLowerCase (s.charAt (k)) != Character.toLowerCase (t.charAt (k))) 529 return k; 530 return m; 531 } 532 533 536 public static String longestCommonSuffixIgnoreCase (String s, String t) { 537 return s.substring (s.length () - longestCommonSuffixLengthIgnoreCase (s, t)); 538 } 539 540 public static int longestCommonSuffixLengthIgnoreCase (String s, String t) { 541 int i = s.length ()-1; 542 int j = t.length ()-1; 543 for (; i >= 0 && j >= 0; --i, --j) 544 if (Character.toLowerCase (s.charAt (i)) != Character.toLowerCase (t.charAt (j))) 545 return s.length () - (i+1); 546 return s.length () - (i+1); 547 } 548 } 549 | Popular Tags |