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