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