branch | datagramsocketimpl-branch |
changeset 58678 | 9cf78a70fa4f |
parent 55062 | 45128070fd15 |
child 58679 | 9c3209ff7550 |
58677:13588c901957 | 58678:9cf78a70fa4f |
---|---|
34 import java.nio.charset.Charset; |
34 import java.nio.charset.Charset; |
35 import java.util.ArrayList; |
35 import java.util.ArrayList; |
36 import java.util.Arrays; |
36 import java.util.Arrays; |
37 import java.util.Comparator; |
37 import java.util.Comparator; |
38 import java.util.Formatter; |
38 import java.util.Formatter; |
39 import java.util.List; |
|
39 import java.util.Locale; |
40 import java.util.Locale; |
40 import java.util.Objects; |
41 import java.util.Objects; |
41 import java.util.Optional; |
42 import java.util.Optional; |
42 import java.util.Spliterator; |
43 import java.util.Spliterator; |
43 import java.util.StringJoiner; |
44 import java.util.StringJoiner; |
169 * us to avoid recalculating this. |
170 * us to avoid recalculating this. |
170 */ |
171 */ |
171 private boolean hashIsZero; // Default to false; |
172 private boolean hashIsZero; // Default to false; |
172 |
173 |
173 /** use serialVersionUID from JDK 1.0.2 for interoperability */ |
174 /** use serialVersionUID from JDK 1.0.2 for interoperability */ |
175 @java.io.Serial |
|
174 private static final long serialVersionUID = -6849794470754667710L; |
176 private static final long serialVersionUID = -6849794470754667710L; |
175 |
177 |
176 /** |
178 /** |
177 * If String compaction is disabled, the bytes in {@code value} are |
179 * If String compaction is disabled, the bytes in {@code value} are |
178 * always encoded in UTF16. |
180 * always encoded in UTF16. |
221 * |
223 * |
222 * A String instance is written into an ObjectOutputStream according to |
224 * A String instance is written into an ObjectOutputStream according to |
223 * <a href="{@docRoot}/../specs/serialization/protocol.html#stream-elements"> |
225 * <a href="{@docRoot}/../specs/serialization/protocol.html#stream-elements"> |
224 * Object Serialization Specification, Section 6.2, "Stream Elements"</a> |
226 * Object Serialization Specification, Section 6.2, "Stream Elements"</a> |
225 */ |
227 */ |
228 @java.io.Serial |
|
226 private static final ObjectStreamField[] serialPersistentFields = |
229 private static final ObjectStreamField[] serialPersistentFields = |
227 new ObjectStreamField[0]; |
230 new ObjectStreamField[0]; |
228 |
231 |
229 /** |
232 /** |
230 * Initializes a newly created {@code String} object so that it represents |
233 * Initializes a newly created {@code String} object so that it represents |
697 * value is returned. |
700 * value is returned. |
698 * |
701 * |
699 * @param index the index of the {@code char} value. |
702 * @param index the index of the {@code char} value. |
700 * @return the {@code char} value at the specified index of this string. |
703 * @return the {@code char} value at the specified index of this string. |
701 * The first {@code char} value is at index {@code 0}. |
704 * The first {@code char} value is at index {@code 0}. |
702 * @exception IndexOutOfBoundsException if the {@code index} |
705 * @throws IndexOutOfBoundsException if the {@code index} |
703 * argument is negative or not less than the length of this |
706 * argument is negative or not less than the length of this |
704 * string. |
707 * string. |
705 */ |
708 */ |
706 public char charAt(int index) { |
709 public char charAt(int index) { |
707 if (isLatin1()) { |
710 if (isLatin1()) { |
726 * the {@code char} value at the given index is returned. |
729 * the {@code char} value at the given index is returned. |
727 * |
730 * |
728 * @param index the index to the {@code char} values |
731 * @param index the index to the {@code char} values |
729 * @return the code point value of the character at the |
732 * @return the code point value of the character at the |
730 * {@code index} |
733 * {@code index} |
731 * @exception IndexOutOfBoundsException if the {@code index} |
734 * @throws IndexOutOfBoundsException if the {@code index} |
732 * argument is negative or not less than the length of this |
735 * argument is negative or not less than the length of this |
733 * string. |
736 * string. |
734 * @since 1.5 |
737 * @since 1.5 |
735 */ |
738 */ |
736 public int codePointAt(int index) { |
739 public int codePointAt(int index) { |
758 * 1} is an unpaired low-surrogate or a high-surrogate, the |
761 * 1} is an unpaired low-surrogate or a high-surrogate, the |
759 * surrogate value is returned. |
762 * surrogate value is returned. |
760 * |
763 * |
761 * @param index the index following the code point that should be returned |
764 * @param index the index following the code point that should be returned |
762 * @return the Unicode code point value before the given index. |
765 * @return the Unicode code point value before the given index. |
763 * @exception IndexOutOfBoundsException if the {@code index} |
766 * @throws IndexOutOfBoundsException if the {@code index} |
764 * argument is less than 1 or greater than the length |
767 * argument is less than 1 or greater than the length |
765 * of this string. |
768 * of this string. |
766 * @since 1.5 |
769 * @since 1.5 |
767 */ |
770 */ |
768 public int codePointBefore(int index) { |
771 public int codePointBefore(int index) { |
789 * the text range. |
792 * the text range. |
790 * @param endIndex the index after the last {@code char} of |
793 * @param endIndex the index after the last {@code char} of |
791 * the text range. |
794 * the text range. |
792 * @return the number of Unicode code points in the specified text |
795 * @return the number of Unicode code points in the specified text |
793 * range |
796 * range |
794 * @exception IndexOutOfBoundsException if the |
797 * @throws IndexOutOfBoundsException if the |
795 * {@code beginIndex} is negative, or {@code endIndex} |
798 * {@code beginIndex} is negative, or {@code endIndex} |
796 * is larger than the length of this {@code String}, or |
799 * is larger than the length of this {@code String}, or |
797 * {@code beginIndex} is larger than {@code endIndex}. |
800 * {@code beginIndex} is larger than {@code endIndex}. |
798 * @since 1.5 |
801 * @since 1.5 |
799 */ |
802 */ |
816 * {@code codePointOffset} count as one code point each. |
819 * {@code codePointOffset} count as one code point each. |
817 * |
820 * |
818 * @param index the index to be offset |
821 * @param index the index to be offset |
819 * @param codePointOffset the offset in code points |
822 * @param codePointOffset the offset in code points |
820 * @return the index within this {@code String} |
823 * @return the index within this {@code String} |
821 * @exception IndexOutOfBoundsException if {@code index} |
824 * @throws IndexOutOfBoundsException if {@code index} |
822 * is negative or larger then the length of this |
825 * is negative or larger then the length of this |
823 * {@code String}, or if {@code codePointOffset} is positive |
826 * {@code String}, or if {@code codePointOffset} is positive |
824 * and the substring starting with {@code index} has fewer |
827 * and the substring starting with {@code index} has fewer |
825 * than {@code codePointOffset} code points, |
828 * than {@code codePointOffset} code points, |
826 * or if {@code codePointOffset} is negative and the substring |
829 * or if {@code codePointOffset} is negative and the substring |
853 * to copy. |
856 * to copy. |
854 * @param srcEnd index after the last character in the string |
857 * @param srcEnd index after the last character in the string |
855 * to copy. |
858 * to copy. |
856 * @param dst the destination array. |
859 * @param dst the destination array. |
857 * @param dstBegin the start offset in the destination array. |
860 * @param dstBegin the start offset in the destination array. |
858 * @exception IndexOutOfBoundsException If any of the following |
861 * @throws IndexOutOfBoundsException If any of the following |
859 * is true: |
862 * is true: |
860 * <ul><li>{@code srcBegin} is negative. |
863 * <ul><li>{@code srcBegin} is negative. |
861 * <li>{@code srcBegin} is greater than {@code srcEnd} |
864 * <li>{@code srcBegin} is greater than {@code srcEnd} |
862 * <li>{@code srcEnd} is greater than the length of this |
865 * <li>{@code srcEnd} is greater than the length of this |
863 * string |
866 * string |
1232 public static final Comparator<String> CASE_INSENSITIVE_ORDER |
1235 public static final Comparator<String> CASE_INSENSITIVE_ORDER |
1233 = new CaseInsensitiveComparator(); |
1236 = new CaseInsensitiveComparator(); |
1234 private static class CaseInsensitiveComparator |
1237 private static class CaseInsensitiveComparator |
1235 implements Comparator<String>, java.io.Serializable { |
1238 implements Comparator<String>, java.io.Serializable { |
1236 // use serialVersionUID from JDK 1.2.2 for interoperability |
1239 // use serialVersionUID from JDK 1.2.2 for interoperability |
1240 @java.io.Serial |
|
1237 private static final long serialVersionUID = 8575799808933029326L; |
1241 private static final long serialVersionUID = 8575799808933029326L; |
1238 |
1242 |
1239 public int compare(String s1, String s2) { |
1243 public int compare(String s1, String s2) { |
1240 byte v1[] = s1.value; |
1244 byte v1[] = s1.value; |
1241 byte v2[] = s2.value; |
1245 byte v2[] = s2.value; |
1247 return coder == LATIN1 ? StringLatin1.compareToCI_UTF16(v1, v2) |
1251 return coder == LATIN1 ? StringLatin1.compareToCI_UTF16(v1, v2) |
1248 : StringUTF16.compareToCI_Latin1(v1, v2); |
1252 : StringUTF16.compareToCI_Latin1(v1, v2); |
1249 } |
1253 } |
1250 |
1254 |
1251 /** Replaces the de-serialized object. */ |
1255 /** Replaces the de-serialized object. */ |
1256 @java.io.Serial |
|
1252 private Object readResolve() { return CASE_INSENSITIVE_ORDER; } |
1257 private Object readResolve() { return CASE_INSENSITIVE_ORDER; } |
1253 } |
1258 } |
1254 |
1259 |
1255 /** |
1260 /** |
1256 * Compares two strings lexicographically, ignoring case |
1261 * Compares two strings lexicographically, ignoring case |
1860 * "emptiness".substring(9) returns "" (an empty string) |
1865 * "emptiness".substring(9) returns "" (an empty string) |
1861 * </pre></blockquote> |
1866 * </pre></blockquote> |
1862 * |
1867 * |
1863 * @param beginIndex the beginning index, inclusive. |
1868 * @param beginIndex the beginning index, inclusive. |
1864 * @return the specified substring. |
1869 * @return the specified substring. |
1865 * @exception IndexOutOfBoundsException if |
1870 * @throws IndexOutOfBoundsException if |
1866 * {@code beginIndex} is negative or larger than the |
1871 * {@code beginIndex} is negative or larger than the |
1867 * length of this {@code String} object. |
1872 * length of this {@code String} object. |
1868 */ |
1873 */ |
1869 public String substring(int beginIndex) { |
1874 public String substring(int beginIndex) { |
1870 if (beginIndex < 0) { |
1875 return substring(beginIndex, length()); |
1871 throw new StringIndexOutOfBoundsException(beginIndex); |
|
1872 } |
|
1873 int subLen = length() - beginIndex; |
|
1874 if (subLen < 0) { |
|
1875 throw new StringIndexOutOfBoundsException(subLen); |
|
1876 } |
|
1877 if (beginIndex == 0) { |
|
1878 return this; |
|
1879 } |
|
1880 return isLatin1() ? StringLatin1.newString(value, beginIndex, subLen) |
|
1881 : StringUTF16.newString(value, beginIndex, subLen); |
|
1882 } |
1876 } |
1883 |
1877 |
1884 /** |
1878 /** |
1885 * Returns a string that is a substring of this string. The |
1879 * Returns a string that is a substring of this string. The |
1886 * substring begins at the specified {@code beginIndex} and |
1880 * substring begins at the specified {@code beginIndex} and |
1894 * </pre></blockquote> |
1888 * </pre></blockquote> |
1895 * |
1889 * |
1896 * @param beginIndex the beginning index, inclusive. |
1890 * @param beginIndex the beginning index, inclusive. |
1897 * @param endIndex the ending index, exclusive. |
1891 * @param endIndex the ending index, exclusive. |
1898 * @return the specified substring. |
1892 * @return the specified substring. |
1899 * @exception IndexOutOfBoundsException if the |
1893 * @throws IndexOutOfBoundsException if the |
1900 * {@code beginIndex} is negative, or |
1894 * {@code beginIndex} is negative, or |
1901 * {@code endIndex} is larger than the length of |
1895 * {@code endIndex} is larger than the length of |
1902 * this {@code String} object, or |
1896 * this {@code String} object, or |
1903 * {@code beginIndex} is larger than |
1897 * {@code beginIndex} is larger than |
1904 * {@code endIndex}. |
1898 * {@code endIndex}. |
2795 */ |
2789 */ |
2796 public boolean isBlank() { |
2790 public boolean isBlank() { |
2797 return indexOfNonWhitespace() == length(); |
2791 return indexOfNonWhitespace() == length(); |
2798 } |
2792 } |
2799 |
2793 |
2800 private Stream<String> lines(int maxLeading, int maxTrailing) { |
|
2801 return isLatin1() ? StringLatin1.lines(value, maxLeading, maxTrailing) |
|
2802 : StringUTF16.lines(value, maxLeading, maxTrailing); |
|
2803 } |
|
2804 |
|
2805 /** |
2794 /** |
2806 * Returns a stream of lines extracted from this string, |
2795 * Returns a stream of lines extracted from this string, |
2807 * separated by line terminators. |
2796 * separated by line terminators. |
2808 * <p> |
2797 * <p> |
2809 * A <i>line terminator</i> is one of the following: |
2798 * A <i>line terminator</i> is one of the following: |
2831 * @return the stream of lines extracted from this string |
2820 * @return the stream of lines extracted from this string |
2832 * |
2821 * |
2833 * @since 11 |
2822 * @since 11 |
2834 */ |
2823 */ |
2835 public Stream<String> lines() { |
2824 public Stream<String> lines() { |
2836 return lines(0, 0); |
2825 return isLatin1() ? StringLatin1.lines(value) : StringUTF16.lines(value); |
2837 } |
2826 } |
2838 |
2827 |
2839 /** |
2828 /** |
2840 * Adjusts the indentation of each line of this string based on the value of |
2829 * Adjusts the indentation of each line of this string based on the value of |
2841 * {@code n}, and normalizes line termination characters. |
2830 * {@code n}, and normalizes line termination characters. |
2871 * @see Character#isWhitespace(int) |
2860 * @see Character#isWhitespace(int) |
2872 * |
2861 * |
2873 * @since 12 |
2862 * @since 12 |
2874 */ |
2863 */ |
2875 public String indent(int n) { |
2864 public String indent(int n) { |
2876 return isEmpty() ? "" : indent(n, false); |
2865 if (isEmpty()) { |
2877 } |
2866 return ""; |
2878 |
2867 } |
2879 private String indent(int n, boolean removeBlanks) { |
2868 Stream<String> stream = lines(); |
2880 Stream<String> stream = removeBlanks ? lines(Integer.MAX_VALUE, Integer.MAX_VALUE) |
|
2881 : lines(); |
|
2882 if (n > 0) { |
2869 if (n > 0) { |
2883 final String spaces = " ".repeat(n); |
2870 final String spaces = " ".repeat(n); |
2884 stream = stream.map(s -> spaces + s); |
2871 stream = stream.map(s -> spaces + s); |
2885 } else if (n == Integer.MIN_VALUE) { |
2872 } else if (n == Integer.MIN_VALUE) { |
2886 stream = stream.map(s -> s.stripLeading()); |
2873 stream = stream.map(s -> s.stripLeading()); |
2896 } |
2883 } |
2897 |
2884 |
2898 private int lastIndexOfNonWhitespace() { |
2885 private int lastIndexOfNonWhitespace() { |
2899 return isLatin1() ? StringLatin1.lastIndexOfNonWhitespace(value) |
2886 return isLatin1() ? StringLatin1.lastIndexOfNonWhitespace(value) |
2900 : StringUTF16.lastIndexOfNonWhitespace(value); |
2887 : StringUTF16.lastIndexOfNonWhitespace(value); |
2888 } |
|
2889 |
|
2890 /** |
|
2891 * Returns a string whose value is this string, with incidental |
|
2892 * {@linkplain Character#isWhitespace(int) white space} removed from |
|
2893 * the beginning and end of every line. |
|
2894 * <p> |
|
2895 * Incidental {@linkplain Character#isWhitespace(int) white space} |
|
2896 * is often present in a text block to align the content with the opening |
|
2897 * delimiter. For example, in the following code, dots represent incidental |
|
2898 * {@linkplain Character#isWhitespace(int) white space}: |
|
2899 * <blockquote><pre> |
|
2900 * String html = """ |
|
2901 * ..............<html> |
|
2902 * .............. <body> |
|
2903 * .............. <p>Hello, world</p> |
|
2904 * .............. </body> |
|
2905 * ..............</html> |
|
2906 * .............."""; |
|
2907 * </pre></blockquote> |
|
2908 * This method treats the incidental |
|
2909 * {@linkplain Character#isWhitespace(int) white space} as indentation to be |
|
2910 * stripped, producing a string that preserves the relative indentation of |
|
2911 * the content. Using | to visualize the start of each line of the string: |
|
2912 * <blockquote><pre> |
|
2913 * |<html> |
|
2914 * | <body> |
|
2915 * | <p>Hello, world</p> |
|
2916 * | </body> |
|
2917 * |</html> |
|
2918 * </pre></blockquote> |
|
2919 * First, the individual lines of this string are extracted as if by using |
|
2920 * {@link String#lines()}. |
|
2921 * <p> |
|
2922 * Then, the <i>minimum indentation</i> (min) is determined as follows. |
|
2923 * For each non-blank line (as defined by {@link String#isBlank()}), the |
|
2924 * leading {@linkplain Character#isWhitespace(int) white space} characters are |
|
2925 * counted. The leading {@linkplain Character#isWhitespace(int) white space} |
|
2926 * characters on the last line are also counted even if |
|
2927 * {@linkplain String#isBlank() blank}. The <i>min</i> value is the smallest |
|
2928 * of these counts. |
|
2929 * <p> |
|
2930 * For each {@linkplain String#isBlank() non-blank} line, <i>min</i> leading |
|
2931 * {@linkplain Character#isWhitespace(int) white space} characters are removed, |
|
2932 * and any trailing {@linkplain Character#isWhitespace(int) white space} |
|
2933 * characters are removed. {@linkplain String#isBlank() Blank} lines are |
|
2934 * replaced with the empty string. |
|
2935 * |
|
2936 * <p> |
|
2937 * Finally, the lines are joined into a new string, using the LF character |
|
2938 * {@code "\n"} (U+000A) to separate lines. |
|
2939 * |
|
2940 * @apiNote |
|
2941 * This method's primary purpose is to shift a block of lines as far as |
|
2942 * possible to the left, while preserving relative indentation. Lines |
|
2943 * that were indented the least will thus have no leading |
|
2944 * {@linkplain Character#isWhitespace(int) white space}. |
|
2945 * The line count of the result will be the same as line count of this |
|
2946 * string. |
|
2947 * If this string ends with a line terminator then the result will end |
|
2948 * with a line terminator. |
|
2949 * |
|
2950 * @implNote |
|
2951 * This method treats all {@linkplain Character#isWhitespace(int) white space} |
|
2952 * characters as having equal width. As long as the indentation on every |
|
2953 * line is consistently composed of the same character sequences, then the |
|
2954 * result will be as described above. |
|
2955 * |
|
2956 * @return string with incidental indentation removed and line |
|
2957 * terminators normalized |
|
2958 * |
|
2959 * @see String#lines() |
|
2960 * @see String#isBlank() |
|
2961 * @see String#indent(int) |
|
2962 * @see Character#isWhitespace(int) |
|
2963 * |
|
2964 * @since 13 |
|
2965 * |
|
2966 * @deprecated This method is associated with text blocks, a preview language feature. |
|
2967 * Text blocks and/or this method may be changed or removed in a future release. |
|
2968 */ |
|
2969 @Deprecated(forRemoval=true, since="13") |
|
2970 public String stripIndent() { |
|
2971 int length = length(); |
|
2972 if (length == 0) { |
|
2973 return ""; |
|
2974 } |
|
2975 char lastChar = charAt(length - 1); |
|
2976 boolean optOut = lastChar == '\n' || lastChar == '\r'; |
|
2977 List<String> lines = lines().collect(Collectors.toList()); |
|
2978 final int outdent = optOut ? 0 : outdent(lines); |
|
2979 return lines.stream() |
|
2980 .map(line -> { |
|
2981 int firstNonWhitespace = line.indexOfNonWhitespace(); |
|
2982 int lastNonWhitespace = line.lastIndexOfNonWhitespace(); |
|
2983 int incidentalWhitespace = Math.min(outdent, firstNonWhitespace); |
|
2984 return firstNonWhitespace > lastNonWhitespace |
|
2985 ? "" : line.substring(incidentalWhitespace, lastNonWhitespace); |
|
2986 }) |
|
2987 .collect(Collectors.joining("\n", "", optOut ? "\n" : "")); |
|
2988 } |
|
2989 |
|
2990 private static int outdent(List<String> lines) { |
|
2991 // Note: outdent is guaranteed to be zero or positive number. |
|
2992 // If there isn't a non-blank line then the last must be blank |
|
2993 int outdent = Integer.MAX_VALUE; |
|
2994 for (String line : lines) { |
|
2995 int leadingWhitespace = line.indexOfNonWhitespace(); |
|
2996 if (leadingWhitespace != line.length()) { |
|
2997 outdent = Integer.min(outdent, leadingWhitespace); |
|
2998 } |
|
2999 } |
|
3000 String lastLine = lines.get(lines.size() - 1); |
|
3001 if (lastLine.isBlank()) { |
|
3002 outdent = Integer.min(outdent, lastLine.length()); |
|
3003 } |
|
3004 return outdent; |
|
3005 } |
|
3006 |
|
3007 /** |
|
3008 * Returns a string whose value is this string, with escape sequences |
|
3009 * translated as if in a string literal. |
|
3010 * <p> |
|
3011 * Escape sequences are translated as follows; |
|
3012 * <table class="striped"> |
|
3013 * <caption style="display:none">Translation</caption> |
|
3014 * <thead> |
|
3015 * <tr> |
|
3016 * <th scope="col">Escape</th> |
|
3017 * <th scope="col">Name</th> |
|
3018 * <th scope="col">Translation</th> |
|
3019 * </tr> |
|
3020 * </thead> |
|
3021 * <tbody> |
|
3022 * <tr> |
|
3023 * <th scope="row">{@code \u005Cb}</th> |
|
3024 * <td>backspace</td> |
|
3025 * <td>{@code U+0008}</td> |
|
3026 * </tr> |
|
3027 * <tr> |
|
3028 * <th scope="row">{@code \u005Ct}</th> |
|
3029 * <td>horizontal tab</td> |
|
3030 * <td>{@code U+0009}</td> |
|
3031 * </tr> |
|
3032 * <tr> |
|
3033 * <th scope="row">{@code \u005Cn}</th> |
|
3034 * <td>line feed</td> |
|
3035 * <td>{@code U+000A}</td> |
|
3036 * </tr> |
|
3037 * <tr> |
|
3038 * <th scope="row">{@code \u005Cf}</th> |
|
3039 * <td>form feed</td> |
|
3040 * <td>{@code U+000C}</td> |
|
3041 * </tr> |
|
3042 * <tr> |
|
3043 * <th scope="row">{@code \u005Cr}</th> |
|
3044 * <td>carriage return</td> |
|
3045 * <td>{@code U+000D}</td> |
|
3046 * </tr> |
|
3047 * <tr> |
|
3048 * <th scope="row">{@code \u005C"}</th> |
|
3049 * <td>double quote</td> |
|
3050 * <td>{@code U+0022}</td> |
|
3051 * </tr> |
|
3052 * <tr> |
|
3053 * <th scope="row">{@code \u005C'}</th> |
|
3054 * <td>single quote</td> |
|
3055 * <td>{@code U+0027}</td> |
|
3056 * </tr> |
|
3057 * <tr> |
|
3058 * <th scope="row">{@code \u005C\u005C}</th> |
|
3059 * <td>backslash</td> |
|
3060 * <td>{@code U+005C}</td> |
|
3061 * </tr> |
|
3062 * <tr> |
|
3063 * <th scope="row">{@code \u005C0 - \u005C377}</th> |
|
3064 * <td>octal escape</td> |
|
3065 * <td>code point equivalents</td> |
|
3066 * </tr> |
|
3067 * </tbody> |
|
3068 * </table> |
|
3069 * |
|
3070 * @implNote |
|
3071 * This method does <em>not</em> translate Unicode escapes such as "{@code \u005cu2022}". |
|
3072 * Unicode escapes are translated by the Java compiler when reading input characters and |
|
3073 * are not part of the string literal specification. |
|
3074 * |
|
3075 * @throws IllegalArgumentException when an escape sequence is malformed. |
|
3076 * |
|
3077 * @return String with escape sequences translated. |
|
3078 * |
|
3079 * @jls 3.10.7 Escape Sequences |
|
3080 * |
|
3081 * @since 13 |
|
3082 * |
|
3083 * @deprecated This method is associated with text blocks, a preview language feature. |
|
3084 * Text blocks and/or this method may be changed or removed in a future release. |
|
3085 */ |
|
3086 @Deprecated(forRemoval=true, since="13") |
|
3087 public String translateEscapes() { |
|
3088 if (isEmpty()) { |
|
3089 return ""; |
|
3090 } |
|
3091 char[] chars = toCharArray(); |
|
3092 int length = chars.length; |
|
3093 int from = 0; |
|
3094 int to = 0; |
|
3095 while (from < length) { |
|
3096 char ch = chars[from++]; |
|
3097 if (ch == '\\') { |
|
3098 ch = from < length ? chars[from++] : '\0'; |
|
3099 switch (ch) { |
|
3100 case 'b': |
|
3101 ch = '\b'; |
|
3102 break; |
|
3103 case 'f': |
|
3104 ch = '\f'; |
|
3105 break; |
|
3106 case 'n': |
|
3107 ch = '\n'; |
|
3108 break; |
|
3109 case 'r': |
|
3110 ch = '\r'; |
|
3111 break; |
|
3112 case 't': |
|
3113 ch = '\t'; |
|
3114 break; |
|
3115 case '\'': |
|
3116 case '\"': |
|
3117 case '\\': |
|
3118 // as is |
|
3119 break; |
|
3120 case '0': case '1': case '2': case '3': |
|
3121 case '4': case '5': case '6': case '7': |
|
3122 int limit = Integer.min(from + (ch <= '3' ? 2 : 1), length); |
|
3123 int code = ch - '0'; |
|
3124 while (from < limit) { |
|
3125 ch = chars[from]; |
|
3126 if (ch < '0' || '7' < ch) { |
|
3127 break; |
|
3128 } |
|
3129 from++; |
|
3130 code = (code << 3) | (ch - '0'); |
|
3131 } |
|
3132 ch = (char)code; |
|
3133 break; |
|
3134 default: { |
|
3135 String msg = String.format( |
|
3136 "Invalid escape sequence: \\%c \\\\u%04X", |
|
3137 ch, (int)ch); |
|
3138 throw new IllegalArgumentException(msg); |
|
3139 } |
|
3140 } |
|
3141 } |
|
3142 |
|
3143 chars[to++] = ch; |
|
3144 } |
|
3145 |
|
3146 return new String(chars, 0, to); |
|
2901 } |
3147 } |
2902 |
3148 |
2903 /** |
3149 /** |
2904 * This method allows the application of a function to {@code this} |
3150 * This method allows the application of a function to {@code this} |
2905 * string. The function should expect a single String argument |
3151 * string. The function should expect a single String argument |
3061 public static String format(Locale l, String format, Object... args) { |
3307 public static String format(Locale l, String format, Object... args) { |
3062 return new Formatter(l).format(format, args).toString(); |
3308 return new Formatter(l).format(format, args).toString(); |
3063 } |
3309 } |
3064 |
3310 |
3065 /** |
3311 /** |
3312 * Formats using this string as the format string, and the supplied |
|
3313 * arguments. |
|
3314 * |
|
3315 * @implSpec This method is equivalent to {@code String.format(this, args)}. |
|
3316 * |
|
3317 * @param args |
|
3318 * Arguments referenced by the format specifiers in this string. |
|
3319 * |
|
3320 * @return A formatted string |
|
3321 * |
|
3322 * @see java.lang.String#format(String,Object...) |
|
3323 * @see java.util.Formatter |
|
3324 * |
|
3325 * @since 13 |
|
3326 * |
|
3327 * @deprecated This method is associated with text blocks, a preview language feature. |
|
3328 * Text blocks and/or this method may be changed or removed in a future release. |
|
3329 */ |
|
3330 @Deprecated(forRemoval=true, since="13") |
|
3331 public String formatted(Object... args) { |
|
3332 return new Formatter().format(this, args).toString(); |
|
3333 } |
|
3334 |
|
3335 /** |
|
3066 * Returns the string representation of the {@code Object} argument. |
3336 * Returns the string representation of the {@code Object} argument. |
3067 * |
3337 * |
3068 * @param obj an {@code Object}. |
3338 * @param obj an {@code Object}. |
3069 * @return if the argument is {@code null}, then a string equal to |
3339 * @return if the argument is {@code null}, then a string equal to |
3070 * {@code "null"}; otherwise, the value of |
3340 * {@code "null"}; otherwise, the value of |
3102 * @param data the character array. |
3372 * @param data the character array. |
3103 * @param offset initial offset of the subarray. |
3373 * @param offset initial offset of the subarray. |
3104 * @param count length of the subarray. |
3374 * @param count length of the subarray. |
3105 * @return a {@code String} that contains the characters of the |
3375 * @return a {@code String} that contains the characters of the |
3106 * specified subarray of the character array. |
3376 * specified subarray of the character array. |
3107 * @exception IndexOutOfBoundsException if {@code offset} is |
3377 * @throws IndexOutOfBoundsException if {@code offset} is |
3108 * negative, or {@code count} is negative, or |
3378 * negative, or {@code count} is negative, or |
3109 * {@code offset+count} is larger than |
3379 * {@code offset+count} is larger than |
3110 * {@code data.length}. |
3380 * {@code data.length}. |
3111 */ |
3381 */ |
3112 public static String valueOf(char data[], int offset, int count) { |
3382 public static String valueOf(char data[], int offset, int count) { |
3119 * @param data the character array. |
3389 * @param data the character array. |
3120 * @param offset initial offset of the subarray. |
3390 * @param offset initial offset of the subarray. |
3121 * @param count length of the subarray. |
3391 * @param count length of the subarray. |
3122 * @return a {@code String} that contains the characters of the |
3392 * @return a {@code String} that contains the characters of the |
3123 * specified subarray of the character array. |
3393 * specified subarray of the character array. |
3124 * @exception IndexOutOfBoundsException if {@code offset} is |
3394 * @throws IndexOutOfBoundsException if {@code offset} is |
3125 * negative, or {@code count} is negative, or |
3395 * negative, or {@code count} is negative, or |
3126 * {@code offset+count} is larger than |
3396 * {@code offset+count} is larger than |
3127 * {@code data.length}. |
3397 * {@code data.length}. |
3128 */ |
3398 */ |
3129 public static String copyValueOf(char data[], int offset, int count) { |
3399 public static String copyValueOf(char data[], int offset, int count) { |
3384 |
3654 |
3385 byte[] value() { |
3655 byte[] value() { |
3386 return value; |
3656 return value; |
3387 } |
3657 } |
3388 |
3658 |
3389 private boolean isLatin1() { |
3659 boolean isLatin1() { |
3390 return COMPACT_STRINGS && coder == LATIN1; |
3660 return COMPACT_STRINGS && coder == LATIN1; |
3391 } |
3661 } |
3392 |
3662 |
3393 @Native static final byte LATIN1 = 0; |
3663 @Native static final byte LATIN1 = 0; |
3394 @Native static final byte UTF16 = 1; |
3664 @Native static final byte UTF16 = 1; |
3398 * negative or greater than or equal to {@code length}. |
3668 * negative or greater than or equal to {@code length}. |
3399 */ |
3669 */ |
3400 static void checkIndex(int index, int length) { |
3670 static void checkIndex(int index, int length) { |
3401 if (index < 0 || index >= length) { |
3671 if (index < 0 || index >= length) { |
3402 throw new StringIndexOutOfBoundsException("index " + index + |
3672 throw new StringIndexOutOfBoundsException("index " + index + |
3403 ",length " + length); |
3673 ", length " + length); |
3404 } |
3674 } |
3405 } |
3675 } |
3406 |
3676 |
3407 /* |
3677 /* |
3408 * StringIndexOutOfBoundsException if {@code offset} |
3678 * StringIndexOutOfBoundsException if {@code offset} |
3409 * is negative or greater than {@code length}. |
3679 * is negative or greater than {@code length}. |
3410 */ |
3680 */ |
3411 static void checkOffset(int offset, int length) { |
3681 static void checkOffset(int offset, int length) { |
3412 if (offset < 0 || offset > length) { |
3682 if (offset < 0 || offset > length) { |
3413 throw new StringIndexOutOfBoundsException("offset " + offset + |
3683 throw new StringIndexOutOfBoundsException("offset " + offset + |
3414 ",length " + length); |
3684 ", length " + length); |
3415 } |
3685 } |
3416 } |
3686 } |
3417 |
3687 |
3418 /* |
3688 /* |
3419 * Check {@code offset}, {@code count} against {@code 0} and {@code length} |
3689 * Check {@code offset}, {@code count} against {@code 0} and {@code length} |