1 12 package org.eclipse.jdt.core.compiler; 13 14 import org.eclipse.jdt.internal.compiler.parser.ScannerHelper; 15 16 24 public final class CharOperation { 25 26 29 public static final char[] NO_CHAR = new char[0]; 30 31 34 public static final char[][] NO_CHAR_CHAR = new char[0][]; 35 36 40 public static final String [] NO_STRINGS = new String [0]; 41 42 65 public static final char[] append(char[] array, char suffix) { 66 if (array == null) 67 return new char[] { suffix }; 68 int length = array.length; 69 System.arraycopy(array, 0, array = new char[length + 1], 0, length); 70 array[length] = suffix; 71 return array; 72 } 73 74 117 public static final char[] append(char[] target, int index, char[] array, int start, int end) { 118 int targetLength = target.length; 119 int subLength = end-start; 120 int newTargetLength = subLength+index; 121 if (newTargetLength > targetLength) { 122 System.arraycopy(target, 0, target = new char[newTargetLength*2], 0, index); 123 } 124 System.arraycopy(array, start, target, index, subLength); 125 return target; 126 } 127 128 166 public static final char[][] arrayConcat(char[][] first, char[][] second) { 167 if (first == null) 168 return second; 169 if (second == null) 170 return first; 171 172 int length1 = first.length; 173 int length2 = second.length; 174 char[][] result = new char[length1 + length2][]; 175 System.arraycopy(first, 0, result, 0, length1); 176 System.arraycopy(second, 0, result, length1, length2); 177 return result; 178 } 179 180 234 public static final boolean camelCaseMatch(char[] pattern, char[] name) { 235 if (pattern == null) 236 return true; if (name == null) 238 return false; 240 return camelCaseMatch(pattern, 0, pattern.length, name, 0, name.length); 241 } 242 243 323 public static final boolean camelCaseMatch(char[] pattern, int patternStart, int patternEnd, char[] name, int nameStart, int nameEnd) { 324 if (name == null) 325 return false; if (pattern == null) 327 return true; if (patternEnd < 0) patternEnd = pattern.length; 329 if (nameEnd < 0) nameEnd = name.length; 330 331 if (patternEnd <= patternStart) return nameEnd <= nameStart; 332 if (nameEnd <= nameStart) return false; 333 if (name[nameStart] != pattern[patternStart]) { 335 return false; 337 } 338 339 char patternChar, nameChar; 340 int iPattern = patternStart; 341 int iName = nameStart; 342 343 while (true) { 345 346 iPattern++; 347 iName++; 348 349 if (iPattern == patternEnd) { 350 return true; 352 } 353 354 if (iName == nameEnd){ 355 return false; 357 } 358 359 if ((patternChar = pattern[iPattern]) == name[iName]) { 361 continue; 362 } 363 364 if (patternChar < ScannerHelper.MAX_OBVIOUS) { 366 if ((ScannerHelper.OBVIOUS_IDENT_CHAR_NATURES[patternChar] & ScannerHelper.C_UPPER_LETTER) == 0) { 367 return false; 368 } 369 } 370 else if (Character.isJavaIdentifierPart(patternChar) && !Character.isUpperCase(patternChar)) { 371 return false; 372 } 373 374 while (true) { 376 if (iName == nameEnd){ 377 return false; 379 } 380 381 nameChar = name[iName]; 382 if (nameChar < ScannerHelper.MAX_OBVIOUS) { 383 if ((ScannerHelper.OBVIOUS_IDENT_CHAR_NATURES[nameChar] & (ScannerHelper.C_LOWER_LETTER | ScannerHelper.C_SPECIAL | ScannerHelper.C_DIGIT)) != 0) { 384 iName++; 386 } else if (patternChar != nameChar) { 388 return false; 390 } else { 391 break; 393 } 394 } 395 else if (Character.isJavaIdentifierPart(nameChar) && !Character.isUpperCase(nameChar)) { 396 iName++; 398 } else if (patternChar != nameChar) { 400 return false; 402 } else { 403 break; 405 } 406 } 407 } 410 } 411 412 419 public static String [] charArrayToStringArray(char[][] charArrays) { 420 if (charArrays == null) 421 return null; 422 int length = charArrays.length; 423 if (length == 0) 424 return NO_STRINGS; 425 String [] strings= new String [length]; 426 for (int i= 0; i < length; i++) 427 strings[i]= new String (charArrays[i]); 428 return strings; 429 } 430 431 438 public static String charToString(char[] charArray) { 439 if (charArray == null) return null; 440 return new String (charArray); 441 } 442 443 475 public static final char[][] arrayConcat(char[][] first, char[] second) { 476 if (second == null) 477 return first; 478 if (first == null) 479 return new char[][] { second }; 480 481 int length = first.length; 482 char[][] result = new char[length + 1][]; 483 System.arraycopy(first, 0, result, 0, length); 484 result[length] = second; 485 return result; 486 } 487 500 public static final int compareTo(char[] array1, char[] array2) { 501 int length1 = array1.length; 502 int length2 = array2.length; 503 int min = Math.min(length1, length2); 504 for (int i = 0; i < min; i++) { 505 if (array1[i] != array2[i]) { 506 return array1[i] - array2[i]; 507 } 508 } 509 return length1 - length2; 510 } 511 566 public static final int compareWith(char[] array, char[] prefix) { 567 int arrayLength = array.length; 568 int prefixLength = prefix.length; 569 int min = Math.min(arrayLength, prefixLength); 570 int i = 0; 571 while (min-- != 0) { 572 char c1 = array[i]; 573 char c2 = prefix[i++]; 574 if (c1 != c2) 575 return c1 - c2; 576 } 577 if (prefixLength == i) 578 return 0; 579 return -1; } 581 582 614 public static final char[] concat(char[] first, char[] second) { 615 if (first == null) 616 return second; 617 if (second == null) 618 return first; 619 620 int length1 = first.length; 621 int length2 = second.length; 622 char[] result = new char[length1 + length2]; 623 System.arraycopy(first, 0, result, 0, length1); 624 System.arraycopy(second, 0, result, length1, length2); 625 return result; 626 } 627 628 680 public static final char[] concat( 681 char[] first, 682 char[] second, 683 char[] third) { 684 if (first == null) 685 return concat(second, third); 686 if (second == null) 687 return concat(first, third); 688 if (third == null) 689 return concat(first, second); 690 691 int length1 = first.length; 692 int length2 = second.length; 693 int length3 = third.length; 694 char[] result = new char[length1 + length2 + length3]; 695 System.arraycopy(first, 0, result, 0, length1); 696 System.arraycopy(second, 0, result, length1, length2); 697 System.arraycopy(third, 0, result, length1 + length2, length3); 698 return result; 699 } 700 701 739 public static final char[] concat( 740 char[] first, 741 char[] second, 742 char separator) { 743 if (first == null) 744 return second; 745 if (second == null) 746 return first; 747 748 int length1 = first.length; 749 if (length1 == 0) 750 return second; 751 int length2 = second.length; 752 if (length2 == 0) 753 return first; 754 755 char[] result = new char[length1 + length2 + 1]; 756 System.arraycopy(first, 0, result, 0, length1); 757 result[length1] = separator; 758 System.arraycopy(second, 0, result, length1 + 1, length2); 759 return result; 760 } 761 762 822 public static final char[] concat( 823 char[] first, 824 char sep1, 825 char[] second, 826 char sep2, 827 char[] third) { 828 if (first == null) 829 return concat(second, third, sep2); 830 if (second == null) 831 return concat(first, third, sep1); 832 if (third == null) 833 return concat(first, second, sep1); 834 835 int length1 = first.length; 836 int length2 = second.length; 837 int length3 = third.length; 838 char[] result = new char[length1 + length2 + length3 + 2]; 839 System.arraycopy(first, 0, result, 0, length1); 840 result[length1] = sep1; 841 System.arraycopy(second, 0, result, length1 + 1, length2); 842 result[length1 + length2 + 1] = sep2; 843 System.arraycopy(third, 0, result, length1 + length2 + 2, length3); 844 return result; 845 } 846 847 875 public static final char[] concat(char prefix, char[] array, char suffix) { 876 if (array == null) 877 return new char[] { prefix, suffix }; 878 879 int length = array.length; 880 char[] result = new char[length + 2]; 881 result[0] = prefix; 882 System.arraycopy(array, 0, result, 1, length); 883 result[length + 1] = suffix; 884 return result; 885 } 886 887 921 public static final char[] concatWith( 922 char[] name, 923 char[][] array, 924 char separator) { 925 int nameLength = name == null ? 0 : name.length; 926 if (nameLength == 0) 927 return concatWith(array, separator); 928 929 int length = array == null ? 0 : array.length; 930 if (length == 0) 931 return name; 932 933 int size = nameLength; 934 int index = length; 935 while (--index >= 0) 936 if (array[index].length > 0) 937 size += array[index].length + 1; 938 char[] result = new char[size]; 939 index = size; 940 for (int i = length - 1; i >= 0; i--) { 941 int subLength = array[i].length; 942 if (subLength > 0) { 943 index -= subLength; 944 System.arraycopy(array[i], 0, result, index, subLength); 945 result[--index] = separator; 946 } 947 } 948 System.arraycopy(name, 0, result, 0, nameLength); 949 return result; 950 } 951 952 986 public static final char[] concatWith( 987 char[][] array, 988 char[] name, 989 char separator) { 990 int nameLength = name == null ? 0 : name.length; 991 if (nameLength == 0) 992 return concatWith(array, separator); 993 994 int length = array == null ? 0 : array.length; 995 if (length == 0) 996 return name; 997 998 int size = nameLength; 999 int index = length; 1000 while (--index >= 0) 1001 if (array[index].length > 0) 1002 size += array[index].length + 1; 1003 char[] result = new char[size]; 1004 index = 0; 1005 for (int i = 0; i < length; i++) { 1006 int subLength = array[i].length; 1007 if (subLength > 0) { 1008 System.arraycopy(array[i], 0, result, index, subLength); 1009 index += subLength; 1010 result[index++] = separator; 1011 } 1012 } 1013 System.arraycopy(name, 0, result, index, nameLength); 1014 return result; 1015} 1016 1017 1040public static final char[] concatWith(char[][] array, char separator) { 1041 int length = array == null ? 0 : array.length; 1042 if (length == 0) 1043 return CharOperation.NO_CHAR; 1044 1045 int size = length - 1; 1046 int index = length; 1047 while (--index >= 0) { 1048 if (array[index].length == 0) 1049 size--; 1050 else 1051 size += array[index].length; 1052 } 1053 if (size <= 0) 1054 return CharOperation.NO_CHAR; 1055 char[] result = new char[size]; 1056 index = length; 1057 while (--index >= 0) { 1058 length = array[index].length; 1059 if (length > 0) { 1060 System.arraycopy( 1061 array[index], 1062 0, 1063 result, 1064 (size -= length), 1065 length); 1066 if (--size >= 0) 1067 result[size] = separator; 1068 } 1069 } 1070 return result; 1071} 1072 1073 1099public static final boolean contains(char character, char[][] array) { 1100 for (int i = array.length; --i >= 0;) { 1101 char[] subarray = array[i]; 1102 for (int j = subarray.length; --j >= 0;) 1103 if (subarray[j] == character) 1104 return true; 1105 } 1106 return false; 1107} 1108 1109 1135public static final boolean contains(char character, char[] array) { 1136 for (int i = array.length; --i >= 0;) 1137 if (array[i] == character) 1138 return true; 1139 return false; 1140} 1141 1142 1169public static final boolean contains(char[] characters, char[] array) { 1170 for (int i = array.length; --i >= 0;) 1171 for (int j = characters.length; --j >= 0;) 1172 if (array[i] == characters[j]) 1173 return true; 1174 return false; 1175} 1176 1177 1183 1184public static final char[][] deepCopy(char[][] toCopy) { 1185 int toCopyLength = toCopy.length; 1186 char[][] result = new char[toCopyLength][]; 1187 for (int i = 0; i < toCopyLength; i++) { 1188 char[] toElement = toCopy[i]; 1189 int toElementLength = toElement.length; 1190 char[] resultElement = new char[toElementLength]; 1191 System.arraycopy(toElement, 0, resultElement, 0, toElementLength); 1192 result[i] = resultElement; 1193 } 1194 return result; 1195} 1196 1197 1224public static final boolean endsWith(char[] array, char[] toBeFound) { 1225 int i = toBeFound.length; 1226 int j = array.length - i; 1227 1228 if (j < 0) 1229 return false; 1230 while (--i >= 0) 1231 if (toBeFound[i] != array[i + j]) 1232 return false; 1233 return true; 1234} 1235 1236 1272public static final boolean equals(char[][] first, char[][] second) { 1273 if (first == second) 1274 return true; 1275 if (first == null || second == null) 1276 return false; 1277 if (first.length != second.length) 1278 return false; 1279 1280 for (int i = first.length; --i >= 0;) 1281 if (!equals(first[i], second[i])) 1282 return false; 1283 return true; 1284} 1285 1286 1331public static final boolean equals( 1332 char[][] first, 1333 char[][] second, 1334 boolean isCaseSensitive) { 1335 1336 if (isCaseSensitive) { 1337 return equals(first, second); 1338 } 1339 if (first == second) 1340 return true; 1341 if (first == null || second == null) 1342 return false; 1343 if (first.length != second.length) 1344 return false; 1345 1346 for (int i = first.length; --i >= 0;) 1347 if (!equals(first[i], second[i], false)) 1348 return false; 1349 return true; 1350} 1351 1352 1388public static final boolean equals(char[] first, char[] second) { 1389 if (first == second) 1390 return true; 1391 if (first == null || second == null) 1392 return false; 1393 if (first.length != second.length) 1394 return false; 1395 1396 for (int i = first.length; --i >= 0;) 1397 if (first[i] != second[i]) 1398 return false; 1399 return true; 1400} 1401 1402 1450public static final boolean equals(char[] first, char[] second, int secondStart, int secondEnd) { 1451 return equals(first, second, secondStart, secondEnd, true); 1452} 1453 1514public static final boolean equals(char[] first, char[] second, int secondStart, int secondEnd, boolean isCaseSensitive) { 1515 if (first == second) 1516 return true; 1517 if (first == null || second == null) 1518 return false; 1519 if (first.length != secondEnd - secondStart) 1520 return false; 1521 if (isCaseSensitive) { 1522 for (int i = first.length; --i >= 0;) 1523 if (first[i] != second[i+secondStart]) 1524 return false; 1525 } else { 1526 for (int i = first.length; --i >= 0;) 1527 if (ScannerHelper.toLowerCase(first[i]) != ScannerHelper.toLowerCase(second[i+secondStart])) 1528 return false; 1529 } 1530 return true; 1531} 1532 1533 1578public static final boolean equals( 1579 char[] first, 1580 char[] second, 1581 boolean isCaseSensitive) { 1582 1583 if (isCaseSensitive) { 1584 return equals(first, second); 1585 } 1586 if (first == second) 1587 return true; 1588 if (first == null || second == null) 1589 return false; 1590 if (first.length != second.length) 1591 return false; 1592 1593 for (int i = first.length; --i >= 0;) 1594 if (ScannerHelper.toLowerCase(first[i]) 1595 != ScannerHelper.toLowerCase(second[i])) 1596 return false; 1597 return true; 1598} 1599 1600 1650public static final boolean fragmentEquals( 1651 char[] fragment, 1652 char[] name, 1653 int startIndex, 1654 boolean isCaseSensitive) { 1655 1656 int max = fragment.length; 1657 if (name.length < max + startIndex) 1658 return false; 1659 if (isCaseSensitive) { 1660 for (int i = max; 1661 --i >= 0; 1662 ) if (fragment[i] != name[i + startIndex]) 1664 return false; 1665 return true; 1666 } 1667 for (int i = max; 1668 --i >= 0; 1669 ) if (ScannerHelper.toLowerCase(fragment[i]) 1671 != ScannerHelper.toLowerCase(name[i + startIndex])) 1672 return false; 1673 return true; 1674} 1675 1676 1683public static final int hashCode(char[] array) { 1684 int length = array.length; 1685 int hash = length == 0 ? 31 : array[0]; 1686 if (length < 8) { 1687 for (int i = length; --i > 0;) 1688 hash = (hash * 31) + array[i]; 1689 } else { 1690 for (int i = length - 1, last = i > 16 ? i - 16 : 0; i > last; i -= 2) 1692 hash = (hash * 31) + array[i]; 1693 } 1694 return hash & 0x7FFFFFFF; 1695} 1696 1697 1718public static boolean isWhitespace(char c) { 1719 return c < ScannerHelper.MAX_OBVIOUS && ((ScannerHelper.OBVIOUS_IDENT_CHAR_NATURES[c] & ScannerHelper.C_JLS_SPACE) != 0); 1720} 1721 1722 1749public static final int indexOf(char toBeFound, char[] array) { 1750 return indexOf(toBeFound, array, 0); 1751} 1752 1753 1782public static final int indexOf(char[] toBeFound, char[] array, boolean isCaseSensitive) { 1783 return indexOf(toBeFound, array, isCaseSensitive, 0); 1784} 1785 1786 1816public static final int indexOf(final char[] toBeFound, final char[] array, final boolean isCaseSensitive, final int start) { 1817 return indexOf(toBeFound, array, isCaseSensitive, start, array.length); 1818} 1819 1820 1851public static final int indexOf(final char[] toBeFound, final char[] array, final boolean isCaseSensitive, final int start, final int end) { 1852 final int arrayLength = end; 1853 final int toBeFoundLength = toBeFound.length; 1854 if (toBeFoundLength > arrayLength) return -1; 1855 if (toBeFoundLength == 0) return 0; 1856 if (toBeFoundLength == arrayLength) { 1857 if (isCaseSensitive) { 1858 for (int i = start; i < arrayLength; i++) { 1859 if (array[i] != toBeFound[i]) return -1; 1860 } 1861 return 0; 1862 } else { 1863 for (int i = start; i < arrayLength; i++) { 1864 if (ScannerHelper.toLowerCase(array[i]) != ScannerHelper.toLowerCase(toBeFound[i])) return -1; 1865 } 1866 return 0; 1867 } 1868 } 1869 if (isCaseSensitive) { 1870 arrayLoop: for (int i = start, max = arrayLength - toBeFoundLength + 1; i < max; i++) { 1871 if (array[i] == toBeFound[0]) { 1872 for (int j = 1; j < toBeFoundLength; j++) { 1873 if (array[i + j] != toBeFound[j]) continue arrayLoop; 1874 } 1875 return i; 1876 } 1877 } 1878 } else { 1879 arrayLoop: for (int i = start, max = arrayLength - toBeFoundLength + 1; i < max; i++) { 1880 if (ScannerHelper.toLowerCase(array[i]) == ScannerHelper.toLowerCase(toBeFound[0])) { 1881 for (int j = 1; j < toBeFoundLength; j++) { 1882 if (ScannerHelper.toLowerCase(array[i + j]) != ScannerHelper.toLowerCase(toBeFound[j])) continue arrayLoop; 1883 } 1884 return i; 1885 } 1886 } 1887 } 1888 return -1; 1889} 1890 1891 1930public static final int indexOf(char toBeFound, char[] array, int start) { 1931 for (int i = start; i < array.length; i++) 1932 if (toBeFound == array[i]) 1933 return i; 1934 return -1; 1935} 1936 1937 1978public static final int indexOf(char toBeFound, char[] array, int start, int end) { 1979 for (int i = start; i < end; i++) 1980 if (toBeFound == array[i]) 1981 return i; 1982 return -1; 1983} 1984 1985 2013public static final int lastIndexOf(char toBeFound, char[] array) { 2014 for (int i = array.length; --i >= 0;) 2015 if (toBeFound == array[i]) 2016 return i; 2017 return -1; 2018} 2019 2020 2059public static final int lastIndexOf( 2060 char toBeFound, 2061 char[] array, 2062 int startIndex) { 2063 for (int i = array.length; --i >= startIndex;) 2064 if (toBeFound == array[i]) 2065 return i; 2066 return -1; 2067} 2068 2069 2112public static final int lastIndexOf( 2113 char toBeFound, 2114 char[] array, 2115 int startIndex, 2116 int endIndex) { 2117 for (int i = endIndex; --i >= startIndex;) 2118 if (toBeFound == array[i]) 2119 return i; 2120 return -1; 2121} 2122 2123 2137final static public char[] lastSegment(char[] array, char separator) { 2138 int pos = lastIndexOf(separator, array); 2139 if (pos < 0) 2140 return array; 2141 return subarray(array, pos + 1, array.length); 2142} 2143 2144 2184public static final boolean match( 2185 char[] pattern, 2186 char[] name, 2187 boolean isCaseSensitive) { 2188 2189 if (name == null) 2190 return false; if (pattern == null) 2192 return true; 2194 return match( 2195 pattern, 2196 0, 2197 pattern.length, 2198 name, 2199 0, 2200 name.length, 2201 isCaseSensitive); 2202} 2203 2204 2248public static final boolean match( 2249 char[] pattern, 2250 int patternStart, 2251 int patternEnd, 2252 char[] name, 2253 int nameStart, 2254 int nameEnd, 2255 boolean isCaseSensitive) { 2256 2257 if (name == null) 2258 return false; if (pattern == null) 2260 return true; int iPattern = patternStart; 2262 int iName = nameStart; 2263 2264 if (patternEnd < 0) 2265 patternEnd = pattern.length; 2266 if (nameEnd < 0) 2267 nameEnd = name.length; 2268 2269 2270 char patternChar = 0; 2271 while ((iPattern < patternEnd) 2272 && (patternChar = pattern[iPattern]) != '*') { 2273 if (iName == nameEnd) 2274 return false; 2275 if (patternChar 2276 != (isCaseSensitive 2277 ? name[iName] 2278 : ScannerHelper.toLowerCase(name[iName])) 2279 && patternChar != '?') { 2280 return false; 2281 } 2282 iName++; 2283 iPattern++; 2284 } 2285 2286 int segmentStart; 2287 if (patternChar == '*') { 2288 segmentStart = ++iPattern; } else { 2290 segmentStart = 0; } 2292 int prefixStart = iName; 2293 checkSegment : while (iName < nameEnd) { 2294 if (iPattern == patternEnd) { 2295 iPattern = segmentStart; iName = ++prefixStart; 2297 continue checkSegment; 2298 } 2299 2300 if ((patternChar = pattern[iPattern]) == '*') { 2301 segmentStart = ++iPattern; if (segmentStart == patternEnd) { 2303 return true; 2304 } 2305 prefixStart = iName; 2306 continue checkSegment; 2307 } 2308 2309 if ((isCaseSensitive ? name[iName] : ScannerHelper.toLowerCase(name[iName])) 2310 != patternChar 2311 && patternChar != '?') { 2312 iPattern = segmentStart; iName = ++prefixStart; 2314 continue checkSegment; 2315 } 2316 iName++; 2317 iPattern++; 2318 } 2319 2320 return (segmentStart == patternEnd) 2321 || (iName == nameEnd && iPattern == patternEnd) 2322 || (iPattern == patternEnd - 1 && pattern[iPattern] == '*'); 2323} 2324 2325 2343public static final boolean pathMatch( 2344 char[] pattern, 2345 char[] filepath, 2346 boolean isCaseSensitive, 2347 char pathSeparator) { 2348 2349 if (filepath == null) 2350 return false; if (pattern == null) 2352 return true; 2354 int pSegmentStart = pattern[0] == pathSeparator ? 1 : 0; 2356 int pLength = pattern.length; 2357 int pSegmentEnd = CharOperation.indexOf(pathSeparator, pattern, pSegmentStart+1); 2358 if (pSegmentEnd < 0) pSegmentEnd = pLength; 2359 2360 boolean freeTrailingDoubleStar = pattern[pLength - 1] == pathSeparator; 2362 2363 int fSegmentStart, fLength = filepath.length; 2365 if (filepath[0] != pathSeparator){ 2366 fSegmentStart = 0; 2367 } else { 2368 fSegmentStart = 1; 2369 } 2370 if (fSegmentStart != pSegmentStart) { 2371 return false; } 2373 int fSegmentEnd = CharOperation.indexOf(pathSeparator, filepath, fSegmentStart+1); 2374 if (fSegmentEnd < 0) fSegmentEnd = fLength; 2375 2376 while (pSegmentStart < pLength 2378 && !(pSegmentEnd == pLength && freeTrailingDoubleStar 2379 || (pSegmentEnd == pSegmentStart + 2 2380 && pattern[pSegmentStart] == '*' 2381 && pattern[pSegmentStart + 1] == '*'))) { 2382 2383 if (fSegmentStart >= fLength) 2384 return false; 2385 if (!CharOperation 2386 .match( 2387 pattern, 2388 pSegmentStart, 2389 pSegmentEnd, 2390 filepath, 2391 fSegmentStart, 2392 fSegmentEnd, 2393 isCaseSensitive)) { 2394 return false; 2395 } 2396 2397 pSegmentEnd = 2399 CharOperation.indexOf( 2400 pathSeparator, 2401 pattern, 2402 pSegmentStart = pSegmentEnd + 1); 2403 if (pSegmentEnd < 0) 2405 pSegmentEnd = pLength; 2406 2407 fSegmentEnd = 2408 CharOperation.indexOf( 2409 pathSeparator, 2410 filepath, 2411 fSegmentStart = fSegmentEnd + 1); 2412 if (fSegmentEnd < 0) fSegmentEnd = fLength; 2414 } 2415 2416 2417 int pSegmentRestart; 2418 if ((pSegmentStart >= pLength && freeTrailingDoubleStar) 2419 || (pSegmentEnd == pSegmentStart + 2 2420 && pattern[pSegmentStart] == '*' 2421 && pattern[pSegmentStart + 1] == '*')) { 2422 pSegmentEnd = 2423 CharOperation.indexOf( 2424 pathSeparator, 2425 pattern, 2426 pSegmentStart = pSegmentEnd + 1); 2427 if (pSegmentEnd < 0) pSegmentEnd = pLength; 2429 pSegmentRestart = pSegmentStart; 2430 } else { 2431 if (pSegmentStart >= pLength) return fSegmentStart >= fLength; pSegmentRestart = 0; } 2434 int fSegmentRestart = fSegmentStart; 2435 checkSegment : while (fSegmentStart < fLength) { 2436 2437 if (pSegmentStart >= pLength) { 2438 if (freeTrailingDoubleStar) return true; 2439 pSegmentEnd = 2441 CharOperation.indexOf(pathSeparator, pattern, pSegmentStart = pSegmentRestart); 2442 if (pSegmentEnd < 0) pSegmentEnd = pLength; 2443 2444 fSegmentRestart = 2445 CharOperation.indexOf(pathSeparator, filepath, fSegmentRestart + 1); 2446 if (fSegmentRestart < 0) { 2448 fSegmentRestart = fLength; 2449 } else { 2450 fSegmentRestart++; 2451 } 2452 fSegmentEnd = 2453 CharOperation.indexOf(pathSeparator, filepath, fSegmentStart = fSegmentRestart); 2454 if (fSegmentEnd < 0) fSegmentEnd = fLength; 2455 continue checkSegment; 2456 } 2457 2458 2459 if (pSegmentEnd == pSegmentStart + 2 2460 && pattern[pSegmentStart] == '*' 2461 && pattern[pSegmentStart + 1] == '*') { 2462 pSegmentEnd = 2463 CharOperation.indexOf(pathSeparator, pattern, pSegmentStart = pSegmentEnd + 1); 2464 if (pSegmentEnd < 0) pSegmentEnd = pLength; 2466 pSegmentRestart = pSegmentStart; 2467 fSegmentRestart = fSegmentStart; 2468 if (pSegmentStart >= pLength) return true; 2469 continue checkSegment; 2470 } 2471 2472 if (!CharOperation.match( 2473 pattern, 2474 pSegmentStart, 2475 pSegmentEnd, 2476 filepath, 2477 fSegmentStart, 2478 fSegmentEnd, 2479 isCaseSensitive)) { 2480 pSegmentEnd = 2482 CharOperation.indexOf(pathSeparator, pattern, pSegmentStart = pSegmentRestart); 2483 if (pSegmentEnd < 0) pSegmentEnd = pLength; 2484 2485 fSegmentRestart = 2486 CharOperation.indexOf(pathSeparator, filepath, fSegmentRestart + 1); 2487 if (fSegmentRestart < 0) { 2489 fSegmentRestart = fLength; 2490 } else { 2491 fSegmentRestart++; 2492 } 2493 fSegmentEnd = 2494 CharOperation.indexOf(pathSeparator, filepath, fSegmentStart = fSegmentRestart); 2495 if (fSegmentEnd < 0) fSegmentEnd = fLength; 2496 continue checkSegment; 2497 } 2498 pSegmentEnd = 2500 CharOperation.indexOf( 2501 pathSeparator, 2502 pattern, 2503 pSegmentStart = pSegmentEnd + 1); 2504 if (pSegmentEnd < 0) 2506 pSegmentEnd = pLength; 2507 2508 fSegmentEnd = 2509 CharOperation.indexOf( 2510 pathSeparator, 2511 filepath, 2512 fSegmentStart = fSegmentEnd + 1); 2513 if (fSegmentEnd < 0) 2515 fSegmentEnd = fLength; 2516 } 2517 2518 return (pSegmentRestart >= pSegmentEnd) 2519 || (fSegmentStart >= fLength && pSegmentStart >= pLength) 2520 || (pSegmentStart == pLength - 2 2521 && pattern[pSegmentStart] == '*' 2522 && pattern[pSegmentStart + 1] == '*') 2523 || (pSegmentStart == pLength && freeTrailingDoubleStar); 2524} 2525 2526 2552public static final int occurencesOf(char toBeFound, char[] array) { 2553 int count = 0; 2554 for (int i = 0; i < array.length; i++) 2555 if (toBeFound == array[i]) 2556 count++; 2557 return count; 2558} 2559 2560 2591public static final int occurencesOf( 2592 char toBeFound, 2593 char[] array, 2594 int start) { 2595 int count = 0; 2596 for (int i = start; i < array.length; i++) 2597 if (toBeFound == array[i]) 2598 count++; 2599 return count; 2600} 2601 2602 2628public static final boolean prefixEquals(char[] prefix, char[] name) { 2629 2630 int max = prefix.length; 2631 if (name.length < max) 2632 return false; 2633 for (int i = max; 2634 --i >= 0; 2635 ) if (prefix[i] != name[i]) 2637 return false; 2638 return true; 2639} 2640 2641 2670public static final boolean prefixEquals( 2671 char[] prefix, 2672 char[] name, 2673 boolean isCaseSensitive) { 2674 2675 int max = prefix.length; 2676 if (name.length < max) 2677 return false; 2678 if (isCaseSensitive) { 2679 for (int i = max; 2680 --i >= 0; 2681 ) if (prefix[i] != name[i]) 2683 return false; 2684 return true; 2685 } 2686 2687 for (int i = max; 2688 --i >= 0; 2689 ) if (ScannerHelper.toLowerCase(prefix[i]) 2691 != ScannerHelper.toLowerCase(name[i])) 2692 return false; 2693 return true; 2694} 2695 2696 2722public static final char[] remove(char[] array, char toBeRemoved) { 2723 2724 if (array == null) return null; 2725 int length = array.length; 2726 if (length == 0) return array; 2727 char[] result = null; 2728 int count = 0; 2729 for (int i = 0; i < length; i++) { 2730 char c = array[i]; 2731 if (c == toBeRemoved) { 2732 if (result == null) { 2733 result = new char[length]; 2734 System.arraycopy(array, 0, result, 0, i); 2735 count = i; 2736 } 2737 } else if (result != null) { 2738 result[count++] = c; 2739 } 2740 } 2741 if (result == null) return array; 2742 System.arraycopy(result, 0, result = new char[count], 0, count); 2743 return result; 2744} 2745 2746 2774public static final void replace( 2775 char[] array, 2776 char toBeReplaced, 2777 char replacementChar) { 2778 if (toBeReplaced != replacementChar) { 2779 for (int i = 0, max = array.length; i < max; i++) { 2780 if (array[i] == toBeReplaced) 2781 array[i] = replacementChar; 2782 } 2783 } 2784} 2785 2786 2808public static final void replace(char[] array, char[] toBeReplaced, char replacementChar) { 2809 replace(array, toBeReplaced, replacementChar, 0, array.length); 2810} 2811 2812 2838public static final void replace(char[] array, char[] toBeReplaced, char replacementChar, int start, int end) { 2839 for (int i = end; --i >= start;) 2840 for (int j = toBeReplaced.length; --j >= 0;) 2841 if (array[i] == toBeReplaced[j]) 2842 array[i] = replacementChar; 2843} 2844 2874public static final char[] replace( 2875 char[] array, 2876 char[] toBeReplaced, 2877 char[] replacementChars) { 2878 2879 int max = array.length; 2880 int replacedLength = toBeReplaced.length; 2881 int replacementLength = replacementChars.length; 2882 2883 int[] starts = new int[5]; 2884 int occurrenceCount = 0; 2885 2886 if (!equals(toBeReplaced, replacementChars)) { 2887 2888 next : for (int i = 0; i < max;) { 2889 int index = indexOf(toBeReplaced, array, true, i); 2890 if (index == -1) { 2891 i++; 2892 continue next; 2893 } 2894 if (occurrenceCount == starts.length) { 2895 System.arraycopy( 2896 starts, 2897 0, 2898 starts = new int[occurrenceCount * 2], 2899 0, 2900 occurrenceCount); 2901 } 2902 starts[occurrenceCount++] = index; 2903 i = index + replacedLength; 2904 } 2905 } 2906 if (occurrenceCount == 0) 2907 return array; 2908 char[] result = 2909 new char[max 2910 + occurrenceCount * (replacementLength - replacedLength)]; 2911 int inStart = 0, outStart = 0; 2912 for (int i = 0; i < occurrenceCount; i++) { 2913 int offset = starts[i] - inStart; 2914 System.arraycopy(array, inStart, result, outStart, offset); 2915 inStart += offset; 2916 outStart += offset; 2917 System.arraycopy( 2918 replacementChars, 2919 0, 2920 result, 2921 outStart, 2922 replacementLength); 2923 inStart += replacedLength; 2924 outStart += replacementLength; 2925 } 2926 System.arraycopy(array, inStart, result, outStart, max - inStart); 2927 return result; 2928} 2929 2930 2960public static final char[] replaceOnCopy( 2961 char[] array, 2962 char toBeReplaced, 2963 char replacementChar) { 2964 2965 char[] result = null; 2966 for (int i = 0, length = array.length; i < length; i++) { 2967 char c = array[i]; 2968 if (c == toBeReplaced) { 2969 if (result == null) { 2970 result = new char[length]; 2971 System.arraycopy(array, 0, result, 0, i); 2972 } 2973 result[i] = replacementChar; 2974 } else if (result != null) { 2975 result[i] = c; 2976 } 2977 } 2978 if (result == null) return array; 2979 return result; 2980} 2981 2982 3020public static final char[][] splitAndTrimOn(char divider, char[] array) { 3021 int length = array == null ? 0 : array.length; 3022 if (length == 0) 3023 return NO_CHAR_CHAR; 3024 3025 int wordCount = 1; 3026 for (int i = 0; i < length; i++) 3027 if (array[i] == divider) 3028 wordCount++; 3029 char[][] split = new char[wordCount][]; 3030 int last = 0, currentWord = 0; 3031 for (int i = 0; i < length; i++) { 3032 if (array[i] == divider) { 3033 int start = last, end = i - 1; 3034 while (start < i && array[start] == ' ') 3035 start++; 3036 while (end > start && array[end] == ' ') 3037 end--; 3038 split[currentWord] = new char[end - start + 1]; 3039 System.arraycopy( 3040 array, 3041 start, 3042 split[currentWord++], 3043 0, 3044 end - start + 1); 3045 last = i + 1; 3046 } 3047 } 3048 int start = last, end = length - 1; 3049 while (start < length && array[start] == ' ') 3050 start++; 3051 while (end > start && array[end] == ' ') 3052 end--; 3053 split[currentWord] = new char[end - start + 1]; 3054 System.arraycopy( 3055 array, 3056 start, 3057 split[currentWord++], 3058 0, 3059 end - start + 1); 3060 return split; 3061} 3062 3063 3093public static final char[][] splitOn(char divider, char[] array) { 3094 int length = array == null ? 0 : array.length; 3095 if (length == 0) 3096 return NO_CHAR_CHAR; 3097 3098 int wordCount = 1; 3099 for (int i = 0; i < length; i++) 3100 if (array[i] == divider) 3101 wordCount++; 3102 char[][] split = new char[wordCount][]; 3103 int last = 0, currentWord = 0; 3104 for (int i = 0; i < length; i++) { 3105 if (array[i] == divider) { 3106 split[currentWord] = new char[i - last]; 3107 System.arraycopy( 3108 array, 3109 last, 3110 split[currentWord++], 3111 0, 3112 i - last); 3113 last = i + 1; 3114 } 3115 } 3116 split[currentWord] = new char[length - last]; 3117 System.arraycopy(array, last, split[currentWord], 0, length - last); 3118 return split; 3119} 3120 3121 3145public static final char[][] splitOn( 3146 char divider, 3147 char[] array, 3148 int start, 3149 int end) { 3150 int length = array == null ? 0 : array.length; 3151 if (length == 0 || start > end) 3152 return NO_CHAR_CHAR; 3153 3154 int wordCount = 1; 3155 for (int i = start; i < end; i++) 3156 if (array[i] == divider) 3157 wordCount++; 3158 char[][] split = new char[wordCount][]; 3159 int last = start, currentWord = 0; 3160 for (int i = start; i < end; i++) { 3161 if (array[i] == divider) { 3162 split[currentWord] = new char[i - last]; 3163 System.arraycopy( 3164 array, 3165 last, 3166 split[currentWord++], 3167 0, 3168 i - last); 3169 last = i + 1; 3170 } 3171 } 3172 split[currentWord] = new char[end - last]; 3173 System.arraycopy(array, last, split[currentWord], 0, end - last); 3174 return split; 3175} 3176 3177 3209public static final char[][] subarray(char[][] array, int start, int end) { 3210 if (end == -1) 3211 end = array.length; 3212 if (start > end) 3213 return null; 3214 if (start < 0) 3215 return null; 3216 if (end > array.length) 3217 return null; 3218 3219 char[][] result = new char[end - start][]; 3220 System.arraycopy(array, start, result, 0, end - start); 3221 return result; 3222} 3223 3224 3256public static final char[] subarray(char[] array, int start, int end) { 3257 if (end == -1) 3258 end = array.length; 3259 if (start > end) 3260 return null; 3261 if (start < 0) 3262 return null; 3263 if (end > array.length) 3264 return null; 3265 3266 char[] result = new char[end - start]; 3267 System.arraycopy(array, start, result, 0, end - start); 3268 return result; 3269} 3270 3271 3294final static public char[] toLowerCase(char[] chars) { 3295 if (chars == null) 3296 return null; 3297 int length = chars.length; 3298 char[] lowerChars = null; 3299 for (int i = 0; i < length; i++) { 3300 char c = chars[i]; 3301 char lc = ScannerHelper.toLowerCase(c); 3302 if ((c != lc) || (lowerChars != null)) { 3303 if (lowerChars == null) { 3304 System.arraycopy( 3305 chars, 3306 0, 3307 lowerChars = new char[length], 3308 0, 3309 i); 3310 } 3311 lowerChars[i] = lc; 3312 } 3313 } 3314 return lowerChars == null ? chars : lowerChars; 3315} 3316 3317 3339final static public char[] trim(char[] chars) { 3340 3341 if (chars == null) 3342 return null; 3343 3344 int start = 0, length = chars.length, end = length - 1; 3345 while (start < length && chars[start] == ' ') { 3346 start++; 3347 } 3348 while (end > start && chars[end] == ' ') { 3349 end--; 3350 } 3351 if (start != 0 || end != length - 1) { 3352 return subarray(chars, start, end + 1); 3353 } 3354 return chars; 3355} 3356 3357 3378final static public String toString(char[][] array) { 3379 char[] result = concatWith(array, '.'); 3380 return new String (result); 3381} 3382 3383 3390final static public String [] toStrings(char[][] array) { 3391 if (array == null) return NO_STRINGS; 3392 int length = array.length; 3393 if (length == 0) return NO_STRINGS; 3394 String [] result = new String [length]; 3395 for (int i = 0; i < length; i++) 3396 result[i] = new String (array[i]); 3397 return result; 3398} 3399} 3400 | Popular Tags |