Determines the index in the specified character sequence that is offset codePointOffset code points from index. @param seq the character sequence to find the index in. @param index the start index in seq. @param codePointOffset the number of
(CharSequence seq, int index,
int codePointOffset)
| 2607 | * @since 1.5 |
| 2608 | */ |
| 2609 | public static int offsetByCodePoints(CharSequence seq, int index, |
| 2610 | int codePointOffset) { |
| 2611 | if (seq == null) { |
| 2612 | throw new NullPointerException(); |
| 2613 | } |
| 2614 | int len = seq.length(); |
| 2615 | if (index < 0 || index > len) { |
| 2616 | throw new IndexOutOfBoundsException(); |
| 2617 | } |
| 2618 | |
| 2619 | if (codePointOffset == 0) { |
| 2620 | return index; |
| 2621 | } |
| 2622 | |
| 2623 | if (codePointOffset > 0) { |
| 2624 | int codePoints = codePointOffset; |
| 2625 | int i = index; |
| 2626 | while (codePoints > 0) { |
| 2627 | codePoints--; |
| 2628 | if (i >= len) { |
| 2629 | throw new IndexOutOfBoundsException(); |
| 2630 | } |
| 2631 | if (isHighSurrogate(seq.charAt(i))) { |
| 2632 | int next = i + 1; |
| 2633 | if (next < len && isLowSurrogate(seq.charAt(next))) { |
| 2634 | i++; |
| 2635 | } |
| 2636 | } |
| 2637 | i++; |
| 2638 | } |
| 2639 | return i; |
| 2640 | } |
| 2641 | |
| 2642 | assert codePointOffset < 0; |
| 2643 | int codePoints = -codePointOffset; |
| 2644 | int i = index; |
| 2645 | while (codePoints > 0) { |
| 2646 | codePoints--; |
| 2647 | i--; |
| 2648 | if (i < 0) { |
| 2649 | throw new IndexOutOfBoundsException(); |
| 2650 | } |
| 2651 | if (isLowSurrogate(seq.charAt(i))) { |
| 2652 | int prev = i - 1; |
| 2653 | if (prev >= 0 && isHighSurrogate(seq.charAt(prev))) { |
| 2654 | i--; |
| 2655 | } |
| 2656 | } |
| 2657 | } |
| 2658 | return i; |
| 2659 | } |
| 2660 | |
| 2661 | /** |
| 2662 | * Determines the index in a subsequence of the specified character array |
no test coverage detected