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