1 33 34 package edu.rice.cs.util; 35 36 import edu.rice.cs.plt.tuple.Pair; 37 import java.io.StringWriter ; 38 import java.io.PrintWriter ; 39 import java.text.DecimalFormat ; 40 41 47 48 public abstract class StringOps { 49 53 public static String replace (String fullString, String toReplace, String replacement) { 54 int index = 0; 55 int pos; 56 int fullStringLength = fullString.length(); 57 int toReplaceLength = toReplace.length(); 58 if (toReplaceLength > 0) { 59 int replacementLength = replacement.length(); 60 StringBuilder buff; 61 while (index < fullStringLength && 62 ((pos = fullString.indexOf(toReplace, index)) >= 0)) { 63 buff = new StringBuilder (fullString.substring(0, pos)); 64 buff.append(replacement); 65 buff.append(fullString.substring(pos + toReplaceLength, fullStringLength)); 66 index = pos + replacementLength; 67 fullString = buff.toString(); 68 fullStringLength = fullString.length(); 69 } 70 } 71 return fullString; 72 } 73 74 82 public static String convertToLiteral(String s) { 83 String output = s; 84 output = replace(output, "\\", "\\\\"); output = replace(output, "\"", "\\\""); output = replace(output, "\t", "\\t"); output = replace(output, "\n", "\\n"); return "\"" + output + "\""; 89 } 90 91 95 private static void _ensureStartBeforeEnd(int startRow, int startCol, 96 int endRow, int endCol) { 97 if (startRow > endRow) { 98 throw new IllegalArgumentException ("end row before start row: " + 99 startRow + " > " + endRow); 100 } 101 else if (startRow == endRow && startCol > endCol) { 102 throw new IllegalArgumentException ("end before start: (" + 103 startRow + ", " + startCol + 104 ") > (" + endRow + ", " + endCol + ")"); 105 } 106 } 107 108 116 private static void _ensureColInRow(String fullString, int col, int rowStartIndex) { 117 int endOfLine = fullString.indexOf("\n",rowStartIndex); 118 if (endOfLine == -1) { 119 endOfLine = fullString.length(); 120 } 121 if (col > (endOfLine - rowStartIndex)) { 122 throw new IllegalArgumentException ("the given column is past the end of its row"); 123 } 124 } 125 126 136 public static Pair<Integer ,Integer > getOffsetAndLength(String fullString, int startRow, 137 int startCol, int endRow, int endCol) { 138 _ensureStartBeforeEnd(startRow, startCol, endRow, endCol); 139 140 int currentChar = 0; 142 int linesSeen = 1; 143 while (startRow > linesSeen) { 144 currentChar = fullString.indexOf("\n",currentChar); 145 if (currentChar == -1) { 146 throw new IllegalArgumentException ("startRow is beyond the end of the string"); 147 } 148 currentChar++; 150 linesSeen++; 151 } 152 153 _ensureColInRow(fullString, startCol, currentChar); 154 int offset = currentChar + startCol - 1; 156 while (endRow > linesSeen) { 158 currentChar = fullString.indexOf("\n",currentChar); 159 if (currentChar == -1) { 160 throw new IllegalArgumentException ("endRow is beyond the end of the string"); 161 } 162 currentChar++; 163 linesSeen++; 164 } 165 166 _ensureColInRow(fullString, endCol, currentChar); 167 int length = currentChar + endCol - offset; 168 169 if (offset + length > fullString.length()) { 171 throw new IllegalArgumentException ("Given positions beyond the end of the string"); 172 } 173 return new Pair<Integer ,Integer >(new Integer (offset), new Integer (length)); 174 } 175 176 181 public static String getStackTrace(Throwable t) { 182 StringWriter sw = new StringWriter (); 183 PrintWriter pw = new PrintWriter (sw); 184 t.printStackTrace(pw); 185 return sw.toString(); 186 } 187 188 192 public static String getStackTrace() { 193 try { throw new Exception (); } catch (Exception e) { 195 StringWriter sw = new StringWriter (); 196 PrintWriter pw = new PrintWriter (sw); 197 StackTraceElement [] stes = e.getStackTrace(); 198 int skip = 1; 199 for(StackTraceElement ste: stes) { 200 if (skip>0) { --skip; } else { pw.print("at "); pw.println(ste); } 201 } 202 return sw.toString(); 203 } 204 } 205 206 210 public static boolean isAsciiDigit(char c) { 211 return '0' <= c && c <= '9'; 212 } 213 214 220 public static boolean isAnonymousClass(Class c) { 221 String simpleName = c.getName(); 222 int idx = simpleName.lastIndexOf('$'); 223 if (idx >= 0) { 224 for (int pos=idx+1; pos < simpleName.length(); ++pos) { 226 if (!isAsciiDigit(simpleName.charAt(pos))) { 227 return false; 228 } 229 } 230 return true; 231 } 232 return false; 233 } 234 235 241 public static boolean isMemberClass(Class c) { 242 String simpleName = c.getName(); 243 int idx = simpleName.lastIndexOf('$'); 244 if (idx == -1) { 245 return false; 246 } 247 return !isAnonymousClass(c); 248 } 249 250 256 public static String getSimpleName(Class c) { 257 if (c.isArray()) 258 return getSimpleName(c.getComponentType())+"[]"; 259 260 if (isAnonymousClass(c)) { 261 return ""; 262 } 263 264 String simpleName = c.getName(); 265 int idx = Math.max(simpleName.lastIndexOf('.'), 266 simpleName.lastIndexOf('$')); 267 return simpleName.substring(idx + 1); } 269 270 273 public static String toString(long[] a) { 274 if (a == null) 275 return "null"; 276 if (a.length == 0) 277 return "[]"; 278 279 final StringBuilder buf = new StringBuilder (); 280 buf.append('['); 281 buf.append(a[0]); 282 283 for (int i = 1; i < a.length; i++) { 284 buf.append(", "); 285 buf.append(a[i]); 286 } 287 288 buf.append("]"); 289 return buf.toString(); 290 } 291 292 295 public static String toString(int[] a) { 296 if (a == null) 297 return "null"; 298 if (a.length == 0) 299 return "[]"; 300 301 final StringBuilder buf = new StringBuilder (); 302 buf.append('['); 303 buf.append(a[0]); 304 305 for (int i = 1; i < a.length; i++) { 306 buf.append(", "); 307 buf.append(a[i]); 308 } 309 310 buf.append("]"); 311 return buf.toString(); 312 } 313 314 317 public static String toString(short[] a) { 318 if (a == null) 319 return "null"; 320 if (a.length == 0) 321 return "[]"; 322 323 final StringBuilder buf = new StringBuilder (); 324 buf.append('['); 325 buf.append(a[0]); 326 327 for (int i = 1; i < a.length; i++) { 328 buf.append(", "); 329 buf.append(a[i]); 330 } 331 332 buf.append("]"); 333 return buf.toString(); 334 } 335 336 339 public static String toString(char[] a) { 340 if (a == null) 341 return "null"; 342 if (a.length == 0) 343 return "[]"; 344 345 final StringBuilder buf = new StringBuilder (); 346 buf.append('['); 347 buf.append(a[0]); 348 349 for (int i = 1; i < a.length; i++) { 350 buf.append(", "); 351 buf.append(a[i]); 352 } 353 354 buf.append("]"); 355 return buf.toString(); 356 } 357 358 361 public static String toString(byte[] a) { 362 if (a == null) 363 return "null"; 364 if (a.length == 0) 365 return "[]"; 366 367 final StringBuilder buf = new StringBuilder (); 368 buf.append('['); 369 buf.append(a[0]); 370 371 for (int i = 1; i < a.length; i++) { 372 buf.append(", "); 373 buf.append(a[i]); 374 } 375 376 buf.append("]"); 377 return buf.toString(); 378 } 379 380 383 public static String toString(boolean[] a) { 384 if (a == null) 385 return "null"; 386 if (a.length == 0) 387 return "[]"; 388 389 final StringBuilder buf = new StringBuilder (); 390 buf.append('['); 391 buf.append(a[0]); 392 393 for (int i = 1; i < a.length; i++) { 394 buf.append(", "); 395 buf.append(a[i]); 396 } 397 398 buf.append("]"); 399 return buf.toString(); 400 } 401 402 405 public static String toString(float[] a) { 406 if (a == null) 407 return "null"; 408 if (a.length == 0) 409 return "[]"; 410 411 final StringBuilder buf = new StringBuilder (); 412 buf.append('['); 413 buf.append(a[0]); 414 415 for (int i = 1; i < a.length; i++) { 416 buf.append(", "); 417 buf.append(a[i]); 418 } 419 420 buf.append("]"); 421 return buf.toString(); 422 } 423 424 427 public static String toString(double[] a) { 428 if (a == null) 429 return "null"; 430 if (a.length == 0) 431 return "[]"; 432 433 final StringBuilder buf = new StringBuilder (); 434 buf.append('['); 435 buf.append(a[0]); 436 437 for (int i = 1; i < a.length; i++) { 438 buf.append(", "); 439 buf.append(a[i]); 440 } 441 442 buf.append("]"); 443 return buf.toString(); 444 } 445 446 449 public static String toString(Object [] a) { 450 if (a == null) 451 return "null"; 452 if (a.length == 0) 453 return "[]"; 454 455 final StringBuilder buf = new StringBuilder (); 456 457 for (int i = 0; i < a.length; i++) { 458 if (i == 0) 459 buf.append('['); 460 else 461 buf.append(", "); 462 463 buf.append(String.valueOf(a[i])); 464 } 465 466 buf.append("]"); 467 return buf.toString(); 468 } 469 470 475 public static String encodeHTML(String s) { 476 s = StringOps.replace(s, "&", "&"); 477 s = StringOps.replace(s, "<", "<"); 478 s = StringOps.replace(s, ">", ">"); 479 s = StringOps.replace(s, System.getProperty("line.separator"),"<br>"); 480 s = StringOps.replace(s, "\n","<br>"); 481 return s; 482 } 483 484 489 public static String memSizeToString(long l) { 490 String [] sizes = new String [] { "byte", "kilobyte", "megabyte", "gigabyte" }; 491 double d = l; 492 int i = 0; 493 while((d >= 1024) && (i<sizes.length)) { 494 ++i; 495 d /= 1024; 496 } 497 if (i>=sizes.length) { i = sizes.length-1; } 498 StringBuilder sb = new StringBuilder (); 499 long whole = (long)d; 500 if (whole==d) { 501 if (whole==1) { 502 sb.append(whole); 503 sb.append(' '); 504 sb.append(sizes[i]); 505 } 506 else { 507 sb.append(whole); 508 sb.append(' '); 509 sb.append(sizes[i]); 510 sb.append('s'); 511 } 512 } 513 else { 514 DecimalFormat df = new DecimalFormat ("#.00"); 516 sb.append(df.format(d)); 517 sb.append(' '); 518 sb.append(sizes[i]); 519 sb.append('s'); 520 } 521 return sb.toString(); 522 } 523 } 524 | Popular Tags |