2532 * <img src="doc-files/captheta.gif" alt="captheta"><img src="doc-files/capupsil.gif" alt="capupsil"> 2533 * <img src="doc-files/capsigma.gif" alt="capsigma"></td> 2534 * <td><img src="doc-files/iota.gif" alt="iota"><img src="doc-files/chi.gif" alt="chi"> 2535 * <img src="doc-files/theta.gif" alt="theta"><img src="doc-files/upsilon.gif" alt="upsilon"> 2536 * <img src="doc-files/sigma1.gif" alt="sigma"></td> 2537 * <td>lowercased all chars in String</td> 2538 * </tr> 2539 * </table> 2540 * 2541 * @param locale use the case transformation rules for this locale 2542 * @return the {@code String}, converted to lowercase. 2543 * @see java.lang.String#toLowerCase() 2544 * @see java.lang.String#toUpperCase() 2545 * @see java.lang.String#toUpperCase(Locale) 2546 * @since 1.1 2547 */ 2548 public String toLowerCase(Locale locale) { 2549 if (locale == null) { 2550 throw new NullPointerException(); 2551 } 2552 2553 int firstUpper; 2554 final int len = value.length; 2555 2556 /* Now check if there are any characters that need to be changed. */ 2557 scan: { 2558 for (firstUpper = 0 ; firstUpper < len; ) { 2559 char c = value[firstUpper]; 2560 if ((c >= Character.MIN_HIGH_SURROGATE) 2561 && (c <= Character.MAX_HIGH_SURROGATE)) { 2562 int supplChar = codePointAt(firstUpper); 2563 if (supplChar != Character.toLowerCase(supplChar)) { 2564 break scan; 2565 } 2566 firstUpper += Character.charCount(supplChar); 2567 } else { 2568 if (c != Character.toLowerCase(c)) { 2569 break scan; 2570 } 2571 firstUpper++; 2572 } 2573 } 2574 return this; 2575 } 2576 2577 char[] result = new char[len]; 2578 int resultOffset = 0; /* result may grow, so i+resultOffset 2579 * is the write location in result */ 2580 2581 /* Just copy the first few lowerCase characters. */ 2582 System.arraycopy(value, 0, result, 0, firstUpper); 2583 2584 String lang = locale.getLanguage(); 2585 boolean localeDependent = 2586 (lang == "tr" || lang == "az" || lang == "lt"); 2587 char[] lowerCharArray; 2588 int lowerChar; 2589 int srcChar; 2590 int srcCount; 2591 for (int i = firstUpper; i < len; i += srcCount) { 2592 srcChar = (int)value[i]; 2593 if ((char)srcChar >= Character.MIN_HIGH_SURROGATE 2594 && (char)srcChar <= Character.MAX_HIGH_SURROGATE) { 2595 srcChar = codePointAt(i); 2596 srcCount = Character.charCount(srcChar); 2597 } else { 2598 srcCount = 1; 2599 } 2600 if (localeDependent || srcChar == '\u03A3') { // GREEK CAPITAL LETTER SIGMA 2601 lowerChar = ConditionalSpecialCasing.toLowerCaseEx(this, i, locale); 2602 } else { 2603 lowerChar = Character.toLowerCase(srcChar); 2604 } 2605 if ((lowerChar == Character.ERROR) 2606 || (lowerChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) { 2607 if (lowerChar == Character.ERROR) { 2608 lowerCharArray = 2609 ConditionalSpecialCasing.toLowerCaseCharArray(this, i, locale); 2610 } else if (srcCount == 2) { 2611 resultOffset += Character.toChars(lowerChar, result, i + resultOffset) - srcCount; 2612 continue; 2613 } else { 2614 lowerCharArray = Character.toChars(lowerChar); 2615 } 2616 2617 /* Grow result if needed */ 2618 int mapLen = lowerCharArray.length; 2619 if (mapLen > srcCount) { 2620 char[] result2 = new char[result.length + mapLen - srcCount]; 2621 System.arraycopy(result, 0, result2, 0, i + resultOffset); 2622 result = result2; 2623 } 2624 for (int x = 0; x < mapLen; ++x) { 2625 result[i + resultOffset + x] = lowerCharArray[x]; 2626 } 2627 resultOffset += (mapLen - srcCount); 2628 } else { 2629 result[i + resultOffset] = (char)lowerChar; 2630 } 2631 } 2632 return new String(result, 0, len + resultOffset); 2633 } 2634 2635 /** 2636 * Converts all of the characters in this {@code String} to lower 2637 * case using the rules of the default locale. This is equivalent to calling 2638 * {@code toLowerCase(Locale.getDefault())}. 2639 * <p> 2640 * <b>Note:</b> This method is locale sensitive, and may produce unexpected 2641 * results if used for strings that are intended to be interpreted locale 2642 * independently. 2643 * Examples are programming language identifiers, protocol keys, and HTML 2644 * tags. 2645 * For instance, {@code "TITLE".toLowerCase()} in a Turkish locale 2646 * returns {@code "t\u005Cu0131tle"}, where '\u005Cu0131' is the 2647 * LATIN SMALL LETTER DOTLESS I character. 2648 * To obtain correct results for locale insensitive strings, use 2649 * {@code toLowerCase(Locale.ROOT)}. 2650 * <p> 2651 * @return the {@code String}, converted to lowercase. 2652 * @see java.lang.String#toLowerCase(Locale) 2690 * <td>small letter sharp s -> two letters: SS</td> 2691 * </tr> 2692 * <tr> 2693 * <td>(all)</td> 2694 * <td>Fahrvergnügen</td> 2695 * <td>FAHRVERGNÜGEN</td> 2696 * <td></td> 2697 * </tr> 2698 * </table> 2699 * @param locale use the case transformation rules for this locale 2700 * @return the {@code String}, converted to uppercase. 2701 * @see java.lang.String#toUpperCase() 2702 * @see java.lang.String#toLowerCase() 2703 * @see java.lang.String#toLowerCase(Locale) 2704 * @since 1.1 2705 */ 2706 public String toUpperCase(Locale locale) { 2707 if (locale == null) { 2708 throw new NullPointerException(); 2709 } 2710 2711 int firstLower; 2712 final int len = value.length; 2713 2714 /* Now check if there are any characters that need to be changed. */ 2715 scan: { 2716 for (firstLower = 0 ; firstLower < len; ) { 2717 int c = (int)value[firstLower]; 2718 int srcCount; 2719 if ((c >= Character.MIN_HIGH_SURROGATE) 2720 && (c <= Character.MAX_HIGH_SURROGATE)) { 2721 c = codePointAt(firstLower); 2722 srcCount = Character.charCount(c); 2723 } else { 2724 srcCount = 1; 2725 } 2726 int upperCaseChar = Character.toUpperCaseEx(c); 2727 if ((upperCaseChar == Character.ERROR) 2728 || (c != upperCaseChar)) { 2729 break scan; 2730 } 2731 firstLower += srcCount; 2732 } 2733 return this; 2734 } 2735 2736 /* result may grow, so i+resultOffset is the write location in result */ 2737 int resultOffset = 0; 2738 char[] result = new char[len]; /* may grow */ 2739 2740 /* Just copy the first few upperCase characters. */ 2741 System.arraycopy(value, 0, result, 0, firstLower); 2742 2743 String lang = locale.getLanguage(); 2744 boolean localeDependent = 2745 (lang == "tr" || lang == "az" || lang == "lt"); 2746 char[] upperCharArray; 2747 int upperChar; 2748 int srcChar; 2749 int srcCount; 2750 for (int i = firstLower; i < len; i += srcCount) { 2751 srcChar = (int)value[i]; 2752 if ((char)srcChar >= Character.MIN_HIGH_SURROGATE && 2753 (char)srcChar <= Character.MAX_HIGH_SURROGATE) { 2754 srcChar = codePointAt(i); 2755 srcCount = Character.charCount(srcChar); 2756 } else { 2757 srcCount = 1; 2758 } 2759 if (localeDependent) { 2760 upperChar = ConditionalSpecialCasing.toUpperCaseEx(this, i, locale); 2761 } else { 2762 upperChar = Character.toUpperCaseEx(srcChar); 2763 } 2764 if ((upperChar == Character.ERROR) 2765 || (upperChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) { 2766 if (upperChar == Character.ERROR) { 2767 if (localeDependent) { 2768 upperCharArray = 2769 ConditionalSpecialCasing.toUpperCaseCharArray(this, i, locale); 2770 } else { 2771 upperCharArray = Character.toUpperCaseCharArray(srcChar); 2772 } 2773 } else if (srcCount == 2) { 2774 resultOffset += Character.toChars(upperChar, result, i + resultOffset) - srcCount; 2775 continue; 2776 } else { 2777 upperCharArray = Character.toChars(upperChar); 2778 } 2779 2780 /* Grow result if needed */ 2781 int mapLen = upperCharArray.length; 2782 if (mapLen > srcCount) { 2783 char[] result2 = new char[result.length + mapLen - srcCount]; 2784 System.arraycopy(result, 0, result2, 0, i + resultOffset); 2785 result = result2; 2786 } 2787 for (int x = 0; x < mapLen; ++x) { 2788 result[i + resultOffset + x] = upperCharArray[x]; 2789 } 2790 resultOffset += (mapLen - srcCount); 2791 } else { 2792 result[i + resultOffset] = (char)upperChar; 2793 } 2794 } 2795 return new String(result, 0, len + resultOffset); 2796 } 2797 2798 /** 2799 * Converts all of the characters in this {@code String} to upper 2800 * case using the rules of the default locale. This method is equivalent to 2801 * {@code toUpperCase(Locale.getDefault())}. 2802 * <p> 2803 * <b>Note:</b> This method is locale sensitive, and may produce unexpected 2804 * results if used for strings that are intended to be interpreted locale 2805 * independently. 2806 * Examples are programming language identifiers, protocol keys, and HTML 2807 * tags. 2808 * For instance, {@code "title".toUpperCase()} in a Turkish locale 2809 * returns {@code "T\u005Cu0130TLE"}, where '\u005Cu0130' is the 2810 * LATIN CAPITAL LETTER I WITH DOT ABOVE character. 2811 * To obtain correct results for locale insensitive strings, use 2812 * {@code toUpperCase(Locale.ROOT)}. 2813 * <p> 2814 * @return the {@code String}, converted to uppercase. 2815 * @see java.lang.String#toUpperCase(Locale) | 2532 * <img src="doc-files/captheta.gif" alt="captheta"><img src="doc-files/capupsil.gif" alt="capupsil"> 2533 * <img src="doc-files/capsigma.gif" alt="capsigma"></td> 2534 * <td><img src="doc-files/iota.gif" alt="iota"><img src="doc-files/chi.gif" alt="chi"> 2535 * <img src="doc-files/theta.gif" alt="theta"><img src="doc-files/upsilon.gif" alt="upsilon"> 2536 * <img src="doc-files/sigma1.gif" alt="sigma"></td> 2537 * <td>lowercased all chars in String</td> 2538 * </tr> 2539 * </table> 2540 * 2541 * @param locale use the case transformation rules for this locale 2542 * @return the {@code String}, converted to lowercase. 2543 * @see java.lang.String#toLowerCase() 2544 * @see java.lang.String#toUpperCase() 2545 * @see java.lang.String#toUpperCase(Locale) 2546 * @since 1.1 2547 */ 2548 public String toLowerCase(Locale locale) { 2549 if (locale == null) { 2550 throw new NullPointerException(); 2551 } 2552 int first; 2553 boolean hasSurr = false; 2554 final int len = value.length; 2555 2556 // Now check if there are any characters that need to be changed, or are surrogate 2557 for (first = 0 ; first < len; first++) { 2558 int cp = (int)value[first]; 2559 if (Character.isSurrogate((char)cp)) { 2560 hasSurr = true; 2561 break; 2562 } 2563 if (cp != Character.toLowerCase(cp)) { // no need to check Character.ERROR 2564 break; 2565 } 2566 } 2567 if (first == len) 2568 return this; 2569 char[] result = new char[len]; 2570 System.arraycopy(value, 0, result, 0, first); // Just copy the first few 2571 // lowerCase characters. 2572 String lang = locale.getLanguage(); 2573 if (lang == "tr" || lang == "az" || lang == "lt") { 2574 return toLowerCaseEx(result, first, locale, true); 2575 } 2576 if (hasSurr) { 2577 return toLowerCaseEx(result, first, locale, false); 2578 } 2579 for (int i = first; i < len; i++) { 2580 int cp = (int)value[i]; 2581 if (cp == '\u03A3') { // GREEK CAPITAL LETTER SIGMA 2582 return toLowerCaseEx(result, i, locale, false); 2583 } 2584 cp = Character.toLowerCase(cp); 2585 if (!Character.isBmpCodePoint(cp)) { 2586 return toLowerCaseEx(result, i, locale, false); 2587 } 2588 result[i] = (char)cp; 2589 } 2590 return new String(result, true); 2591 } 2592 2593 private String toLowerCaseEx(char[] result, int first, Locale locale, boolean localeDependent) { 2594 int resultOffset = first; 2595 int srcCount; 2596 for (int i = first; i < value.length; i += srcCount) { 2597 int srcChar = (int)value[i]; 2598 int lowerChar; 2599 char[] lowerCharArray; 2600 srcCount = 1; 2601 if (Character.isSurrogate((char)srcChar)) { 2602 srcChar = codePointAt(i); 2603 srcCount = Character.charCount(srcChar); 2604 } 2605 if (localeDependent || srcChar == '\u03A3') { // GREEK CAPITAL LETTER SIGMA 2606 lowerChar = ConditionalSpecialCasing.toLowerCaseEx(this, i, locale); 2607 } else { 2608 lowerChar = Character.toLowerCase(srcChar); 2609 } 2610 if (Character.isBmpCodePoint(lowerChar)) { // Character.ERROR is not a bmp 2611 result[resultOffset++] = (char)lowerChar; 2612 } else { 2613 if (lowerChar == Character.ERROR) { 2614 lowerCharArray = ConditionalSpecialCasing.toLowerCaseCharArray(this, i, locale); 2615 } else if (srcCount == 2) { 2616 resultOffset += Character.toChars(lowerChar, result, resultOffset); 2617 continue; 2618 } else { 2619 lowerCharArray = Character.toChars(lowerChar); 2620 } 2621 /* Grow result if needed */ 2622 int mapLen = lowerCharArray.length; 2623 if (mapLen > srcCount) { 2624 char[] result2 = new char[result.length + mapLen - srcCount]; 2625 System.arraycopy(result, 0, result2, 0, resultOffset); 2626 result = result2; 2627 } 2628 for (int x = 0; x < mapLen; ++x) { 2629 result[resultOffset++] = lowerCharArray[x]; 2630 } 2631 } 2632 } 2633 return new String(result, 0, resultOffset); 2634 } 2635 2636 /** 2637 * Converts all of the characters in this {@code String} to lower 2638 * case using the rules of the default locale. This is equivalent to calling 2639 * {@code toLowerCase(Locale.getDefault())}. 2640 * <p> 2641 * <b>Note:</b> This method is locale sensitive, and may produce unexpected 2642 * results if used for strings that are intended to be interpreted locale 2643 * independently. 2644 * Examples are programming language identifiers, protocol keys, and HTML 2645 * tags. 2646 * For instance, {@code "TITLE".toLowerCase()} in a Turkish locale 2647 * returns {@code "t\u005Cu0131tle"}, where '\u005Cu0131' is the 2648 * LATIN SMALL LETTER DOTLESS I character. 2649 * To obtain correct results for locale insensitive strings, use 2650 * {@code toLowerCase(Locale.ROOT)}. 2651 * <p> 2652 * @return the {@code String}, converted to lowercase. 2653 * @see java.lang.String#toLowerCase(Locale) 2691 * <td>small letter sharp s -> two letters: SS</td> 2692 * </tr> 2693 * <tr> 2694 * <td>(all)</td> 2695 * <td>Fahrvergnügen</td> 2696 * <td>FAHRVERGNÜGEN</td> 2697 * <td></td> 2698 * </tr> 2699 * </table> 2700 * @param locale use the case transformation rules for this locale 2701 * @return the {@code String}, converted to uppercase. 2702 * @see java.lang.String#toUpperCase() 2703 * @see java.lang.String#toLowerCase() 2704 * @see java.lang.String#toLowerCase(Locale) 2705 * @since 1.1 2706 */ 2707 public String toUpperCase(Locale locale) { 2708 if (locale == null) { 2709 throw new NullPointerException(); 2710 } 2711 int first; 2712 boolean hasSurr = false; 2713 final int len = value.length; 2714 2715 // Now check if there are any characters that need to be changed, or are surrogate 2716 for (first = 0 ; first < len; first++ ) { 2717 int cp = (int)value[first]; 2718 if (Character.isSurrogate((char)cp)) { 2719 hasSurr = true; 2720 break; 2721 } 2722 if (cp != Character.toUpperCaseEx(cp)) { // no need to check Character.ERROR 2723 break; 2724 } 2725 } 2726 if (first == len) { 2727 return this; 2728 } 2729 char[] result = new char[len]; 2730 System.arraycopy(value, 0, result, 0, first); // Just copy the first few 2731 // upperCase characters. 2732 String lang = locale.getLanguage(); 2733 if (lang == "tr" || lang == "az" || lang == "lt") { 2734 return toUpperCaseEx(result, first, locale, true); 2735 } 2736 if (hasSurr) { 2737 return toUpperCaseEx(result, first, locale, false); 2738 } 2739 for (int i = first; i < len; i++) { 2740 int cp = Character.toUpperCaseEx((int)value[i]); 2741 if (!Character.isBmpCodePoint(cp)) { // Character.ERROR is not bmp 2742 return toUpperCaseEx(result, i, locale, false); 2743 } 2744 result[i] = (char)cp; 2745 } 2746 return new String(result, true); 2747 } 2748 2749 private String toUpperCaseEx(char[] result, int first, Locale locale, 2750 boolean localeDependent) { 2751 int resultOffset = first; 2752 int srcCount; 2753 for (int i = first; i < value.length; i += srcCount) { 2754 int srcChar = (int)value[i]; 2755 int upperChar; 2756 char[] upperCharArray; 2757 srcCount = 1; 2758 if (Character.isSurrogate((char)srcChar)) { 2759 srcChar = codePointAt(i); 2760 srcCount = Character.charCount(srcChar); 2761 } 2762 if (localeDependent) { 2763 upperChar = ConditionalSpecialCasing.toUpperCaseEx(this, i, locale); 2764 } else { 2765 upperChar = Character.toUpperCaseEx(srcChar); 2766 } 2767 if (Character.isBmpCodePoint(upperChar)) { 2768 result[resultOffset++] = (char)upperChar; 2769 } else { 2770 if (upperChar == Character.ERROR) { 2771 if (localeDependent) { 2772 upperCharArray = 2773 ConditionalSpecialCasing.toUpperCaseCharArray(this, i, locale); 2774 } else { 2775 upperCharArray = Character.toUpperCaseCharArray(srcChar); 2776 } 2777 } else if (srcCount == 2) { 2778 resultOffset += Character.toChars(upperChar, result, resultOffset); 2779 continue; 2780 } else { 2781 upperCharArray = Character.toChars(upperChar); 2782 } 2783 /* Grow result if needed */ 2784 int mapLen = upperCharArray.length; 2785 if (mapLen > srcCount) { 2786 char[] result2 = new char[result.length + mapLen - srcCount]; 2787 System.arraycopy(result, 0, result2, 0, resultOffset); 2788 result = result2; 2789 } 2790 for (int x = 0; x < mapLen; ++x) { 2791 result[resultOffset++] = upperCharArray[x]; 2792 } 2793 } 2794 } 2795 return new String(result, 0, resultOffset); 2796 } 2797 2798 /** 2799 * Converts all of the characters in this {@code String} to upper 2800 * case using the rules of the default locale. This method is equivalent to 2801 * {@code toUpperCase(Locale.getDefault())}. 2802 * <p> 2803 * <b>Note:</b> This method is locale sensitive, and may produce unexpected 2804 * results if used for strings that are intended to be interpreted locale 2805 * independently. 2806 * Examples are programming language identifiers, protocol keys, and HTML 2807 * tags. 2808 * For instance, {@code "title".toUpperCase()} in a Turkish locale 2809 * returns {@code "T\u005Cu0130TLE"}, where '\u005Cu0130' is the 2810 * LATIN CAPITAL LETTER I WITH DOT ABOVE character. 2811 * To obtain correct results for locale insensitive strings, use 2812 * {@code toUpperCase(Locale.ROOT)}. 2813 * <p> 2814 * @return the {@code String}, converted to uppercase. 2815 * @see java.lang.String#toUpperCase(Locale) |