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