author | redestad |
Wed, 17 Apr 2019 00:06:38 +0200 | |
changeset 54550 | 5fa7fbddfe9d |
parent 54486 | 7fd299216e97 |
child 54597 | ab57d6bebed8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
53563 | 2 |
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.lang; |
|
27 |
||
28 |
import java.io.ObjectStreamField; |
|
29 |
import java.io.UnsupportedEncodingException; |
|
45572
07f412070bd9
8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents:
45138
diff
changeset
|
30 |
import java.lang.annotation.Native; |
52914 | 31 |
import java.lang.invoke.MethodHandles; |
32 |
import java.lang.constant.Constable; |
|
33 |
import java.lang.constant.ConstantDesc; |
|
2 | 34 |
import java.nio.charset.Charset; |
35 |
import java.util.ArrayList; |
|
36 |
import java.util.Arrays; |
|
37 |
import java.util.Comparator; |
|
38 |
import java.util.Formatter; |
|
39 |
import java.util.Locale; |
|
15312
4b57f9da8192
4247235: (spec str) StringBuffer.insert(int, char[]) specification is inconsistent
jgish
parents:
14997
diff
changeset
|
40 |
import java.util.Objects; |
52914 | 41 |
import java.util.Optional; |
28667
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
42 |
import java.util.Spliterator; |
17181
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
43 |
import java.util.StringJoiner; |
52682 | 44 |
import java.util.function.Function; |
2 | 45 |
import java.util.regex.Matcher; |
46 |
import java.util.regex.Pattern; |
|
47 |
import java.util.regex.PatternSyntaxException; |
|
51714 | 48 |
import java.util.stream.Collectors; |
28667
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
49 |
import java.util.stream.IntStream; |
50215 | 50 |
import java.util.stream.Stream; |
28667
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
51 |
import java.util.stream.StreamSupport; |
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
30913
diff
changeset
|
52 |
import jdk.internal.HotSpotIntrinsicCandidate; |
36413 | 53 |
import jdk.internal.vm.annotation.Stable; |
2 | 54 |
|
51714 | 55 |
import static java.util.function.Predicate.not; |
56 |
||
2 | 57 |
/** |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
58 |
* The {@code String} class represents character strings. All |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
59 |
* string literals in Java programs, such as {@code "abc"}, are |
2 | 60 |
* implemented as instances of this class. |
61 |
* <p> |
|
62 |
* Strings are constant; their values cannot be changed after they |
|
63 |
* are created. String buffers support mutable strings. |
|
64 |
* Because String objects are immutable they can be shared. For example: |
|
21334 | 65 |
* <blockquote><pre> |
2 | 66 |
* String str = "abc"; |
67 |
* </pre></blockquote><p> |
|
68 |
* is equivalent to: |
|
21334 | 69 |
* <blockquote><pre> |
2 | 70 |
* char data[] = {'a', 'b', 'c'}; |
71 |
* String str = new String(data); |
|
72 |
* </pre></blockquote><p> |
|
73 |
* Here are some more examples of how strings can be used: |
|
21334 | 74 |
* <blockquote><pre> |
2 | 75 |
* System.out.println("abc"); |
76 |
* String cde = "cde"; |
|
77 |
* System.out.println("abc" + cde); |
|
78 |
* String c = "abc".substring(2,3); |
|
79 |
* String d = cde.substring(1, 2); |
|
80 |
* </pre></blockquote> |
|
81 |
* <p> |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
82 |
* The class {@code String} includes methods for examining |
2 | 83 |
* individual characters of the sequence, for comparing strings, for |
84 |
* searching strings, for extracting substrings, and for creating a |
|
85 |
* copy of a string with all characters translated to uppercase or to |
|
86 |
* lowercase. Case mapping is based on the Unicode Standard version |
|
87 |
* specified by the {@link java.lang.Character Character} class. |
|
88 |
* <p> |
|
89 |
* The Java language provides special support for the string |
|
90 |
* concatenation operator ( + ), and for conversion of |
|
37718
79b158dae903
8155215: java.lang.String concatenation spec is unnecessarily strong
shade
parents:
37521
diff
changeset
|
91 |
* other objects to strings. For additional information on string |
79b158dae903
8155215: java.lang.String concatenation spec is unnecessarily strong
shade
parents:
37521
diff
changeset
|
92 |
* concatenation and conversion, see <i>The Java™ Language Specification</i>. |
2 | 93 |
* |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31671
diff
changeset
|
94 |
* <p> Unless otherwise noted, passing a {@code null} argument to a constructor |
2 | 95 |
* or method in this class will cause a {@link NullPointerException} to be |
96 |
* thrown. |
|
97 |
* |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
98 |
* <p>A {@code String} represents a string in the UTF-16 format |
2 | 99 |
* in which <em>supplementary characters</em> are represented by <em>surrogate |
100 |
* pairs</em> (see the section <a href="Character.html#unicode">Unicode |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
101 |
* Character Representations</a> in the {@code Character} class for |
2 | 102 |
* more information). |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
103 |
* Index values refer to {@code char} code units, so a supplementary |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
104 |
* character uses two positions in a {@code String}. |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
105 |
* <p>The {@code String} class provides methods for dealing with |
2 | 106 |
* Unicode code points (i.e., characters), in addition to those for |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
107 |
* dealing with Unicode code units (i.e., {@code char} values). |
2 | 108 |
* |
33314
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
109 |
* <p>Unless otherwise noted, methods for comparing Strings do not take locale |
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
110 |
* into account. The {@link java.text.Collator} class provides methods for |
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
111 |
* finer-grain, locale-sensitive String comparison. |
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
112 |
* |
37718
79b158dae903
8155215: java.lang.String concatenation spec is unnecessarily strong
shade
parents:
37521
diff
changeset
|
113 |
* @implNote The implementation of the string concatenation operator is left to |
79b158dae903
8155215: java.lang.String concatenation spec is unnecessarily strong
shade
parents:
37521
diff
changeset
|
114 |
* the discretion of a Java compiler, as long as the compiler ultimately conforms |
79b158dae903
8155215: java.lang.String concatenation spec is unnecessarily strong
shade
parents:
37521
diff
changeset
|
115 |
* to <i>The Java™ Language Specification</i>. For example, the {@code javac} compiler |
79b158dae903
8155215: java.lang.String concatenation spec is unnecessarily strong
shade
parents:
37521
diff
changeset
|
116 |
* may implement the operator with {@code StringBuffer}, {@code StringBuilder}, |
79b158dae903
8155215: java.lang.String concatenation spec is unnecessarily strong
shade
parents:
37521
diff
changeset
|
117 |
* or {@code java.lang.invoke.StringConcatFactory} depending on the JDK version. The |
79b158dae903
8155215: java.lang.String concatenation spec is unnecessarily strong
shade
parents:
37521
diff
changeset
|
118 |
* implementation of string conversion is typically through the method {@code toString}, |
79b158dae903
8155215: java.lang.String concatenation spec is unnecessarily strong
shade
parents:
37521
diff
changeset
|
119 |
* defined by {@code Object} and inherited by all classes in Java. |
79b158dae903
8155215: java.lang.String concatenation spec is unnecessarily strong
shade
parents:
37521
diff
changeset
|
120 |
* |
2 | 121 |
* @author Lee Boynton |
122 |
* @author Arthur van Hoff |
|
5986
04eb44085c00
6934265: Add public method Character.isBmpCodePoint
martin
parents:
5506
diff
changeset
|
123 |
* @author Martin Buchholz |
04eb44085c00
6934265: Add public method Character.isBmpCodePoint
martin
parents:
5506
diff
changeset
|
124 |
* @author Ulf Zibis |
2 | 125 |
* @see java.lang.Object#toString() |
126 |
* @see java.lang.StringBuffer |
|
127 |
* @see java.lang.StringBuilder |
|
128 |
* @see java.nio.charset.Charset |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24374
diff
changeset
|
129 |
* @since 1.0 |
37723 | 130 |
* @jls 15.18.1 String Concatenation Operator + |
2 | 131 |
*/ |
132 |
||
133 |
public final class String |
|
52914 | 134 |
implements java.io.Serializable, Comparable<String>, CharSequence, |
135 |
Constable, ConstantDesc { |
|
33663 | 136 |
|
36413 | 137 |
/** |
138 |
* The value is used for character storage. |
|
139 |
* |
|
140 |
* @implNote This field is trusted by the VM, and is a subject to |
|
141 |
* constant folding if String instance is constant. Overwriting this |
|
142 |
* field after construction will cause problems. |
|
143 |
* |
|
144 |
* Additionally, it is marked with {@link Stable} to trust the contents |
|
145 |
* of the array. No other facility in JDK provides this functionality (yet). |
|
146 |
* {@link Stable} is safe here, because value is never null. |
|
147 |
*/ |
|
148 |
@Stable |
|
33663 | 149 |
private final byte[] value; |
150 |
||
151 |
/** |
|
152 |
* The identifier of the encoding used to encode the bytes in |
|
153 |
* {@code value}. The supported values in this implementation are |
|
154 |
* |
|
155 |
* LATIN1 |
|
156 |
* UTF16 |
|
157 |
* |
|
36413 | 158 |
* @implNote This field is trusted by the VM, and is a subject to |
159 |
* constant folding if String instance is constant. Overwriting this |
|
160 |
* field after construction will cause problems. |
|
33663 | 161 |
*/ |
162 |
private final byte coder; |
|
2 | 163 |
|
164 |
/** Cache the hash code for the string */ |
|
165 |
private int hash; // Default to 0 |
|
166 |
||
54486
7fd299216e97
8221836: Avoid recalculating String.hash when zero
redestad
parents:
54378
diff
changeset
|
167 |
/** |
7fd299216e97
8221836: Avoid recalculating String.hash when zero
redestad
parents:
54378
diff
changeset
|
168 |
* Cache if the hash has been calculated as actually being zero, enabling |
7fd299216e97
8221836: Avoid recalculating String.hash when zero
redestad
parents:
54378
diff
changeset
|
169 |
* us to avoid recalculating this. |
7fd299216e97
8221836: Avoid recalculating String.hash when zero
redestad
parents:
54378
diff
changeset
|
170 |
*/ |
7fd299216e97
8221836: Avoid recalculating String.hash when zero
redestad
parents:
54378
diff
changeset
|
171 |
private boolean hashIsZero; // Default to false; |
7fd299216e97
8221836: Avoid recalculating String.hash when zero
redestad
parents:
54378
diff
changeset
|
172 |
|
2 | 173 |
/** use serialVersionUID from JDK 1.0.2 for interoperability */ |
174 |
private static final long serialVersionUID = -6849794470754667710L; |
|
175 |
||
176 |
/** |
|
33663 | 177 |
* If String compaction is disabled, the bytes in {@code value} are |
178 |
* always encoded in UTF16. |
|
179 |
* |
|
180 |
* For methods with several possible implementation paths, when String |
|
181 |
* compaction is disabled, only one code path is taken. |
|
182 |
* |
|
183 |
* The instance field value is generally opaque to optimizing JIT |
|
184 |
* compilers. Therefore, in performance-sensitive place, an explicit |
|
185 |
* check of the static boolean {@code COMPACT_STRINGS} is done first |
|
186 |
* before checking the {@code coder} field since the static boolean |
|
187 |
* {@code COMPACT_STRINGS} would be constant folded away by an |
|
188 |
* optimizing JIT compiler. The idioms for these cases are as follows. |
|
189 |
* |
|
190 |
* For code such as: |
|
191 |
* |
|
192 |
* if (coder == LATIN1) { ... } |
|
193 |
* |
|
194 |
* can be written more optimally as |
|
195 |
* |
|
196 |
* if (coder() == LATIN1) { ... } |
|
197 |
* |
|
198 |
* or: |
|
199 |
* |
|
200 |
* if (COMPACT_STRINGS && coder == LATIN1) { ... } |
|
201 |
* |
|
202 |
* An optimizing JIT compiler can fold the above conditional as: |
|
203 |
* |
|
204 |
* COMPACT_STRINGS == true => if (coder == LATIN1) { ... } |
|
205 |
* COMPACT_STRINGS == false => if (false) { ... } |
|
206 |
* |
|
207 |
* @implNote |
|
208 |
* The actual value for this field is injected by JVM. The static |
|
209 |
* initialization block is used to set the value here to communicate |
|
210 |
* that this static final field is not statically foldable, and to |
|
211 |
* avoid any possible circular dependency during vm initialization. |
|
212 |
*/ |
|
213 |
static final boolean COMPACT_STRINGS; |
|
214 |
||
215 |
static { |
|
216 |
COMPACT_STRINGS = true; |
|
217 |
} |
|
218 |
||
219 |
/** |
|
2 | 220 |
* Class String is special cased within the Serialization Stream Protocol. |
221 |
* |
|
21636
fcec9002d5f4
8028041: Serialized Form description of j.l.String is not consistent with the implementation
rriggs
parents:
21334
diff
changeset
|
222 |
* A String instance is written into an ObjectOutputStream according to |
45138 | 223 |
* <a href="{@docRoot}/../specs/serialization/protocol.html#stream-elements"> |
21636
fcec9002d5f4
8028041: Serialized Form description of j.l.String is not consistent with the implementation
rriggs
parents:
21334
diff
changeset
|
224 |
* Object Serialization Specification, Section 6.2, "Stream Elements"</a> |
2 | 225 |
*/ |
226 |
private static final ObjectStreamField[] serialPersistentFields = |
|
17181
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
227 |
new ObjectStreamField[0]; |
2 | 228 |
|
229 |
/** |
|
230 |
* Initializes a newly created {@code String} object so that it represents |
|
231 |
* an empty character sequence. Note that use of this constructor is |
|
232 |
* unnecessary since Strings are immutable. |
|
233 |
*/ |
|
234 |
public String() { |
|
28423
0bf78b38bc0b
8067471: Use private static final char[0] for empty Strings
lpriima
parents:
27955
diff
changeset
|
235 |
this.value = "".value; |
33663 | 236 |
this.coder = "".coder; |
2 | 237 |
} |
238 |
||
239 |
/** |
|
240 |
* Initializes a newly created {@code String} object so that it represents |
|
241 |
* the same sequence of characters as the argument; in other words, the |
|
242 |
* newly created string is a copy of the argument string. Unless an |
|
243 |
* explicit copy of {@code original} is needed, use of this constructor is |
|
244 |
* unnecessary since Strings are immutable. |
|
245 |
* |
|
246 |
* @param original |
|
247 |
* A {@code String} |
|
248 |
*/ |
|
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
30913
diff
changeset
|
249 |
@HotSpotIntrinsicCandidate |
2 | 250 |
public String(String original) { |
12858
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
251 |
this.value = original.value; |
33663 | 252 |
this.coder = original.coder; |
12858
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
253 |
this.hash = original.hash; |
2 | 254 |
} |
255 |
||
256 |
/** |
|
257 |
* Allocates a new {@code String} so that it represents the sequence of |
|
258 |
* characters currently contained in the character array argument. The |
|
259 |
* contents of the character array are copied; subsequent modification of |
|
260 |
* the character array does not affect the newly created string. |
|
261 |
* |
|
262 |
* @param value |
|
263 |
* The initial value of the string |
|
264 |
*/ |
|
265 |
public String(char value[]) { |
|
33663 | 266 |
this(value, 0, value.length, null); |
2 | 267 |
} |
268 |
||
269 |
/** |
|
270 |
* Allocates a new {@code String} that contains characters from a subarray |
|
271 |
* of the character array argument. The {@code offset} argument is the |
|
272 |
* index of the first character of the subarray and the {@code count} |
|
273 |
* argument specifies the length of the subarray. The contents of the |
|
274 |
* subarray are copied; subsequent modification of the character array does |
|
275 |
* not affect the newly created string. |
|
276 |
* |
|
277 |
* @param value |
|
30642
d9891f85d583
8071571: Move substring of same string to slow path
igerasim
parents:
30640
diff
changeset
|
278 |
* Array that is the source of characters |
2 | 279 |
* |
280 |
* @param offset |
|
281 |
* The initial offset |
|
282 |
* |
|
283 |
* @param count |
|
284 |
* The length |
|
285 |
* |
|
286 |
* @throws IndexOutOfBoundsException |
|
27955
8d98ed07bc28
8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents:
27087
diff
changeset
|
287 |
* If {@code offset} is negative, {@code count} is negative, or |
8d98ed07bc28
8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents:
27087
diff
changeset
|
288 |
* {@code offset} is greater than {@code value.length - count} |
2 | 289 |
*/ |
290 |
public String(char value[], int offset, int count) { |
|
33663 | 291 |
this(value, offset, count, rangeCheck(value, offset, count)); |
292 |
} |
|
293 |
||
294 |
private static Void rangeCheck(char[] value, int offset, int count) { |
|
295 |
checkBoundsOffCount(offset, count, value.length); |
|
296 |
return null; |
|
2 | 297 |
} |
298 |
||
299 |
/** |
|
300 |
* Allocates a new {@code String} that contains characters from a subarray |
|
301 |
* of the <a href="Character.html#unicode">Unicode code point</a> array |
|
302 |
* argument. The {@code offset} argument is the index of the first code |
|
303 |
* point of the subarray and the {@code count} argument specifies the |
|
304 |
* length of the subarray. The contents of the subarray are converted to |
|
305 |
* {@code char}s; subsequent modification of the {@code int} array does not |
|
306 |
* affect the newly created string. |
|
307 |
* |
|
308 |
* @param codePoints |
|
309 |
* Array that is the source of Unicode code points |
|
310 |
* |
|
311 |
* @param offset |
|
312 |
* The initial offset |
|
313 |
* |
|
314 |
* @param count |
|
315 |
* The length |
|
316 |
* |
|
317 |
* @throws IllegalArgumentException |
|
318 |
* If any invalid Unicode code point is found in {@code |
|
319 |
* codePoints} |
|
320 |
* |
|
321 |
* @throws IndexOutOfBoundsException |
|
27955
8d98ed07bc28
8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents:
27087
diff
changeset
|
322 |
* If {@code offset} is negative, {@code count} is negative, or |
8d98ed07bc28
8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents:
27087
diff
changeset
|
323 |
* {@code offset} is greater than {@code codePoints.length - count} |
2 | 324 |
* |
325 |
* @since 1.5 |
|
326 |
*/ |
|
327 |
public String(int[] codePoints, int offset, int count) { |
|
33663 | 328 |
checkBoundsOffCount(offset, count, codePoints.length); |
329 |
if (count == 0) { |
|
330 |
this.value = "".value; |
|
331 |
this.coder = "".coder; |
|
332 |
return; |
|
2 | 333 |
} |
33663 | 334 |
if (COMPACT_STRINGS) { |
335 |
byte[] val = StringLatin1.toBytes(codePoints, offset, count); |
|
336 |
if (val != null) { |
|
337 |
this.coder = LATIN1; |
|
338 |
this.value = val; |
|
28423
0bf78b38bc0b
8067471: Use private static final char[0] for empty Strings
lpriima
parents:
27955
diff
changeset
|
339 |
return; |
0bf78b38bc0b
8067471: Use private static final char[0] for empty Strings
lpriima
parents:
27955
diff
changeset
|
340 |
} |
2 | 341 |
} |
33663 | 342 |
this.coder = UTF16; |
343 |
this.value = StringUTF16.toBytes(codePoints, offset, count); |
|
2 | 344 |
} |
345 |
||
346 |
/** |
|
347 |
* Allocates a new {@code String} constructed from a subarray of an array |
|
348 |
* of 8-bit integer values. |
|
349 |
* |
|
350 |
* <p> The {@code offset} argument is the index of the first byte of the |
|
351 |
* subarray, and the {@code count} argument specifies the length of the |
|
352 |
* subarray. |
|
353 |
* |
|
354 |
* <p> Each {@code byte} in the subarray is converted to a {@code char} as |
|
45121 | 355 |
* specified in the {@link #String(byte[],int) String(byte[],int)} constructor. |
2 | 356 |
* |
357 |
* @deprecated This method does not properly convert bytes into characters. |
|
358 |
* As of JDK 1.1, the preferred way to do this is via the |
|
359 |
* {@code String} constructors that take a {@link |
|
360 |
* java.nio.charset.Charset}, charset name, or that use the platform's |
|
361 |
* default charset. |
|
362 |
* |
|
363 |
* @param ascii |
|
364 |
* The bytes to be converted to characters |
|
365 |
* |
|
366 |
* @param hibyte |
|
367 |
* The top 8 bits of each 16-bit Unicode code unit |
|
368 |
* |
|
369 |
* @param offset |
|
370 |
* The initial offset |
|
371 |
* @param count |
|
372 |
* The length |
|
373 |
* |
|
374 |
* @throws IndexOutOfBoundsException |
|
27955
8d98ed07bc28
8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents:
27087
diff
changeset
|
375 |
* If {@code offset} is negative, {@code count} is negative, or |
8d98ed07bc28
8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents:
27087
diff
changeset
|
376 |
* {@code offset} is greater than {@code ascii.length - count} |
2 | 377 |
* |
378 |
* @see #String(byte[], int) |
|
379 |
* @see #String(byte[], int, int, java.lang.String) |
|
380 |
* @see #String(byte[], int, int, java.nio.charset.Charset) |
|
381 |
* @see #String(byte[], int, int) |
|
382 |
* @see #String(byte[], java.lang.String) |
|
383 |
* @see #String(byte[], java.nio.charset.Charset) |
|
384 |
* @see #String(byte[]) |
|
385 |
*/ |
|
37521
b6e0f285c998
8145468: update java.lang APIs with new deprecations
smarks
parents:
36431
diff
changeset
|
386 |
@Deprecated(since="1.1") |
2 | 387 |
public String(byte ascii[], int hibyte, int offset, int count) { |
33663 | 388 |
checkBoundsOffCount(offset, count, ascii.length); |
389 |
if (count == 0) { |
|
390 |
this.value = "".value; |
|
391 |
this.coder = "".coder; |
|
392 |
return; |
|
393 |
} |
|
394 |
if (COMPACT_STRINGS && (byte)hibyte == 0) { |
|
395 |
this.value = Arrays.copyOfRange(ascii, offset, offset + count); |
|
396 |
this.coder = LATIN1; |
|
2 | 397 |
} else { |
398 |
hibyte <<= 8; |
|
33663 | 399 |
byte[] val = StringUTF16.newBytesFor(count); |
400 |
for (int i = 0; i < count; i++) { |
|
401 |
StringUTF16.putChar(val, i, hibyte | (ascii[offset++] & 0xff)); |
|
2 | 402 |
} |
33663 | 403 |
this.value = val; |
404 |
this.coder = UTF16; |
|
2 | 405 |
} |
406 |
} |
|
407 |
||
408 |
/** |
|
409 |
* Allocates a new {@code String} containing characters constructed from |
|
45121 | 410 |
* an array of 8-bit integer values. Each character <i>c</i> in the |
2 | 411 |
* resulting string is constructed from the corresponding component |
412 |
* <i>b</i> in the byte array such that: |
|
413 |
* |
|
414 |
* <blockquote><pre> |
|
415 |
* <b><i>c</i></b> == (char)(((hibyte & 0xff) << 8) |
|
416 |
* | (<b><i>b</i></b> & 0xff)) |
|
417 |
* </pre></blockquote> |
|
418 |
* |
|
419 |
* @deprecated This method does not properly convert bytes into |
|
420 |
* characters. As of JDK 1.1, the preferred way to do this is via the |
|
421 |
* {@code String} constructors that take a {@link |
|
422 |
* java.nio.charset.Charset}, charset name, or that use the platform's |
|
423 |
* default charset. |
|
424 |
* |
|
425 |
* @param ascii |
|
426 |
* The bytes to be converted to characters |
|
427 |
* |
|
428 |
* @param hibyte |
|
429 |
* The top 8 bits of each 16-bit Unicode code unit |
|
430 |
* |
|
431 |
* @see #String(byte[], int, int, java.lang.String) |
|
432 |
* @see #String(byte[], int, int, java.nio.charset.Charset) |
|
433 |
* @see #String(byte[], int, int) |
|
434 |
* @see #String(byte[], java.lang.String) |
|
435 |
* @see #String(byte[], java.nio.charset.Charset) |
|
436 |
* @see #String(byte[]) |
|
437 |
*/ |
|
37521
b6e0f285c998
8145468: update java.lang APIs with new deprecations
smarks
parents:
36431
diff
changeset
|
438 |
@Deprecated(since="1.1") |
2 | 439 |
public String(byte ascii[], int hibyte) { |
440 |
this(ascii, hibyte, 0, ascii.length); |
|
441 |
} |
|
442 |
||
443 |
/** |
|
444 |
* Constructs a new {@code String} by decoding the specified subarray of |
|
445 |
* bytes using the specified charset. The length of the new {@code String} |
|
446 |
* is a function of the charset, and hence may not be equal to the length |
|
447 |
* of the subarray. |
|
448 |
* |
|
449 |
* <p> The behavior of this constructor when the given bytes are not valid |
|
450 |
* in the given charset is unspecified. The {@link |
|
451 |
* java.nio.charset.CharsetDecoder} class should be used when more control |
|
452 |
* over the decoding process is required. |
|
453 |
* |
|
454 |
* @param bytes |
|
455 |
* The bytes to be decoded into characters |
|
456 |
* |
|
457 |
* @param offset |
|
458 |
* The index of the first byte to decode |
|
459 |
* |
|
460 |
* @param length |
|
461 |
* The number of bytes to decode |
|
462 |
||
463 |
* @param charsetName |
|
464 |
* The name of a supported {@linkplain java.nio.charset.Charset |
|
465 |
* charset} |
|
466 |
* |
|
467 |
* @throws UnsupportedEncodingException |
|
468 |
* If the named charset is not supported |
|
469 |
* |
|
470 |
* @throws IndexOutOfBoundsException |
|
27955
8d98ed07bc28
8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents:
27087
diff
changeset
|
471 |
* If {@code offset} is negative, {@code length} is negative, or |
8d98ed07bc28
8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents:
27087
diff
changeset
|
472 |
* {@code offset} is greater than {@code bytes.length - length} |
2 | 473 |
* |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24374
diff
changeset
|
474 |
* @since 1.1 |
2 | 475 |
*/ |
476 |
public String(byte bytes[], int offset, int length, String charsetName) |
|
12858
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
477 |
throws UnsupportedEncodingException { |
2 | 478 |
if (charsetName == null) |
479 |
throw new NullPointerException("charsetName"); |
|
33663 | 480 |
checkBoundsOffCount(offset, length, bytes.length); |
481 |
StringCoding.Result ret = |
|
482 |
StringCoding.decode(charsetName, bytes, offset, length); |
|
483 |
this.value = ret.value; |
|
484 |
this.coder = ret.coder; |
|
2 | 485 |
} |
486 |
||
487 |
/** |
|
488 |
* Constructs a new {@code String} by decoding the specified subarray of |
|
489 |
* bytes using the specified {@linkplain java.nio.charset.Charset charset}. |
|
490 |
* The length of the new {@code String} is a function of the charset, and |
|
491 |
* hence may not be equal to the length of the subarray. |
|
492 |
* |
|
493 |
* <p> This method always replaces malformed-input and unmappable-character |
|
494 |
* sequences with this charset's default replacement string. The {@link |
|
495 |
* java.nio.charset.CharsetDecoder} class should be used when more control |
|
496 |
* over the decoding process is required. |
|
497 |
* |
|
498 |
* @param bytes |
|
499 |
* The bytes to be decoded into characters |
|
500 |
* |
|
501 |
* @param offset |
|
502 |
* The index of the first byte to decode |
|
503 |
* |
|
504 |
* @param length |
|
505 |
* The number of bytes to decode |
|
506 |
* |
|
507 |
* @param charset |
|
508 |
* The {@linkplain java.nio.charset.Charset charset} to be used to |
|
509 |
* decode the {@code bytes} |
|
510 |
* |
|
511 |
* @throws IndexOutOfBoundsException |
|
27955
8d98ed07bc28
8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents:
27087
diff
changeset
|
512 |
* If {@code offset} is negative, {@code length} is negative, or |
8d98ed07bc28
8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents:
27087
diff
changeset
|
513 |
* {@code offset} is greater than {@code bytes.length - length} |
2 | 514 |
* |
515 |
* @since 1.6 |
|
516 |
*/ |
|
517 |
public String(byte bytes[], int offset, int length, Charset charset) { |
|
518 |
if (charset == null) |
|
519 |
throw new NullPointerException("charset"); |
|
33663 | 520 |
checkBoundsOffCount(offset, length, bytes.length); |
521 |
StringCoding.Result ret = |
|
522 |
StringCoding.decode(charset, bytes, offset, length); |
|
523 |
this.value = ret.value; |
|
524 |
this.coder = ret.coder; |
|
2 | 525 |
} |
526 |
||
527 |
/** |
|
528 |
* Constructs a new {@code String} by decoding the specified array of bytes |
|
529 |
* using the specified {@linkplain java.nio.charset.Charset charset}. The |
|
530 |
* length of the new {@code String} is a function of the charset, and hence |
|
531 |
* may not be equal to the length of the byte array. |
|
532 |
* |
|
533 |
* <p> The behavior of this constructor when the given bytes are not valid |
|
534 |
* in the given charset is unspecified. The {@link |
|
535 |
* java.nio.charset.CharsetDecoder} class should be used when more control |
|
536 |
* over the decoding process is required. |
|
537 |
* |
|
538 |
* @param bytes |
|
539 |
* The bytes to be decoded into characters |
|
540 |
* |
|
541 |
* @param charsetName |
|
542 |
* The name of a supported {@linkplain java.nio.charset.Charset |
|
543 |
* charset} |
|
544 |
* |
|
545 |
* @throws UnsupportedEncodingException |
|
546 |
* If the named charset is not supported |
|
547 |
* |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24374
diff
changeset
|
548 |
* @since 1.1 |
2 | 549 |
*/ |
550 |
public String(byte bytes[], String charsetName) |
|
12858
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
551 |
throws UnsupportedEncodingException { |
2 | 552 |
this(bytes, 0, bytes.length, charsetName); |
553 |
} |
|
554 |
||
555 |
/** |
|
556 |
* Constructs a new {@code String} by decoding the specified array of |
|
557 |
* bytes using the specified {@linkplain java.nio.charset.Charset charset}. |
|
558 |
* The length of the new {@code String} is a function of the charset, and |
|
559 |
* hence may not be equal to the length of the byte array. |
|
560 |
* |
|
561 |
* <p> This method always replaces malformed-input and unmappable-character |
|
562 |
* sequences with this charset's default replacement string. The {@link |
|
563 |
* java.nio.charset.CharsetDecoder} class should be used when more control |
|
564 |
* over the decoding process is required. |
|
565 |
* |
|
566 |
* @param bytes |
|
567 |
* The bytes to be decoded into characters |
|
568 |
* |
|
569 |
* @param charset |
|
570 |
* The {@linkplain java.nio.charset.Charset charset} to be used to |
|
571 |
* decode the {@code bytes} |
|
572 |
* |
|
573 |
* @since 1.6 |
|
574 |
*/ |
|
575 |
public String(byte bytes[], Charset charset) { |
|
576 |
this(bytes, 0, bytes.length, charset); |
|
577 |
} |
|
578 |
||
579 |
/** |
|
580 |
* Constructs a new {@code String} by decoding the specified subarray of |
|
581 |
* bytes using the platform's default charset. The length of the new |
|
582 |
* {@code String} is a function of the charset, and hence may not be equal |
|
583 |
* to the length of the subarray. |
|
584 |
* |
|
585 |
* <p> The behavior of this constructor when the given bytes are not valid |
|
586 |
* in the default charset is unspecified. The {@link |
|
587 |
* java.nio.charset.CharsetDecoder} class should be used when more control |
|
588 |
* over the decoding process is required. |
|
589 |
* |
|
590 |
* @param bytes |
|
591 |
* The bytes to be decoded into characters |
|
592 |
* |
|
593 |
* @param offset |
|
594 |
* The index of the first byte to decode |
|
595 |
* |
|
596 |
* @param length |
|
597 |
* The number of bytes to decode |
|
598 |
* |
|
599 |
* @throws IndexOutOfBoundsException |
|
27955
8d98ed07bc28
8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents:
27087
diff
changeset
|
600 |
* If {@code offset} is negative, {@code length} is negative, or |
8d98ed07bc28
8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents:
27087
diff
changeset
|
601 |
* {@code offset} is greater than {@code bytes.length - length} |
2 | 602 |
* |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24374
diff
changeset
|
603 |
* @since 1.1 |
2 | 604 |
*/ |
605 |
public String(byte bytes[], int offset, int length) { |
|
33663 | 606 |
checkBoundsOffCount(offset, length, bytes.length); |
607 |
StringCoding.Result ret = StringCoding.decode(bytes, offset, length); |
|
608 |
this.value = ret.value; |
|
609 |
this.coder = ret.coder; |
|
2 | 610 |
} |
611 |
||
612 |
/** |
|
613 |
* Constructs a new {@code String} by decoding the specified array of bytes |
|
614 |
* using the platform's default charset. The length of the new {@code |
|
615 |
* String} is a function of the charset, and hence may not be equal to the |
|
616 |
* length of the byte array. |
|
617 |
* |
|
618 |
* <p> The behavior of this constructor when the given bytes are not valid |
|
619 |
* in the default charset is unspecified. The {@link |
|
620 |
* java.nio.charset.CharsetDecoder} class should be used when more control |
|
621 |
* over the decoding process is required. |
|
622 |
* |
|
623 |
* @param bytes |
|
624 |
* The bytes to be decoded into characters |
|
625 |
* |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24374
diff
changeset
|
626 |
* @since 1.1 |
2 | 627 |
*/ |
30642
d9891f85d583
8071571: Move substring of same string to slow path
igerasim
parents:
30640
diff
changeset
|
628 |
public String(byte[] bytes) { |
2 | 629 |
this(bytes, 0, bytes.length); |
630 |
} |
|
631 |
||
632 |
/** |
|
633 |
* Allocates a new string that contains the sequence of characters |
|
634 |
* currently contained in the string buffer argument. The contents of the |
|
635 |
* string buffer are copied; subsequent modification of the string buffer |
|
636 |
* does not affect the newly created string. |
|
637 |
* |
|
638 |
* @param buffer |
|
639 |
* A {@code StringBuffer} |
|
640 |
*/ |
|
641 |
public String(StringBuffer buffer) { |
|
33663 | 642 |
this(buffer.toString()); |
2 | 643 |
} |
644 |
||
645 |
/** |
|
646 |
* Allocates a new string that contains the sequence of characters |
|
647 |
* currently contained in the string builder argument. The contents of the |
|
648 |
* string builder are copied; subsequent modification of the string builder |
|
649 |
* does not affect the newly created string. |
|
650 |
* |
|
651 |
* <p> This constructor is provided to ease migration to {@code |
|
652 |
* StringBuilder}. Obtaining a string from a string builder via the {@code |
|
653 |
* toString} method is likely to run faster and is generally preferred. |
|
654 |
* |
|
655 |
* @param builder |
|
656 |
* A {@code StringBuilder} |
|
657 |
* |
|
658 |
* @since 1.5 |
|
659 |
*/ |
|
660 |
public String(StringBuilder builder) { |
|
33663 | 661 |
this(builder, null); |
2 | 662 |
} |
663 |
||
664 |
/** |
|
665 |
* Returns the length of this string. |
|
666 |
* The length is equal to the number of <a href="Character.html#unicode">Unicode |
|
667 |
* code units</a> in the string. |
|
668 |
* |
|
669 |
* @return the length of the sequence of characters represented by this |
|
670 |
* object. |
|
671 |
*/ |
|
672 |
public int length() { |
|
53049
c74f074c532a
8215380: Backout accidental change to String::length
redestad
parents:
53019
diff
changeset
|
673 |
return value.length >> coder(); |
2 | 674 |
} |
675 |
||
676 |
/** |
|
14997 | 677 |
* Returns {@code true} if, and only if, {@link #length()} is {@code 0}. |
2 | 678 |
* |
14997 | 679 |
* @return {@code true} if {@link #length()} is {@code 0}, otherwise |
680 |
* {@code false} |
|
2 | 681 |
* |
682 |
* @since 1.6 |
|
683 |
*/ |
|
684 |
public boolean isEmpty() { |
|
12858
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
685 |
return value.length == 0; |
2 | 686 |
} |
687 |
||
688 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
689 |
* Returns the {@code char} value at the |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
690 |
* specified index. An index ranges from {@code 0} to |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
691 |
* {@code length() - 1}. The first {@code char} value of the sequence |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
692 |
* is at index {@code 0}, the next at index {@code 1}, |
2 | 693 |
* and so on, as for array indexing. |
694 |
* |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
695 |
* <p>If the {@code char} value specified by the index is a |
2 | 696 |
* <a href="Character.html#unicode">surrogate</a>, the surrogate |
697 |
* value is returned. |
|
698 |
* |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
699 |
* @param index the index of the {@code char} value. |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
700 |
* @return the {@code char} value at the specified index of this string. |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
701 |
* The first {@code char} value is at index {@code 0}. |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
702 |
* @exception IndexOutOfBoundsException if the {@code index} |
2 | 703 |
* argument is negative or not less than the length of this |
704 |
* string. |
|
705 |
*/ |
|
706 |
public char charAt(int index) { |
|
33663 | 707 |
if (isLatin1()) { |
708 |
return StringLatin1.charAt(value, index); |
|
709 |
} else { |
|
710 |
return StringUTF16.charAt(value, index); |
|
2 | 711 |
} |
712 |
} |
|
713 |
||
714 |
/** |
|
715 |
* Returns the character (Unicode code point) at the specified |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
716 |
* index. The index refers to {@code char} values |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
717 |
* (Unicode code units) and ranges from {@code 0} to |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
718 |
* {@link #length()}{@code - 1}. |
2 | 719 |
* |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
720 |
* <p> If the {@code char} value specified at the given index |
2 | 721 |
* is in the high-surrogate range, the following index is less |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
722 |
* than the length of this {@code String}, and the |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
723 |
* {@code char} value at the following index is in the |
2 | 724 |
* low-surrogate range, then the supplementary code point |
725 |
* corresponding to this surrogate pair is returned. Otherwise, |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
726 |
* the {@code char} value at the given index is returned. |
2 | 727 |
* |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
728 |
* @param index the index to the {@code char} values |
2 | 729 |
* @return the code point value of the character at the |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
730 |
* {@code index} |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
731 |
* @exception IndexOutOfBoundsException if the {@code index} |
2 | 732 |
* argument is negative or not less than the length of this |
733 |
* string. |
|
734 |
* @since 1.5 |
|
735 |
*/ |
|
736 |
public int codePointAt(int index) { |
|
33663 | 737 |
if (isLatin1()) { |
738 |
checkIndex(index, value.length); |
|
739 |
return value[index] & 0xff; |
|
2 | 740 |
} |
33663 | 741 |
int length = value.length >> 1; |
742 |
checkIndex(index, length); |
|
743 |
return StringUTF16.codePointAt(value, index, length); |
|
2 | 744 |
} |
745 |
||
746 |
/** |
|
747 |
* Returns the character (Unicode code point) before the specified |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
748 |
* index. The index refers to {@code char} values |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
749 |
* (Unicode code units) and ranges from {@code 1} to {@link |
2 | 750 |
* CharSequence#length() length}. |
751 |
* |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
752 |
* <p> If the {@code char} value at {@code (index - 1)} |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
753 |
* is in the low-surrogate range, {@code (index - 2)} is not |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
754 |
* negative, and the {@code char} value at {@code (index - |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
755 |
* 2)} is in the high-surrogate range, then the |
2 | 756 |
* supplementary code point value of the surrogate pair is |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
757 |
* returned. If the {@code char} value at {@code index - |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
758 |
* 1} is an unpaired low-surrogate or a high-surrogate, the |
2 | 759 |
* surrogate value is returned. |
760 |
* |
|
761 |
* @param index the index following the code point that should be returned |
|
762 |
* @return the Unicode code point value before the given index. |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
763 |
* @exception IndexOutOfBoundsException if the {@code index} |
2 | 764 |
* argument is less than 1 or greater than the length |
765 |
* of this string. |
|
766 |
* @since 1.5 |
|
767 |
*/ |
|
768 |
public int codePointBefore(int index) { |
|
769 |
int i = index - 1; |
|
33663 | 770 |
if (i < 0 || i >= length()) { |
2 | 771 |
throw new StringIndexOutOfBoundsException(index); |
772 |
} |
|
33663 | 773 |
if (isLatin1()) { |
774 |
return (value[i] & 0xff); |
|
775 |
} |
|
776 |
return StringUTF16.codePointBefore(value, index); |
|
2 | 777 |
} |
778 |
||
779 |
/** |
|
780 |
* Returns the number of Unicode code points in the specified text |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
781 |
* range of this {@code String}. The text range begins at the |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
782 |
* specified {@code beginIndex} and extends to the |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
783 |
* {@code char} at index {@code endIndex - 1}. Thus the |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
784 |
* length (in {@code char}s) of the text range is |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
785 |
* {@code endIndex-beginIndex}. Unpaired surrogates within |
2 | 786 |
* the text range count as one code point each. |
787 |
* |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
788 |
* @param beginIndex the index to the first {@code char} of |
2 | 789 |
* the text range. |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
790 |
* @param endIndex the index after the last {@code char} of |
2 | 791 |
* the text range. |
792 |
* @return the number of Unicode code points in the specified text |
|
793 |
* range |
|
794 |
* @exception IndexOutOfBoundsException if the |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
795 |
* {@code beginIndex} is negative, or {@code endIndex} |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
796 |
* is larger than the length of this {@code String}, or |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
797 |
* {@code beginIndex} is larger than {@code endIndex}. |
2 | 798 |
* @since 1.5 |
799 |
*/ |
|
800 |
public int codePointCount(int beginIndex, int endIndex) { |
|
33663 | 801 |
if (beginIndex < 0 || beginIndex > endIndex || |
802 |
endIndex > length()) { |
|
2 | 803 |
throw new IndexOutOfBoundsException(); |
804 |
} |
|
33663 | 805 |
if (isLatin1()) { |
806 |
return endIndex - beginIndex; |
|
807 |
} |
|
808 |
return StringUTF16.codePointCount(value, beginIndex, endIndex); |
|
2 | 809 |
} |
810 |
||
811 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
812 |
* Returns the index within this {@code String} that is |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
813 |
* offset from the given {@code index} by |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
814 |
* {@code codePointOffset} code points. Unpaired surrogates |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
815 |
* within the text range given by {@code index} and |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
816 |
* {@code codePointOffset} count as one code point each. |
2 | 817 |
* |
818 |
* @param index the index to be offset |
|
819 |
* @param codePointOffset the offset in code points |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
820 |
* @return the index within this {@code String} |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
821 |
* @exception IndexOutOfBoundsException if {@code index} |
2 | 822 |
* is negative or larger then the length of this |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
823 |
* {@code String}, or if {@code codePointOffset} is positive |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
824 |
* and the substring starting with {@code index} has fewer |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
825 |
* than {@code codePointOffset} code points, |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
826 |
* or if {@code codePointOffset} is negative and the substring |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
827 |
* before {@code index} has fewer than the absolute value |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
828 |
* of {@code codePointOffset} code points. |
2 | 829 |
* @since 1.5 |
830 |
*/ |
|
831 |
public int offsetByCodePoints(int index, int codePointOffset) { |
|
33663 | 832 |
if (index < 0 || index > length()) { |
2 | 833 |
throw new IndexOutOfBoundsException(); |
834 |
} |
|
33663 | 835 |
return Character.offsetByCodePoints(this, index, codePointOffset); |
2 | 836 |
} |
837 |
||
838 |
/** |
|
839 |
* Copies characters from this string into the destination character |
|
840 |
* array. |
|
841 |
* <p> |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
842 |
* The first character to be copied is at index {@code srcBegin}; |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
843 |
* the last character to be copied is at index {@code srcEnd-1} |
2 | 844 |
* (thus the total number of characters to be copied is |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
845 |
* {@code srcEnd-srcBegin}). The characters are copied into the |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
846 |
* subarray of {@code dst} starting at index {@code dstBegin} |
2 | 847 |
* and ending at index: |
21334 | 848 |
* <blockquote><pre> |
28423
0bf78b38bc0b
8067471: Use private static final char[0] for empty Strings
lpriima
parents:
27955
diff
changeset
|
849 |
* dstBegin + (srcEnd-srcBegin) - 1 |
2 | 850 |
* </pre></blockquote> |
851 |
* |
|
852 |
* @param srcBegin index of the first character in the string |
|
853 |
* to copy. |
|
854 |
* @param srcEnd index after the last character in the string |
|
855 |
* to copy. |
|
856 |
* @param dst the destination array. |
|
857 |
* @param dstBegin the start offset in the destination array. |
|
858 |
* @exception IndexOutOfBoundsException If any of the following |
|
859 |
* is true: |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
860 |
* <ul><li>{@code srcBegin} is negative. |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
861 |
* <li>{@code srcBegin} is greater than {@code srcEnd} |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
862 |
* <li>{@code srcEnd} is greater than the length of this |
2 | 863 |
* string |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
864 |
* <li>{@code dstBegin} is negative |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
865 |
* <li>{@code dstBegin+(srcEnd-srcBegin)} is larger than |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
866 |
* {@code dst.length}</ul> |
2 | 867 |
*/ |
868 |
public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) { |
|
33663 | 869 |
checkBoundsBeginEnd(srcBegin, srcEnd, length()); |
870 |
checkBoundsOffCount(dstBegin, srcEnd - srcBegin, dst.length); |
|
871 |
if (isLatin1()) { |
|
872 |
StringLatin1.getChars(value, srcBegin, srcEnd, dst, dstBegin); |
|
873 |
} else { |
|
874 |
StringUTF16.getChars(value, srcBegin, srcEnd, dst, dstBegin); |
|
2 | 875 |
} |
876 |
} |
|
877 |
||
878 |
/** |
|
879 |
* Copies characters from this string into the destination byte array. Each |
|
880 |
* byte receives the 8 low-order bits of the corresponding character. The |
|
881 |
* eight high-order bits of each character are not copied and do not |
|
882 |
* participate in the transfer in any way. |
|
883 |
* |
|
884 |
* <p> The first character to be copied is at index {@code srcBegin}; the |
|
885 |
* last character to be copied is at index {@code srcEnd-1}. The total |
|
886 |
* number of characters to be copied is {@code srcEnd-srcBegin}. The |
|
887 |
* characters, converted to bytes, are copied into the subarray of {@code |
|
888 |
* dst} starting at index {@code dstBegin} and ending at index: |
|
889 |
* |
|
890 |
* <blockquote><pre> |
|
28423
0bf78b38bc0b
8067471: Use private static final char[0] for empty Strings
lpriima
parents:
27955
diff
changeset
|
891 |
* dstBegin + (srcEnd-srcBegin) - 1 |
2 | 892 |
* </pre></blockquote> |
893 |
* |
|
894 |
* @deprecated This method does not properly convert characters into |
|
895 |
* bytes. As of JDK 1.1, the preferred way to do this is via the |
|
896 |
* {@link #getBytes()} method, which uses the platform's default charset. |
|
897 |
* |
|
898 |
* @param srcBegin |
|
899 |
* Index of the first character in the string to copy |
|
900 |
* |
|
901 |
* @param srcEnd |
|
902 |
* Index after the last character in the string to copy |
|
903 |
* |
|
904 |
* @param dst |
|
905 |
* The destination array |
|
906 |
* |
|
907 |
* @param dstBegin |
|
908 |
* The start offset in the destination array |
|
909 |
* |
|
910 |
* @throws IndexOutOfBoundsException |
|
911 |
* If any of the following is true: |
|
912 |
* <ul> |
|
913 |
* <li> {@code srcBegin} is negative |
|
914 |
* <li> {@code srcBegin} is greater than {@code srcEnd} |
|
915 |
* <li> {@code srcEnd} is greater than the length of this String |
|
916 |
* <li> {@code dstBegin} is negative |
|
917 |
* <li> {@code dstBegin+(srcEnd-srcBegin)} is larger than {@code |
|
918 |
* dst.length} |
|
919 |
* </ul> |
|
920 |
*/ |
|
37521
b6e0f285c998
8145468: update java.lang APIs with new deprecations
smarks
parents:
36431
diff
changeset
|
921 |
@Deprecated(since="1.1") |
2 | 922 |
public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) { |
33663 | 923 |
checkBoundsBeginEnd(srcBegin, srcEnd, length()); |
15312
4b57f9da8192
4247235: (spec str) StringBuffer.insert(int, char[]) specification is inconsistent
jgish
parents:
14997
diff
changeset
|
924 |
Objects.requireNonNull(dst); |
33663 | 925 |
checkBoundsOffCount(dstBegin, srcEnd - srcBegin, dst.length); |
926 |
if (isLatin1()) { |
|
927 |
StringLatin1.getBytes(value, srcBegin, srcEnd, dst, dstBegin); |
|
928 |
} else { |
|
929 |
StringUTF16.getBytes(value, srcBegin, srcEnd, dst, dstBegin); |
|
2 | 930 |
} |
931 |
} |
|
932 |
||
933 |
/** |
|
934 |
* Encodes this {@code String} into a sequence of bytes using the named |
|
935 |
* charset, storing the result into a new byte array. |
|
936 |
* |
|
937 |
* <p> The behavior of this method when this string cannot be encoded in |
|
938 |
* the given charset is unspecified. The {@link |
|
939 |
* java.nio.charset.CharsetEncoder} class should be used when more control |
|
940 |
* over the encoding process is required. |
|
941 |
* |
|
942 |
* @param charsetName |
|
943 |
* The name of a supported {@linkplain java.nio.charset.Charset |
|
944 |
* charset} |
|
945 |
* |
|
946 |
* @return The resultant byte array |
|
947 |
* |
|
948 |
* @throws UnsupportedEncodingException |
|
949 |
* If the named charset is not supported |
|
950 |
* |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24374
diff
changeset
|
951 |
* @since 1.1 |
2 | 952 |
*/ |
953 |
public byte[] getBytes(String charsetName) |
|
12858
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
954 |
throws UnsupportedEncodingException { |
2 | 955 |
if (charsetName == null) throw new NullPointerException(); |
33663 | 956 |
return StringCoding.encode(charsetName, coder(), value); |
2 | 957 |
} |
958 |
||
959 |
/** |
|
960 |
* Encodes this {@code String} into a sequence of bytes using the given |
|
961 |
* {@linkplain java.nio.charset.Charset charset}, storing the result into a |
|
962 |
* new byte array. |
|
963 |
* |
|
964 |
* <p> This method always replaces malformed-input and unmappable-character |
|
965 |
* sequences with this charset's default replacement byte array. The |
|
966 |
* {@link java.nio.charset.CharsetEncoder} class should be used when more |
|
967 |
* control over the encoding process is required. |
|
968 |
* |
|
969 |
* @param charset |
|
970 |
* The {@linkplain java.nio.charset.Charset} to be used to encode |
|
971 |
* the {@code String} |
|
972 |
* |
|
973 |
* @return The resultant byte array |
|
974 |
* |
|
975 |
* @since 1.6 |
|
976 |
*/ |
|
977 |
public byte[] getBytes(Charset charset) { |
|
978 |
if (charset == null) throw new NullPointerException(); |
|
33663 | 979 |
return StringCoding.encode(charset, coder(), value); |
980 |
} |
|
2 | 981 |
|
982 |
/** |
|
983 |
* Encodes this {@code String} into a sequence of bytes using the |
|
984 |
* platform's default charset, storing the result into a new byte array. |
|
985 |
* |
|
986 |
* <p> The behavior of this method when this string cannot be encoded in |
|
987 |
* the default charset is unspecified. The {@link |
|
988 |
* java.nio.charset.CharsetEncoder} class should be used when more control |
|
989 |
* over the encoding process is required. |
|
990 |
* |
|
991 |
* @return The resultant byte array |
|
992 |
* |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24374
diff
changeset
|
993 |
* @since 1.1 |
2 | 994 |
*/ |
995 |
public byte[] getBytes() { |
|
33663 | 996 |
return StringCoding.encode(coder(), value); |
2 | 997 |
} |
998 |
||
999 |
/** |
|
1000 |
* Compares this string to the specified object. The result is {@code |
|
1001 |
* true} if and only if the argument is not {@code null} and is a {@code |
|
1002 |
* String} object that represents the same sequence of characters as this |
|
1003 |
* object. |
|
1004 |
* |
|
33314
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1005 |
* <p>For finer-grained String comparison, refer to |
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1006 |
* {@link java.text.Collator}. |
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1007 |
* |
2 | 1008 |
* @param anObject |
1009 |
* The object to compare this {@code String} against |
|
1010 |
* |
|
1011 |
* @return {@code true} if the given object represents a {@code String} |
|
1012 |
* equivalent to this string, {@code false} otherwise |
|
1013 |
* |
|
1014 |
* @see #compareTo(String) |
|
1015 |
* @see #equalsIgnoreCase(String) |
|
1016 |
*/ |
|
1017 |
public boolean equals(Object anObject) { |
|
1018 |
if (this == anObject) { |
|
1019 |
return true; |
|
1020 |
} |
|
1021 |
if (anObject instanceof String) { |
|
33663 | 1022 |
String aString = (String)anObject; |
1023 |
if (coder() == aString.coder()) { |
|
1024 |
return isLatin1() ? StringLatin1.equals(value, aString.value) |
|
1025 |
: StringUTF16.equals(value, aString.value); |
|
2 | 1026 |
} |
1027 |
} |
|
1028 |
return false; |
|
1029 |
} |
|
1030 |
||
1031 |
/** |
|
1032 |
* Compares this string to the specified {@code StringBuffer}. The result |
|
1033 |
* is {@code true} if and only if this {@code String} represents the same |
|
13404
8e63aa2e956c
6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents:
13156
diff
changeset
|
1034 |
* sequence of characters as the specified {@code StringBuffer}. This method |
8e63aa2e956c
6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents:
13156
diff
changeset
|
1035 |
* synchronizes on the {@code StringBuffer}. |
2 | 1036 |
* |
33314
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1037 |
* <p>For finer-grained String comparison, refer to |
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1038 |
* {@link java.text.Collator}. |
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1039 |
* |
2 | 1040 |
* @param sb |
1041 |
* The {@code StringBuffer} to compare this {@code String} against |
|
1042 |
* |
|
1043 |
* @return {@code true} if this {@code String} represents the same |
|
1044 |
* sequence of characters as the specified {@code StringBuffer}, |
|
1045 |
* {@code false} otherwise |
|
1046 |
* |
|
1047 |
* @since 1.4 |
|
1048 |
*/ |
|
1049 |
public boolean contentEquals(StringBuffer sb) { |
|
17181
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
1050 |
return contentEquals((CharSequence)sb); |
13404
8e63aa2e956c
6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents:
13156
diff
changeset
|
1051 |
} |
8e63aa2e956c
6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents:
13156
diff
changeset
|
1052 |
|
8e63aa2e956c
6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents:
13156
diff
changeset
|
1053 |
private boolean nonSyncContentEquals(AbstractStringBuilder sb) { |
33663 | 1054 |
int len = length(); |
1055 |
if (len != sb.length()) { |
|
17698
ab37c47ff886
8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
17181
diff
changeset
|
1056 |
return false; |
ab37c47ff886
8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
17181
diff
changeset
|
1057 |
} |
33663 | 1058 |
byte v1[] = value; |
1059 |
byte v2[] = sb.getValue(); |
|
1060 |
if (coder() == sb.getCoder()) { |
|
1061 |
int n = v1.length; |
|
1062 |
for (int i = 0; i < n; i++) { |
|
1063 |
if (v1[i] != v2[i]) { |
|
1064 |
return false; |
|
1065 |
} |
|
1066 |
} |
|
1067 |
} else { |
|
1068 |
if (!isLatin1()) { // utf16 str and latin1 abs can never be "equal" |
|
13404
8e63aa2e956c
6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents:
13156
diff
changeset
|
1069 |
return false; |
8e63aa2e956c
6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents:
13156
diff
changeset
|
1070 |
} |
44642
331e669007f7
8158168: Missing bounds checks for some String intrinsics
dlong
parents:
44369
diff
changeset
|
1071 |
return StringUTF16.contentEquals(v1, v2, len); |
2 | 1072 |
} |
13404
8e63aa2e956c
6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents:
13156
diff
changeset
|
1073 |
return true; |
2 | 1074 |
} |
1075 |
||
1076 |
/** |
|
13404
8e63aa2e956c
6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents:
13156
diff
changeset
|
1077 |
* Compares this string to the specified {@code CharSequence}. The |
8e63aa2e956c
6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents:
13156
diff
changeset
|
1078 |
* result is {@code true} if and only if this {@code String} represents the |
8e63aa2e956c
6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents:
13156
diff
changeset
|
1079 |
* same sequence of char values as the specified sequence. Note that if the |
8e63aa2e956c
6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents:
13156
diff
changeset
|
1080 |
* {@code CharSequence} is a {@code StringBuffer} then the method |
8e63aa2e956c
6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents:
13156
diff
changeset
|
1081 |
* synchronizes on it. |
2 | 1082 |
* |
33314
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1083 |
* <p>For finer-grained String comparison, refer to |
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1084 |
* {@link java.text.Collator}. |
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1085 |
* |
2 | 1086 |
* @param cs |
1087 |
* The sequence to compare this {@code String} against |
|
1088 |
* |
|
1089 |
* @return {@code true} if this {@code String} represents the same |
|
1090 |
* sequence of char values as the specified sequence, {@code |
|
1091 |
* false} otherwise |
|
1092 |
* |
|
1093 |
* @since 1.5 |
|
1094 |
*/ |
|
1095 |
public boolean contentEquals(CharSequence cs) { |
|
1096 |
// Argument is a StringBuffer, StringBuilder |
|
1097 |
if (cs instanceof AbstractStringBuilder) { |
|
13404
8e63aa2e956c
6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents:
13156
diff
changeset
|
1098 |
if (cs instanceof StringBuffer) { |
8e63aa2e956c
6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents:
13156
diff
changeset
|
1099 |
synchronized(cs) { |
8e63aa2e956c
6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents:
13156
diff
changeset
|
1100 |
return nonSyncContentEquals((AbstractStringBuilder)cs); |
8e63aa2e956c
6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents:
13156
diff
changeset
|
1101 |
} |
8e63aa2e956c
6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents:
13156
diff
changeset
|
1102 |
} else { |
8e63aa2e956c
6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents:
13156
diff
changeset
|
1103 |
return nonSyncContentEquals((AbstractStringBuilder)cs); |
2 | 1104 |
} |
1105 |
} |
|
1106 |
// Argument is a String |
|
27087
de850fa3be4d
8060485: (str) contentEquals checks the String contents twice on mismatch
shade
parents:
26731
diff
changeset
|
1107 |
if (cs instanceof String) { |
de850fa3be4d
8060485: (str) contentEquals checks the String contents twice on mismatch
shade
parents:
26731
diff
changeset
|
1108 |
return equals(cs); |
de850fa3be4d
8060485: (str) contentEquals checks the String contents twice on mismatch
shade
parents:
26731
diff
changeset
|
1109 |
} |
2 | 1110 |
// Argument is a generic CharSequence |
33663 | 1111 |
int n = cs.length(); |
1112 |
if (n != length()) { |
|
17698
ab37c47ff886
8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
17181
diff
changeset
|
1113 |
return false; |
ab37c47ff886
8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
17181
diff
changeset
|
1114 |
} |
33663 | 1115 |
byte[] val = this.value; |
1116 |
if (isLatin1()) { |
|
1117 |
for (int i = 0; i < n; i++) { |
|
1118 |
if ((val[i] & 0xff) != cs.charAt(i)) { |
|
1119 |
return false; |
|
1120 |
} |
|
1121 |
} |
|
1122 |
} else { |
|
44642
331e669007f7
8158168: Missing bounds checks for some String intrinsics
dlong
parents:
44369
diff
changeset
|
1123 |
if (!StringUTF16.contentEquals(val, cs, n)) { |
331e669007f7
8158168: Missing bounds checks for some String intrinsics
dlong
parents:
44369
diff
changeset
|
1124 |
return false; |
17698
ab37c47ff886
8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
17181
diff
changeset
|
1125 |
} |
2 | 1126 |
} |
1127 |
return true; |
|
1128 |
} |
|
1129 |
||
1130 |
/** |
|
1131 |
* Compares this {@code String} to another {@code String}, ignoring case |
|
1132 |
* considerations. Two strings are considered equal ignoring case if they |
|
1133 |
* are of the same length and corresponding characters in the two strings |
|
1134 |
* are equal ignoring case. |
|
1135 |
* |
|
1136 |
* <p> Two characters {@code c1} and {@code c2} are considered the same |
|
1137 |
* ignoring case if at least one of the following is true: |
|
1138 |
* <ul> |
|
1139 |
* <li> The two characters are the same (as compared by the |
|
1140 |
* {@code ==} operator) |
|
33314
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1141 |
* <li> Calling {@code Character.toLowerCase(Character.toUpperCase(char))} |
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1142 |
* on each character produces the same result |
2 | 1143 |
* </ul> |
1144 |
* |
|
33314
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1145 |
* <p>Note that this method does <em>not</em> take locale into account, and |
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1146 |
* will result in unsatisfactory results for certain locales. The |
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1147 |
* {@link java.text.Collator} class provides locale-sensitive comparison. |
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1148 |
* |
2 | 1149 |
* @param anotherString |
1150 |
* The {@code String} to compare this {@code String} against |
|
1151 |
* |
|
1152 |
* @return {@code true} if the argument is not {@code null} and it |
|
1153 |
* represents an equivalent {@code String} ignoring case; {@code |
|
1154 |
* false} otherwise |
|
1155 |
* |
|
1156 |
* @see #equals(Object) |
|
1157 |
*/ |
|
1158 |
public boolean equalsIgnoreCase(String anotherString) { |
|
12858
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
1159 |
return (this == anotherString) ? true |
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
1160 |
: (anotherString != null) |
33663 | 1161 |
&& (anotherString.length() == length()) |
1162 |
&& regionMatches(true, 0, anotherString, 0, length()); |
|
2 | 1163 |
} |
1164 |
||
1165 |
/** |
|
1166 |
* Compares two strings lexicographically. |
|
1167 |
* The comparison is based on the Unicode value of each character in |
|
1168 |
* the strings. The character sequence represented by this |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1169 |
* {@code String} object is compared lexicographically to the |
2 | 1170 |
* character sequence represented by the argument string. The result is |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1171 |
* a negative integer if this {@code String} object |
2 | 1172 |
* lexicographically precedes the argument string. The result is a |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1173 |
* positive integer if this {@code String} object lexicographically |
2 | 1174 |
* follows the argument string. The result is zero if the strings |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1175 |
* are equal; {@code compareTo} returns {@code 0} exactly when |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1176 |
* the {@link #equals(Object)} method would return {@code true}. |
2 | 1177 |
* <p> |
1178 |
* This is the definition of lexicographic ordering. If two strings are |
|
1179 |
* different, then either they have different characters at some index |
|
1180 |
* that is a valid index for both strings, or their lengths are different, |
|
1181 |
* or both. If they have different characters at one or more index |
|
1182 |
* positions, let <i>k</i> be the smallest such index; then the string |
|
1183 |
* whose character at position <i>k</i> has the smaller value, as |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31671
diff
changeset
|
1184 |
* determined by using the {@code <} operator, lexicographically precedes the |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1185 |
* other string. In this case, {@code compareTo} returns the |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1186 |
* difference of the two character values at position {@code k} in |
2 | 1187 |
* the two string -- that is, the value: |
1188 |
* <blockquote><pre> |
|
1189 |
* this.charAt(k)-anotherString.charAt(k) |
|
1190 |
* </pre></blockquote> |
|
1191 |
* If there is no index position at which they differ, then the shorter |
|
1192 |
* string lexicographically precedes the longer string. In this case, |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1193 |
* {@code compareTo} returns the difference of the lengths of the |
2 | 1194 |
* strings -- that is, the value: |
1195 |
* <blockquote><pre> |
|
1196 |
* this.length()-anotherString.length() |
|
1197 |
* </pre></blockquote> |
|
1198 |
* |
|
33314
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1199 |
* <p>For finer-grained String comparison, refer to |
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1200 |
* {@link java.text.Collator}. |
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1201 |
* |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1202 |
* @param anotherString the {@code String} to be compared. |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1203 |
* @return the value {@code 0} if the argument string is equal to |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1204 |
* this string; a value less than {@code 0} if this string |
2 | 1205 |
* is lexicographically less than the string argument; and a |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1206 |
* value greater than {@code 0} if this string is |
2 | 1207 |
* lexicographically greater than the string argument. |
1208 |
*/ |
|
1209 |
public int compareTo(String anotherString) { |
|
33663 | 1210 |
byte v1[] = value; |
1211 |
byte v2[] = anotherString.value; |
|
1212 |
if (coder() == anotherString.coder()) { |
|
1213 |
return isLatin1() ? StringLatin1.compareTo(v1, v2) |
|
1214 |
: StringUTF16.compareTo(v1, v2); |
|
2 | 1215 |
} |
33663 | 1216 |
return isLatin1() ? StringLatin1.compareToUTF16(v1, v2) |
1217 |
: StringUTF16.compareToLatin1(v1, v2); |
|
1218 |
} |
|
2 | 1219 |
|
1220 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1221 |
* A Comparator that orders {@code String} objects as by |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1222 |
* {@code compareToIgnoreCase}. This comparator is serializable. |
2 | 1223 |
* <p> |
1224 |
* Note that this Comparator does <em>not</em> take locale into account, |
|
1225 |
* and will result in an unsatisfactory ordering for certain locales. |
|
33314
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1226 |
* The {@link java.text.Collator} class provides locale-sensitive comparison. |
2 | 1227 |
* |
33314
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1228 |
* @see java.text.Collator |
2 | 1229 |
* @since 1.2 |
1230 |
*/ |
|
1231 |
public static final Comparator<String> CASE_INSENSITIVE_ORDER |
|
1232 |
= new CaseInsensitiveComparator(); |
|
1233 |
private static class CaseInsensitiveComparator |
|
12858
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
1234 |
implements Comparator<String>, java.io.Serializable { |
2 | 1235 |
// use serialVersionUID from JDK 1.2.2 for interoperability |
1236 |
private static final long serialVersionUID = 8575799808933029326L; |
|
1237 |
||
1238 |
public int compare(String s1, String s2) { |
|
33663 | 1239 |
byte v1[] = s1.value; |
1240 |
byte v2[] = s2.value; |
|
36411
f0cd8358b5ea
8151384: Improve String.CASE_INSENSITIVE_ORDER and remove sun.misc.ASCIICaseInsensitiveComparator
chegar
parents:
35302
diff
changeset
|
1241 |
if (s1.coder() == s2.coder()) { |
f0cd8358b5ea
8151384: Improve String.CASE_INSENSITIVE_ORDER and remove sun.misc.ASCIICaseInsensitiveComparator
chegar
parents:
35302
diff
changeset
|
1242 |
return s1.isLatin1() ? StringLatin1.compareToCI(v1, v2) |
f0cd8358b5ea
8151384: Improve String.CASE_INSENSITIVE_ORDER and remove sun.misc.ASCIICaseInsensitiveComparator
chegar
parents:
35302
diff
changeset
|
1243 |
: StringUTF16.compareToCI(v1, v2); |
2 | 1244 |
} |
36411
f0cd8358b5ea
8151384: Improve String.CASE_INSENSITIVE_ORDER and remove sun.misc.ASCIICaseInsensitiveComparator
chegar
parents:
35302
diff
changeset
|
1245 |
return s1.isLatin1() ? StringLatin1.compareToCI_UTF16(v1, v2) |
f0cd8358b5ea
8151384: Improve String.CASE_INSENSITIVE_ORDER and remove sun.misc.ASCIICaseInsensitiveComparator
chegar
parents:
35302
diff
changeset
|
1246 |
: StringUTF16.compareToCI_Latin1(v1, v2); |
2 | 1247 |
} |
11127
6d29e4d16530
5035850: (str) String.CASE_INSENSITIVE_ORDER should override readResolve()
sherman
parents:
9266
diff
changeset
|
1248 |
|
6d29e4d16530
5035850: (str) String.CASE_INSENSITIVE_ORDER should override readResolve()
sherman
parents:
9266
diff
changeset
|
1249 |
/** Replaces the de-serialized object. */ |
6d29e4d16530
5035850: (str) String.CASE_INSENSITIVE_ORDER should override readResolve()
sherman
parents:
9266
diff
changeset
|
1250 |
private Object readResolve() { return CASE_INSENSITIVE_ORDER; } |
2 | 1251 |
} |
1252 |
||
1253 |
/** |
|
1254 |
* Compares two strings lexicographically, ignoring case |
|
1255 |
* differences. This method returns an integer whose sign is that of |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1256 |
* calling {@code compareTo} with normalized versions of the strings |
2 | 1257 |
* where case differences have been eliminated by calling |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1258 |
* {@code Character.toLowerCase(Character.toUpperCase(character))} on |
2 | 1259 |
* each character. |
1260 |
* <p> |
|
1261 |
* Note that this method does <em>not</em> take locale into account, |
|
1262 |
* and will result in an unsatisfactory ordering for certain locales. |
|
33314
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1263 |
* The {@link java.text.Collator} class provides locale-sensitive comparison. |
2 | 1264 |
* |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1265 |
* @param str the {@code String} to be compared. |
2 | 1266 |
* @return a negative integer, zero, or a positive integer as the |
1267 |
* specified String is greater than, equal to, or less |
|
1268 |
* than this String, ignoring case considerations. |
|
33314
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1269 |
* @see java.text.Collator |
2 | 1270 |
* @since 1.2 |
1271 |
*/ |
|
1272 |
public int compareToIgnoreCase(String str) { |
|
1273 |
return CASE_INSENSITIVE_ORDER.compare(this, str); |
|
1274 |
} |
|
1275 |
||
1276 |
/** |
|
1277 |
* Tests if two string regions are equal. |
|
1278 |
* <p> |
|
14997 | 1279 |
* A substring of this {@code String} object is compared to a substring |
2 | 1280 |
* of the argument other. The result is true if these substrings |
1281 |
* represent identical character sequences. The substring of this |
|
14997 | 1282 |
* {@code String} object to be compared begins at index {@code toffset} |
1283 |
* and has length {@code len}. The substring of other to be compared |
|
1284 |
* begins at index {@code ooffset} and has length {@code len}. The |
|
1285 |
* result is {@code false} if and only if at least one of the following |
|
2 | 1286 |
* is true: |
14997 | 1287 |
* <ul><li>{@code toffset} is negative. |
1288 |
* <li>{@code ooffset} is negative. |
|
1289 |
* <li>{@code toffset+len} is greater than the length of this |
|
1290 |
* {@code String} object. |
|
1291 |
* <li>{@code ooffset+len} is greater than the length of the other |
|
2 | 1292 |
* argument. |
14997 | 1293 |
* <li>There is some nonnegative integer <i>k</i> less than {@code len} |
2 | 1294 |
* such that: |
17181
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
1295 |
* {@code this.charAt(toffset + }<i>k</i>{@code ) != other.charAt(ooffset + } |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
1296 |
* <i>k</i>{@code )} |
2 | 1297 |
* </ul> |
1298 |
* |
|
33314
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1299 |
* <p>Note that this method does <em>not</em> take locale into account. The |
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1300 |
* {@link java.text.Collator} class provides locale-sensitive comparison. |
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1301 |
* |
2 | 1302 |
* @param toffset the starting offset of the subregion in this string. |
1303 |
* @param other the string argument. |
|
1304 |
* @param ooffset the starting offset of the subregion in the string |
|
1305 |
* argument. |
|
1306 |
* @param len the number of characters to compare. |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1307 |
* @return {@code true} if the specified subregion of this string |
2 | 1308 |
* exactly matches the specified subregion of the string argument; |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1309 |
* {@code false} otherwise. |
2 | 1310 |
*/ |
33663 | 1311 |
public boolean regionMatches(int toffset, String other, int ooffset, int len) { |
1312 |
byte tv[] = value; |
|
1313 |
byte ov[] = other.value; |
|
2 | 1314 |
// Note: toffset, ooffset, or len might be near -1>>>1. |
33663 | 1315 |
if ((ooffset < 0) || (toffset < 0) || |
1316 |
(toffset > (long)length() - len) || |
|
1317 |
(ooffset > (long)other.length() - len)) { |
|
2 | 1318 |
return false; |
1319 |
} |
|
33663 | 1320 |
if (coder() == other.coder()) { |
1321 |
if (!isLatin1() && (len > 0)) { |
|
1322 |
toffset = toffset << 1; |
|
1323 |
ooffset = ooffset << 1; |
|
1324 |
len = len << 1; |
|
1325 |
} |
|
1326 |
while (len-- > 0) { |
|
1327 |
if (tv[toffset++] != ov[ooffset++]) { |
|
1328 |
return false; |
|
1329 |
} |
|
1330 |
} |
|
1331 |
} else { |
|
1332 |
if (coder() == LATIN1) { |
|
1333 |
while (len-- > 0) { |
|
1334 |
if (StringLatin1.getChar(tv, toffset++) != |
|
1335 |
StringUTF16.getChar(ov, ooffset++)) { |
|
1336 |
return false; |
|
1337 |
} |
|
1338 |
} |
|
1339 |
} else { |
|
1340 |
while (len-- > 0) { |
|
1341 |
if (StringUTF16.getChar(tv, toffset++) != |
|
1342 |
StringLatin1.getChar(ov, ooffset++)) { |
|
1343 |
return false; |
|
1344 |
} |
|
1345 |
} |
|
2 | 1346 |
} |
1347 |
} |
|
1348 |
return true; |
|
1349 |
} |
|
1350 |
||
1351 |
/** |
|
1352 |
* Tests if two string regions are equal. |
|
1353 |
* <p> |
|
14997 | 1354 |
* A substring of this {@code String} object is compared to a substring |
1355 |
* of the argument {@code other}. The result is {@code true} if these |
|
2 | 1356 |
* substrings represent character sequences that are the same, ignoring |
14997 | 1357 |
* case if and only if {@code ignoreCase} is true. The substring of |
1358 |
* this {@code String} object to be compared begins at index |
|
1359 |
* {@code toffset} and has length {@code len}. The substring of |
|
1360 |
* {@code other} to be compared begins at index {@code ooffset} and |
|
1361 |
* has length {@code len}. The result is {@code false} if and only if |
|
2 | 1362 |
* at least one of the following is true: |
14997 | 1363 |
* <ul><li>{@code toffset} is negative. |
1364 |
* <li>{@code ooffset} is negative. |
|
1365 |
* <li>{@code toffset+len} is greater than the length of this |
|
1366 |
* {@code String} object. |
|
1367 |
* <li>{@code ooffset+len} is greater than the length of the other |
|
2 | 1368 |
* argument. |
14997 | 1369 |
* <li>{@code ignoreCase} is {@code false} and there is some nonnegative |
1370 |
* integer <i>k</i> less than {@code len} such that: |
|
2 | 1371 |
* <blockquote><pre> |
1372 |
* this.charAt(toffset+k) != other.charAt(ooffset+k) |
|
1373 |
* </pre></blockquote> |
|
14997 | 1374 |
* <li>{@code ignoreCase} is {@code true} and there is some nonnegative |
1375 |
* integer <i>k</i> less than {@code len} such that: |
|
2 | 1376 |
* <blockquote><pre> |
33314
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1377 |
* Character.toLowerCase(Character.toUpperCase(this.charAt(toffset+k))) != |
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1378 |
Character.toLowerCase(Character.toUpperCase(other.charAt(ooffset+k))) |
2 | 1379 |
* </pre></blockquote> |
1380 |
* </ul> |
|
1381 |
* |
|
33314
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1382 |
* <p>Note that this method does <em>not</em> take locale into account, |
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1383 |
* and will result in unsatisfactory results for certain locales when |
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1384 |
* {@code ignoreCase} is {@code true}. The {@link java.text.Collator} class |
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1385 |
* provides locale-sensitive comparison. |
777bf87e5050
8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents:
32033
diff
changeset
|
1386 |
* |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1387 |
* @param ignoreCase if {@code true}, ignore case when comparing |
2 | 1388 |
* characters. |
1389 |
* @param toffset the starting offset of the subregion in this |
|
1390 |
* string. |
|
1391 |
* @param other the string argument. |
|
1392 |
* @param ooffset the starting offset of the subregion in the string |
|
1393 |
* argument. |
|
1394 |
* @param len the number of characters to compare. |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1395 |
* @return {@code true} if the specified subregion of this string |
2 | 1396 |
* matches the specified subregion of the string argument; |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1397 |
* {@code false} otherwise. Whether the matching is exact |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1398 |
* or case insensitive depends on the {@code ignoreCase} |
2 | 1399 |
* argument. |
1400 |
*/ |
|
1401 |
public boolean regionMatches(boolean ignoreCase, int toffset, |
|
12858
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
1402 |
String other, int ooffset, int len) { |
33663 | 1403 |
if (!ignoreCase) { |
1404 |
return regionMatches(toffset, other, ooffset, len); |
|
1405 |
} |
|
2 | 1406 |
// Note: toffset, ooffset, or len might be near -1>>>1. |
12858
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
1407 |
if ((ooffset < 0) || (toffset < 0) |
33663 | 1408 |
|| (toffset > (long)length() - len) |
1409 |
|| (ooffset > (long)other.length() - len)) { |
|
2 | 1410 |
return false; |
1411 |
} |
|
33663 | 1412 |
byte tv[] = value; |
1413 |
byte ov[] = other.value; |
|
1414 |
if (coder() == other.coder()) { |
|
1415 |
return isLatin1() |
|
1416 |
? StringLatin1.regionMatchesCI(tv, toffset, ov, ooffset, len) |
|
1417 |
: StringUTF16.regionMatchesCI(tv, toffset, ov, ooffset, len); |
|
2 | 1418 |
} |
33663 | 1419 |
return isLatin1() |
1420 |
? StringLatin1.regionMatchesCI_UTF16(tv, toffset, ov, ooffset, len) |
|
1421 |
: StringUTF16.regionMatchesCI_Latin1(tv, toffset, ov, ooffset, len); |
|
2 | 1422 |
} |
1423 |
||
1424 |
/** |
|
1425 |
* Tests if the substring of this string beginning at the |
|
1426 |
* specified index starts with the specified prefix. |
|
1427 |
* |
|
1428 |
* @param prefix the prefix. |
|
1429 |
* @param toffset where to begin looking in this string. |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1430 |
* @return {@code true} if the character sequence represented by the |
2 | 1431 |
* argument is a prefix of the substring of this object starting |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1432 |
* at index {@code toffset}; {@code false} otherwise. |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1433 |
* The result is {@code false} if {@code toffset} is |
2 | 1434 |
* negative or greater than the length of this |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1435 |
* {@code String} object; otherwise the result is the same |
2 | 1436 |
* as the result of the expression |
1437 |
* <pre> |
|
1438 |
* this.substring(toffset).startsWith(prefix) |
|
1439 |
* </pre> |
|
1440 |
*/ |
|
1441 |
public boolean startsWith(String prefix, int toffset) { |
|
33663 | 1442 |
// Note: toffset might be near -1>>>1. |
1443 |
if (toffset < 0 || toffset > length() - prefix.length()) { |
|
1444 |
return false; |
|
1445 |
} |
|
1446 |
byte ta[] = value; |
|
1447 |
byte pa[] = prefix.value; |
|
12858
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
1448 |
int po = 0; |
30642
d9891f85d583
8071571: Move substring of same string to slow path
igerasim
parents:
30640
diff
changeset
|
1449 |
int pc = pa.length; |
33663 | 1450 |
if (coder() == prefix.coder()) { |
1451 |
int to = isLatin1() ? toffset : toffset << 1; |
|
1452 |
while (po < pc) { |
|
1453 |
if (ta[to++] != pa[po++]) { |
|
1454 |
return false; |
|
1455 |
} |
|
1456 |
} |
|
1457 |
} else { |
|
1458 |
if (isLatin1()) { // && pcoder == UTF16 |
|
2 | 1459 |
return false; |
1460 |
} |
|
33663 | 1461 |
// coder == UTF16 && pcoder == LATIN1) |
1462 |
while (po < pc) { |
|
1463 |
if (StringUTF16.getChar(ta, toffset++) != (pa[po++] & 0xff)) { |
|
1464 |
return false; |
|
1465 |
} |
|
1466 |
} |
|
2 | 1467 |
} |
1468 |
return true; |
|
1469 |
} |
|
1470 |
||
1471 |
/** |
|
1472 |
* Tests if this string starts with the specified prefix. |
|
1473 |
* |
|
1474 |
* @param prefix the prefix. |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1475 |
* @return {@code true} if the character sequence represented by the |
2 | 1476 |
* argument is a prefix of the character sequence represented by |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1477 |
* this string; {@code false} otherwise. |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1478 |
* Note also that {@code true} will be returned if the |
2 | 1479 |
* argument is an empty string or is equal to this |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1480 |
* {@code String} object as determined by the |
2 | 1481 |
* {@link #equals(Object)} method. |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24374
diff
changeset
|
1482 |
* @since 1.0 |
2 | 1483 |
*/ |
1484 |
public boolean startsWith(String prefix) { |
|
1485 |
return startsWith(prefix, 0); |
|
1486 |
} |
|
1487 |
||
1488 |
/** |
|
1489 |
* Tests if this string ends with the specified suffix. |
|
1490 |
* |
|
1491 |
* @param suffix the suffix. |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1492 |
* @return {@code true} if the character sequence represented by the |
2 | 1493 |
* argument is a suffix of the character sequence represented by |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1494 |
* this object; {@code false} otherwise. Note that the |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1495 |
* result will be {@code true} if the argument is the |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1496 |
* empty string or is equal to this {@code String} object |
2 | 1497 |
* as determined by the {@link #equals(Object)} method. |
1498 |
*/ |
|
1499 |
public boolean endsWith(String suffix) { |
|
33663 | 1500 |
return startsWith(suffix, length() - suffix.length()); |
2 | 1501 |
} |
1502 |
||
1503 |
/** |
|
1504 |
* Returns a hash code for this string. The hash code for a |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1505 |
* {@code String} object is computed as |
2 | 1506 |
* <blockquote><pre> |
1507 |
* s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] |
|
1508 |
* </pre></blockquote> |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1509 |
* using {@code int} arithmetic, where {@code s[i]} is the |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1510 |
* <i>i</i>th character of the string, {@code n} is the length of |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1511 |
* the string, and {@code ^} indicates exponentiation. |
2 | 1512 |
* (The hash value of the empty string is zero.) |
1513 |
* |
|
1514 |
* @return a hash code value for this object. |
|
1515 |
*/ |
|
1516 |
public int hashCode() { |
|
54486
7fd299216e97
8221836: Avoid recalculating String.hash when zero
redestad
parents:
54378
diff
changeset
|
1517 |
// The hash or hashIsZero fields are subject to a benign data race, |
7fd299216e97
8221836: Avoid recalculating String.hash when zero
redestad
parents:
54378
diff
changeset
|
1518 |
// making it crucial to ensure that any observable result of the |
7fd299216e97
8221836: Avoid recalculating String.hash when zero
redestad
parents:
54378
diff
changeset
|
1519 |
// calculation in this method stays correct under any possible read of |
7fd299216e97
8221836: Avoid recalculating String.hash when zero
redestad
parents:
54378
diff
changeset
|
1520 |
// these fields. Necessary restrictions to allow this to be correct |
7fd299216e97
8221836: Avoid recalculating String.hash when zero
redestad
parents:
54378
diff
changeset
|
1521 |
// without explicit memory fences or similar concurrency primitives is |
7fd299216e97
8221836: Avoid recalculating String.hash when zero
redestad
parents:
54378
diff
changeset
|
1522 |
// that we can ever only write to one of these two fields for a given |
7fd299216e97
8221836: Avoid recalculating String.hash when zero
redestad
parents:
54378
diff
changeset
|
1523 |
// String instance, and that the computation is idempotent and derived |
7fd299216e97
8221836: Avoid recalculating String.hash when zero
redestad
parents:
54378
diff
changeset
|
1524 |
// from immutable state |
41219
eab7a5086e95
8166842: String.hashCode() has a non-benign data race
plevart
parents:
38786
diff
changeset
|
1525 |
int h = hash; |
54486
7fd299216e97
8221836: Avoid recalculating String.hash when zero
redestad
parents:
54378
diff
changeset
|
1526 |
if (h == 0 && !hashIsZero) { |
54378 | 1527 |
h = isLatin1() ? StringLatin1.hashCode(value) |
1528 |
: StringUTF16.hashCode(value); |
|
54486
7fd299216e97
8221836: Avoid recalculating String.hash when zero
redestad
parents:
54378
diff
changeset
|
1529 |
if (h == 0) { |
7fd299216e97
8221836: Avoid recalculating String.hash when zero
redestad
parents:
54378
diff
changeset
|
1530 |
hashIsZero = true; |
7fd299216e97
8221836: Avoid recalculating String.hash when zero
redestad
parents:
54378
diff
changeset
|
1531 |
} else { |
54378 | 1532 |
hash = h; |
1533 |
} |
|
2 | 1534 |
} |
41219
eab7a5086e95
8166842: String.hashCode() has a non-benign data race
plevart
parents:
38786
diff
changeset
|
1535 |
return h; |
2 | 1536 |
} |
1537 |
||
1538 |
/** |
|
1539 |
* Returns the index within this string of the first occurrence of |
|
1540 |
* the specified character. If a character with value |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1541 |
* {@code ch} occurs in the character sequence represented by |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1542 |
* this {@code String} object, then the index (in Unicode |
2 | 1543 |
* code units) of the first such occurrence is returned. For |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1544 |
* values of {@code ch} in the range from 0 to 0xFFFF |
2 | 1545 |
* (inclusive), this is the smallest value <i>k</i> such that: |
1546 |
* <blockquote><pre> |
|
1547 |
* this.charAt(<i>k</i>) == ch |
|
1548 |
* </pre></blockquote> |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1549 |
* is true. For other values of {@code ch}, it is the |
2 | 1550 |
* smallest value <i>k</i> such that: |
1551 |
* <blockquote><pre> |
|
1552 |
* this.codePointAt(<i>k</i>) == ch |
|
1553 |
* </pre></blockquote> |
|
1554 |
* is true. In either case, if no such character occurs in this |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1555 |
* string, then {@code -1} is returned. |
2 | 1556 |
* |
1557 |
* @param ch a character (Unicode code point). |
|
1558 |
* @return the index of the first occurrence of the character in the |
|
1559 |
* character sequence represented by this object, or |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1560 |
* {@code -1} if the character does not occur. |
2 | 1561 |
*/ |
1562 |
public int indexOf(int ch) { |
|
1563 |
return indexOf(ch, 0); |
|
1564 |
} |
|
1565 |
||
1566 |
/** |
|
1567 |
* Returns the index within this string of the first occurrence of the |
|
1568 |
* specified character, starting the search at the specified index. |
|
1569 |
* <p> |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1570 |
* If a character with value {@code ch} occurs in the |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1571 |
* character sequence represented by this {@code String} |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1572 |
* object at an index no smaller than {@code fromIndex}, then |
2 | 1573 |
* the index of the first such occurrence is returned. For values |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1574 |
* of {@code ch} in the range from 0 to 0xFFFF (inclusive), |
2 | 1575 |
* this is the smallest value <i>k</i> such that: |
1576 |
* <blockquote><pre> |
|
14997 | 1577 |
* (this.charAt(<i>k</i>) == ch) {@code &&} (<i>k</i> >= fromIndex) |
2 | 1578 |
* </pre></blockquote> |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1579 |
* is true. For other values of {@code ch}, it is the |
2 | 1580 |
* smallest value <i>k</i> such that: |
1581 |
* <blockquote><pre> |
|
14997 | 1582 |
* (this.codePointAt(<i>k</i>) == ch) {@code &&} (<i>k</i> >= fromIndex) |
2 | 1583 |
* </pre></blockquote> |
1584 |
* is true. In either case, if no such character occurs in this |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1585 |
* string at or after position {@code fromIndex}, then |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1586 |
* {@code -1} is returned. |
2 | 1587 |
* |
1588 |
* <p> |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1589 |
* There is no restriction on the value of {@code fromIndex}. If it |
2 | 1590 |
* is negative, it has the same effect as if it were zero: this entire |
1591 |
* string may be searched. If it is greater than the length of this |
|
1592 |
* string, it has the same effect as if it were equal to the length of |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1593 |
* this string: {@code -1} is returned. |
2 | 1594 |
* |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1595 |
* <p>All indices are specified in {@code char} values |
2 | 1596 |
* (Unicode code units). |
1597 |
* |
|
1598 |
* @param ch a character (Unicode code point). |
|
1599 |
* @param fromIndex the index to start the search from. |
|
1600 |
* @return the index of the first occurrence of the character in the |
|
1601 |
* character sequence represented by this object that is greater |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1602 |
* than or equal to {@code fromIndex}, or {@code -1} |
2 | 1603 |
* if the character does not occur. |
1604 |
*/ |
|
1605 |
public int indexOf(int ch, int fromIndex) { |
|
33663 | 1606 |
return isLatin1() ? StringLatin1.indexOf(value, ch, fromIndex) |
1607 |
: StringUTF16.indexOf(value, ch, fromIndex); |
|
2 | 1608 |
} |
1609 |
||
1610 |
/** |
|
1611 |
* Returns the index within this string of the last occurrence of |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1612 |
* the specified character. For values of {@code ch} in the |
2 | 1613 |
* range from 0 to 0xFFFF (inclusive), the index (in Unicode code |
1614 |
* units) returned is the largest value <i>k</i> such that: |
|
1615 |
* <blockquote><pre> |
|
1616 |
* this.charAt(<i>k</i>) == ch |
|
1617 |
* </pre></blockquote> |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1618 |
* is true. For other values of {@code ch}, it is the |
2 | 1619 |
* largest value <i>k</i> such that: |
1620 |
* <blockquote><pre> |
|
1621 |
* this.codePointAt(<i>k</i>) == ch |
|
1622 |
* </pre></blockquote> |
|
1623 |
* is true. In either case, if no such character occurs in this |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1624 |
* string, then {@code -1} is returned. The |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1625 |
* {@code String} is searched backwards starting at the last |
2 | 1626 |
* character. |
1627 |
* |
|
1628 |
* @param ch a character (Unicode code point). |
|
1629 |
* @return the index of the last occurrence of the character in the |
|
1630 |
* character sequence represented by this object, or |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1631 |
* {@code -1} if the character does not occur. |
2 | 1632 |
*/ |
1633 |
public int lastIndexOf(int ch) { |
|
33663 | 1634 |
return lastIndexOf(ch, length() - 1); |
2 | 1635 |
} |
1636 |
||
1637 |
/** |
|
1638 |
* Returns the index within this string of the last occurrence of |
|
1639 |
* the specified character, searching backward starting at the |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1640 |
* specified index. For values of {@code ch} in the range |
2 | 1641 |
* from 0 to 0xFFFF (inclusive), the index returned is the largest |
1642 |
* value <i>k</i> such that: |
|
1643 |
* <blockquote><pre> |
|
14997 | 1644 |
* (this.charAt(<i>k</i>) == ch) {@code &&} (<i>k</i> <= fromIndex) |
2 | 1645 |
* </pre></blockquote> |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1646 |
* is true. For other values of {@code ch}, it is the |
2 | 1647 |
* largest value <i>k</i> such that: |
1648 |
* <blockquote><pre> |
|
14997 | 1649 |
* (this.codePointAt(<i>k</i>) == ch) {@code &&} (<i>k</i> <= fromIndex) |
2 | 1650 |
* </pre></blockquote> |
1651 |
* is true. In either case, if no such character occurs in this |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1652 |
* string at or before position {@code fromIndex}, then |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1653 |
* {@code -1} is returned. |
2 | 1654 |
* |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1655 |
* <p>All indices are specified in {@code char} values |
2 | 1656 |
* (Unicode code units). |
1657 |
* |
|
1658 |
* @param ch a character (Unicode code point). |
|
1659 |
* @param fromIndex the index to start the search from. There is no |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1660 |
* restriction on the value of {@code fromIndex}. If it is |
2 | 1661 |
* greater than or equal to the length of this string, it has |
1662 |
* the same effect as if it were equal to one less than the |
|
1663 |
* length of this string: this entire string may be searched. |
|
1664 |
* If it is negative, it has the same effect as if it were -1: |
|
1665 |
* -1 is returned. |
|
1666 |
* @return the index of the last occurrence of the character in the |
|
1667 |
* character sequence represented by this object that is less |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1668 |
* than or equal to {@code fromIndex}, or {@code -1} |
2 | 1669 |
* if the character does not occur before that point. |
1670 |
*/ |
|
1671 |
public int lastIndexOf(int ch, int fromIndex) { |
|
33663 | 1672 |
return isLatin1() ? StringLatin1.lastIndexOf(value, ch, fromIndex) |
1673 |
: StringUTF16.lastIndexOf(value, ch, fromIndex); |
|
2 | 1674 |
} |
1675 |
||
1676 |
/** |
|
1677 |
* Returns the index within this string of the first occurrence of the |
|
5988
2c984724db38
6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents:
5987
diff
changeset
|
1678 |
* specified substring. |
2c984724db38
6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents:
5987
diff
changeset
|
1679 |
* |
23024
7d5ecb31115f
8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents:
22943
diff
changeset
|
1680 |
* <p>The returned index is the smallest value {@code k} for which: |
7d5ecb31115f
8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents:
22943
diff
changeset
|
1681 |
* <pre>{@code |
7d5ecb31115f
8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents:
22943
diff
changeset
|
1682 |
* this.startsWith(str, k) |
7d5ecb31115f
8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents:
22943
diff
changeset
|
1683 |
* }</pre> |
7d5ecb31115f
8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents:
22943
diff
changeset
|
1684 |
* If no such value of {@code k} exists, then {@code -1} is returned. |
2 | 1685 |
* |
5988
2c984724db38
6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents:
5987
diff
changeset
|
1686 |
* @param str the substring to search for. |
2c984724db38
6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents:
5987
diff
changeset
|
1687 |
* @return the index of the first occurrence of the specified substring, |
2c984724db38
6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents:
5987
diff
changeset
|
1688 |
* or {@code -1} if there is no such occurrence. |
2 | 1689 |
*/ |
1690 |
public int indexOf(String str) { |
|
33663 | 1691 |
if (coder() == str.coder()) { |
1692 |
return isLatin1() ? StringLatin1.indexOf(value, str.value) |
|
1693 |
: StringUTF16.indexOf(value, str.value); |
|
1694 |
} |
|
1695 |
if (coder() == LATIN1) { // str.coder == UTF16 |
|
1696 |
return -1; |
|
1697 |
} |
|
1698 |
return StringUTF16.indexOfLatin1(value, str.value); |
|
2 | 1699 |
} |
1700 |
||
1701 |
/** |
|
1702 |
* Returns the index within this string of the first occurrence of the |
|
5988
2c984724db38
6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents:
5987
diff
changeset
|
1703 |
* specified substring, starting at the specified index. |
2c984724db38
6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents:
5987
diff
changeset
|
1704 |
* |
23024
7d5ecb31115f
8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents:
22943
diff
changeset
|
1705 |
* <p>The returned index is the smallest value {@code k} for which: |
7d5ecb31115f
8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents:
22943
diff
changeset
|
1706 |
* <pre>{@code |
7d5ecb31115f
8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents:
22943
diff
changeset
|
1707 |
* k >= Math.min(fromIndex, this.length()) && |
7d5ecb31115f
8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents:
22943
diff
changeset
|
1708 |
* this.startsWith(str, k) |
7d5ecb31115f
8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents:
22943
diff
changeset
|
1709 |
* }</pre> |
7d5ecb31115f
8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents:
22943
diff
changeset
|
1710 |
* If no such value of {@code k} exists, then {@code -1} is returned. |
2 | 1711 |
* |
5988
2c984724db38
6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents:
5987
diff
changeset
|
1712 |
* @param str the substring to search for. |
2 | 1713 |
* @param fromIndex the index from which to start the search. |
5988
2c984724db38
6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents:
5987
diff
changeset
|
1714 |
* @return the index of the first occurrence of the specified substring, |
2c984724db38
6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents:
5987
diff
changeset
|
1715 |
* starting at the specified index, |
2c984724db38
6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents:
5987
diff
changeset
|
1716 |
* or {@code -1} if there is no such occurrence. |
2 | 1717 |
*/ |
1718 |
public int indexOf(String str, int fromIndex) { |
|
33663 | 1719 |
return indexOf(value, coder(), length(), str, fromIndex); |
2 | 1720 |
} |
1721 |
||
1722 |
/** |
|
14686
fb59583d33b2
6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents:
14014
diff
changeset
|
1723 |
* Code shared by String and AbstractStringBuilder to do searches. The |
fb59583d33b2
6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents:
14014
diff
changeset
|
1724 |
* source is the character array being searched, and the target |
fb59583d33b2
6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents:
14014
diff
changeset
|
1725 |
* is the string being searched for. |
fb59583d33b2
6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents:
14014
diff
changeset
|
1726 |
* |
33663 | 1727 |
* @param src the characters being searched. |
1728 |
* @param srcCoder the coder of the source string. |
|
1729 |
* @param srcCount length of the source string. |
|
1730 |
* @param tgtStr the characters being searched for. |
|
1731 |
* @param fromIndex the index to begin searching from. |
|
14686
fb59583d33b2
6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents:
14014
diff
changeset
|
1732 |
*/ |
33663 | 1733 |
static int indexOf(byte[] src, byte srcCoder, int srcCount, |
1734 |
String tgtStr, int fromIndex) { |
|
1735 |
byte[] tgt = tgtStr.value; |
|
1736 |
byte tgtCoder = tgtStr.coder(); |
|
1737 |
int tgtCount = tgtStr.length(); |
|
1738 |
||
1739 |
if (fromIndex >= srcCount) { |
|
1740 |
return (tgtCount == 0 ? srcCount : -1); |
|
2 | 1741 |
} |
1742 |
if (fromIndex < 0) { |
|
1743 |
fromIndex = 0; |
|
1744 |
} |
|
33663 | 1745 |
if (tgtCount == 0) { |
2 | 1746 |
return fromIndex; |
1747 |
} |
|
44642
331e669007f7
8158168: Missing bounds checks for some String intrinsics
dlong
parents:
44369
diff
changeset
|
1748 |
if (tgtCount > srcCount) { |
331e669007f7
8158168: Missing bounds checks for some String intrinsics
dlong
parents:
44369
diff
changeset
|
1749 |
return -1; |
331e669007f7
8158168: Missing bounds checks for some String intrinsics
dlong
parents:
44369
diff
changeset
|
1750 |
} |
33663 | 1751 |
if (srcCoder == tgtCoder) { |
1752 |
return srcCoder == LATIN1 |
|
1753 |
? StringLatin1.indexOf(src, srcCount, tgt, tgtCount, fromIndex) |
|
1754 |
: StringUTF16.indexOf(src, srcCount, tgt, tgtCount, fromIndex); |
|
2 | 1755 |
} |
33663 | 1756 |
if (srcCoder == LATIN1) { // && tgtCoder == UTF16 |
1757 |
return -1; |
|
1758 |
} |
|
1759 |
// srcCoder == UTF16 && tgtCoder == LATIN1) { |
|
1760 |
return StringUTF16.indexOfLatin1(src, srcCount, tgt, tgtCount, fromIndex); |
|
2 | 1761 |
} |
1762 |
||
1763 |
/** |
|
5988
2c984724db38
6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents:
5987
diff
changeset
|
1764 |
* Returns the index within this string of the last occurrence of the |
2c984724db38
6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents:
5987
diff
changeset
|
1765 |
* specified substring. The last occurrence of the empty string "" |
2c984724db38
6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents:
5987
diff
changeset
|
1766 |
* is considered to occur at the index value {@code this.length()}. |
2c984724db38
6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents:
5987
diff
changeset
|
1767 |
* |
23024
7d5ecb31115f
8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents:
22943
diff
changeset
|
1768 |
* <p>The returned index is the largest value {@code k} for which: |
7d5ecb31115f
8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents:
22943
diff
changeset
|
1769 |
* <pre>{@code |
7d5ecb31115f
8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents:
22943
diff
changeset
|
1770 |
* this.startsWith(str, k) |
7d5ecb31115f
8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents:
22943
diff
changeset
|
1771 |
* }</pre> |
7d5ecb31115f
8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents:
22943
diff
changeset
|
1772 |
* If no such value of {@code k} exists, then {@code -1} is returned. |
2 | 1773 |
* |
1774 |
* @param str the substring to search for. |
|
5988
2c984724db38
6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents:
5987
diff
changeset
|
1775 |
* @return the index of the last occurrence of the specified substring, |
2c984724db38
6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents:
5987
diff
changeset
|
1776 |
* or {@code -1} if there is no such occurrence. |
2 | 1777 |
*/ |
1778 |
public int lastIndexOf(String str) { |
|
33663 | 1779 |
return lastIndexOf(str, length()); |
2 | 1780 |
} |
1781 |
||
1782 |
/** |
|
1783 |
* Returns the index within this string of the last occurrence of the |
|
1784 |
* specified substring, searching backward starting at the specified index. |
|
5988
2c984724db38
6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents:
5987
diff
changeset
|
1785 |
* |
23024
7d5ecb31115f
8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents:
22943
diff
changeset
|
1786 |
* <p>The returned index is the largest value {@code k} for which: |
7d5ecb31115f
8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents:
22943
diff
changeset
|
1787 |
* <pre>{@code |
7d5ecb31115f
8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents:
22943
diff
changeset
|
1788 |
* k <= Math.min(fromIndex, this.length()) && |
7d5ecb31115f
8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents:
22943
diff
changeset
|
1789 |
* this.startsWith(str, k) |
7d5ecb31115f
8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents:
22943
diff
changeset
|
1790 |
* }</pre> |
7d5ecb31115f
8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents:
22943
diff
changeset
|
1791 |
* If no such value of {@code k} exists, then {@code -1} is returned. |
2 | 1792 |
* |
1793 |
* @param str the substring to search for. |
|
1794 |
* @param fromIndex the index to start the search from. |
|
5988
2c984724db38
6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents:
5987
diff
changeset
|
1795 |
* @return the index of the last occurrence of the specified substring, |
2c984724db38
6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents:
5987
diff
changeset
|
1796 |
* searching backward from the specified index, |
2c984724db38
6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents:
5987
diff
changeset
|
1797 |
* or {@code -1} if there is no such occurrence. |
2 | 1798 |
*/ |
1799 |
public int lastIndexOf(String str, int fromIndex) { |
|
33663 | 1800 |
return lastIndexOf(value, coder(), length(), str, fromIndex); |
2 | 1801 |
} |
1802 |
||
1803 |
/** |
|
14686
fb59583d33b2
6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents:
14014
diff
changeset
|
1804 |
* Code shared by String and AbstractStringBuilder to do searches. The |
fb59583d33b2
6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents:
14014
diff
changeset
|
1805 |
* source is the character array being searched, and the target |
fb59583d33b2
6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents:
14014
diff
changeset
|
1806 |
* is the string being searched for. |
fb59583d33b2
6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents:
14014
diff
changeset
|
1807 |
* |
33663 | 1808 |
* @param src the characters being searched. |
44642
331e669007f7
8158168: Missing bounds checks for some String intrinsics
dlong
parents:
44369
diff
changeset
|
1809 |
* @param srcCoder coder handles the mapping between bytes/chars |
33663 | 1810 |
* @param srcCount count of the source string. |
1811 |
* @param tgt the characters being searched for. |
|
1812 |
* @param fromIndex the index to begin searching from. |
|
14686
fb59583d33b2
6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents:
14014
diff
changeset
|
1813 |
*/ |
33663 | 1814 |
static int lastIndexOf(byte[] src, byte srcCoder, int srcCount, |
1815 |
String tgtStr, int fromIndex) { |
|
1816 |
byte[] tgt = tgtStr.value; |
|
1817 |
byte tgtCoder = tgtStr.coder(); |
|
1818 |
int tgtCount = tgtStr.length(); |
|
2 | 1819 |
/* |
1820 |
* Check arguments; return immediately where possible. For |
|
1821 |
* consistency, don't check for null str. |
|
1822 |
*/ |
|
33663 | 1823 |
int rightIndex = srcCount - tgtCount; |
44642
331e669007f7
8158168: Missing bounds checks for some String intrinsics
dlong
parents:
44369
diff
changeset
|
1824 |
if (fromIndex > rightIndex) { |
331e669007f7
8158168: Missing bounds checks for some String intrinsics
dlong
parents:
44369
diff
changeset
|
1825 |
fromIndex = rightIndex; |
331e669007f7
8158168: Missing bounds checks for some String intrinsics
dlong
parents:
44369
diff
changeset
|
1826 |
} |
2 | 1827 |
if (fromIndex < 0) { |
1828 |
return -1; |
|
1829 |
} |
|
1830 |
/* Empty string always matches. */ |
|
33663 | 1831 |
if (tgtCount == 0) { |
2 | 1832 |
return fromIndex; |
1833 |
} |
|
33663 | 1834 |
if (srcCoder == tgtCoder) { |
1835 |
return srcCoder == LATIN1 |
|
1836 |
? StringLatin1.lastIndexOf(src, srcCount, tgt, tgtCount, fromIndex) |
|
1837 |
: StringUTF16.lastIndexOf(src, srcCount, tgt, tgtCount, fromIndex); |
|
1838 |
} |
|
1839 |
if (srcCoder == LATIN1) { // && tgtCoder == UTF16 |
|
1840 |
return -1; |
|
1841 |
} |
|
44642
331e669007f7
8158168: Missing bounds checks for some String intrinsics
dlong
parents:
44369
diff
changeset
|
1842 |
// srcCoder == UTF16 && tgtCoder == LATIN1 |
331e669007f7
8158168: Missing bounds checks for some String intrinsics
dlong
parents:
44369
diff
changeset
|
1843 |
return StringUTF16.lastIndexOfLatin1(src, srcCount, tgt, tgtCount, fromIndex); |
2 | 1844 |
} |
1845 |
||
1846 |
/** |
|
21841
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
1847 |
* Returns a string that is a substring of this string. The |
2 | 1848 |
* substring begins with the character at the specified index and |
1849 |
* extends to the end of this string. <p> |
|
1850 |
* Examples: |
|
1851 |
* <blockquote><pre> |
|
1852 |
* "unhappy".substring(2) returns "happy" |
|
1853 |
* "Harbison".substring(3) returns "bison" |
|
1854 |
* "emptiness".substring(9) returns "" (an empty string) |
|
1855 |
* </pre></blockquote> |
|
1856 |
* |
|
1857 |
* @param beginIndex the beginning index, inclusive. |
|
1858 |
* @return the specified substring. |
|
1859 |
* @exception IndexOutOfBoundsException if |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1860 |
* {@code beginIndex} is negative or larger than the |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1861 |
* length of this {@code String} object. |
2 | 1862 |
*/ |
1863 |
public String substring(int beginIndex) { |
|
33663 | 1864 |
if (beginIndex < 0) { |
1865 |
throw new StringIndexOutOfBoundsException(beginIndex); |
|
12858
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
1866 |
} |
33663 | 1867 |
int subLen = length() - beginIndex; |
12858
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
1868 |
if (subLen < 0) { |
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
1869 |
throw new StringIndexOutOfBoundsException(subLen); |
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
1870 |
} |
33663 | 1871 |
if (beginIndex == 0) { |
1872 |
return this; |
|
1873 |
} |
|
1874 |
return isLatin1() ? StringLatin1.newString(value, beginIndex, subLen) |
|
1875 |
: StringUTF16.newString(value, beginIndex, subLen); |
|
2 | 1876 |
} |
1877 |
||
1878 |
/** |
|
21841
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
1879 |
* Returns a string that is a substring of this string. The |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1880 |
* substring begins at the specified {@code beginIndex} and |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1881 |
* extends to the character at index {@code endIndex - 1}. |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1882 |
* Thus the length of the substring is {@code endIndex-beginIndex}. |
2 | 1883 |
* <p> |
1884 |
* Examples: |
|
1885 |
* <blockquote><pre> |
|
1886 |
* "hamburger".substring(4, 8) returns "urge" |
|
1887 |
* "smiles".substring(1, 5) returns "mile" |
|
1888 |
* </pre></blockquote> |
|
1889 |
* |
|
1890 |
* @param beginIndex the beginning index, inclusive. |
|
1891 |
* @param endIndex the ending index, exclusive. |
|
1892 |
* @return the specified substring. |
|
1893 |
* @exception IndexOutOfBoundsException if the |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1894 |
* {@code beginIndex} is negative, or |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1895 |
* {@code endIndex} is larger than the length of |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1896 |
* this {@code String} object, or |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1897 |
* {@code beginIndex} is larger than |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1898 |
* {@code endIndex}. |
2 | 1899 |
*/ |
1900 |
public String substring(int beginIndex, int endIndex) { |
|
33663 | 1901 |
int length = length(); |
1902 |
checkBoundsBeginEnd(beginIndex, endIndex, length); |
|
1903 |
int subLen = endIndex - beginIndex; |
|
1904 |
if (beginIndex == 0 && endIndex == length) { |
|
1905 |
return this; |
|
2 | 1906 |
} |
33663 | 1907 |
return isLatin1() ? StringLatin1.newString(value, beginIndex, subLen) |
1908 |
: StringUTF16.newString(value, beginIndex, subLen); |
|
2 | 1909 |
} |
1910 |
||
1911 |
/** |
|
21959
0767c3d7c550
8028757: CharSequence.subSequence improperly requires a "new" CharSequence be returned
smarks
parents:
21841
diff
changeset
|
1912 |
* Returns a character sequence that is a subsequence of this sequence. |
2 | 1913 |
* |
1914 |
* <p> An invocation of this method of the form |
|
1915 |
* |
|
1916 |
* <blockquote><pre> |
|
1917 |
* str.subSequence(begin, end)</pre></blockquote> |
|
1918 |
* |
|
1919 |
* behaves in exactly the same way as the invocation |
|
1920 |
* |
|
1921 |
* <blockquote><pre> |
|
1922 |
* str.substring(begin, end)</pre></blockquote> |
|
1923 |
* |
|
21841
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
1924 |
* @apiNote |
13156
e88d9099b6f0
7170938: (str) incorrect wording in doc for String.subSequence
smarks
parents:
12859
diff
changeset
|
1925 |
* This method is defined so that the {@code String} class can implement |
17181
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
1926 |
* the {@link CharSequence} interface. |
2 | 1927 |
* |
13156
e88d9099b6f0
7170938: (str) incorrect wording in doc for String.subSequence
smarks
parents:
12859
diff
changeset
|
1928 |
* @param beginIndex the begin index, inclusive. |
e88d9099b6f0
7170938: (str) incorrect wording in doc for String.subSequence
smarks
parents:
12859
diff
changeset
|
1929 |
* @param endIndex the end index, exclusive. |
e88d9099b6f0
7170938: (str) incorrect wording in doc for String.subSequence
smarks
parents:
12859
diff
changeset
|
1930 |
* @return the specified subsequence. |
2 | 1931 |
* |
1932 |
* @throws IndexOutOfBoundsException |
|
13156
e88d9099b6f0
7170938: (str) incorrect wording in doc for String.subSequence
smarks
parents:
12859
diff
changeset
|
1933 |
* if {@code beginIndex} or {@code endIndex} is negative, |
e88d9099b6f0
7170938: (str) incorrect wording in doc for String.subSequence
smarks
parents:
12859
diff
changeset
|
1934 |
* if {@code endIndex} is greater than {@code length()}, |
e88d9099b6f0
7170938: (str) incorrect wording in doc for String.subSequence
smarks
parents:
12859
diff
changeset
|
1935 |
* or if {@code beginIndex} is greater than {@code endIndex} |
2 | 1936 |
* |
1937 |
* @since 1.4 |
|
1938 |
* @spec JSR-51 |
|
1939 |
*/ |
|
1940 |
public CharSequence subSequence(int beginIndex, int endIndex) { |
|
1941 |
return this.substring(beginIndex, endIndex); |
|
1942 |
} |
|
1943 |
||
1944 |
/** |
|
1945 |
* Concatenates the specified string to the end of this string. |
|
1946 |
* <p> |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1947 |
* If the length of the argument string is {@code 0}, then this |
21841
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
1948 |
* {@code String} object is returned. Otherwise, a |
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
1949 |
* {@code String} object is returned that represents a character |
2 | 1950 |
* sequence that is the concatenation of the character sequence |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1951 |
* represented by this {@code String} object and the character |
2 | 1952 |
* sequence represented by the argument string.<p> |
1953 |
* Examples: |
|
1954 |
* <blockquote><pre> |
|
1955 |
* "cares".concat("s") returns "caress" |
|
1956 |
* "to".concat("get").concat("her") returns "together" |
|
1957 |
* </pre></blockquote> |
|
1958 |
* |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1959 |
* @param str the {@code String} that is concatenated to the end |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1960 |
* of this {@code String}. |
2 | 1961 |
* @return a string that represents the concatenation of this object's |
1962 |
* characters followed by the string argument's characters. |
|
1963 |
*/ |
|
1964 |
public String concat(String str) { |
|
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
52914
diff
changeset
|
1965 |
if (str.isEmpty()) { |
2 | 1966 |
return this; |
1967 |
} |
|
54550
5fa7fbddfe9d
8222484: Specialize generation of simple String concatenation expressions
redestad
parents:
54486
diff
changeset
|
1968 |
return StringConcatHelper.simpleConcat(this, str); |
2 | 1969 |
} |
1970 |
||
1971 |
/** |
|
21841
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
1972 |
* Returns a string resulting from replacing all occurrences of |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1973 |
* {@code oldChar} in this string with {@code newChar}. |
2 | 1974 |
* <p> |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1975 |
* If the character {@code oldChar} does not occur in the |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1976 |
* character sequence represented by this {@code String} object, |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1977 |
* then a reference to this {@code String} object is returned. |
21841
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
1978 |
* Otherwise, a {@code String} object is returned that |
2 | 1979 |
* represents a character sequence identical to the character sequence |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1980 |
* represented by this {@code String} object, except that every |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1981 |
* occurrence of {@code oldChar} is replaced by an occurrence |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1982 |
* of {@code newChar}. |
2 | 1983 |
* <p> |
1984 |
* Examples: |
|
1985 |
* <blockquote><pre> |
|
1986 |
* "mesquite in your cellar".replace('e', 'o') |
|
1987 |
* returns "mosquito in your collar" |
|
1988 |
* "the war of baronets".replace('r', 'y') |
|
1989 |
* returns "the way of bayonets" |
|
1990 |
* "sparring with a purple porpoise".replace('p', 't') |
|
1991 |
* returns "starring with a turtle tortoise" |
|
1992 |
* "JonL".replace('q', 'x') returns "JonL" (no change) |
|
1993 |
* </pre></blockquote> |
|
1994 |
* |
|
1995 |
* @param oldChar the old character. |
|
1996 |
* @param newChar the new character. |
|
1997 |
* @return a string derived from this string by replacing every |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
1998 |
* occurrence of {@code oldChar} with {@code newChar}. |
2 | 1999 |
*/ |
2000 |
public String replace(char oldChar, char newChar) { |
|
2001 |
if (oldChar != newChar) { |
|
33663 | 2002 |
String ret = isLatin1() ? StringLatin1.replace(value, oldChar, newChar) |
2003 |
: StringUTF16.replace(value, oldChar, newChar); |
|
2004 |
if (ret != null) { |
|
2005 |
return ret; |
|
2 | 2006 |
} |
2007 |
} |
|
2008 |
return this; |
|
2009 |
} |
|
2010 |
||
2011 |
/** |
|
2012 |
* Tells whether or not this string matches the given <a |
|
2013 |
* href="../util/regex/Pattern.html#sum">regular expression</a>. |
|
2014 |
* |
|
2015 |
* <p> An invocation of this method of the form |
|
14997 | 2016 |
* <i>str</i>{@code .matches(}<i>regex</i>{@code )} yields exactly the |
2 | 2017 |
* same result as the expression |
2018 |
* |
|
14997 | 2019 |
* <blockquote> |
2020 |
* {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#matches(String,CharSequence) |
|
2021 |
* matches(<i>regex</i>, <i>str</i>)} |
|
2022 |
* </blockquote> |
|
2 | 2023 |
* |
2024 |
* @param regex |
|
2025 |
* the regular expression to which this string is to be matched |
|
2026 |
* |
|
14997 | 2027 |
* @return {@code true} if, and only if, this string matches the |
2 | 2028 |
* given regular expression |
2029 |
* |
|
2030 |
* @throws PatternSyntaxException |
|
2031 |
* if the regular expression's syntax is invalid |
|
2032 |
* |
|
2033 |
* @see java.util.regex.Pattern |
|
2034 |
* |
|
2035 |
* @since 1.4 |
|
2036 |
* @spec JSR-51 |
|
2037 |
*/ |
|
2038 |
public boolean matches(String regex) { |
|
2039 |
return Pattern.matches(regex, this); |
|
2040 |
} |
|
2041 |
||
2042 |
/** |
|
2043 |
* Returns true if and only if this string contains the specified |
|
2044 |
* sequence of char values. |
|
2045 |
* |
|
2046 |
* @param s the sequence to search for |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
2047 |
* @return true if this string contains {@code s}, false otherwise |
2 | 2048 |
* @since 1.5 |
2049 |
*/ |
|
2050 |
public boolean contains(CharSequence s) { |
|
26451 | 2051 |
return indexOf(s.toString()) >= 0; |
2 | 2052 |
} |
2053 |
||
2054 |
/** |
|
2055 |
* Replaces the first substring of this string that matches the given <a |
|
2056 |
* href="../util/regex/Pattern.html#sum">regular expression</a> with the |
|
2057 |
* given replacement. |
|
2058 |
* |
|
2059 |
* <p> An invocation of this method of the form |
|
14997 | 2060 |
* <i>str</i>{@code .replaceFirst(}<i>regex</i>{@code ,} <i>repl</i>{@code )} |
2 | 2061 |
* yields exactly the same result as the expression |
2062 |
* |
|
14997 | 2063 |
* <blockquote> |
2064 |
* <code> |
|
2065 |
* {@link java.util.regex.Pattern}.{@link |
|
2066 |
* java.util.regex.Pattern#compile compile}(<i>regex</i>).{@link |
|
2067 |
* java.util.regex.Pattern#matcher(java.lang.CharSequence) matcher}(<i>str</i>).{@link |
|
2068 |
* java.util.regex.Matcher#replaceFirst replaceFirst}(<i>repl</i>) |
|
2069 |
* </code> |
|
2070 |
* </blockquote> |
|
2 | 2071 |
* |
2072 |
*<p> |
|
14997 | 2073 |
* Note that backslashes ({@code \}) and dollar signs ({@code $}) in the |
2 | 2074 |
* replacement string may cause the results to be different than if it were |
2075 |
* being treated as a literal replacement string; see |
|
2076 |
* {@link java.util.regex.Matcher#replaceFirst}. |
|
2077 |
* Use {@link java.util.regex.Matcher#quoteReplacement} to suppress the special |
|
2078 |
* meaning of these characters, if desired. |
|
2079 |
* |
|
2080 |
* @param regex |
|
2081 |
* the regular expression to which this string is to be matched |
|
2082 |
* @param replacement |
|
2083 |
* the string to be substituted for the first match |
|
2084 |
* |
|
14997 | 2085 |
* @return The resulting {@code String} |
2 | 2086 |
* |
2087 |
* @throws PatternSyntaxException |
|
2088 |
* if the regular expression's syntax is invalid |
|
2089 |
* |
|
2090 |
* @see java.util.regex.Pattern |
|
2091 |
* |
|
2092 |
* @since 1.4 |
|
2093 |
* @spec JSR-51 |
|
2094 |
*/ |
|
2095 |
public String replaceFirst(String regex, String replacement) { |
|
2096 |
return Pattern.compile(regex).matcher(this).replaceFirst(replacement); |
|
2097 |
} |
|
2098 |
||
2099 |
/** |
|
2100 |
* Replaces each substring of this string that matches the given <a |
|
2101 |
* href="../util/regex/Pattern.html#sum">regular expression</a> with the |
|
2102 |
* given replacement. |
|
2103 |
* |
|
2104 |
* <p> An invocation of this method of the form |
|
14997 | 2105 |
* <i>str</i>{@code .replaceAll(}<i>regex</i>{@code ,} <i>repl</i>{@code )} |
2 | 2106 |
* yields exactly the same result as the expression |
2107 |
* |
|
14997 | 2108 |
* <blockquote> |
2109 |
* <code> |
|
2110 |
* {@link java.util.regex.Pattern}.{@link |
|
2111 |
* java.util.regex.Pattern#compile compile}(<i>regex</i>).{@link |
|
2112 |
* java.util.regex.Pattern#matcher(java.lang.CharSequence) matcher}(<i>str</i>).{@link |
|
2113 |
* java.util.regex.Matcher#replaceAll replaceAll}(<i>repl</i>) |
|
2114 |
* </code> |
|
2115 |
* </blockquote> |
|
2 | 2116 |
* |
2117 |
*<p> |
|
14997 | 2118 |
* Note that backslashes ({@code \}) and dollar signs ({@code $}) in the |
2 | 2119 |
* replacement string may cause the results to be different than if it were |
2120 |
* being treated as a literal replacement string; see |
|
2121 |
* {@link java.util.regex.Matcher#replaceAll Matcher.replaceAll}. |
|
2122 |
* Use {@link java.util.regex.Matcher#quoteReplacement} to suppress the special |
|
2123 |
* meaning of these characters, if desired. |
|
2124 |
* |
|
2125 |
* @param regex |
|
2126 |
* the regular expression to which this string is to be matched |
|
2127 |
* @param replacement |
|
2128 |
* the string to be substituted for each match |
|
2129 |
* |
|
14997 | 2130 |
* @return The resulting {@code String} |
2 | 2131 |
* |
2132 |
* @throws PatternSyntaxException |
|
2133 |
* if the regular expression's syntax is invalid |
|
2134 |
* |
|
2135 |
* @see java.util.regex.Pattern |
|
2136 |
* |
|
2137 |
* @since 1.4 |
|
2138 |
* @spec JSR-51 |
|
2139 |
*/ |
|
2140 |
public String replaceAll(String regex, String replacement) { |
|
2141 |
return Pattern.compile(regex).matcher(this).replaceAll(replacement); |
|
2142 |
} |
|
2143 |
||
2144 |
/** |
|
2145 |
* Replaces each substring of this string that matches the literal target |
|
2146 |
* sequence with the specified literal replacement sequence. The |
|
2147 |
* replacement proceeds from the beginning of the string to the end, for |
|
2148 |
* example, replacing "aa" with "b" in the string "aaa" will result in |
|
2149 |
* "ba" rather than "ab". |
|
2150 |
* |
|
2151 |
* @param target The sequence of char values to be replaced |
|
2152 |
* @param replacement The replacement sequence of char values |
|
2153 |
* @return The resulting string |
|
2154 |
* @since 1.5 |
|
2155 |
*/ |
|
2156 |
public String replace(CharSequence target, CharSequence replacement) { |
|
33663 | 2157 |
String tgtStr = target.toString(); |
2158 |
String replStr = replacement.toString(); |
|
2159 |
int j = indexOf(tgtStr); |
|
30913
bb5c454d451e
8058779: Faster implementation of String.replace(CharSequence, CharSequence)
igerasim
parents:
30642
diff
changeset
|
2160 |
if (j < 0) { |
bb5c454d451e
8058779: Faster implementation of String.replace(CharSequence, CharSequence)
igerasim
parents:
30642
diff
changeset
|
2161 |
return this; |
bb5c454d451e
8058779: Faster implementation of String.replace(CharSequence, CharSequence)
igerasim
parents:
30642
diff
changeset
|
2162 |
} |
33663 | 2163 |
int tgtLen = tgtStr.length(); |
2164 |
int tgtLen1 = Math.max(tgtLen, 1); |
|
2165 |
int thisLen = length(); |
|
2166 |
||
2167 |
int newLenHint = thisLen - tgtLen + replStr.length(); |
|
30913
bb5c454d451e
8058779: Faster implementation of String.replace(CharSequence, CharSequence)
igerasim
parents:
30642
diff
changeset
|
2168 |
if (newLenHint < 0) { |
bb5c454d451e
8058779: Faster implementation of String.replace(CharSequence, CharSequence)
igerasim
parents:
30642
diff
changeset
|
2169 |
throw new OutOfMemoryError(); |
bb5c454d451e
8058779: Faster implementation of String.replace(CharSequence, CharSequence)
igerasim
parents:
30642
diff
changeset
|
2170 |
} |
bb5c454d451e
8058779: Faster implementation of String.replace(CharSequence, CharSequence)
igerasim
parents:
30642
diff
changeset
|
2171 |
StringBuilder sb = new StringBuilder(newLenHint); |
bb5c454d451e
8058779: Faster implementation of String.replace(CharSequence, CharSequence)
igerasim
parents:
30642
diff
changeset
|
2172 |
int i = 0; |
bb5c454d451e
8058779: Faster implementation of String.replace(CharSequence, CharSequence)
igerasim
parents:
30642
diff
changeset
|
2173 |
do { |
33663 | 2174 |
sb.append(this, i, j).append(replStr); |
2175 |
i = j + tgtLen; |
|
2176 |
} while (j < thisLen && (j = indexOf(tgtStr, j + tgtLen1)) > 0); |
|
2177 |
return sb.append(this, i, thisLen).toString(); |
|
2 | 2178 |
} |
2179 |
||
2180 |
/** |
|
2181 |
* Splits this string around matches of the given |
|
2182 |
* <a href="../util/regex/Pattern.html#sum">regular expression</a>. |
|
2183 |
* |
|
2184 |
* <p> The array returned by this method contains each substring of this |
|
2185 |
* string that is terminated by another substring that matches the given |
|
2186 |
* expression or is terminated by the end of the string. The substrings in |
|
2187 |
* the array are in the order in which they occur in this string. If the |
|
2188 |
* expression does not match any part of the input then the resulting array |
|
21670
ca3553133ede
8028321: Fix for String.split() empty input sequence/JDK-6559590 triggers regression
sherman
parents:
21668
diff
changeset
|
2189 |
* has just one element, namely this string. |
21668 | 2190 |
* |
2191 |
* <p> When there is a positive-width match at the beginning of this |
|
2192 |
* string then an empty leading substring is included at the beginning |
|
2193 |
* of the resulting array. A zero-width match at the beginning however |
|
2194 |
* never produces such empty leading substring. |
|
2 | 2195 |
* |
14997 | 2196 |
* <p> The {@code limit} parameter controls the number of times the |
2 | 2197 |
* pattern is applied and therefore affects the length of the resulting |
50237
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
50215
diff
changeset
|
2198 |
* array. |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
50215
diff
changeset
|
2199 |
* <ul> |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
50215
diff
changeset
|
2200 |
* <li><p> |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
50215
diff
changeset
|
2201 |
* If the <i>limit</i> is positive then the pattern will be applied |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
50215
diff
changeset
|
2202 |
* at most <i>limit</i> - 1 times, the array's length will be |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
50215
diff
changeset
|
2203 |
* no greater than <i>limit</i>, and the array's last entry will contain |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
50215
diff
changeset
|
2204 |
* all input beyond the last matched delimiter.</p></li> |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
50215
diff
changeset
|
2205 |
* |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
50215
diff
changeset
|
2206 |
* <li><p> |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
50215
diff
changeset
|
2207 |
* If the <i>limit</i> is zero then the pattern will be applied as |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
50215
diff
changeset
|
2208 |
* many times as possible, the array can have any length, and trailing |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
50215
diff
changeset
|
2209 |
* empty strings will be discarded.</p></li> |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
50215
diff
changeset
|
2210 |
* |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
50215
diff
changeset
|
2211 |
* <li><p> |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
50215
diff
changeset
|
2212 |
* If the <i>limit</i> is negative then the pattern will be applied |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
50215
diff
changeset
|
2213 |
* as many times as possible and the array can have any length.</p></li> |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
50215
diff
changeset
|
2214 |
* </ul> |
2 | 2215 |
* |
14997 | 2216 |
* <p> The string {@code "boo:and:foo"}, for example, yields the |
2 | 2217 |
* following results with these parameters: |
2218 |
* |
|
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2219 |
* <blockquote><table class="plain"> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2220 |
* <caption style="display:none">Split example showing regex, limit, and result</caption> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2221 |
* <thead> |
2 | 2222 |
* <tr> |
46148
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2223 |
* <th scope="col">Regex</th> |
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2224 |
* <th scope="col">Limit</th> |
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2225 |
* <th scope="col">Result</th> |
2 | 2226 |
* </tr> |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2227 |
* </thead> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2228 |
* <tbody> |
46148
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2229 |
* <tr><th scope="row" rowspan="3" style="font-weight:normal">:</th> |
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2230 |
* <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">2</th> |
14997 | 2231 |
* <td>{@code { "boo", "and:foo" }}</td></tr> |
46148
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2232 |
* <tr><!-- : --> |
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2233 |
* <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">5</th> |
14997 | 2234 |
* <td>{@code { "boo", "and", "foo" }}</td></tr> |
46148
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2235 |
* <tr><!-- : --> |
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2236 |
* <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">-2</th> |
14997 | 2237 |
* <td>{@code { "boo", "and", "foo" }}</td></tr> |
46148
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2238 |
* <tr><th scope="row" rowspan="3" style="font-weight:normal">o</th> |
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2239 |
* <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">5</th> |
14997 | 2240 |
* <td>{@code { "b", "", ":and:f", "", "" }}</td></tr> |
46148
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2241 |
* <tr><!-- o --> |
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2242 |
* <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">-2</th> |
14997 | 2243 |
* <td>{@code { "b", "", ":and:f", "", "" }}</td></tr> |
46148
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2244 |
* <tr><!-- o --> |
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2245 |
* <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">0</th> |
14997 | 2246 |
* <td>{@code { "b", "", ":and:f" }}</td></tr> |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2247 |
* </tbody> |
2 | 2248 |
* </table></blockquote> |
2249 |
* |
|
2250 |
* <p> An invocation of this method of the form |
|
14997 | 2251 |
* <i>str.</i>{@code split(}<i>regex</i>{@code ,} <i>n</i>{@code )} |
2 | 2252 |
* yields the same result as the expression |
2253 |
* |
|
2254 |
* <blockquote> |
|
14997 | 2255 |
* <code> |
2256 |
* {@link java.util.regex.Pattern}.{@link |
|
2257 |
* java.util.regex.Pattern#compile compile}(<i>regex</i>).{@link |
|
2258 |
* java.util.regex.Pattern#split(java.lang.CharSequence,int) split}(<i>str</i>, <i>n</i>) |
|
2259 |
* </code> |
|
2 | 2260 |
* </blockquote> |
2261 |
* |
|
2262 |
* |
|
2263 |
* @param regex |
|
2264 |
* the delimiting regular expression |
|
2265 |
* |
|
2266 |
* @param limit |
|
2267 |
* the result threshold, as described above |
|
2268 |
* |
|
2269 |
* @return the array of strings computed by splitting this string |
|
2270 |
* around matches of the given regular expression |
|
2271 |
* |
|
2272 |
* @throws PatternSyntaxException |
|
2273 |
* if the regular expression's syntax is invalid |
|
2274 |
* |
|
2275 |
* @see java.util.regex.Pattern |
|
2276 |
* |
|
2277 |
* @since 1.4 |
|
2278 |
* @spec JSR-51 |
|
2279 |
*/ |
|
2280 |
public String[] split(String regex, int limit) { |
|
3617
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2281 |
/* fastpath if the regex is a |
12858
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
2282 |
(1)one-char String and this character is not one of the |
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
2283 |
RegEx's meta characters ".$|()[{^?*+\\", or |
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
2284 |
(2)two-char String and the first char is the backslash and |
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
2285 |
the second is not the ascii digit or ascii letter. |
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
2286 |
*/ |
3617
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2287 |
char ch = 0; |
33663 | 2288 |
if (((regex.length() == 1 && |
3617
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2289 |
".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) || |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2290 |
(regex.length() == 2 && |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2291 |
regex.charAt(0) == '\\' && |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2292 |
(((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 && |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2293 |
((ch-'a')|('z'-ch)) < 0 && |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2294 |
((ch-'A')|('Z'-ch)) < 0)) && |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2295 |
(ch < Character.MIN_HIGH_SURROGATE || |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2296 |
ch > Character.MAX_LOW_SURROGATE)) |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2297 |
{ |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2298 |
int off = 0; |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2299 |
int next = 0; |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2300 |
boolean limited = limit > 0; |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
5991
diff
changeset
|
2301 |
ArrayList<String> list = new ArrayList<>(); |
3617
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2302 |
while ((next = indexOf(ch, off)) != -1) { |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2303 |
if (!limited || list.size() < limit - 1) { |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2304 |
list.add(substring(off, next)); |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2305 |
off = next + 1; |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2306 |
} else { // last one |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2307 |
//assert (list.size() == limit - 1); |
33663 | 2308 |
int last = length(); |
2309 |
list.add(substring(off, last)); |
|
2310 |
off = last; |
|
3617
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2311 |
break; |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2312 |
} |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2313 |
} |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2314 |
// If no match was found, return this |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2315 |
if (off == 0) |
12858
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
2316 |
return new String[]{this}; |
3617
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2317 |
|
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2318 |
// Add remaining segment |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2319 |
if (!limited || list.size() < limit) |
33663 | 2320 |
list.add(substring(off, length())); |
3617
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2321 |
|
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2322 |
// Construct result |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2323 |
int resultSize = list.size(); |
17181
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2324 |
if (limit == 0) { |
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
52914
diff
changeset
|
2325 |
while (resultSize > 0 && list.get(resultSize - 1).isEmpty()) { |
3617
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2326 |
resultSize--; |
17181
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2327 |
} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2328 |
} |
3617
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2329 |
String[] result = new String[resultSize]; |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2330 |
return list.subList(0, resultSize).toArray(result); |
422105639b62
6840246: Lightweight implementation of String.split for simple use case
sherman
parents:
2497
diff
changeset
|
2331 |
} |
2 | 2332 |
return Pattern.compile(regex).split(this, limit); |
2333 |
} |
|
2334 |
||
2335 |
/** |
|
2336 |
* Splits this string around matches of the given <a |
|
2337 |
* href="../util/regex/Pattern.html#sum">regular expression</a>. |
|
2338 |
* |
|
2339 |
* <p> This method works as if by invoking the two-argument {@link |
|
2340 |
* #split(String, int) split} method with the given expression and a limit |
|
2341 |
* argument of zero. Trailing empty strings are therefore not included in |
|
2342 |
* the resulting array. |
|
2343 |
* |
|
14997 | 2344 |
* <p> The string {@code "boo:and:foo"}, for example, yields the following |
2 | 2345 |
* results with these expressions: |
2346 |
* |
|
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2347 |
* <blockquote><table class="plain"> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2348 |
* <caption style="display:none">Split examples showing regex and result</caption> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2349 |
* <thead> |
2 | 2350 |
* <tr> |
46148
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2351 |
* <th scope="col">Regex</th> |
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2352 |
* <th scope="col">Result</th> |
2 | 2353 |
* </tr> |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2354 |
* </thead> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2355 |
* <tbody> |
46148
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2356 |
* <tr><th scope="row" style="text-weight:normal">:</th> |
14997 | 2357 |
* <td>{@code { "boo", "and", "foo" }}</td></tr> |
46148
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2358 |
* <tr><th scope="row" style="text-weight:normal">o</th> |
14997 | 2359 |
* <td>{@code { "b", "", ":and:f" }}</td></tr> |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2360 |
* </tbody> |
2 | 2361 |
* </table></blockquote> |
2362 |
* |
|
2363 |
* |
|
2364 |
* @param regex |
|
2365 |
* the delimiting regular expression |
|
2366 |
* |
|
2367 |
* @return the array of strings computed by splitting this string |
|
2368 |
* around matches of the given regular expression |
|
2369 |
* |
|
2370 |
* @throws PatternSyntaxException |
|
2371 |
* if the regular expression's syntax is invalid |
|
2372 |
* |
|
2373 |
* @see java.util.regex.Pattern |
|
2374 |
* |
|
2375 |
* @since 1.4 |
|
2376 |
* @spec JSR-51 |
|
2377 |
*/ |
|
2378 |
public String[] split(String regex) { |
|
2379 |
return split(regex, 0); |
|
2380 |
} |
|
2381 |
||
2382 |
/** |
|
17181
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2383 |
* Returns a new String composed of copies of the |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2384 |
* {@code CharSequence elements} joined together with a copy of |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2385 |
* the specified {@code delimiter}. |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2386 |
* |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2387 |
* <blockquote>For example, |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2388 |
* <pre>{@code |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2389 |
* String message = String.join("-", "Java", "is", "cool"); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2390 |
* // message returned is: "Java-is-cool" |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2391 |
* }</pre></blockquote> |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2392 |
* |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2393 |
* Note that if an element is null, then {@code "null"} is added. |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2394 |
* |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2395 |
* @param delimiter the delimiter that separates each element |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2396 |
* @param elements the elements to join together. |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2397 |
* |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2398 |
* @return a new {@code String} that is composed of the {@code elements} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2399 |
* separated by the {@code delimiter} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2400 |
* |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2401 |
* @throws NullPointerException If {@code delimiter} or {@code elements} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2402 |
* is {@code null} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2403 |
* |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2404 |
* @see java.util.StringJoiner |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2405 |
* @since 1.8 |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2406 |
*/ |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2407 |
public static String join(CharSequence delimiter, CharSequence... elements) { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2408 |
Objects.requireNonNull(delimiter); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2409 |
Objects.requireNonNull(elements); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2410 |
// Number of elements not likely worth Arrays.stream overhead. |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2411 |
StringJoiner joiner = new StringJoiner(delimiter); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2412 |
for (CharSequence cs: elements) { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2413 |
joiner.add(cs); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2414 |
} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2415 |
return joiner.toString(); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2416 |
} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2417 |
|
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2418 |
/** |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2419 |
* Returns a new {@code String} composed of copies of the |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2420 |
* {@code CharSequence elements} joined together with a copy of the |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2421 |
* specified {@code delimiter}. |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2422 |
* |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2423 |
* <blockquote>For example, |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2424 |
* <pre>{@code |
38765
b64f3ec3420e
8158312: Update String.join example code to use List convenience factory methods
darcy
parents:
37723
diff
changeset
|
2425 |
* List<String> strings = List.of("Java", "is", "cool"); |
17181
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2426 |
* String message = String.join(" ", strings); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2427 |
* //message returned is: "Java is cool" |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2428 |
* |
38765
b64f3ec3420e
8158312: Update String.join example code to use List convenience factory methods
darcy
parents:
37723
diff
changeset
|
2429 |
* Set<String> strings = |
b64f3ec3420e
8158312: Update String.join example code to use List convenience factory methods
darcy
parents:
37723
diff
changeset
|
2430 |
* new LinkedHashSet<>(List.of("Java", "is", "very", "cool")); |
17181
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2431 |
* String message = String.join("-", strings); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2432 |
* //message returned is: "Java-is-very-cool" |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2433 |
* }</pre></blockquote> |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2434 |
* |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2435 |
* Note that if an individual element is {@code null}, then {@code "null"} is added. |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2436 |
* |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2437 |
* @param delimiter a sequence of characters that is used to separate each |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2438 |
* of the {@code elements} in the resulting {@code String} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2439 |
* @param elements an {@code Iterable} that will have its {@code elements} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2440 |
* joined together. |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2441 |
* |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2442 |
* @return a new {@code String} that is composed from the {@code elements} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2443 |
* argument |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2444 |
* |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2445 |
* @throws NullPointerException If {@code delimiter} or {@code elements} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2446 |
* is {@code null} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2447 |
* |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2448 |
* @see #join(CharSequence,CharSequence...) |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2449 |
* @see java.util.StringJoiner |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2450 |
* @since 1.8 |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2451 |
*/ |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2452 |
public static String join(CharSequence delimiter, |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2453 |
Iterable<? extends CharSequence> elements) { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2454 |
Objects.requireNonNull(delimiter); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2455 |
Objects.requireNonNull(elements); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2456 |
StringJoiner joiner = new StringJoiner(delimiter); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2457 |
for (CharSequence cs: elements) { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2458 |
joiner.add(cs); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2459 |
} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2460 |
return joiner.toString(); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2461 |
} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2462 |
|
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2463 |
/** |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
2464 |
* Converts all of the characters in this {@code String} to lower |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
2465 |
* case using the rules of the given {@code Locale}. Case mapping is based |
2 | 2466 |
* on the Unicode Standard version specified by the {@link java.lang.Character Character} |
2467 |
* class. Since case mappings are not always 1:1 char mappings, the resulting |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
2468 |
* {@code String} may be a different length than the original {@code String}. |
2 | 2469 |
* <p> |
2470 |
* Examples of lowercase mappings are in the following table: |
|
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2471 |
* <table class="plain"> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2472 |
* <caption style="display:none">Lowercase mapping examples showing language code of locale, upper case, lower case, and description</caption> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2473 |
* <thead> |
2 | 2474 |
* <tr> |
46148
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2475 |
* <th scope="col">Language Code of Locale</th> |
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2476 |
* <th scope="col">Upper Case</th> |
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2477 |
* <th scope="col">Lower Case</th> |
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2478 |
* <th scope="col">Description</th> |
2 | 2479 |
* </tr> |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2480 |
* </thead> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2481 |
* <tbody> |
2 | 2482 |
* <tr> |
2483 |
* <td>tr (Turkish)</td> |
|
46148
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2484 |
* <th scope="row" style="font-weight:normal; text-align:left">\u0130</th> |
2 | 2485 |
* <td>\u0069</td> |
2486 |
* <td>capital letter I with dot above -> small letter i</td> |
|
2487 |
* </tr> |
|
2488 |
* <tr> |
|
2489 |
* <td>tr (Turkish)</td> |
|
46148
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2490 |
* <th scope="row" style="font-weight:normal; text-align:left">\u0049</th> |
2 | 2491 |
* <td>\u0131</td> |
2492 |
* <td>capital letter I -> small letter dotless i </td> |
|
2493 |
* </tr> |
|
2494 |
* <tr> |
|
2495 |
* <td>(all)</td> |
|
46148
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2496 |
* <th scope="row" style="font-weight:normal; text-align:left">French Fries</th> |
2 | 2497 |
* <td>french fries</td> |
2498 |
* <td>lowercased all chars in String</td> |
|
2499 |
* </tr> |
|
2500 |
* <tr> |
|
2501 |
* <td>(all)</td> |
|
46148
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2502 |
* <th scope="row" style="font-weight:normal; text-align:left"> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46148
diff
changeset
|
2503 |
* ΙΧΘΥΣ</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46148
diff
changeset
|
2504 |
* <td>ιχθυσ</td> |
2 | 2505 |
* <td>lowercased all chars in String</td> |
2506 |
* </tr> |
|
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2507 |
* </tbody> |
2 | 2508 |
* </table> |
2509 |
* |
|
2510 |
* @param locale use the case transformation rules for this locale |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
2511 |
* @return the {@code String}, converted to lowercase. |
2 | 2512 |
* @see java.lang.String#toLowerCase() |
2513 |
* @see java.lang.String#toUpperCase() |
|
2514 |
* @see java.lang.String#toUpperCase(Locale) |
|
2515 |
* @since 1.1 |
|
2516 |
*/ |
|
2517 |
public String toLowerCase(Locale locale) { |
|
33663 | 2518 |
return isLatin1() ? StringLatin1.toLowerCase(this, value, locale) |
2519 |
: StringUTF16.toLowerCase(this, value, locale); |
|
2 | 2520 |
} |
2521 |
||
2522 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
2523 |
* Converts all of the characters in this {@code String} to lower |
2 | 2524 |
* case using the rules of the default locale. This is equivalent to calling |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
2525 |
* {@code toLowerCase(Locale.getDefault())}. |
2 | 2526 |
* <p> |
2527 |
* <b>Note:</b> This method is locale sensitive, and may produce unexpected |
|
2528 |
* results if used for strings that are intended to be interpreted locale |
|
2529 |
* independently. |
|
2530 |
* Examples are programming language identifiers, protocol keys, and HTML |
|
2531 |
* tags. |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
2532 |
* For instance, {@code "TITLE".toLowerCase()} in a Turkish locale |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
2533 |
* returns {@code "t\u005Cu0131tle"}, where '\u005Cu0131' is the |
5469 | 2534 |
* LATIN SMALL LETTER DOTLESS I character. |
2 | 2535 |
* To obtain correct results for locale insensitive strings, use |
19815
5b15fa787fab
8023943: Method description fix for String.toLower/UpperCase() methods
naoto
parents:
19802
diff
changeset
|
2536 |
* {@code toLowerCase(Locale.ROOT)}. |
24367
705490680527
8030709: Tidy warnings cleanup for java.lang package; minor cleanup in java.math, javax.script
yan
parents:
23024
diff
changeset
|
2537 |
* |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
2538 |
* @return the {@code String}, converted to lowercase. |
2 | 2539 |
* @see java.lang.String#toLowerCase(Locale) |
2540 |
*/ |
|
2541 |
public String toLowerCase() { |
|
2542 |
return toLowerCase(Locale.getDefault()); |
|
2543 |
} |
|
2544 |
||
2545 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
2546 |
* Converts all of the characters in this {@code String} to upper |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
2547 |
* case using the rules of the given {@code Locale}. Case mapping is based |
2 | 2548 |
* on the Unicode Standard version specified by the {@link java.lang.Character Character} |
2549 |
* class. Since case mappings are not always 1:1 char mappings, the resulting |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
2550 |
* {@code String} may be a different length than the original {@code String}. |
2 | 2551 |
* <p> |
2552 |
* Examples of locale-sensitive and 1:M case mappings are in the following table. |
|
21334 | 2553 |
* |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2554 |
* <table class="plain"> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2555 |
* <caption style="display:none">Examples of locale-sensitive and 1:M case mappings. Shows Language code of locale, lower case, upper case, and description.</caption> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2556 |
* <thead> |
2 | 2557 |
* <tr> |
46148
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2558 |
* <th scope="col">Language Code of Locale</th> |
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2559 |
* <th scope="col">Lower Case</th> |
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2560 |
* <th scope="col">Upper Case</th> |
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2561 |
* <th scope="col">Description</th> |
2 | 2562 |
* </tr> |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2563 |
* </thead> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2564 |
* <tbody> |
2 | 2565 |
* <tr> |
2566 |
* <td>tr (Turkish)</td> |
|
46148
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2567 |
* <th scope="row" style="font-weight:normal; text-align:left">\u0069</th> |
2 | 2568 |
* <td>\u0130</td> |
2569 |
* <td>small letter i -> capital letter I with dot above</td> |
|
2570 |
* </tr> |
|
2571 |
* <tr> |
|
2572 |
* <td>tr (Turkish)</td> |
|
46148
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2573 |
* <th scope="row" style="font-weight:normal; text-align:left">\u0131</th> |
2 | 2574 |
* <td>\u0049</td> |
2575 |
* <td>small letter dotless i -> capital letter I</td> |
|
2576 |
* </tr> |
|
2577 |
* <tr> |
|
2578 |
* <td>(all)</td> |
|
46148
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2579 |
* <th scope="row" style="font-weight:normal; text-align:left">\u00df</th> |
2 | 2580 |
* <td>\u0053 \u0053</td> |
2581 |
* <td>small letter sharp s -> two letters: SS</td> |
|
2582 |
* </tr> |
|
2583 |
* <tr> |
|
2584 |
* <td>(all)</td> |
|
46148
6d8e27cd2f1e
8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents:
45572
diff
changeset
|
2585 |
* <th scope="row" style="font-weight:normal; text-align:left">Fahrvergnügen</th> |
2 | 2586 |
* <td>FAHRVERGNÜGEN</td> |
2587 |
* <td></td> |
|
2588 |
* </tr> |
|
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
45121
diff
changeset
|
2589 |
* </tbody> |
2 | 2590 |
* </table> |
2591 |
* @param locale use the case transformation rules for this locale |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
2592 |
* @return the {@code String}, converted to uppercase. |
2 | 2593 |
* @see java.lang.String#toUpperCase() |
2594 |
* @see java.lang.String#toLowerCase() |
|
2595 |
* @see java.lang.String#toLowerCase(Locale) |
|
2596 |
* @since 1.1 |
|
2597 |
*/ |
|
2598 |
public String toUpperCase(Locale locale) { |
|
33663 | 2599 |
return isLatin1() ? StringLatin1.toUpperCase(this, value, locale) |
2600 |
: StringUTF16.toUpperCase(this, value, locale); |
|
2 | 2601 |
} |
2602 |
||
2603 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
2604 |
* Converts all of the characters in this {@code String} to upper |
2 | 2605 |
* case using the rules of the default locale. This method is equivalent to |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
2606 |
* {@code toUpperCase(Locale.getDefault())}. |
2 | 2607 |
* <p> |
2608 |
* <b>Note:</b> This method is locale sensitive, and may produce unexpected |
|
2609 |
* results if used for strings that are intended to be interpreted locale |
|
2610 |
* independently. |
|
2611 |
* Examples are programming language identifiers, protocol keys, and HTML |
|
2612 |
* tags. |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
2613 |
* For instance, {@code "title".toUpperCase()} in a Turkish locale |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
2614 |
* returns {@code "T\u005Cu0130TLE"}, where '\u005Cu0130' is the |
5469 | 2615 |
* LATIN CAPITAL LETTER I WITH DOT ABOVE character. |
2 | 2616 |
* To obtain correct results for locale insensitive strings, use |
19815
5b15fa787fab
8023943: Method description fix for String.toLower/UpperCase() methods
naoto
parents:
19802
diff
changeset
|
2617 |
* {@code toUpperCase(Locale.ROOT)}. |
24367
705490680527
8030709: Tidy warnings cleanup for java.lang package; minor cleanup in java.math, javax.script
yan
parents:
23024
diff
changeset
|
2618 |
* |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
2619 |
* @return the {@code String}, converted to uppercase. |
2 | 2620 |
* @see java.lang.String#toUpperCase(Locale) |
2621 |
*/ |
|
2622 |
public String toUpperCase() { |
|
2623 |
return toUpperCase(Locale.getDefault()); |
|
2624 |
} |
|
2625 |
||
2626 |
/** |
|
50051
f5231f5762fc
8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents:
49433
diff
changeset
|
2627 |
* Returns a string whose value is this string, with all leading |
f5231f5762fc
8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents:
49433
diff
changeset
|
2628 |
* and trailing space removed, where space is defined |
f5231f5762fc
8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents:
49433
diff
changeset
|
2629 |
* as any character whose codepoint is less than or equal to |
50098
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2630 |
* {@code 'U+0020'} (the space character). |
2 | 2631 |
* <p> |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
2632 |
* If this {@code String} object represents an empty character |
2 | 2633 |
* sequence, or the first and last characters of character sequence |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
2634 |
* represented by this {@code String} object both have codes |
50051
f5231f5762fc
8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents:
49433
diff
changeset
|
2635 |
* that are not space (as defined above), then a |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
2636 |
* reference to this {@code String} object is returned. |
2 | 2637 |
* <p> |
50051
f5231f5762fc
8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents:
49433
diff
changeset
|
2638 |
* Otherwise, if all characters in this string are space (as |
f5231f5762fc
8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents:
49433
diff
changeset
|
2639 |
* defined above), then a {@code String} object representing an |
f5231f5762fc
8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents:
49433
diff
changeset
|
2640 |
* empty string is returned. |
2 | 2641 |
* <p> |
2642 |
* Otherwise, let <i>k</i> be the index of the first character in the |
|
50051
f5231f5762fc
8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents:
49433
diff
changeset
|
2643 |
* string whose code is not a space (as defined above) and let |
2 | 2644 |
* <i>m</i> be the index of the last character in the string whose code |
50051
f5231f5762fc
8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents:
49433
diff
changeset
|
2645 |
* is not a space (as defined above). A {@code String} |
21841
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
2646 |
* object is returned, representing the substring of this string that |
2 | 2647 |
* begins with the character at index <i>k</i> and ends with the |
2648 |
* character at index <i>m</i>-that is, the result of |
|
17181
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
15312
diff
changeset
|
2649 |
* {@code this.substring(k, m + 1)}. |
2 | 2650 |
* <p> |
50051
f5231f5762fc
8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents:
49433
diff
changeset
|
2651 |
* This method may be used to trim space (as defined above) from |
2 | 2652 |
* the beginning and end of a string. |
2653 |
* |
|
50051
f5231f5762fc
8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents:
49433
diff
changeset
|
2654 |
* @return a string whose value is this string, with all leading |
f5231f5762fc
8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents:
49433
diff
changeset
|
2655 |
* and trailing space removed, or this string if it |
f5231f5762fc
8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents:
49433
diff
changeset
|
2656 |
* has no leading or trailing space. |
2 | 2657 |
*/ |
2658 |
public String trim() { |
|
33663 | 2659 |
String ret = isLatin1() ? StringLatin1.trim(value) |
2660 |
: StringUTF16.trim(value); |
|
2661 |
return ret == null ? this : ret; |
|
2 | 2662 |
} |
2663 |
||
2664 |
/** |
|
50098
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2665 |
* Returns a string whose value is this string, with all leading |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2666 |
* and trailing {@link Character#isWhitespace(int) white space} |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2667 |
* removed. |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2668 |
* <p> |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2669 |
* If this {@code String} object represents an empty string, |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2670 |
* or if all code points in this string are |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2671 |
* {@link Character#isWhitespace(int) white space}, then an empty string |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2672 |
* is returned. |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2673 |
* <p> |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2674 |
* Otherwise, returns a substring of this string beginning with the first |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2675 |
* code point that is not a {@link Character#isWhitespace(int) white space} |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2676 |
* up to and including the last code point that is not a |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2677 |
* {@link Character#isWhitespace(int) white space}. |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2678 |
* <p> |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2679 |
* This method may be used to strip |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2680 |
* {@link Character#isWhitespace(int) white space} from |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2681 |
* the beginning and end of a string. |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2682 |
* |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2683 |
* @return a string whose value is this string, with all leading |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2684 |
* and trailing white space removed |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2685 |
* |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2686 |
* @see Character#isWhitespace(int) |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2687 |
* |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2688 |
* @since 11 |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2689 |
*/ |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2690 |
public String strip() { |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2691 |
String ret = isLatin1() ? StringLatin1.strip(value) |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2692 |
: StringUTF16.strip(value); |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2693 |
return ret == null ? this : ret; |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2694 |
} |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2695 |
|
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2696 |
/** |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2697 |
* Returns a string whose value is this string, with all leading |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2698 |
* {@link Character#isWhitespace(int) white space} removed. |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2699 |
* <p> |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2700 |
* If this {@code String} object represents an empty string, |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2701 |
* or if all code points in this string are |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2702 |
* {@link Character#isWhitespace(int) white space}, then an empty string |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2703 |
* is returned. |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2704 |
* <p> |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2705 |
* Otherwise, returns a substring of this string beginning with the first |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2706 |
* code point that is not a {@link Character#isWhitespace(int) white space} |
53563 | 2707 |
* up to and including the last code point of this string. |
50098
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2708 |
* <p> |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2709 |
* This method may be used to trim |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2710 |
* {@link Character#isWhitespace(int) white space} from |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2711 |
* the beginning of a string. |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2712 |
* |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2713 |
* @return a string whose value is this string, with all leading white |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2714 |
* space removed |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2715 |
* |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2716 |
* @see Character#isWhitespace(int) |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2717 |
* |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2718 |
* @since 11 |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2719 |
*/ |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2720 |
public String stripLeading() { |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2721 |
String ret = isLatin1() ? StringLatin1.stripLeading(value) |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2722 |
: StringUTF16.stripLeading(value); |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2723 |
return ret == null ? this : ret; |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2724 |
} |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2725 |
|
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2726 |
/** |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2727 |
* Returns a string whose value is this string, with all trailing |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2728 |
* {@link Character#isWhitespace(int) white space} removed. |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2729 |
* <p> |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2730 |
* If this {@code String} object represents an empty string, |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2731 |
* or if all characters in this string are |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2732 |
* {@link Character#isWhitespace(int) white space}, then an empty string |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2733 |
* is returned. |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2734 |
* <p> |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2735 |
* Otherwise, returns a substring of this string beginning with the first |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2736 |
* code point of this string up to and including the last code point |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2737 |
* that is not a {@link Character#isWhitespace(int) white space}. |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2738 |
* <p> |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2739 |
* This method may be used to trim |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2740 |
* {@link Character#isWhitespace(int) white space} from |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2741 |
* the end of a string. |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2742 |
* |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2743 |
* @return a string whose value is this string, with all trailing white |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2744 |
* space removed |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2745 |
* |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2746 |
* @see Character#isWhitespace(int) |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2747 |
* |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2748 |
* @since 11 |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2749 |
*/ |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2750 |
public String stripTrailing() { |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2751 |
String ret = isLatin1() ? StringLatin1.stripTrailing(value) |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2752 |
: StringUTF16.stripTrailing(value); |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2753 |
return ret == null ? this : ret; |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2754 |
} |
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2755 |
|
92560438d306
8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents:
50051
diff
changeset
|
2756 |
/** |
50175 | 2757 |
* Returns {@code true} if the string is empty or contains only |
2758 |
* {@link Character#isWhitespace(int) white space} codepoints, |
|
2759 |
* otherwise {@code false}. |
|
2760 |
* |
|
2761 |
* @return {@code true} if the string is empty or contains only |
|
2762 |
* {@link Character#isWhitespace(int) white space} codepoints, |
|
2763 |
* otherwise {@code false} |
|
2764 |
* |
|
2765 |
* @see Character#isWhitespace(int) |
|
2766 |
* |
|
2767 |
* @since 11 |
|
2768 |
*/ |
|
2769 |
public boolean isBlank() { |
|
2770 |
return indexOfNonWhitespace() == length(); |
|
2771 |
} |
|
2772 |
||
51714 | 2773 |
private Stream<String> lines(int maxLeading, int maxTrailing) { |
2774 |
return isLatin1() ? StringLatin1.lines(value, maxLeading, maxTrailing) |
|
2775 |
: StringUTF16.lines(value, maxLeading, maxTrailing); |
|
50175 | 2776 |
} |
2777 |
||
2778 |
/** |
|
51249
3fe0329588b8
8208164: (str) improve specification of String::lines
jlaskey
parents:
50237
diff
changeset
|
2779 |
* Returns a stream of lines extracted from this string, |
3fe0329588b8
8208164: (str) improve specification of String::lines
jlaskey
parents:
50237
diff
changeset
|
2780 |
* separated by line terminators. |
3fe0329588b8
8208164: (str) improve specification of String::lines
jlaskey
parents:
50237
diff
changeset
|
2781 |
* <p> |
3fe0329588b8
8208164: (str) improve specification of String::lines
jlaskey
parents:
50237
diff
changeset
|
2782 |
* A <i>line terminator</i> is one of the following: |
3fe0329588b8
8208164: (str) improve specification of String::lines
jlaskey
parents:
50237
diff
changeset
|
2783 |
* a line feed character {@code "\n"} (U+000A), |
3fe0329588b8
8208164: (str) improve specification of String::lines
jlaskey
parents:
50237
diff
changeset
|
2784 |
* a carriage return character {@code "\r"} (U+000D), |
3fe0329588b8
8208164: (str) improve specification of String::lines
jlaskey
parents:
50237
diff
changeset
|
2785 |
* or a carriage return followed immediately by a line feed |
3fe0329588b8
8208164: (str) improve specification of String::lines
jlaskey
parents:
50237
diff
changeset
|
2786 |
* {@code "\r\n"} (U+000D U+000A). |
50215 | 2787 |
* <p> |
51249
3fe0329588b8
8208164: (str) improve specification of String::lines
jlaskey
parents:
50237
diff
changeset
|
2788 |
* A <i>line</i> is either a sequence of zero or more characters |
3fe0329588b8
8208164: (str) improve specification of String::lines
jlaskey
parents:
50237
diff
changeset
|
2789 |
* followed by a line terminator, or it is a sequence of one or |
3fe0329588b8
8208164: (str) improve specification of String::lines
jlaskey
parents:
50237
diff
changeset
|
2790 |
* more characters followed by the end of the string. A |
3fe0329588b8
8208164: (str) improve specification of String::lines
jlaskey
parents:
50237
diff
changeset
|
2791 |
* line does not include the line terminator. |
50215 | 2792 |
* <p> |
51249
3fe0329588b8
8208164: (str) improve specification of String::lines
jlaskey
parents:
50237
diff
changeset
|
2793 |
* The stream returned by this method contains the lines from |
3fe0329588b8
8208164: (str) improve specification of String::lines
jlaskey
parents:
50237
diff
changeset
|
2794 |
* this string in the order in which they occur. |
3fe0329588b8
8208164: (str) improve specification of String::lines
jlaskey
parents:
50237
diff
changeset
|
2795 |
* |
3fe0329588b8
8208164: (str) improve specification of String::lines
jlaskey
parents:
50237
diff
changeset
|
2796 |
* @apiNote This definition of <i>line</i> implies that an empty |
3fe0329588b8
8208164: (str) improve specification of String::lines
jlaskey
parents:
50237
diff
changeset
|
2797 |
* string has zero lines and that there is no empty line |
3fe0329588b8
8208164: (str) improve specification of String::lines
jlaskey
parents:
50237
diff
changeset
|
2798 |
* following a line terminator at the end of a string. |
50215 | 2799 |
* |
2800 |
* @implNote This method provides better performance than |
|
2801 |
* split("\R") by supplying elements lazily and |
|
2802 |
* by faster search of new line terminators. |
|
2803 |
* |
|
51249
3fe0329588b8
8208164: (str) improve specification of String::lines
jlaskey
parents:
50237
diff
changeset
|
2804 |
* @return the stream of lines extracted from this string |
50215 | 2805 |
* |
2806 |
* @since 11 |
|
2807 |
*/ |
|
2808 |
public Stream<String> lines() { |
|
51714 | 2809 |
return lines(0, 0); |
2810 |
} |
|
2811 |
||
2812 |
/** |
|
2813 |
* Adjusts the indentation of each line of this string based on the value of |
|
2814 |
* {@code n}, and normalizes line termination characters. |
|
2815 |
* <p> |
|
2816 |
* This string is conceptually separated into lines using |
|
2817 |
* {@link String#lines()}. Each line is then adjusted as described below |
|
2818 |
* and then suffixed with a line feed {@code "\n"} (U+000A). The resulting |
|
2819 |
* lines are then concatenated and returned. |
|
2820 |
* <p> |
|
2821 |
* If {@code n > 0} then {@code n} spaces (U+0020) are inserted at the |
|
53245
05c5c27b5a54
8215493: String::indent inconsistency with blank lines
jlaskey
parents:
53231
diff
changeset
|
2822 |
* beginning of each line. |
51714 | 2823 |
* <p> |
2824 |
* If {@code n < 0} then up to {@code n} |
|
2825 |
* {@link Character#isWhitespace(int) white space characters} are removed |
|
2826 |
* from the beginning of each line. If a given line does not contain |
|
2827 |
* sufficient white space then all leading |
|
2828 |
* {@link Character#isWhitespace(int) white space characters} are removed. |
|
2829 |
* Each white space character is treated as a single character. In |
|
2830 |
* particular, the tab character {@code "\t"} (U+0009) is considered a |
|
2831 |
* single character; it is not expanded. |
|
2832 |
* <p> |
|
2833 |
* If {@code n == 0} then the line remains unchanged. However, line |
|
2834 |
* terminators are still normalized. |
|
2835 |
* |
|
2836 |
* @param n number of leading |
|
2837 |
* {@link Character#isWhitespace(int) white space characters} |
|
2838 |
* to add or remove |
|
2839 |
* |
|
2840 |
* @return string with indentation adjusted and line endings normalized |
|
2841 |
* |
|
2842 |
* @see String#lines() |
|
2843 |
* @see String#isBlank() |
|
2844 |
* @see Character#isWhitespace(int) |
|
2845 |
* |
|
2846 |
* @since 12 |
|
2847 |
*/ |
|
2848 |
public String indent(int n) { |
|
2849 |
return isEmpty() ? "" : indent(n, false); |
|
2850 |
} |
|
2851 |
||
2852 |
private String indent(int n, boolean removeBlanks) { |
|
2853 |
Stream<String> stream = removeBlanks ? lines(Integer.MAX_VALUE, Integer.MAX_VALUE) |
|
2854 |
: lines(); |
|
2855 |
if (n > 0) { |
|
2856 |
final String spaces = " ".repeat(n); |
|
53245
05c5c27b5a54
8215493: String::indent inconsistency with blank lines
jlaskey
parents:
53231
diff
changeset
|
2857 |
stream = stream.map(s -> spaces + s); |
51714 | 2858 |
} else if (n == Integer.MIN_VALUE) { |
2859 |
stream = stream.map(s -> s.stripLeading()); |
|
2860 |
} else if (n < 0) { |
|
2861 |
stream = stream.map(s -> s.substring(Math.min(-n, s.indexOfNonWhitespace()))); |
|
2862 |
} |
|
2863 |
return stream.collect(Collectors.joining("\n", "", "\n")); |
|
2864 |
} |
|
2865 |
||
2866 |
private int indexOfNonWhitespace() { |
|
2867 |
return isLatin1() ? StringLatin1.indexOfNonWhitespace(value) |
|
2868 |
: StringUTF16.indexOfNonWhitespace(value); |
|
2869 |
} |
|
2870 |
||
2871 |
private int lastIndexOfNonWhitespace() { |
|
2872 |
return isLatin1() ? StringLatin1.lastIndexOfNonWhitespace(value) |
|
2873 |
: StringUTF16.lastIndexOfNonWhitespace(value); |
|
2874 |
} |
|
2875 |
||
2876 |
/** |
|
52682 | 2877 |
* This method allows the application of a function to {@code this} |
2878 |
* string. The function should expect a single String argument |
|
2879 |
* and produce an {@code R} result. |
|
53230 | 2880 |
* <p> |
2881 |
* Any exception thrown by {@code f()} will be propagated to the |
|
2882 |
* caller. |
|
52682 | 2883 |
* |
2884 |
* @param f functional interface to a apply |
|
2885 |
* |
|
2886 |
* @param <R> class of the result |
|
2887 |
* |
|
2888 |
* @return the result of applying the function to this string |
|
2889 |
* |
|
2890 |
* @see java.util.function.Function |
|
2891 |
* |
|
2892 |
* @since 12 |
|
2893 |
*/ |
|
2894 |
public <R> R transform(Function<? super String, ? extends R> f) { |
|
2895 |
return f.apply(this); |
|
2896 |
} |
|
2897 |
||
2898 |
/** |
|
2 | 2899 |
* This object (which is already a string!) is itself returned. |
2900 |
* |
|
2901 |
* @return the string itself. |
|
2902 |
*/ |
|
2903 |
public String toString() { |
|
2904 |
return this; |
|
2905 |
} |
|
2906 |
||
28667
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2907 |
/** |
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2908 |
* Returns a stream of {@code int} zero-extending the {@code char} values |
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2909 |
* from this sequence. Any char which maps to a <a |
49433
b6671a111395
8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents:
49211
diff
changeset
|
2910 |
* href="{@docRoot}/java.base/java/lang/Character.html#unicode">surrogate code |
28667
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2911 |
* point</a> is passed through uninterpreted. |
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2912 |
* |
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2913 |
* @return an IntStream of char values from this sequence |
47463
30186b6741b8
8189952: New methods on String: chars() and codePoints() should be marked since 9
darcy
parents:
47216
diff
changeset
|
2914 |
* @since 9 |
28667
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2915 |
*/ |
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2916 |
@Override |
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2917 |
public IntStream chars() { |
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2918 |
return StreamSupport.intStream( |
33663 | 2919 |
isLatin1() ? new StringLatin1.CharsSpliterator(value, Spliterator.IMMUTABLE) |
2920 |
: new StringUTF16.CharsSpliterator(value, Spliterator.IMMUTABLE), |
|
2921 |
false); |
|
28667
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2922 |
} |
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2923 |
|
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2924 |
|
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2925 |
/** |
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2926 |
* Returns a stream of code point values from this sequence. Any surrogate |
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2927 |
* pairs encountered in the sequence are combined as if by {@linkplain |
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2928 |
* Character#toCodePoint Character.toCodePoint} and the result is passed |
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2929 |
* to the stream. Any other code units, including ordinary BMP characters, |
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2930 |
* unpaired surrogates, and undefined code units, are zero-extended to |
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2931 |
* {@code int} values which are then passed to the stream. |
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2932 |
* |
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2933 |
* @return an IntStream of Unicode code points from this sequence |
47463
30186b6741b8
8189952: New methods on String: chars() and codePoints() should be marked since 9
darcy
parents:
47216
diff
changeset
|
2934 |
* @since 9 |
28667
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2935 |
*/ |
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2936 |
@Override |
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2937 |
public IntStream codePoints() { |
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2938 |
return StreamSupport.intStream( |
33663 | 2939 |
isLatin1() ? new StringLatin1.CharsSpliterator(value, Spliterator.IMMUTABLE) |
2940 |
: new StringUTF16.CodePointsSpliterator(value, Spliterator.IMMUTABLE), |
|
2941 |
false); |
|
28667
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2942 |
} |
2245cc40bf5d
8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents:
28423
diff
changeset
|
2943 |
|
2 | 2944 |
/** |
2945 |
* Converts this string to a new character array. |
|
2946 |
* |
|
2947 |
* @return a newly allocated character array whose length is the length |
|
2948 |
* of this string and whose contents are initialized to contain |
|
2949 |
* the character sequence represented by this string. |
|
2950 |
*/ |
|
2951 |
public char[] toCharArray() { |
|
33663 | 2952 |
return isLatin1() ? StringLatin1.toChars(value) |
2953 |
: StringUTF16.toChars(value); |
|
2 | 2954 |
} |
2955 |
||
2956 |
/** |
|
2957 |
* Returns a formatted string using the specified format string and |
|
2958 |
* arguments. |
|
2959 |
* |
|
2960 |
* <p> The locale always used is the one returned by {@link |
|
38786
8e7b0ac05815
8146156: Inconsistent default locale in string formatter methods
naoto
parents:
38765
diff
changeset
|
2961 |
* java.util.Locale#getDefault(java.util.Locale.Category) |
8e7b0ac05815
8146156: Inconsistent default locale in string formatter methods
naoto
parents:
38765
diff
changeset
|
2962 |
* Locale.getDefault(Locale.Category)} with |
8e7b0ac05815
8146156: Inconsistent default locale in string formatter methods
naoto
parents:
38765
diff
changeset
|
2963 |
* {@link java.util.Locale.Category#FORMAT FORMAT} category specified. |
2 | 2964 |
* |
2965 |
* @param format |
|
2966 |
* A <a href="../util/Formatter.html#syntax">format string</a> |
|
2967 |
* |
|
2968 |
* @param args |
|
2969 |
* Arguments referenced by the format specifiers in the format |
|
2970 |
* string. If there are more arguments than format specifiers, the |
|
2971 |
* extra arguments are ignored. The number of arguments is |
|
2972 |
* variable and may be zero. The maximum number of arguments is |
|
2973 |
* limited by the maximum dimension of a Java array as defined by |
|
9266
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
7816
diff
changeset
|
2974 |
* <cite>The Java™ Virtual Machine Specification</cite>. |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
7816
diff
changeset
|
2975 |
* The behaviour on a |
14997 | 2976 |
* {@code null} argument depends on the <a |
2 | 2977 |
* href="../util/Formatter.html#syntax">conversion</a>. |
2978 |
* |
|
14014 | 2979 |
* @throws java.util.IllegalFormatException |
2 | 2980 |
* If a format string contains an illegal syntax, a format |
2981 |
* specifier that is incompatible with the given arguments, |
|
2982 |
* insufficient arguments given the format string, or other |
|
2983 |
* illegal conditions. For specification of all possible |
|
2984 |
* formatting errors, see the <a |
|
2985 |
* href="../util/Formatter.html#detail">Details</a> section of the |
|
2986 |
* formatter class specification. |
|
2987 |
* |
|
2988 |
* @return A formatted string |
|
2989 |
* |
|
2990 |
* @see java.util.Formatter |
|
2991 |
* @since 1.5 |
|
2992 |
*/ |
|
12858
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
2993 |
public static String format(String format, Object... args) { |
2 | 2994 |
return new Formatter().format(format, args).toString(); |
2995 |
} |
|
2996 |
||
2997 |
/** |
|
2998 |
* Returns a formatted string using the specified locale, format string, |
|
2999 |
* and arguments. |
|
3000 |
* |
|
3001 |
* @param l |
|
3002 |
* The {@linkplain java.util.Locale locale} to apply during |
|
14997 | 3003 |
* formatting. If {@code l} is {@code null} then no localization |
2 | 3004 |
* is applied. |
3005 |
* |
|
3006 |
* @param format |
|
3007 |
* A <a href="../util/Formatter.html#syntax">format string</a> |
|
3008 |
* |
|
3009 |
* @param args |
|
3010 |
* Arguments referenced by the format specifiers in the format |
|
3011 |
* string. If there are more arguments than format specifiers, the |
|
3012 |
* extra arguments are ignored. The number of arguments is |
|
3013 |
* variable and may be zero. The maximum number of arguments is |
|
3014 |
* limited by the maximum dimension of a Java array as defined by |
|
9266
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
7816
diff
changeset
|
3015 |
* <cite>The Java™ Virtual Machine Specification</cite>. |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
7816
diff
changeset
|
3016 |
* The behaviour on a |
15312
4b57f9da8192
4247235: (spec str) StringBuffer.insert(int, char[]) specification is inconsistent
jgish
parents:
14997
diff
changeset
|
3017 |
* {@code null} argument depends on the |
4b57f9da8192
4247235: (spec str) StringBuffer.insert(int, char[]) specification is inconsistent
jgish
parents:
14997
diff
changeset
|
3018 |
* <a href="../util/Formatter.html#syntax">conversion</a>. |
2 | 3019 |
* |
14014 | 3020 |
* @throws java.util.IllegalFormatException |
2 | 3021 |
* If a format string contains an illegal syntax, a format |
3022 |
* specifier that is incompatible with the given arguments, |
|
3023 |
* insufficient arguments given the format string, or other |
|
3024 |
* illegal conditions. For specification of all possible |
|
3025 |
* formatting errors, see the <a |
|
3026 |
* href="../util/Formatter.html#detail">Details</a> section of the |
|
3027 |
* formatter class specification |
|
3028 |
* |
|
3029 |
* @return A formatted string |
|
3030 |
* |
|
3031 |
* @see java.util.Formatter |
|
3032 |
* @since 1.5 |
|
3033 |
*/ |
|
12858
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
3034 |
public static String format(Locale l, String format, Object... args) { |
2 | 3035 |
return new Formatter(l).format(format, args).toString(); |
3036 |
} |
|
3037 |
||
3038 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3039 |
* Returns the string representation of the {@code Object} argument. |
2 | 3040 |
* |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3041 |
* @param obj an {@code Object}. |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3042 |
* @return if the argument is {@code null}, then a string equal to |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3043 |
* {@code "null"}; otherwise, the value of |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3044 |
* {@code obj.toString()} is returned. |
2 | 3045 |
* @see java.lang.Object#toString() |
3046 |
*/ |
|
3047 |
public static String valueOf(Object obj) { |
|
3048 |
return (obj == null) ? "null" : obj.toString(); |
|
3049 |
} |
|
3050 |
||
3051 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3052 |
* Returns the string representation of the {@code char} array |
2 | 3053 |
* argument. The contents of the character array are copied; subsequent |
21841
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
3054 |
* modification of the character array does not affect the returned |
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
3055 |
* string. |
2 | 3056 |
* |
21841
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
3057 |
* @param data the character array. |
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
3058 |
* @return a {@code String} that contains the characters of the |
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
3059 |
* character array. |
2 | 3060 |
*/ |
3061 |
public static String valueOf(char data[]) { |
|
3062 |
return new String(data); |
|
3063 |
} |
|
3064 |
||
3065 |
/** |
|
3066 |
* Returns the string representation of a specific subarray of the |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3067 |
* {@code char} array argument. |
2 | 3068 |
* <p> |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3069 |
* The {@code offset} argument is the index of the first |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3070 |
* character of the subarray. The {@code count} argument |
2 | 3071 |
* specifies the length of the subarray. The contents of the subarray |
3072 |
* are copied; subsequent modification of the character array does not |
|
21841
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
3073 |
* affect the returned string. |
2 | 3074 |
* |
3075 |
* @param data the character array. |
|
21841
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
3076 |
* @param offset initial offset of the subarray. |
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
3077 |
* @param count length of the subarray. |
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
3078 |
* @return a {@code String} that contains the characters of the |
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
3079 |
* specified subarray of the character array. |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3080 |
* @exception IndexOutOfBoundsException if {@code offset} is |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3081 |
* negative, or {@code count} is negative, or |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3082 |
* {@code offset+count} is larger than |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3083 |
* {@code data.length}. |
2 | 3084 |
*/ |
3085 |
public static String valueOf(char data[], int offset, int count) { |
|
3086 |
return new String(data, offset, count); |
|
3087 |
} |
|
3088 |
||
3089 |
/** |
|
21841
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
3090 |
* Equivalent to {@link #valueOf(char[], int, int)}. |
2 | 3091 |
* |
3092 |
* @param data the character array. |
|
3093 |
* @param offset initial offset of the subarray. |
|
3094 |
* @param count length of the subarray. |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3095 |
* @return a {@code String} that contains the characters of the |
2 | 3096 |
* specified subarray of the character array. |
21841
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
3097 |
* @exception IndexOutOfBoundsException if {@code offset} is |
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
3098 |
* negative, or {@code count} is negative, or |
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
3099 |
* {@code offset+count} is larger than |
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
3100 |
* {@code data.length}. |
2 | 3101 |
*/ |
3102 |
public static String copyValueOf(char data[], int offset, int count) { |
|
3103 |
return new String(data, offset, count); |
|
3104 |
} |
|
3105 |
||
3106 |
/** |
|
21841
5e5571b9a6a2
7174936: several String methods claim to always create new String
smarks
parents:
21670
diff
changeset
|
3107 |
* Equivalent to {@link #valueOf(char[])}. |
2 | 3108 |
* |
3109 |
* @param data the character array. |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3110 |
* @return a {@code String} that contains the characters of the |
2 | 3111 |
* character array. |
3112 |
*/ |
|
3113 |
public static String copyValueOf(char data[]) { |
|
12858
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
3114 |
return new String(data); |
2 | 3115 |
} |
3116 |
||
3117 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3118 |
* Returns the string representation of the {@code boolean} argument. |
2 | 3119 |
* |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3120 |
* @param b a {@code boolean}. |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3121 |
* @return if the argument is {@code true}, a string equal to |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3122 |
* {@code "true"} is returned; otherwise, a string equal to |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3123 |
* {@code "false"} is returned. |
2 | 3124 |
*/ |
3125 |
public static String valueOf(boolean b) { |
|
3126 |
return b ? "true" : "false"; |
|
3127 |
} |
|
3128 |
||
3129 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3130 |
* Returns the string representation of the {@code char} |
2 | 3131 |
* argument. |
3132 |
* |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3133 |
* @param c a {@code char}. |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3134 |
* @return a string of length {@code 1} containing |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3135 |
* as its single character the argument {@code c}. |
2 | 3136 |
*/ |
3137 |
public static String valueOf(char c) { |
|
33663 | 3138 |
if (COMPACT_STRINGS && StringLatin1.canEncode(c)) { |
3139 |
return new String(StringLatin1.toBytes(c), LATIN1); |
|
3140 |
} |
|
3141 |
return new String(StringUTF16.toBytes(c), UTF16); |
|
2 | 3142 |
} |
3143 |
||
3144 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3145 |
* Returns the string representation of the {@code int} argument. |
2 | 3146 |
* <p> |
3147 |
* The representation is exactly the one returned by the |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3148 |
* {@code Integer.toString} method of one argument. |
2 | 3149 |
* |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3150 |
* @param i an {@code int}. |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3151 |
* @return a string representation of the {@code int} argument. |
2 | 3152 |
* @see java.lang.Integer#toString(int, int) |
3153 |
*/ |
|
3154 |
public static String valueOf(int i) { |
|
3964
cf913644be58
6480728: Byte.valueOf(byte) returns a cached value but Byte.valueOf(String)
darcy
parents:
3617
diff
changeset
|
3155 |
return Integer.toString(i); |
2 | 3156 |
} |
3157 |
||
3158 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3159 |
* Returns the string representation of the {@code long} argument. |
2 | 3160 |
* <p> |
3161 |
* The representation is exactly the one returned by the |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3162 |
* {@code Long.toString} method of one argument. |
2 | 3163 |
* |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3164 |
* @param l a {@code long}. |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3165 |
* @return a string representation of the {@code long} argument. |
2 | 3166 |
* @see java.lang.Long#toString(long) |
3167 |
*/ |
|
3168 |
public static String valueOf(long l) { |
|
3964
cf913644be58
6480728: Byte.valueOf(byte) returns a cached value but Byte.valueOf(String)
darcy
parents:
3617
diff
changeset
|
3169 |
return Long.toString(l); |
2 | 3170 |
} |
3171 |
||
3172 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3173 |
* Returns the string representation of the {@code float} argument. |
2 | 3174 |
* <p> |
3175 |
* The representation is exactly the one returned by the |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3176 |
* {@code Float.toString} method of one argument. |
2 | 3177 |
* |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3178 |
* @param f a {@code float}. |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3179 |
* @return a string representation of the {@code float} argument. |
2 | 3180 |
* @see java.lang.Float#toString(float) |
3181 |
*/ |
|
3182 |
public static String valueOf(float f) { |
|
3183 |
return Float.toString(f); |
|
3184 |
} |
|
3185 |
||
3186 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3187 |
* Returns the string representation of the {@code double} argument. |
2 | 3188 |
* <p> |
3189 |
* The representation is exactly the one returned by the |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3190 |
* {@code Double.toString} method of one argument. |
2 | 3191 |
* |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3192 |
* @param d a {@code double}. |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3193 |
* @return a string representation of the {@code double} argument. |
2 | 3194 |
* @see java.lang.Double#toString(double) |
3195 |
*/ |
|
3196 |
public static String valueOf(double d) { |
|
3197 |
return Double.toString(d); |
|
3198 |
} |
|
3199 |
||
3200 |
/** |
|
3201 |
* Returns a canonical representation for the string object. |
|
3202 |
* <p> |
|
3203 |
* A pool of strings, initially empty, is maintained privately by the |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3204 |
* class {@code String}. |
2 | 3205 |
* <p> |
3206 |
* When the intern method is invoked, if the pool already contains a |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3207 |
* string equal to this {@code String} object as determined by |
2 | 3208 |
* the {@link #equals(Object)} method, then the string from the pool is |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3209 |
* returned. Otherwise, this {@code String} object is added to the |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3210 |
* pool and a reference to this {@code String} object is returned. |
2 | 3211 |
* <p> |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3212 |
* It follows that for any two strings {@code s} and {@code t}, |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3213 |
* {@code s.intern() == t.intern()} is {@code true} |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11127
diff
changeset
|
3214 |
* if and only if {@code s.equals(t)} is {@code true}. |
2 | 3215 |
* <p> |
3216 |
* All literal strings and string-valued constant expressions are |
|
9266
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
7816
diff
changeset
|
3217 |
* interned. String literals are defined in section 3.10.5 of the |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
7816
diff
changeset
|
3218 |
* <cite>The Java™ Language Specification</cite>. |
2 | 3219 |
* |
3220 |
* @return a string that has the same contents as this string, but is |
|
3221 |
* guaranteed to be from a pool of unique strings. |
|
37723 | 3222 |
* @jls 3.10.5 String Literals |
2 | 3223 |
*/ |
3224 |
public native String intern(); |
|
33663 | 3225 |
|
49109 | 3226 |
/** |
3227 |
* Returns a string whose value is the concatenation of this |
|
3228 |
* string repeated {@code count} times. |
|
3229 |
* <p> |
|
3230 |
* If this string is empty or count is zero then the empty |
|
3231 |
* string is returned. |
|
3232 |
* |
|
3233 |
* @param count number of times to repeat |
|
3234 |
* |
|
3235 |
* @return A string composed of this string repeated |
|
3236 |
* {@code count} times or the empty string if this |
|
3237 |
* string is empty or count is zero |
|
3238 |
* |
|
3239 |
* @throws IllegalArgumentException if the {@code count} is |
|
3240 |
* negative. |
|
3241 |
* |
|
3242 |
* @since 11 |
|
3243 |
*/ |
|
3244 |
public String repeat(int count) { |
|
3245 |
if (count < 0) { |
|
3246 |
throw new IllegalArgumentException("count is negative: " + count); |
|
3247 |
} |
|
3248 |
if (count == 1) { |
|
3249 |
return this; |
|
3250 |
} |
|
3251 |
final int len = value.length; |
|
3252 |
if (len == 0 || count == 0) { |
|
3253 |
return ""; |
|
3254 |
} |
|
3255 |
if (len == 1) { |
|
3256 |
final byte[] single = new byte[count]; |
|
3257 |
Arrays.fill(single, value[0]); |
|
3258 |
return new String(single, coder); |
|
3259 |
} |
|
3260 |
if (Integer.MAX_VALUE / count < len) { |
|
3261 |
throw new OutOfMemoryError("Repeating " + len + " bytes String " + count + |
|
3262 |
" times will produce a String exceeding maximum size."); |
|
3263 |
} |
|
3264 |
final int limit = len * count; |
|
3265 |
final byte[] multiple = new byte[limit]; |
|
3266 |
System.arraycopy(value, 0, multiple, 0, len); |
|
3267 |
int copied = len; |
|
49122 | 3268 |
for (; copied < limit - copied; copied <<= 1) { |
49109 | 3269 |
System.arraycopy(multiple, 0, multiple, copied, copied); |
3270 |
} |
|
3271 |
System.arraycopy(multiple, 0, multiple, copied, limit - copied); |
|
3272 |
return new String(multiple, coder); |
|
3273 |
} |
|
3274 |
||
33663 | 3275 |
//////////////////////////////////////////////////////////////// |
3276 |
||
3277 |
/** |
|
3278 |
* Copy character bytes from this string into dst starting at dstBegin. |
|
3279 |
* This method doesn't perform any range checking. |
|
3280 |
* |
|
3281 |
* Invoker guarantees: dst is in UTF16 (inflate itself for asb), if two |
|
3282 |
* coders are different, and dst is big enough (range check) |
|
3283 |
* |
|
3284 |
* @param dstBegin the char index, not offset of byte[] |
|
3285 |
* @param coder the coder of dst[] |
|
3286 |
*/ |
|
3287 |
void getBytes(byte dst[], int dstBegin, byte coder) { |
|
3288 |
if (coder() == coder) { |
|
3289 |
System.arraycopy(value, 0, dst, dstBegin << coder, value.length); |
|
3290 |
} else { // this.coder == LATIN && coder == UTF16 |
|
3291 |
StringLatin1.inflate(value, 0, dst, dstBegin, value.length); |
|
3292 |
} |
|
3293 |
} |
|
3294 |
||
3295 |
/* |
|
3296 |
* Package private constructor. Trailing Void argument is there for |
|
3297 |
* disambiguating it against other (public) constructors. |
|
3298 |
* |
|
3299 |
* Stores the char[] value into a byte[] that each byte represents |
|
3300 |
* the8 low-order bits of the corresponding character, if the char[] |
|
3301 |
* contains only latin1 character. Or a byte[] that stores all |
|
3302 |
* characters in their byte sequences defined by the {@code StringUTF16}. |
|
3303 |
*/ |
|
3304 |
String(char[] value, int off, int len, Void sig) { |
|
3305 |
if (len == 0) { |
|
3306 |
this.value = "".value; |
|
3307 |
this.coder = "".coder; |
|
3308 |
return; |
|
3309 |
} |
|
3310 |
if (COMPACT_STRINGS) { |
|
3311 |
byte[] val = StringUTF16.compress(value, off, len); |
|
3312 |
if (val != null) { |
|
3313 |
this.value = val; |
|
3314 |
this.coder = LATIN1; |
|
3315 |
return; |
|
3316 |
} |
|
3317 |
} |
|
3318 |
this.coder = UTF16; |
|
3319 |
this.value = StringUTF16.toBytes(value, off, len); |
|
3320 |
} |
|
3321 |
||
3322 |
/* |
|
3323 |
* Package private constructor. Trailing Void argument is there for |
|
3324 |
* disambiguating it against other (public) constructors. |
|
3325 |
*/ |
|
3326 |
String(AbstractStringBuilder asb, Void sig) { |
|
3327 |
byte[] val = asb.getValue(); |
|
3328 |
int length = asb.length(); |
|
3329 |
if (asb.isLatin1()) { |
|
3330 |
this.coder = LATIN1; |
|
3331 |
this.value = Arrays.copyOfRange(val, 0, length); |
|
3332 |
} else { |
|
3333 |
if (COMPACT_STRINGS) { |
|
3334 |
byte[] buf = StringUTF16.compress(val, 0, length); |
|
3335 |
if (buf != null) { |
|
3336 |
this.coder = LATIN1; |
|
3337 |
this.value = buf; |
|
3338 |
return; |
|
3339 |
} |
|
3340 |
} |
|
3341 |
this.coder = UTF16; |
|
3342 |
this.value = Arrays.copyOfRange(val, 0, length << 1); |
|
3343 |
} |
|
3344 |
} |
|
3345 |
||
3346 |
/* |
|
3347 |
* Package private constructor which shares value array for speed. |
|
3348 |
*/ |
|
3349 |
String(byte[] value, byte coder) { |
|
3350 |
this.value = value; |
|
3351 |
this.coder = coder; |
|
3352 |
} |
|
3353 |
||
3354 |
byte coder() { |
|
3355 |
return COMPACT_STRINGS ? coder : UTF16; |
|
3356 |
} |
|
3357 |
||
48262 | 3358 |
byte[] value() { |
3359 |
return value; |
|
3360 |
} |
|
3361 |
||
33663 | 3362 |
private boolean isLatin1() { |
3363 |
return COMPACT_STRINGS && coder == LATIN1; |
|
3364 |
} |
|
3365 |
||
45572
07f412070bd9
8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents:
45138
diff
changeset
|
3366 |
@Native static final byte LATIN1 = 0; |
07f412070bd9
8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents:
45138
diff
changeset
|
3367 |
@Native static final byte UTF16 = 1; |
33663 | 3368 |
|
3369 |
/* |
|
3370 |
* StringIndexOutOfBoundsException if {@code index} is |
|
3371 |
* negative or greater than or equal to {@code length}. |
|
3372 |
*/ |
|
3373 |
static void checkIndex(int index, int length) { |
|
3374 |
if (index < 0 || index >= length) { |
|
44642
331e669007f7
8158168: Missing bounds checks for some String intrinsics
dlong
parents:
44369
diff
changeset
|
3375 |
throw new StringIndexOutOfBoundsException("index " + index + |
331e669007f7
8158168: Missing bounds checks for some String intrinsics
dlong
parents:
44369
diff
changeset
|
3376 |
",length " + length); |
33663 | 3377 |
} |
3378 |
} |
|
3379 |
||
3380 |
/* |
|
3381 |
* StringIndexOutOfBoundsException if {@code offset} |
|
3382 |
* is negative or greater than {@code length}. |
|
3383 |
*/ |
|
3384 |
static void checkOffset(int offset, int length) { |
|
3385 |
if (offset < 0 || offset > length) { |
|
3386 |
throw new StringIndexOutOfBoundsException("offset " + offset + |
|
3387 |
",length " + length); |
|
3388 |
} |
|
3389 |
} |
|
3390 |
||
3391 |
/* |
|
3392 |
* Check {@code offset}, {@code count} against {@code 0} and {@code length} |
|
3393 |
* bounds. |
|
3394 |
* |
|
3395 |
* @throws StringIndexOutOfBoundsException |
|
3396 |
* If {@code offset} is negative, {@code count} is negative, |
|
3397 |
* or {@code offset} is greater than {@code length - count} |
|
3398 |
*/ |
|
34517
c6e795a80c80
8142303: C2 compilation fails with "bad AD file"
thartmann
parents:
33663
diff
changeset
|
3399 |
static void checkBoundsOffCount(int offset, int count, int length) { |
33663 | 3400 |
if (offset < 0 || count < 0 || offset > length - count) { |
3401 |
throw new StringIndexOutOfBoundsException( |
|
3402 |
"offset " + offset + ", count " + count + ", length " + length); |
|
3403 |
} |
|
3404 |
} |
|
3405 |
||
3406 |
/* |
|
3407 |
* Check {@code begin}, {@code end} against {@code 0} and {@code length} |
|
3408 |
* bounds. |
|
3409 |
* |
|
3410 |
* @throws StringIndexOutOfBoundsException |
|
3411 |
* If {@code begin} is negative, {@code begin} is greater than |
|
3412 |
* {@code end}, or {@code end} is greater than {@code length}. |
|
3413 |
*/ |
|
44642
331e669007f7
8158168: Missing bounds checks for some String intrinsics
dlong
parents:
44369
diff
changeset
|
3414 |
static void checkBoundsBeginEnd(int begin, int end, int length) { |
33663 | 3415 |
if (begin < 0 || begin > end || end > length) { |
3416 |
throw new StringIndexOutOfBoundsException( |
|
3417 |
"begin " + begin + ", end " + end + ", length " + length); |
|
3418 |
} |
|
3419 |
} |
|
49129
fb9f590b9eee
4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents:
49122
diff
changeset
|
3420 |
|
fb9f590b9eee
4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents:
49122
diff
changeset
|
3421 |
/** |
fb9f590b9eee
4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents:
49122
diff
changeset
|
3422 |
* Returns the string representation of the {@code codePoint} |
fb9f590b9eee
4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents:
49122
diff
changeset
|
3423 |
* argument. |
fb9f590b9eee
4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents:
49122
diff
changeset
|
3424 |
* |
fb9f590b9eee
4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents:
49122
diff
changeset
|
3425 |
* @param codePoint a {@code codePoint}. |
fb9f590b9eee
4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents:
49122
diff
changeset
|
3426 |
* @return a string of length {@code 1} or {@code 2} containing |
fb9f590b9eee
4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents:
49122
diff
changeset
|
3427 |
* as its single character the argument {@code codePoint}. |
fb9f590b9eee
4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents:
49122
diff
changeset
|
3428 |
* @throws IllegalArgumentException if the specified |
fb9f590b9eee
4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents:
49122
diff
changeset
|
3429 |
* {@code codePoint} is not a {@linkplain Character#isValidCodePoint |
fb9f590b9eee
4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents:
49122
diff
changeset
|
3430 |
* valid Unicode code point}. |
fb9f590b9eee
4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents:
49122
diff
changeset
|
3431 |
*/ |
fb9f590b9eee
4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents:
49122
diff
changeset
|
3432 |
static String valueOfCodePoint(int codePoint) { |
fb9f590b9eee
4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents:
49122
diff
changeset
|
3433 |
if (COMPACT_STRINGS && StringLatin1.canEncode(codePoint)) { |
fb9f590b9eee
4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents:
49122
diff
changeset
|
3434 |
return new String(StringLatin1.toBytes((char)codePoint), LATIN1); |
fb9f590b9eee
4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents:
49122
diff
changeset
|
3435 |
} else if (Character.isBmpCodePoint(codePoint)) { |
fb9f590b9eee
4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents:
49122
diff
changeset
|
3436 |
return new String(StringUTF16.toBytes((char)codePoint), UTF16); |
fb9f590b9eee
4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents:
49122
diff
changeset
|
3437 |
} else if (Character.isSupplementaryCodePoint(codePoint)) { |
fb9f590b9eee
4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents:
49122
diff
changeset
|
3438 |
return new String(StringUTF16.toBytesSupplementary(codePoint), UTF16); |
fb9f590b9eee
4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents:
49122
diff
changeset
|
3439 |
} |
fb9f590b9eee
4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents:
49122
diff
changeset
|
3440 |
|
49211 | 3441 |
throw new IllegalArgumentException( |
3442 |
format("Not a valid Unicode code point: 0x%X", codePoint)); |
|
49129
fb9f590b9eee
4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents:
49122
diff
changeset
|
3443 |
} |
52914 | 3444 |
|
3445 |
/** |
|
53019 | 3446 |
* Returns an {@link Optional} containing the nominal descriptor for this |
3447 |
* instance, which is the instance itself. |
|
52914 | 3448 |
* |
3449 |
* @return an {@link Optional} describing the {@linkplain String} instance |
|
3450 |
* @since 12 |
|
3451 |
*/ |
|
3452 |
@Override |
|
3453 |
public Optional<String> describeConstable() { |
|
3454 |
return Optional.of(this); |
|
3455 |
} |
|
3456 |
||
3457 |
/** |
|
3458 |
* Resolves this instance as a {@link ConstantDesc}, the result of which is |
|
3459 |
* the instance itself. |
|
3460 |
* |
|
3461 |
* @param lookup ignored |
|
3462 |
* @return the {@linkplain String} instance |
|
3463 |
* @since 12 |
|
3464 |
*/ |
|
3465 |
@Override |
|
3466 |
public String resolveConstantDesc(MethodHandles.Lookup lookup) { |
|
3467 |
return this; |
|
3468 |
} |
|
3469 |
||
2 | 3470 |
} |