src/java.base/share/classes/java/lang/StringUTF16.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File jdk Sdiff src/java.base/share/classes/java/lang

src/java.base/share/classes/java/lang/StringUTF16.java

Print this page




 311             // negative value (invalid code point))
 312             return indexOfChar(value, ch, fromIndex, max);
 313         } else {
 314             return indexOfSupplementary(value, ch, fromIndex, max);
 315         }
 316     }
 317 
 318     @HotSpotIntrinsicCandidate
 319     public static int indexOf(byte[] value, byte[] str) {
 320         if (str.length == 0) {
 321             return 0;
 322         }
 323         if (value.length == 0) {
 324             return -1;
 325         }
 326         return indexOf(value, length(value), str, length(str), 0);
 327     }
 328 
 329     @HotSpotIntrinsicCandidate
 330     public static int indexOf(byte[] value, int valueCount, byte[] str, int strCount, int fromIndex) {




















 331         char first = getChar(str, 0);
 332         int max = (valueCount - strCount);
 333         for (int i = fromIndex; i <= max; i++) {
 334             // Look for first character.
 335             if (getChar(value, i) != first) {
 336                 while (++i <= max && getChar(value, i) != first);
 337             }
 338             // Found first character, now look at the rest of value
 339             if (i <= max) {
 340                 int j = i + 1;
 341                 int end = j + strCount - 1;
 342                 for (int k = 1; j < end && getChar(value, j) == getChar(str, k); j++, k++);
 343                 if (j == end) {
 344                     // Found whole string.
 345                     return i;
 346                 }
 347             }
 348         }
 349         return -1;
 350     }
 351 
 352     /**
 353      * Handles indexOf Latin1 substring in UTF16 string.
 354      */
 355     @HotSpotIntrinsicCandidate
 356     public static int indexOfLatin1(byte[] value, byte[] str) {
 357         if (str.length == 0) {
 358             return 0;
 359         }
 360         if (value.length == 0) {
 361             return -1;
 362         }
 363         return indexOfLatin1(value, length(value), str, str.length, 0);
 364     }
 365 
 366     @HotSpotIntrinsicCandidate
 367     public static int indexOfLatin1(byte[] src, int srcCount, byte[] tgt, int tgtCount, int fromIndex) {




 368         char first = (char)(tgt[0] & 0xff);
 369         int max = (srcCount - tgtCount);
 370         for (int i = fromIndex; i <= max; i++) {
 371             // Look for first character.
 372             if (getChar(src, i) != first) {
 373                 while (++i <= max && getChar(src, i) != first);
 374             }
 375             // Found first character, now look at the rest of v2
 376             if (i <= max) {
 377                 int j = i + 1;
 378                 int end = j + tgtCount - 1;
 379                 for (int k = 1;
 380                      j < end && getChar(src, j) == (tgt[k] & 0xff);
 381                      j++, k++);
 382                 if (j == end) {
 383                     // Found whole string.
 384                     return i;
 385                 }
 386             }
 387         }
 388         return -1;
 389     }
 390 
 391     @HotSpotIntrinsicCandidate
 392     private static int indexOfChar(byte[] value, int ch, int fromIndex, int max) {




 393         for (int i = fromIndex; i < max; i++) {
 394             if (getChar(value, i) == ch) {
 395                 return i;
 396             }
 397         }
 398         return -1;
 399     }
 400 
 401     /**
 402      * Handles (rare) calls of indexOf with a supplementary character.
 403      */
 404     private static int indexOfSupplementary(byte[] value, int ch, int fromIndex, int max) {
 405         if (Character.isValidCodePoint(ch)) {
 406             final char hi = Character.highSurrogate(ch);
 407             final char lo = Character.lowSurrogate(ch);




 408             for (int i = fromIndex; i < max - 1; i++) {
 409                 if (getChar(value, i) == hi && getChar(value, i + 1 ) == lo) {
 410                     return i;
 411                 }
 412             }
 413         }
 414         return -1;
 415     }
 416 
 417     // srcCoder == UTF16 && tgtCoder == UTF16
 418     public static int lastIndexOf(byte[] src, int srcCount,
 419                                   byte[] tgt, int tgtCount, int fromIndex) {
 420         int min = tgtCount - 1;
 421         int i = min + fromIndex;
 422         int strLastIndex = tgtCount - 1;



 423         char strLastChar = getChar(tgt, strLastIndex);
 424 



 425     startSearchForLastChar:
 426         while (true) {
 427             while (i >= min && getChar(src, i) != strLastChar) {
 428                 i--;
 429             }
 430             if (i < min) {
 431                 return -1;
 432             }
 433             int j = i - 1;
 434             int start = j - strLastIndex;
 435             int k = strLastIndex - 1;
 436             while (j > start) {
 437                 if (getChar(src, j--) != getChar(tgt, k--)) {
 438                     i--;
 439                     continue startSearchForLastChar;
 440                 }
 441             }
 442             return start + 1;
 443         }
 444     }




 311             // negative value (invalid code point))
 312             return indexOfChar(value, ch, fromIndex, max);
 313         } else {
 314             return indexOfSupplementary(value, ch, fromIndex, max);
 315         }
 316     }
 317 
 318     @HotSpotIntrinsicCandidate
 319     public static int indexOf(byte[] value, byte[] str) {
 320         if (str.length == 0) {
 321             return 0;
 322         }
 323         if (value.length == 0) {
 324             return -1;
 325         }
 326         return indexOf(value, length(value), str, length(str), 0);
 327     }
 328 
 329     @HotSpotIntrinsicCandidate
 330     public static int indexOf(byte[] value, int valueCount, byte[] str, int strCount, int fromIndex) {
 331         // Redundant.
 332         // Callers:
 333         //   StringUTF16.indexOf(byte[] value, byte[] str): valid by construction
 334         //     return indexOf(value, length(value), str, length(str), 0);
 335         //
 336         //   String.indexOf:
 337         //     StringUTF16.indexOf(src, srcCount, tgt, tgtCount, fromIndex);
 338         //     0 <= tgtCount < tgtStr.length >> 1
 339         //        byte[] tgt    = tgtStr.value;
 340         //        int tgtCount  = tgtStr.length();
 341         //
 342         //     0 < fromIndex < srcCount
 343         //
 344         //     0 <= srcCount < src.length
 345         //     Covered by ABS.indexOf():
 346         //        checkIndex(count, value.length >> coder);
 347         //        return String.indexOf(value, coder, count, str, fromIndex);
 348         checkBoundsBeginEnd(fromIndex, valueCount, value.length >> 1);
 349         checkBoundsBeginEnd(0, strCount, str.length >> 1);
 350 
 351         char first = getChar(str, 0);
 352         int max = (valueCount - strCount);
 353         for (int i = fromIndex; i <= max; i++) {
 354             // Look for first character.
 355             if (getChar(value, i) != first) {
 356                 while (++i <= max && getChar(value, i) != first);
 357             }
 358             // Found first character, now look at the rest of value
 359             if (i <= max) {
 360                 int j = i + 1;
 361                 int end = j + strCount - 1;
 362                 for (int k = 1; j < end && getChar(value, j) == getChar(str, k); j++, k++);
 363                 if (j == end) {
 364                     // Found whole string.
 365                     return i;
 366                 }
 367             }
 368         }
 369         return -1;
 370     }
 371 
 372     /**
 373      * Handles indexOf Latin1 substring in UTF16 string.
 374      */
 375     @HotSpotIntrinsicCandidate
 376     public static int indexOfLatin1(byte[] value, byte[] str) {
 377         if (str.length == 0) {
 378             return 0;
 379         }
 380         if (value.length == 0) {
 381             return -1;
 382         }
 383         return indexOfLatin1(value, length(value), str, str.length, 0);
 384     }
 385 
 386     @HotSpotIntrinsicCandidate
 387     public static int indexOfLatin1(byte[] src, int srcCount, byte[] tgt, int tgtCount, int fromIndex) {
 388         // Redundant (similar to StringUTF16.indexOf).
 389         checkBoundsBeginEnd(fromIndex, srcCount, src.length >> 1);
 390         checkBoundsBeginEnd(0, tgtCount, tgt.length >> 1);
 391 
 392         char first = (char)(tgt[0] & 0xff);
 393         int max = (srcCount - tgtCount);
 394         for (int i = fromIndex; i <= max; i++) {
 395             // Look for first character.
 396             if (getChar(src, i) != first) {
 397                 while (++i <= max && getChar(src, i) != first);
 398             }
 399             // Found first character, now look at the rest of v2
 400             if (i <= max) {
 401                 int j = i + 1;
 402                 int end = j + tgtCount - 1;
 403                 for (int k = 1;
 404                      j < end && getChar(src, j) == (tgt[k] & 0xff);
 405                      j++, k++);
 406                 if (j == end) {
 407                     // Found whole string.
 408                     return i;
 409                 }
 410             }
 411         }
 412         return -1;
 413     }
 414 
 415     @HotSpotIntrinsicCandidate
 416     private static int indexOfChar(byte[] value, int ch, int fromIndex, int max) {
 417         // Redundant: StringUTF16.indexOf (caller) does the checks
 418         //   max == value.length >> 1
 419         //   0 <= fromIndex <= max
 420         checkBoundsBeginEnd(fromIndex, max, value.length >> 1);
 421         for (int i = fromIndex; i < max; i++) {
 422             if (getChar(value, i) == ch) {
 423                 return i;
 424             }
 425         }
 426         return -1;
 427     }
 428 
 429     /**
 430      * Handles (rare) calls of indexOf with a supplementary character.
 431      */
 432     private static int indexOfSupplementary(byte[] value, int ch, int fromIndex, int max) {
 433         if (Character.isValidCodePoint(ch)) {
 434             final char hi = Character.highSurrogate(ch);
 435             final char lo = Character.lowSurrogate(ch);
 436             // Redundant: StringUTF16.indexOf (caller) does the checks
 437             //   max == value.length >> 1
 438             //   0 <= fromIndex <= max
 439             checkBoundsBeginEnd(fromIndex, max, value.length >> 1);
 440             for (int i = fromIndex; i < max - 1; i++) {
 441                 if (getChar(value, i) == hi && getChar(value, i + 1 ) == lo) {
 442                     return i;
 443                 }
 444             }
 445         }
 446         return -1;
 447     }
 448 
 449     // srcCoder == UTF16 && tgtCoder == UTF16
 450     public static int lastIndexOf(byte[] src, int srcCount,
 451                                   byte[] tgt, int tgtCount, int fromIndex) {
 452         int min = tgtCount - 1;
 453         int i = min + fromIndex;
 454         int strLastIndex = tgtCount - 1;
 455 
 456         // Redundant: covered by checks in String.lastIndexOf
 457         checkIndex(strLastIndex, tgt.length >> 1);
 458         char strLastChar = getChar(tgt, strLastIndex);
 459 
 460         // Redundant: not needed for String.lastIndexOf(), covered by ASB.lastIndexOf().
 461         checkIndex(i, src.length >> 1);
 462 
 463     startSearchForLastChar:
 464         while (true) {
 465             while (i >= min && getChar(src, i) != strLastChar) {
 466                 i--;
 467             }
 468             if (i < min) {
 469                 return -1;
 470             }
 471             int j = i - 1;
 472             int start = j - strLastIndex;
 473             int k = strLastIndex - 1;
 474             while (j > start) {
 475                 if (getChar(src, j--) != getChar(tgt, k--)) {
 476                     i--;
 477                     continue startSearchForLastChar;
 478                 }
 479             }
 480             return start + 1;
 481         }
 482     }


src/java.base/share/classes/java/lang/StringUTF16.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File