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