1 19 20 package org.netbeans.lib.editor.util; 21 22 28 29 public final class CharSequenceUtilities { 30 31 private CharSequenceUtilities() { 32 } 34 35 41 public static int stringLikeHashCode(CharSequence text) { 42 int len = text.length(); 43 44 int h = 0; 45 for (int i = 0; i < len; i++) { 46 h = 31 * h + text.charAt(i); 47 } 48 return h; 49 } 50 51 63 public static boolean equals(CharSequence text, Object o) { 64 if (text == o) { 65 return true; 66 } 67 68 if (o instanceof CharSequence ) { CharSequence text2 = (CharSequence )o; 70 int len = text.length(); 71 if (len == text2.length()) { 72 for (int i = len - 1; i >= 0; i--) { 73 if (text.charAt(i) != text2.charAt(i)) { 74 return false; 75 } 76 } 77 return true; 78 } 79 } 80 return false; 81 } 82 83 96 public static boolean textEquals(CharSequence text1, CharSequence text2) { 97 if (text1 == text2) { 98 return true; 99 } 100 int len = text1.length(); 101 if (len == text2.length()) { 102 for (int i = len - 1; i >= 0; i--) { 103 if (text1.charAt(i) != text2.charAt(i)) { 104 return false; 105 } 106 } 107 return true; 108 } 109 return false; 110 } 111 112 124 public static String toString(CharSequence text) { 125 StringBuilder sb = new StringBuilder (text.length()); 126 sb.append(text); 127 return sb.toString(); 128 } 129 130 139 public static String toString(CharSequence text, int start, int end) { 140 checkIndexesValid(text, start, end); 141 StringBuilder sb = new StringBuilder (end - start); 142 sb.append(text, start, end); 143 return sb.toString(); 144 } 145 146 152 public static void append(StringBuffer sb, CharSequence text) { 153 sb.append(text); } 155 156 162 public static void append(StringBuffer sb, CharSequence text, int start, int end) { 163 checkIndexesValid(text, start, end); 164 while (start < end) { 165 sb.append(text.charAt(start++)); 166 } 167 } 168 169 172 public static int indexOf(CharSequence text, int ch) { 173 return indexOf(text, ch, 0); 174 } 175 176 179 public static int indexOf(CharSequence text, int ch, int fromIndex) { 180 int length = text.length(); 181 while (fromIndex < length) { 182 if (text.charAt(fromIndex) == ch) { 183 return fromIndex; 184 } 185 fromIndex++; 186 } 187 return -1; 188 } 189 190 193 public static int indexOf(CharSequence text, CharSequence seq) { 194 return indexOf(text, seq, 0); 195 } 196 197 200 public static int indexOf(CharSequence text, CharSequence seq, int fromIndex) { 201 int textLength = text.length(); 202 int seqLength = seq.length(); 203 if (fromIndex >= textLength) { 204 return (seqLength == 0 ? textLength : -1); 205 } 206 if (fromIndex < 0) { 207 fromIndex = 0; 208 } 209 if (seqLength == 0) { 210 return fromIndex; 211 } 212 213 char first = seq.charAt(0); 214 int max = textLength - seqLength; 215 216 for (int i = fromIndex; i <= max; i++) { 217 if (text.charAt(i) != first) { 219 while (++i <= max && text.charAt(i) != first); 220 } 221 222 if (i <= max) { 224 int j = i + 1; 225 int end = j + seqLength - 1; 226 for (int k = 1; j < end && text.charAt(j) == seq.charAt(k); j++, k++); 227 if (j == end) { 228 return i; 230 } 231 } 232 } 233 return -1; 234 } 235 236 239 public static int lastIndexOf(CharSequence text, CharSequence seq) { 240 return lastIndexOf(text, seq, text.length()); 241 } 242 243 246 public static int lastIndexOf(CharSequence text, CharSequence seq, int fromIndex) { 247 int textLength = text.length(); 248 int seqLength = seq.length(); 249 int rightIndex = textLength - seqLength; 250 if (fromIndex < 0) { 251 return -1; 252 } 253 if (fromIndex > rightIndex) { 254 fromIndex = rightIndex; 255 } 256 if (seqLength == 0) { 258 return fromIndex; 259 } 260 261 int strLastIndex = seqLength - 1; 262 char strLastChar = seq.charAt(strLastIndex); 263 int min = seqLength - 1; 264 int i = min + fromIndex; 265 266 startSearchForLastChar: 267 while (true) { 268 while (i >= min && text.charAt(i) != strLastChar) { 269 i--; 270 } 271 272 if (i < min) { 273 return -1; 274 } 275 int j = i - 1; 276 int start = j - (seqLength - 1); 277 int k = strLastIndex - 1; 278 279 while (j > start) { 280 if (text.charAt(j--) != seq.charAt(k--)) { 281 i--; 282 continue startSearchForLastChar; 283 } 284 } 285 return start + 1; 286 } 287 } 288 289 292 public static int lastIndexOf(CharSequence text, int ch) { 293 return lastIndexOf(text, ch, text.length() - 1); 294 } 295 296 299 public static int lastIndexOf(CharSequence text, int ch, int fromIndex) { 300 if (fromIndex > text.length() - 1) { 301 fromIndex = text.length() - 1; 302 } 303 while (fromIndex >= 0) { 304 if (text.charAt(fromIndex) == ch) { 305 return fromIndex; 306 } 307 fromIndex--; 308 } 309 return -1; 310 } 311 312 315 public static boolean startsWith(CharSequence text, CharSequence prefix) { 316 int p_length = prefix.length(); 317 if (p_length > text.length()) { 318 return false; 319 } 320 for (int x = 0; x < p_length; x++) { 321 if (text.charAt(x) != prefix.charAt(x)) 322 return false; 323 } 324 return true; 325 } 326 327 330 public static boolean endsWith(CharSequence text, CharSequence suffix) { 331 int s_length = suffix.length(); 332 int text_length = text.length(); 333 if (s_length > text_length) { 334 return false; 335 } 336 for (int x = 0; x < s_length; x++) { 337 if (text.charAt(text_length - s_length + x) != suffix.charAt(x)) 338 return false; 339 } 340 return true; 341 } 342 343 346 public static CharSequence trim(CharSequence text) { 347 int length = text.length(); 348 if (length == 0) 349 return text; 350 int start = 0; 351 int end = length - 1; 352 while (start < length && text.charAt(start) <= ' ') { 353 start++; 354 } 355 if (start == length) 356 return text.subSequence(0, 0); 357 while (end > start && text.charAt(end) <= ' ') { 358 end--; 359 } 360 return text.subSequence(start, end + 1); 361 } 362 363 370 public static void debugChar(StringBuffer sb, char ch) { 371 switch (ch) { 372 case '\n': 373 sb.append("\\n"); 374 break; 375 case '\r': 376 sb.append("\\r"); 377 break; 378 case '\t': 379 sb.append("\\t"); 380 break; 381 case '\b': 382 sb.append("\\b"); 383 break; 384 case '\f': 385 sb.append("\\f"); 386 break; 387 case '\\': 388 sb.append("\\\\"); 389 break; 390 default: 391 sb.append(ch); 392 break; 393 } 394 } 395 396 403 public static void debugChar(StringBuilder sb, char ch) { 404 switch (ch) { 405 case '\n': 406 sb.append("\\n"); 407 break; 408 case '\r': 409 sb.append("\\r"); 410 break; 411 case '\t': 412 sb.append("\\t"); 413 break; 414 case '\b': 415 sb.append("\\b"); 416 break; 417 case '\f': 418 sb.append("\\f"); 419 break; 420 case '\\': 421 sb.append("\\\\"); 422 break; 423 default: 424 sb.append(ch); 425 break; 426 } 427 } 428 429 436 public static String debugChar(char ch) { 437 StringBuilder sb = new StringBuilder (); 438 debugChar(sb, ch); 439 return sb.toString(); 440 } 441 442 449 public static void debugText(StringBuffer sb, CharSequence text) { 450 for (int i = 0; i < text.length(); i++) { 451 debugChar(sb, text.charAt(i)); 452 } 453 } 454 455 462 public static void debugText(StringBuilder sb, CharSequence text) { 463 for (int i = 0; i < text.length(); i++) { 464 debugChar(sb, text.charAt(i)); 465 } 466 } 467 468 475 public static String debugText(CharSequence text) { 476 StringBuilder sb = new StringBuilder (); 477 debugText(sb, text); 478 return sb.toString(); 479 } 480 481 485 public static void checkIndexNonNegative(int index) { 486 if (index < 0) { 487 throw new IndexOutOfBoundsException ("index=" + index + " < 0"); 488 } 489 } 490 491 495 public static void checkIndexValid(int index, int length) { 496 checkIndexNonNegative(index); 497 if (index >= length) { 498 throw new IndexOutOfBoundsException ("index=" + index 499 + " >= length()=" + length); 500 } 501 } 502 503 509 public static void checkIndexesValid(CharSequence text, int start, int end) { 510 if (start < 0) { 511 throw new IndexOutOfBoundsException ("start=" + start + " < 0"); 512 } 513 if (end < start) { 514 throw new IndexOutOfBoundsException ("end=" + end + " < start=" + start); 515 } 516 if (end > text.length()) { 517 throw new IndexOutOfBoundsException ("end=" + end 518 + " > text.length()=" + text.length()); 519 } 520 } 521 522 523 } 524 | Popular Tags |