1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. Oracle designates this 9 * particular file as subject to the "Classpath" exception as provided 10 * by Oracle in the LICENSE file that accompanied this code. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 * or visit www.oracle.com if you need additional information or have any 24 * questions. 25 */ 26 27 package java.lang; 28 29 import dalvik.annotation.optimization.FastNative; 30 import dalvik.annotation.optimization.NeverInline; 31 import java.io.ObjectStreamField; 32 import java.io.UnsupportedEncodingException; 33 import java.lang.annotation.Native; 34 import java.lang.invoke.MethodHandles; 35 import java.lang.constant.Constable; 36 import java.lang.constant.ConstantDesc; 37 import java.nio.charset.Charset; 38 import java.nio.ByteBuffer; 39 import java.util.Comparator; 40 import java.util.Formatter; 41 import java.util.List; 42 import java.util.Locale; 43 import java.util.Objects; 44 import java.util.Optional; 45 import java.util.Spliterator; 46 import java.util.StringJoiner; 47 import java.util.function.Function; 48 import java.util.regex.Pattern; 49 import java.util.regex.PatternSyntaxException; 50 import java.util.stream.Collectors; 51 import java.util.stream.IntStream; 52 import java.util.stream.Stream; 53 import java.util.stream.StreamSupport; 54 import jdk.internal.vm.annotation.IntrinsicCandidate; 55 56 import libcore.util.CharsetUtils; 57 58 /** 59 * The {@code String} class represents character strings. All 60 * string literals in Java programs, such as {@code "abc"}, are 61 * implemented as instances of this class. 62 * <p> 63 * Strings are constant; their values cannot be changed after they 64 * are created. String buffers support mutable strings. 65 * Because String objects are immutable they can be shared. For example: 66 * <blockquote><pre> 67 * String str = "abc"; 68 * </pre></blockquote><p> 69 * is equivalent to: 70 * <blockquote><pre> 71 * char data[] = {'a', 'b', 'c'}; 72 * String str = new String(data); 73 * </pre></blockquote><p> 74 * Here are some more examples of how strings can be used: 75 * <blockquote><pre> 76 * System.out.println("abc"); 77 * String cde = "cde"; 78 * System.out.println("abc" + cde); 79 * String c = "abc".substring(2,3); 80 * String d = cde.substring(1, 2); 81 * </pre></blockquote> 82 * <p> 83 * The class {@code String} includes methods for examining 84 * individual characters of the sequence, for comparing strings, for 85 * searching strings, for extracting substrings, and for creating a 86 * copy of a string with all characters translated to uppercase or to 87 * lowercase. Case mapping is based on the Unicode Standard version 88 * specified by the {@link java.lang.Character Character} class. 89 * <p> 90 * The Java language provides special support for the string 91 * concatenation operator ( + ), and for conversion of 92 * other objects to strings. For additional information on string 93 * concatenation and conversion, see <i>The Java™ Language Specification</i>. 94 * 95 * <p> Unless otherwise noted, passing a {@code null} argument to a constructor 96 * or method in this class will cause a {@link NullPointerException} to be 97 * thrown. 98 * 99 * <p>A {@code String} represents a string in the UTF-16 format 100 * in which <em>supplementary characters</em> are represented by <em>surrogate 101 * pairs</em> (see the section <a href="Character.html#unicode">Unicode 102 * Character Representations</a> in the {@code Character} class for 103 * more information). 104 * Index values refer to {@code char} code units, so a supplementary 105 * character uses two positions in a {@code String}. 106 * <p>The {@code String} class provides methods for dealing with 107 * Unicode code points (i.e., characters), in addition to those for 108 * dealing with Unicode code units (i.e., {@code char} values). 109 * 110 * <p>Unless otherwise noted, methods for comparing Strings do not take locale 111 * into account. The {@link java.text.Collator} class provides methods for 112 * finer-grain, locale-sensitive String comparison. 113 * 114 * @implNote The implementation of the string concatenation operator is left to 115 * the discretion of a Java compiler, as long as the compiler ultimately conforms 116 * to <i>The Java™ Language Specification</i>. For example, the {@code javac} compiler 117 * may implement the operator with {@code StringBuffer}, {@code StringBuilder}, 118 * or {@code java.lang.invoke.StringConcatFactory} depending on the JDK version. The 119 * implementation of string conversion is typically through the method {@code toString}, 120 * defined by {@code Object} and inherited by all classes in Java. 121 * 122 * @author Lee Boynton 123 * @author Arthur van Hoff 124 * @author Martin Buchholz 125 * @author Ulf Zibis 126 * @see java.lang.Object#toString() 127 * @see java.lang.StringBuffer 128 * @see java.lang.StringBuilder 129 * @see java.nio.charset.Charset 130 * @since 1.0 131 * @jls 15.18.1 String Concatenation Operator + 132 */ 133 134 public final class String 135 implements java.io.Serializable, Comparable<String>, CharSequence, 136 Constable, ConstantDesc { 137 // BEGIN Android-changed: The character data is managed by the runtime. 138 /* 139 We only keep track of the length here and compression here. This has several consequences 140 throughout this class: 141 - References to value[i] are replaced by charAt(i). 142 - References to value.length are replaced by calls to length(). 143 - Sometimes the result of length() is assigned to a local variable to avoid repeated calls. 144 - We skip several attempts at optimization where the values field was assigned to a local 145 variable to avoid the getfield opcode. 146 These changes are not all marked individually. 147 148 If STRING_COMPRESSION_ENABLED, count stores the length shifted one bit to the left with the 149 lowest bit used to indicate whether or not the bytes are compressed (see GetFlaggedCount in 150 the native code). 151 /** 152 * The value is used for character storage. 153 * 154 * @implNote This field is trusted by the VM, and is a subject to 155 * constant folding if String instance is constant. Overwriting this 156 * field after construction will cause problems. 157 * 158 * Additionally, it is marked with {@link Stable} to trust the contents 159 * of the array. No other facility in JDK provides this functionality (yet). 160 * {@link Stable} is safe here, because value is never null. 161 * 162 @Stable 163 private final byte[] value; 164 */ 165 private final int count; 166 // END Android-changed: The character data is managed by the runtime. 167 168 // Android-changed: We make use of new StringIndexOutOfBoundsException constructor signatures. 169 // These improve some error messages. These changes are not all marked individually. 170 171 /** Cache the hash code for the string */ 172 private int hash; // Default to 0 173 174 /** use serialVersionUID from JDK 1.0.2 for interoperability */ 175 private static final long serialVersionUID = -6849794470754667710L; 176 177 // Android-changed: Modified the javadoc for the ART environment. 178 // Note that this COMPACT_STRINGS value is mainly used by the StringBuilder, not by String. 179 /** 180 * If String compaction is disabled, the bytes in {@code value} are 181 * always encoded in UTF16. 182 * 183 * For methods with several possible implementation paths, when String 184 * compaction is disabled, only one code path is taken. 185 * 186 * The instance field value is generally opaque to optimizing JIT 187 * compilers. Therefore, in performance-sensitive place, an explicit 188 * check of the static boolean {@code COMPACT_STRINGS} is done first 189 * before checking the {@code coder} field since the static boolean 190 * {@code COMPACT_STRINGS} would be constant folded away by an 191 * optimizing JIT compiler. The idioms for these cases are as follows. 192 * 193 * For code such as: 194 * 195 * if (coder == LATIN1) { ... } 196 * 197 * can be written more optimally as 198 * 199 * if (coder() == LATIN1) { ... } 200 * 201 * or: 202 * 203 * if (COMPACT_STRINGS && coder == LATIN1) { ... } 204 * 205 * An optimizing JIT compiler can fold the above conditional as: 206 * 207 * COMPACT_STRINGS == true => if (coder == LATIN1) { ... } 208 * COMPACT_STRINGS == false => if (false) { ... } 209 */ 210 // Android-changed: Inline the constant on ART. 211 static final boolean COMPACT_STRINGS = true; 212 213 // Android-added: Add a canonical empty string used by ART. 214 /** @hide */ 215 public static final String EMPTY = ""; 216 217 @Native static final byte LATIN1 = 0; 218 @Native static final byte UTF16 = 1; 219 220 /** 221 * Class String is special cased within the Serialization Stream Protocol. 222 * 223 * A String instance is written into an ObjectOutputStream according to 224 * <a href="{@docRoot}/../specs/serialization/protocol.html#stream-elements"> 225 * Object Serialization Specification, Section 6.2, "Stream Elements"</a> 226 */ 227 private static final ObjectStreamField[] serialPersistentFields = 228 new ObjectStreamField[0]; 229 230 /** 231 * Initializes a newly created {@code String} object so that it represents 232 * an empty character sequence. Note that use of this constructor is 233 * unnecessary since Strings are immutable. 234 */ String()235 public String() { 236 // BEGIN Android-changed: Implemented as compiler and runtime intrinsics. 237 /* 238 this.value = "".value; 239 this.coder = "".coder; 240 */ 241 throw new UnsupportedOperationException("Use StringFactory instead."); 242 // END Android-changed: Implemented as compiler and runtime intrinsics. 243 } 244 245 /** 246 * Initializes a newly created {@code String} object so that it represents 247 * the same sequence of characters as the argument; in other words, the 248 * newly created string is a copy of the argument string. Unless an 249 * explicit copy of {@code original} is needed, use of this constructor is 250 * unnecessary since Strings are immutable. 251 * 252 * @param original 253 * A {@code String} 254 */ 255 @IntrinsicCandidate String(String original)256 public String(String original) { 257 // BEGIN Android-changed: Implemented as compiler and runtime intrinsics. 258 /* 259 this.value = original.value; 260 this.coder = original.coder; 261 this.hash = original.hash; 262 */ 263 throw new UnsupportedOperationException("Use StringFactory instead."); 264 // END Android-changed: Implemented as compiler and runtime intrinsics. 265 } 266 267 /** 268 * Allocates a new {@code String} so that it represents the sequence of 269 * characters currently contained in the character array argument. The 270 * contents of the character array are copied; subsequent modification of 271 * the character array does not affect the newly created string. 272 * 273 * @param value 274 * The initial value of the string 275 */ String(char value[])276 public String(char value[]) { 277 // BEGIN Android-changed: Implemented as compiler and runtime intrinsics. 278 /* 279 this(value, 0, value.length, null); 280 */ 281 throw new UnsupportedOperationException("Use StringFactory instead."); 282 // END Android-changed: Implemented as compiler and runtime intrinsics. 283 } 284 285 /** 286 * Allocates a new {@code String} that contains characters from a subarray 287 * of the character array argument. The {@code offset} argument is the 288 * index of the first character of the subarray and the {@code count} 289 * argument specifies the length of the subarray. The contents of the 290 * subarray are copied; subsequent modification of the character array does 291 * not affect the newly created string. 292 * 293 * @param value 294 * Array that is the source of characters 295 * 296 * @param offset 297 * The initial offset 298 * 299 * @param count 300 * The length 301 * 302 * @throws IndexOutOfBoundsException 303 * If {@code offset} is negative, {@code count} is negative, or 304 * {@code offset} is greater than {@code value.length - count} 305 */ String(char value[], int offset, int count)306 public String(char value[], int offset, int count) { 307 // BEGIN Android-changed: Implemented as compiler and runtime intrinsics. 308 /* 309 this(value, offset, count, rangeCheck(value, offset, count)); 310 } 311 312 private static Void rangeCheck(char[] value, int offset, int count) { 313 checkBoundsOffCount(offset, count, value.length); 314 return null; 315 */ 316 throw new UnsupportedOperationException("Use StringFactory instead."); 317 // END Android-changed: Implemented as compiler and runtime intrinsics. 318 } 319 320 /** 321 * Allocates a new {@code String} that contains characters from a subarray 322 * of the <a href="Character.html#unicode">Unicode code point</a> array 323 * argument. The {@code offset} argument is the index of the first code 324 * point of the subarray and the {@code count} argument specifies the 325 * length of the subarray. The contents of the subarray are converted to 326 * {@code char}s; subsequent modification of the {@code int} array does not 327 * affect the newly created string. 328 * 329 * @param codePoints 330 * Array that is the source of Unicode code points 331 * 332 * @param offset 333 * The initial offset 334 * 335 * @param count 336 * The length 337 * 338 * @throws IllegalArgumentException 339 * If any invalid Unicode code point is found in {@code 340 * codePoints} 341 * 342 * @throws IndexOutOfBoundsException 343 * If {@code offset} is negative, {@code count} is negative, or 344 * {@code offset} is greater than {@code codePoints.length - count} 345 * 346 * @since 1.5 347 */ String(int[] codePoints, int offset, int count)348 public String(int[] codePoints, int offset, int count) { 349 // BEGIN Android-changed: Implemented as compiler and runtime intrinsics. 350 /* 351 checkBoundsOffCount(offset, count, codePoints.length); 352 if (count == 0) { 353 this.value = "".value; 354 this.coder = "".coder; 355 return; 356 } 357 if (COMPACT_STRINGS) { 358 byte[] val = StringLatin1.toBytes(codePoints, offset, count); 359 if (val != null) { 360 this.coder = LATIN1; 361 this.value = val; 362 return; 363 } 364 } 365 this.coder = UTF16; 366 this.value = StringUTF16.toBytes(codePoints, offset, count); 367 */ 368 throw new UnsupportedOperationException("Use StringFactory instead."); 369 // END Android-changed: Implemented as compiler and runtime intrinsics. 370 } 371 372 /** 373 * Allocates a new {@code String} constructed from a subarray of an array 374 * of 8-bit integer values. 375 * 376 * <p> The {@code offset} argument is the index of the first byte of the 377 * subarray, and the {@code count} argument specifies the length of the 378 * subarray. 379 * 380 * <p> Each {@code byte} in the subarray is converted to a {@code char} as 381 * specified in the {@link #String(byte[],int) String(byte[],int)} constructor. 382 * 383 * @deprecated This method does not properly convert bytes into characters. 384 * As of JDK 1.1, the preferred way to do this is via the 385 * {@code String} constructors that take a {@link 386 * java.nio.charset.Charset}, charset name, or that use the platform's 387 * default charset. 388 * 389 * @param ascii 390 * The bytes to be converted to characters 391 * 392 * @param hibyte 393 * The top 8 bits of each 16-bit Unicode code unit 394 * 395 * @param offset 396 * The initial offset 397 * @param count 398 * The length 399 * 400 * @throws IndexOutOfBoundsException 401 * If {@code offset} is negative, {@code count} is negative, or 402 * {@code offset} is greater than {@code ascii.length - count} 403 * 404 * @see #String(byte[], int) 405 * @see #String(byte[], int, int, java.lang.String) 406 * @see #String(byte[], int, int, java.nio.charset.Charset) 407 * @see #String(byte[], int, int) 408 * @see #String(byte[], java.lang.String) 409 * @see #String(byte[], java.nio.charset.Charset) 410 * @see #String(byte[]) 411 */ 412 @Deprecated(since="1.1") String(byte ascii[], int hibyte, int offset, int count)413 public String(byte ascii[], int hibyte, int offset, int count) { 414 // BEGIN Android-changed: Implemented as compiler and runtime intrinsics. 415 /* 416 checkBoundsOffCount(offset, count, ascii.length); 417 if (count == 0) { 418 this.value = "".value; 419 this.coder = "".coder; 420 return; 421 } 422 if (COMPACT_STRINGS && (byte)hibyte == 0) { 423 this.value = Arrays.copyOfRange(ascii, offset, offset + count); 424 this.coder = LATIN1; 425 } else { 426 hibyte <<= 8; 427 byte[] val = StringUTF16.newBytesFor(count); 428 for (int i = 0; i < count; i++) { 429 StringUTF16.putChar(val, i, hibyte | (ascii[offset++] & 0xff)); 430 } 431 this.value = val; 432 this.coder = UTF16; 433 } 434 */ 435 throw new UnsupportedOperationException("Use StringFactory instead."); 436 // END Android-changed: Implemented as compiler and runtime intrinsics. 437 } 438 439 /** 440 * Allocates a new {@code String} containing characters constructed from 441 * an array of 8-bit integer values. Each character <i>c</i> in the 442 * resulting string is constructed from the corresponding component 443 * <i>b</i> in the byte array such that: 444 * 445 * <blockquote><pre> 446 * <b><i>c</i></b> == (char)(((hibyte & 0xff) << 8) 447 * | (<b><i>b</i></b> & 0xff)) 448 * </pre></blockquote> 449 * 450 * @deprecated This method does not properly convert bytes into 451 * characters. As of JDK 1.1, the preferred way to do this is via the 452 * {@code String} constructors that take a {@link 453 * java.nio.charset.Charset}, charset name, or that use the platform's 454 * default charset. 455 * 456 * @param ascii 457 * The bytes to be converted to characters 458 * 459 * @param hibyte 460 * The top 8 bits of each 16-bit Unicode code unit 461 * 462 * @see #String(byte[], int, int, java.lang.String) 463 * @see #String(byte[], int, int, java.nio.charset.Charset) 464 * @see #String(byte[], int, int) 465 * @see #String(byte[], java.lang.String) 466 * @see #String(byte[], java.nio.charset.Charset) 467 * @see #String(byte[]) 468 */ 469 @Deprecated(since="1.1") String(byte ascii[], int hibyte)470 public String(byte ascii[], int hibyte) { 471 // BEGIN Android-changed: Implemented as compiler and runtime intrinsics. 472 /* 473 this(ascii, hibyte, 0, ascii.length); 474 */ 475 throw new UnsupportedOperationException("Use StringFactory instead."); 476 // END Android-changed: Implemented as compiler and runtime intrinsics. 477 } 478 479 // BEGIN Android-removed: checkBounds(byte[] bytes, int offset, int length) utility method. 480 /* Common private utility method used to bounds check the byte array 481 * and requested offset & length values used by the String(byte[],..) 482 * constructors. 483 * 484 private static void checkBounds(byte[] bytes, int offset, int length) { 485 if (length < 0) 486 throw new StringIndexOutOfBoundsException(length); 487 if (offset < 0) 488 throw new StringIndexOutOfBoundsException(offset); 489 if (offset > bytes.length - length) 490 throw new StringIndexOutOfBoundsException(offset + length); 491 } 492 // END Android-removed: checkBounds(byte[] bytes, int offset, int length) utility method. 493 494 /** 495 * Constructs a new {@code String} by decoding the specified subarray of 496 * bytes using the specified charset. The length of the new {@code String} 497 * is a function of the charset, and hence may not be equal to the length 498 * of the subarray. 499 * 500 * <p> The behavior of this constructor when the given bytes are not valid 501 * in the given charset is unspecified. The {@link 502 * java.nio.charset.CharsetDecoder} class should be used when more control 503 * over the decoding process is required. 504 * 505 * @param bytes 506 * The bytes to be decoded into characters 507 * 508 * @param offset 509 * The index of the first byte to decode 510 * 511 * @param length 512 * The number of bytes to decode 513 514 * @param charsetName 515 * The name of a supported {@linkplain java.nio.charset.Charset 516 * charset} 517 * 518 * @throws UnsupportedEncodingException 519 * If the named charset is not supported 520 * 521 * @throws IndexOutOfBoundsException 522 * If {@code offset} is negative, {@code length} is negative, or 523 * {@code offset} is greater than {@code bytes.length - length} 524 * 525 * @since 1.1 526 */ String(byte bytes[], int offset, int length, String charsetName)527 public String(byte bytes[], int offset, int length, String charsetName) 528 throws UnsupportedEncodingException { 529 // BEGIN Android-changed: Implemented as compiler and runtime intrinsics. 530 /* 531 if (charsetName == null) 532 throw new NullPointerException("charsetName"); 533 checkBoundsOffCount(offset, length, bytes.length); 534 StringCoding.Result ret = 535 StringCoding.decode(charsetName, bytes, offset, length); 536 this.value = ret.value; 537 this.coder = ret.coder; 538 */ 539 throw new UnsupportedOperationException("Use StringFactory instead."); 540 // END Android-changed: Implemented as compiler and runtime intrinsics. 541 } 542 543 /** 544 * Constructs a new {@code String} by decoding the specified subarray of 545 * bytes using the specified {@linkplain java.nio.charset.Charset charset}. 546 * The length of the new {@code String} is a function of the charset, and 547 * hence may not be equal to the length of the subarray. 548 * 549 * <p> This method always replaces malformed-input and unmappable-character 550 * sequences with this charset's default replacement string. The {@link 551 * java.nio.charset.CharsetDecoder} class should be used when more control 552 * over the decoding process is required. 553 * 554 * @param bytes 555 * The bytes to be decoded into characters 556 * 557 * @param offset 558 * The index of the first byte to decode 559 * 560 * @param length 561 * The number of bytes to decode 562 * 563 * @param charset 564 * The {@linkplain java.nio.charset.Charset charset} to be used to 565 * decode the {@code bytes} 566 * 567 * @throws IndexOutOfBoundsException 568 * If {@code offset} is negative, {@code length} is negative, or 569 * {@code offset} is greater than {@code bytes.length - length} 570 * 571 * @since 1.6 572 */ String(byte bytes[], int offset, int length, Charset charset)573 public String(byte bytes[], int offset, int length, Charset charset) { 574 // BEGIN Android-changed: Implemented as compiler and runtime intrinsics. 575 /* 576 if (charset == null) 577 throw new NullPointerException("charset"); 578 checkBoundsOffCount(offset, length, bytes.length); 579 StringCoding.Result ret = 580 StringCoding.decode(charset, bytes, offset, length); 581 this.value = ret.value; 582 this.coder = ret.coder; 583 */ 584 throw new UnsupportedOperationException("Use StringFactory instead."); 585 // END Android-changed: Implemented as compiler and runtime intrinsics. 586 } 587 588 /** 589 * Constructs a new {@code String} by decoding the specified array of bytes 590 * using the specified {@linkplain java.nio.charset.Charset charset}. The 591 * length of the new {@code String} is a function of the charset, and hence 592 * may not be equal to the length of the byte array. 593 * 594 * <p> The behavior of this constructor when the given bytes are not valid 595 * in the given charset is unspecified. The {@link 596 * java.nio.charset.CharsetDecoder} class should be used when more control 597 * over the decoding process is required. 598 * 599 * @param bytes 600 * The bytes to be decoded into characters 601 * 602 * @param charsetName 603 * The name of a supported {@linkplain java.nio.charset.Charset 604 * charset} 605 * 606 * @throws UnsupportedEncodingException 607 * If the named charset is not supported 608 * 609 * @since 1.1 610 */ String(byte bytes[], String charsetName)611 public String(byte bytes[], String charsetName) 612 throws UnsupportedEncodingException { 613 // BEGIN Android-changed: Implemented as compiler and runtime intrinsics. 614 /* 615 this(bytes, 0, bytes.length, charsetName); 616 */ 617 throw new UnsupportedOperationException("Use StringFactory instead."); 618 // END Android-changed: Implemented as compiler and runtime intrinsics. 619 } 620 621 /** 622 * Constructs a new {@code String} by decoding the specified array of 623 * bytes using the specified {@linkplain java.nio.charset.Charset charset}. 624 * The length of the new {@code String} is a function of the charset, and 625 * hence may not be equal to the length of the byte array. 626 * 627 * <p> This method always replaces malformed-input and unmappable-character 628 * sequences with this charset's default replacement string. The {@link 629 * java.nio.charset.CharsetDecoder} class should be used when more control 630 * over the decoding process is required. 631 * 632 * @param bytes 633 * The bytes to be decoded into characters 634 * 635 * @param charset 636 * The {@linkplain java.nio.charset.Charset charset} to be used to 637 * decode the {@code bytes} 638 * 639 * @since 1.6 640 */ String(byte bytes[], Charset charset)641 public String(byte bytes[], Charset charset) { 642 // BEGIN Android-changed: Implemented as compiler and runtime intrinsics. 643 /* 644 this(bytes, 0, bytes.length, charset); 645 */ 646 throw new UnsupportedOperationException("Use StringFactory instead."); 647 // END Android-changed: Implemented as compiler and runtime intrinsics. 648 } 649 650 /** 651 * Constructs a new {@code String} by decoding the specified subarray of 652 * bytes using the platform's default charset. The length of the new 653 * {@code String} is a function of the charset, and hence may not be equal 654 * to the length of the subarray. 655 * 656 * <p> The behavior of this constructor when the given bytes are not valid 657 * in the default charset is unspecified. The {@link 658 * java.nio.charset.CharsetDecoder} class should be used when more control 659 * over the decoding process is required. 660 * 661 * @param bytes 662 * The bytes to be decoded into characters 663 * 664 * @param offset 665 * The index of the first byte to decode 666 * 667 * @param length 668 * The number of bytes to decode 669 * 670 * @throws IndexOutOfBoundsException 671 * If {@code offset} is negative, {@code length} is negative, or 672 * {@code offset} is greater than {@code bytes.length - length} 673 * 674 * @since 1.1 675 */ String(byte bytes[], int offset, int length)676 public String(byte bytes[], int offset, int length) { 677 // BEGIN Android-changed: Implemented as compiler and runtime intrinsics. 678 /* 679 checkBoundsOffCount(offset, length, bytes.length); 680 StringCoding.Result ret = StringCoding.decode(bytes, offset, length); 681 this.value = ret.value; 682 this.coder = ret.coder; 683 */ 684 throw new UnsupportedOperationException("Use StringFactory instead."); 685 // END Android-changed: Implemented as compiler and runtime intrinsics. 686 } 687 688 /** 689 * Constructs a new {@code String} by decoding the specified array of bytes 690 * using the platform's default charset. The length of the new {@code 691 * String} is a function of the charset, and hence may not be equal to the 692 * length of the byte array. 693 * 694 * <p> The behavior of this constructor when the given bytes are not valid 695 * in the default charset is unspecified. The {@link 696 * java.nio.charset.CharsetDecoder} class should be used when more control 697 * over the decoding process is required. 698 * 699 * @param bytes 700 * The bytes to be decoded into characters 701 * 702 * @since 1.1 703 */ String(byte[] bytes)704 public String(byte[] bytes) { 705 // BEGIN Android-changed: Implemented as compiler and runtime intrinsics. 706 /* 707 this(bytes, 0, bytes.length); 708 */ 709 throw new UnsupportedOperationException("Use StringFactory instead."); 710 // END Android-changed: Implemented as compiler and runtime intrinsics. 711 } 712 713 /** 714 * Allocates a new string that contains the sequence of characters 715 * currently contained in the string buffer argument. The contents of the 716 * string buffer are copied; subsequent modification of the string buffer 717 * does not affect the newly created string. 718 * 719 * @param buffer 720 * A {@code StringBuffer} 721 */ String(StringBuffer buffer)722 public String(StringBuffer buffer) { 723 // BEGIN Android-changed: Implemented as compiler and runtime intrinsics. 724 /* 725 this(buffer.toString()); 726 */ 727 throw new UnsupportedOperationException("Use StringFactory instead."); 728 // END Android-changed: Implemented as compiler and runtime intrinsics. 729 } 730 731 /** 732 * Allocates a new string that contains the sequence of characters 733 * currently contained in the string builder argument. The contents of the 734 * string builder are copied; subsequent modification of the string builder 735 * does not affect the newly created string. 736 * 737 * <p> This constructor is provided to ease migration to {@code 738 * StringBuilder}. Obtaining a string from a string builder via the {@code 739 * toString} method is likely to run faster and is generally preferred. 740 * 741 * @param builder 742 * A {@code StringBuilder} 743 * 744 * @since 1.5 745 */ String(StringBuilder builder)746 public String(StringBuilder builder) { 747 // BEGIN Android-changed: Implemented as compiler and runtime intrinsics. 748 /* 749 this(builder, null); 750 */ 751 throw new UnsupportedOperationException("Use StringFactory instead."); 752 // END Android-changed: Implemented as compiler and runtime intrinsics. 753 } 754 755 // BEGIN Android-removed: Unused package-private constructor String(char[] value, boolean share). 756 /* 757 /* 758 * Package private constructor which shares value array for speed. 759 * this constructor is always expected to be called with share==true. 760 * a separate constructor is needed because we already have a public 761 * String(char[]) constructor that makes a copy of the given char[]. 762 * 763 String(char[] value, boolean share) { 764 // assert share : "unshared not supported"; 765 this.value = value; 766 } 767 */ 768 // END Android-removed: Unused package-private constructor String(char[] value, boolean share). 769 770 // BEGIN Android-added: Constructor for internal use. 771 // Not implemented in java as all calls are intercepted by the runtime. 772 /** 773 * Package private constructor 774 * 775 * @deprecated Use {@link #String(char[],int,int)} instead. 776 */ 777 @Deprecated String(int offset, int count, char[] value)778 String(int offset, int count, char[] value) { 779 throw new UnsupportedOperationException("Use StringFactory instead."); 780 } 781 // END Android-added: Constructor for internal use. 782 783 /** 784 * Returns the length of this string. 785 * The length is equal to the number of <a href="Character.html#unicode">Unicode 786 * code units</a> in the string. 787 * 788 * @return the length of the sequence of characters represented by this 789 * object. 790 */ length()791 public int length() { 792 // BEGIN Android-changed: Get length from count field rather than value array (see above). 793 /* 794 return value.length >> coder(); 795 */ 796 final boolean STRING_COMPRESSION_ENABLED = true; 797 if (STRING_COMPRESSION_ENABLED) { 798 // For the compression purposes (save the characters as 8-bit if all characters 799 // are ASCII), the least significant bit of "count" is used as the compression flag. 800 return (count >>> 1); 801 } else { 802 return count; 803 } 804 // END Android-changed: Get length from count field rather than value array (see above). 805 } 806 807 /** 808 * Returns {@code true} if, and only if, {@link #length()} is {@code 0}. 809 * 810 * @return {@code true} if {@link #length()} is {@code 0}, otherwise 811 * {@code false} 812 * 813 * @since 1.6 814 */ 815 @Override isEmpty()816 public boolean isEmpty() { 817 // BEGIN Android-changed: Get length from count field rather than value array (see above). 818 // Empty string has {@code count == 0} with or without string compression enabled. 819 /* 820 return value.length == 0; 821 */ 822 return count == 0; 823 // END Android-changed: Get length from count field rather than value array (see above). 824 } 825 826 /** 827 * Returns the {@code char} value at the 828 * specified index. An index ranges from {@code 0} to 829 * {@code length() - 1}. The first {@code char} value of the sequence 830 * is at index {@code 0}, the next at index {@code 1}, 831 * and so on, as for array indexing. 832 * 833 * <p>If the {@code char} value specified by the index is a 834 * <a href="Character.html#unicode">surrogate</a>, the surrogate 835 * value is returned. 836 * 837 * @param index the index of the {@code char} value. 838 * @return the {@code char} value at the specified index of this string. 839 * The first {@code char} value is at index {@code 0}. 840 * @exception IndexOutOfBoundsException if the {@code index} 841 * argument is negative or not less than the length of this 842 * string. 843 */ 844 // BEGIN Android-changed: Replace with implementation in runtime to access chars (see above). 845 /* 846 public char charAt(int index) { 847 if (isLatin1()) { 848 return StringLatin1.charAt(value, index); 849 } else { 850 return StringUTF16.charAt(value, index); 851 } 852 } 853 */ 854 @FastNative charAt(int index)855 public native char charAt(int index); 856 // END Android-changed: Replace with implementation in runtime to access chars (see above). 857 858 /** 859 * Returns the character (Unicode code point) at the specified 860 * index. The index refers to {@code char} values 861 * (Unicode code units) and ranges from {@code 0} to 862 * {@link #length()}{@code - 1}. 863 * 864 * <p> If the {@code char} value specified at the given index 865 * is in the high-surrogate range, the following index is less 866 * than the length of this {@code String}, and the 867 * {@code char} value at the following index is in the 868 * low-surrogate range, then the supplementary code point 869 * corresponding to this surrogate pair is returned. Otherwise, 870 * the {@code char} value at the given index is returned. 871 * 872 * @param index the index to the {@code char} values 873 * @return the code point value of the character at the 874 * {@code index} 875 * @exception IndexOutOfBoundsException if the {@code index} 876 * argument is negative or not less than the length of this 877 * string. 878 * @since 1.5 879 */ codePointAt(int index)880 public int codePointAt(int index) { 881 // BEGIN Android-changed: delegate codePointAt() to Character class. 882 /* 883 if (isLatin1()) { 884 checkIndex(index, value.length); 885 return value[index] & 0xff; 886 } 887 int length = value.length >> 1; 888 checkIndex(index, length); 889 return StringUTF16.codePointAt(value, index, length); 890 */ 891 checkIndex(index, length()); 892 return Character.codePointAt(this, index); 893 } 894 895 /** 896 * Returns the character (Unicode code point) before the specified 897 * index. The index refers to {@code char} values 898 * (Unicode code units) and ranges from {@code 1} to {@link 899 * CharSequence#length() length}. 900 * 901 * <p> If the {@code char} value at {@code (index - 1)} 902 * is in the low-surrogate range, {@code (index - 2)} is not 903 * negative, and the {@code char} value at {@code (index - 904 * 2)} is in the high-surrogate range, then the 905 * supplementary code point value of the surrogate pair is 906 * returned. If the {@code char} value at {@code index - 907 * 1} is an unpaired low-surrogate or a high-surrogate, the 908 * surrogate value is returned. 909 * 910 * @param index the index following the code point that should be returned 911 * @return the Unicode code point value before the given index. 912 * @exception IndexOutOfBoundsException if the {@code index} 913 * argument is less than 1 or greater than the length 914 * of this string. 915 * @since 1.5 916 */ codePointBefore(int index)917 public int codePointBefore(int index) { 918 int i = index - 1; 919 if (i < 0 || i >= length()) { 920 throw new StringIndexOutOfBoundsException(index); 921 } 922 // BEGIN Android-changed: delegate codePointBefore to Character class. 923 /* 924 if (isLatin1()) { 925 return (value[i] & 0xff); 926 } 927 return StringUTF16.codePointBefore(value, index); 928 */ 929 return Character.codePointBefore(this, index); 930 } 931 932 /** 933 * Returns the number of Unicode code points in the specified text 934 * range of this {@code String}. The text range begins at the 935 * specified {@code beginIndex} and extends to the 936 * {@code char} at index {@code endIndex - 1}. Thus the 937 * length (in {@code char}s) of the text range is 938 * {@code endIndex-beginIndex}. Unpaired surrogates within 939 * the text range count as one code point each. 940 * 941 * @param beginIndex the index to the first {@code char} of 942 * the text range. 943 * @param endIndex the index after the last {@code char} of 944 * the text range. 945 * @return the number of Unicode code points in the specified text 946 * range 947 * @exception IndexOutOfBoundsException if the 948 * {@code beginIndex} is negative, or {@code endIndex} 949 * is larger than the length of this {@code String}, or 950 * {@code beginIndex} is larger than {@code endIndex}. 951 * @since 1.5 952 */ codePointCount(int beginIndex, int endIndex)953 public int codePointCount(int beginIndex, int endIndex) { 954 if (beginIndex < 0 || beginIndex > endIndex || 955 endIndex > length()) { 956 throw new IndexOutOfBoundsException(); 957 } 958 // BEGIN Android-changed: delegate codePointCount to Character class. 959 /* 960 if (isLatin1()) { 961 return endIndex - beginIndex; 962 } 963 return StringUTF16.codePointCount(value, beginIndex, endIndex); 964 */ 965 return Character.codePointCount(this, beginIndex, endIndex); 966 // END Android-changed: delegate codePointCount to Character class. 967 } 968 969 /** 970 * Returns the index within this {@code String} that is 971 * offset from the given {@code index} by 972 * {@code codePointOffset} code points. Unpaired surrogates 973 * within the text range given by {@code index} and 974 * {@code codePointOffset} count as one code point each. 975 * 976 * @param index the index to be offset 977 * @param codePointOffset the offset in code points 978 * @return the index within this {@code String} 979 * @exception IndexOutOfBoundsException if {@code index} 980 * is negative or larger then the length of this 981 * {@code String}, or if {@code codePointOffset} is positive 982 * and the substring starting with {@code index} has fewer 983 * than {@code codePointOffset} code points, 984 * or if {@code codePointOffset} is negative and the substring 985 * before {@code index} has fewer than the absolute value 986 * of {@code codePointOffset} code points. 987 * @since 1.5 988 */ offsetByCodePoints(int index, int codePointOffset)989 public int offsetByCodePoints(int index, int codePointOffset) { 990 if (index < 0 || index > length()) { 991 throw new IndexOutOfBoundsException(); 992 } 993 return Character.offsetByCodePoints(this, index, codePointOffset); 994 } 995 996 /** 997 * Copy characters from this string into dst starting at dstBegin. 998 * This method doesn't perform any range checking. 999 */ getChars(char dst[], int dstBegin)1000 void getChars(char dst[], int dstBegin) { 1001 // Android-changed: Replace arraycopy with native call since chars are managed by runtime. 1002 // System.arraycopy(value, 0, dst, dstBegin, value.length); 1003 getCharsNoCheck(0, length(), dst, dstBegin); 1004 } 1005 1006 /** 1007 * Copies characters from this string into the destination character 1008 * array. 1009 * <p> 1010 * The first character to be copied is at index {@code srcBegin}; 1011 * the last character to be copied is at index {@code srcEnd-1} 1012 * (thus the total number of characters to be copied is 1013 * {@code srcEnd-srcBegin}). The characters are copied into the 1014 * subarray of {@code dst} starting at index {@code dstBegin} 1015 * and ending at index: 1016 * <blockquote><pre> 1017 * dstBegin + (srcEnd-srcBegin) - 1 1018 * </pre></blockquote> 1019 * 1020 * @param srcBegin index of the first character in the string 1021 * to copy. 1022 * @param srcEnd index after the last character in the string 1023 * to copy. 1024 * @param dst the destination array. 1025 * @param dstBegin the start offset in the destination array. 1026 * @exception IndexOutOfBoundsException If any of the following 1027 * is true: 1028 * <ul><li>{@code srcBegin} is negative. 1029 * <li>{@code srcBegin} is greater than {@code srcEnd} 1030 * <li>{@code srcEnd} is greater than the length of this 1031 * string 1032 * <li>{@code dstBegin} is negative 1033 * <li>{@code dstBegin+(srcEnd-srcBegin)} is larger than 1034 * {@code dst.length}</ul> 1035 */ getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)1036 public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) { 1037 // BEGIN Android-added: Null pointer check. 1038 if (dst == null) { 1039 throw new NullPointerException("dst == null"); 1040 } 1041 // END Android-added: Null pointer check. 1042 checkBoundsBeginEnd(srcBegin, srcEnd, length()); 1043 // BEGIN Android-changed: Implement in terms of length() and native getCharsNoCheck method. 1044 /* 1045 checkBoundsOffCount(dstBegin, srcEnd - srcBegin, dst.length); 1046 if (isLatin1()) { 1047 StringLatin1.getChars(value, srcBegin, srcEnd, dst, dstBegin); 1048 } else { 1049 StringUTF16.getChars(value, srcBegin, srcEnd, dst, dstBegin); 1050 } 1051 */ 1052 if (dstBegin < 0) { 1053 throw new ArrayIndexOutOfBoundsException("dstBegin < 0. dstBegin=" + dstBegin); 1054 } 1055 // dstBegin can be equal to dst.length, but only in the case where zero chars are to be 1056 // copied. 1057 if (dstBegin > dst.length) { 1058 throw new ArrayIndexOutOfBoundsException( 1059 "dstBegin > dst.length. dstBegin=" + dstBegin + ", dst.length=" + dst.length); 1060 } 1061 1062 int n = srcEnd - srcBegin; 1063 if (n > dst.length - dstBegin) { 1064 throw new ArrayIndexOutOfBoundsException( 1065 "n > dst.length - dstBegin. n=" + n + ", dst.length=" + dst.length 1066 + "dstBegin=" + dstBegin); 1067 } 1068 1069 getCharsNoCheck(srcBegin, srcEnd, dst, dstBegin); 1070 // END Android-changed: Implement in terms of length() and native getCharsNoCheck method. 1071 } 1072 1073 // BEGIN Android-added: Native method to access char storage managed by runtime. 1074 /** 1075 * getChars without bounds checks, for use by other classes 1076 * within the java.lang package only. The caller is responsible for 1077 * ensuring that start >= 0 && start <= end && end <= count. 1078 */ 1079 @FastNative getCharsNoCheck(int start, int end, char[] buffer, int index)1080 native void getCharsNoCheck(int start, int end, char[] buffer, int index); 1081 // END Android-added: Native method to access char storage managed by runtime. 1082 1083 /** 1084 * Copies characters from this string into the destination byte array. Each 1085 * byte receives the 8 low-order bits of the corresponding character. The 1086 * eight high-order bits of each character are not copied and do not 1087 * participate in the transfer in any way. 1088 * 1089 * <p> The first character to be copied is at index {@code srcBegin}; the 1090 * last character to be copied is at index {@code srcEnd-1}. The total 1091 * number of characters to be copied is {@code srcEnd-srcBegin}. The 1092 * characters, converted to bytes, are copied into the subarray of {@code 1093 * dst} starting at index {@code dstBegin} and ending at index: 1094 * 1095 * <blockquote><pre> 1096 * dstBegin + (srcEnd-srcBegin) - 1 1097 * </pre></blockquote> 1098 * 1099 * @deprecated This method does not properly convert characters into 1100 * bytes. As of JDK 1.1, the preferred way to do this is via the 1101 * {@link #getBytes()} method, which uses the platform's default charset. 1102 * 1103 * @param srcBegin 1104 * Index of the first character in the string to copy 1105 * 1106 * @param srcEnd 1107 * Index after the last character in the string to copy 1108 * 1109 * @param dst 1110 * The destination array 1111 * 1112 * @param dstBegin 1113 * The start offset in the destination array 1114 * 1115 * @throws IndexOutOfBoundsException 1116 * If any of the following is true: 1117 * <ul> 1118 * <li> {@code srcBegin} is negative 1119 * <li> {@code srcBegin} is greater than {@code srcEnd} 1120 * <li> {@code srcEnd} is greater than the length of this String 1121 * <li> {@code dstBegin} is negative 1122 * <li> {@code dstBegin+(srcEnd-srcBegin)} is larger than {@code 1123 * dst.length} 1124 * </ul> 1125 */ 1126 @Deprecated(since="1.1") getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin)1127 public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) { 1128 checkBoundsBeginEnd(srcBegin, srcEnd, length()); 1129 Objects.requireNonNull(dst); 1130 checkBoundsOffCount(dstBegin, srcEnd - srcBegin, dst.length); 1131 // BEGIN Android-changed: Implement in terms of charAt(). 1132 /* 1133 if (isLatin1()) { 1134 StringLatin1.getBytes(value, srcBegin, srcEnd, dst, dstBegin); 1135 } else { 1136 StringUTF16.getBytes(value, srcBegin, srcEnd, dst, dstBegin); 1137 } 1138 */ 1139 int j = dstBegin; 1140 int n = srcEnd; 1141 int i = srcBegin; 1142 1143 while (i < n) { 1144 dst[j++] = (byte)charAt(i++); 1145 } 1146 // END Android-changed: Implement in terms of charAt(). 1147 } 1148 1149 /** 1150 * Encodes this {@code String} into a sequence of bytes using the named 1151 * charset, storing the result into a new byte array. 1152 * 1153 * <p> The behavior of this method when this string cannot be encoded in 1154 * the given charset is unspecified. The {@link 1155 * java.nio.charset.CharsetEncoder} class should be used when more control 1156 * over the encoding process is required. 1157 * 1158 * @param charsetName 1159 * The name of a supported {@linkplain java.nio.charset.Charset 1160 * charset} 1161 * 1162 * @return The resultant byte array 1163 * 1164 * @throws UnsupportedEncodingException 1165 * If the named charset is not supported 1166 * 1167 * @since 1.1 1168 */ getBytes(String charsetName)1169 public byte[] getBytes(String charsetName) 1170 throws UnsupportedEncodingException { 1171 if (charsetName == null) throw new NullPointerException(); 1172 // BEGIN Android-changed: Skip StringCoding optimization that needs access to java chars. 1173 /* 1174 return StringCoding.encode(charsetName, coder(), value); 1175 */ 1176 return getBytes(Charset.forNameUEE(charsetName)); 1177 // END Android-changed: Skip StringCoding optimization that needs access to java chars. 1178 } 1179 1180 /** 1181 * Encodes this {@code String} into a sequence of bytes using the given 1182 * {@linkplain java.nio.charset.Charset charset}, storing the result into a 1183 * new byte array. 1184 * 1185 * <p> This method always replaces malformed-input and unmappable-character 1186 * sequences with this charset's default replacement byte array. The 1187 * {@link java.nio.charset.CharsetEncoder} class should be used when more 1188 * control over the encoding process is required. 1189 * 1190 * @param charset 1191 * The {@linkplain java.nio.charset.Charset} to be used to encode 1192 * the {@code String} 1193 * 1194 * @return The resultant byte array 1195 * 1196 * @since 1.6 1197 */ getBytes(Charset charset)1198 public byte[] getBytes(Charset charset) { 1199 if (charset == null) throw new NullPointerException(); 1200 // BEGIN Android-changed: Skip StringCoding optimization that needs access to java chars. 1201 /* 1202 return StringCoding.encode(charset, coder(), value); 1203 */ 1204 final int len = length(); 1205 final String name = charset.name(); 1206 if ("UTF-8".equals(name)) { 1207 return CharsetUtils.toUtf8Bytes(this, 0, len); 1208 } else if ("ISO-8859-1".equals(name)) { 1209 return CharsetUtils.toIsoLatin1Bytes(this, 0, len); 1210 } else if ("US-ASCII".equals(name)) { 1211 return CharsetUtils.toAsciiBytes(this, 0, len); 1212 } else if ("UTF-16BE".equals(name)) { 1213 return CharsetUtils.toBigEndianUtf16Bytes(this, 0, len); 1214 } 1215 1216 ByteBuffer buffer = charset.encode(this); 1217 byte[] bytes = new byte[buffer.limit()]; 1218 buffer.get(bytes); 1219 return bytes; 1220 // END Android-changed: Skip StringCoding optimization that needs access to java chars. 1221 } 1222 1223 /** 1224 * Encodes this {@code String} into a sequence of bytes using the 1225 * platform's default charset, storing the result into a new byte array. 1226 * 1227 * <p> The behavior of this method when this string cannot be encoded in 1228 * the default charset is unspecified. The {@link 1229 * java.nio.charset.CharsetEncoder} class should be used when more control 1230 * over the encoding process is required. 1231 * 1232 * @return The resultant byte array 1233 * 1234 * @since 1.1 1235 */ getBytes()1236 public byte[] getBytes() { 1237 // BEGIN Android-changed: Skip StringCoding optimization that needs access to java chars. 1238 /* 1239 return StringCoding.encode(coder(), value); 1240 */ 1241 return getBytes(Charset.defaultCharset()); 1242 // END Android-changed: Skip StringCoding optimization that needs access to java chars. 1243 } 1244 1245 /** 1246 * Compares this string to the specified object. The result is {@code 1247 * true} if and only if the argument is not {@code null} and is a {@code 1248 * String} object that represents the same sequence of characters as this 1249 * object. 1250 * 1251 * <p>For finer-grained String comparison, refer to 1252 * {@link java.text.Collator}. 1253 * 1254 * @param anObject 1255 * The object to compare this {@code String} against 1256 * 1257 * @return {@code true} if the given object represents a {@code String} 1258 * equivalent to this string, {@code false} otherwise 1259 * 1260 * @see #compareTo(String) 1261 * @see #equalsIgnoreCase(String) 1262 */ equals(Object anObject)1263 public boolean equals(Object anObject) { 1264 if (this == anObject) { 1265 return true; 1266 } 1267 if (anObject instanceof String) { 1268 // BEGIN Android-changed: Implement in terms of charAt(). 1269 /* 1270 String aString = (String)anObject; 1271 if (coder() == aString.coder()) { 1272 return isLatin1() ? StringLatin1.equals(value, aString.value) 1273 : StringUTF16.equals(value, aString.value); 1274 } 1275 */ 1276 String anotherString = (String)anObject; 1277 int n = length(); 1278 if (n == anotherString.length()) { 1279 int i = 0; 1280 while (n-- != 0) { 1281 if (charAt(i) != anotherString.charAt(i)) 1282 return false; 1283 i++; 1284 } 1285 return true; 1286 } 1287 // END Android-changed: Implement in terms of charAt(). 1288 } 1289 return false; 1290 } 1291 1292 /** 1293 * Compares this string to the specified {@code StringBuffer}. The result 1294 * is {@code true} if and only if this {@code String} represents the same 1295 * sequence of characters as the specified {@code StringBuffer}. This method 1296 * synchronizes on the {@code StringBuffer}. 1297 * 1298 * <p>For finer-grained String comparison, refer to 1299 * {@link java.text.Collator}. 1300 * 1301 * @param sb 1302 * The {@code StringBuffer} to compare this {@code String} against 1303 * 1304 * @return {@code true} if this {@code String} represents the same 1305 * sequence of characters as the specified {@code StringBuffer}, 1306 * {@code false} otherwise 1307 * 1308 * @since 1.4 1309 */ contentEquals(StringBuffer sb)1310 public boolean contentEquals(StringBuffer sb) { 1311 return contentEquals((CharSequence)sb); 1312 } 1313 nonSyncContentEquals(AbstractStringBuilder sb)1314 private boolean nonSyncContentEquals(AbstractStringBuilder sb) { 1315 int len = length(); 1316 if (len != sb.length()) { 1317 return false; 1318 } 1319 // BEGIN Android-changed: Implement in terms of charAt(). 1320 /* 1321 byte v1[] = value; 1322 byte v2[] = sb.getValue(); 1323 if (coder() == sb.getCoder()) { 1324 int n = v1.length; 1325 for (int i = 0; i < n; i++) { 1326 if (v1[i] != v2[i]) { 1327 return false; 1328 } 1329 } 1330 } else { 1331 if (!isLatin1()) { // utf16 str and latin1 abs can never be "equal" 1332 return false; 1333 } 1334 return StringUTF16.contentEquals(v1, v2, len); 1335 } 1336 */ 1337 for (int i = 0; i < len; i++) { 1338 if (charAt(i) != sb.charAt(i)) { 1339 return false; 1340 } 1341 } 1342 // END Android-changed: Implement in terms of charAt(). 1343 return true; 1344 } 1345 1346 /** 1347 * Compares this string to the specified {@code CharSequence}. The 1348 * result is {@code true} if and only if this {@code String} represents the 1349 * same sequence of char values as the specified sequence. Note that if the 1350 * {@code CharSequence} is a {@code StringBuffer} then the method 1351 * synchronizes on it. 1352 * 1353 * <p>For finer-grained String comparison, refer to 1354 * {@link java.text.Collator}. 1355 * 1356 * @param cs 1357 * The sequence to compare this {@code String} against 1358 * 1359 * @return {@code true} if this {@code String} represents the same 1360 * sequence of char values as the specified sequence, {@code 1361 * false} otherwise 1362 * 1363 * @since 1.5 1364 */ contentEquals(CharSequence cs)1365 public boolean contentEquals(CharSequence cs) { 1366 // Argument is a StringBuffer, StringBuilder 1367 if (cs instanceof AbstractStringBuilder) { 1368 if (cs instanceof StringBuffer) { 1369 synchronized(cs) { 1370 return nonSyncContentEquals((AbstractStringBuilder)cs); 1371 } 1372 } else { 1373 return nonSyncContentEquals((AbstractStringBuilder)cs); 1374 } 1375 } 1376 // Argument is a String 1377 if (cs instanceof String) { 1378 return equals(cs); 1379 } 1380 // Argument is a generic CharSequence 1381 int n = cs.length(); 1382 if (n != length()) { 1383 return false; 1384 } 1385 // BEGIN Android-changed: Implement in terms of charAt(). 1386 /* 1387 byte[] val = this.value; 1388 if (isLatin1()) { 1389 for (int i = 0; i < n; i++) { 1390 if ((val[i] & 0xff) != cs.charAt(i)) { 1391 return false; 1392 } 1393 } 1394 } else { 1395 if (!StringUTF16.contentEquals(val, cs, n)) { 1396 */ 1397 for (int i = 0; i < n; i++) { 1398 if (charAt(i) != cs.charAt(i)) { 1399 // END Android-changed: Implement in terms of charAt(). 1400 return false; 1401 } 1402 } 1403 return true; 1404 } 1405 1406 /** 1407 * Compares this {@code String} to another {@code String}, ignoring case 1408 * considerations. Two strings are considered equal ignoring case if they 1409 * are of the same length and corresponding characters in the two strings 1410 * are equal ignoring case. 1411 * 1412 * <p> Two characters {@code c1} and {@code c2} are considered the same 1413 * ignoring case if at least one of the following is true: 1414 * <ul> 1415 * <li> The two characters are the same (as compared by the 1416 * {@code ==} operator) 1417 * <li> Calling {@code Character.toLowerCase(Character.toUpperCase(char))} 1418 * on each character produces the same result 1419 * </ul> 1420 * 1421 * <p>Note that this method does <em>not</em> take locale into account, and 1422 * will result in unsatisfactory results for certain locales. The 1423 * {@link java.text.Collator} class provides locale-sensitive comparison. 1424 * 1425 * @param anotherString 1426 * The {@code String} to compare this {@code String} against 1427 * 1428 * @return {@code true} if the argument is not {@code null} and it 1429 * represents an equivalent {@code String} ignoring case; {@code 1430 * false} otherwise 1431 * 1432 * @see #equals(Object) 1433 */ equalsIgnoreCase(String anotherString)1434 public boolean equalsIgnoreCase(String anotherString) { 1435 // Android-added: Cache length() result so it's called once. 1436 final int len = length(); 1437 return (this == anotherString) ? true 1438 : (anotherString != null) 1439 && (anotherString.length() == len) 1440 && regionMatches(true, 0, anotherString, 0, len); 1441 } 1442 1443 /** 1444 * Compares two strings lexicographically. 1445 * The comparison is based on the Unicode value of each character in 1446 * the strings. The character sequence represented by this 1447 * {@code String} object is compared lexicographically to the 1448 * character sequence represented by the argument string. The result is 1449 * a negative integer if this {@code String} object 1450 * lexicographically precedes the argument string. The result is a 1451 * positive integer if this {@code String} object lexicographically 1452 * follows the argument string. The result is zero if the strings 1453 * are equal; {@code compareTo} returns {@code 0} exactly when 1454 * the {@link #equals(Object)} method would return {@code true}. 1455 * <p> 1456 * This is the definition of lexicographic ordering. If two strings are 1457 * different, then either they have different characters at some index 1458 * that is a valid index for both strings, or their lengths are different, 1459 * or both. If they have different characters at one or more index 1460 * positions, let <i>k</i> be the smallest such index; then the string 1461 * whose character at position <i>k</i> has the smaller value, as 1462 * determined by using the {@code <} operator, lexicographically precedes the 1463 * other string. In this case, {@code compareTo} returns the 1464 * difference of the two character values at position {@code k} in 1465 * the two string -- that is, the value: 1466 * <blockquote><pre> 1467 * this.charAt(k)-anotherString.charAt(k) 1468 * </pre></blockquote> 1469 * If there is no index position at which they differ, then the shorter 1470 * string lexicographically precedes the longer string. In this case, 1471 * {@code compareTo} returns the difference of the lengths of the 1472 * strings -- that is, the value: 1473 * <blockquote><pre> 1474 * this.length()-anotherString.length() 1475 * </pre></blockquote> 1476 * 1477 * <p>For finer-grained String comparison, refer to 1478 * {@link java.text.Collator}. 1479 * 1480 * @param anotherString the {@code String} to be compared. 1481 * @return the value {@code 0} if the argument string is equal to 1482 * this string; a value less than {@code 0} if this string 1483 * is lexicographically less than the string argument; and a 1484 * value greater than {@code 0} if this string is 1485 * lexicographically greater than the string argument. 1486 */ 1487 // BEGIN Android-changed: Replace with implementation in runtime to access chars (see above). 1488 /* 1489 public int compareTo(String anotherString) { 1490 byte v1[] = value; 1491 byte v2[] = anotherString.value; 1492 if (coder() == anotherString.coder()) { 1493 return isLatin1() ? StringLatin1.compareTo(v1, v2) 1494 : StringUTF16.compareTo(v1, v2); 1495 } 1496 return isLatin1() ? StringLatin1.compareToUTF16(v1, v2) 1497 : StringUTF16.compareToLatin1(v1, v2); 1498 } 1499 */ 1500 @FastNative compareTo(String anotherString)1501 public native int compareTo(String anotherString); 1502 // END Android-changed: Replace with implementation in runtime to access chars (see above). 1503 1504 /** 1505 * A Comparator that orders {@code String} objects as by 1506 * {@code compareToIgnoreCase}. This comparator is serializable. 1507 * <p> 1508 * Note that this Comparator does <em>not</em> take locale into account, 1509 * and will result in an unsatisfactory ordering for certain locales. 1510 * The {@link java.text.Collator} class provides locale-sensitive comparison. 1511 * 1512 * @see java.text.Collator 1513 * @since 1.2 1514 */ 1515 public static final Comparator<String> CASE_INSENSITIVE_ORDER 1516 = new CaseInsensitiveComparator(); 1517 private static class CaseInsensitiveComparator 1518 implements Comparator<String>, java.io.Serializable { 1519 // use serialVersionUID from JDK 1.2.2 for interoperability 1520 private static final long serialVersionUID = 8575799808933029326L; 1521 compare(String s1, String s2)1522 public int compare(String s1, String s2) { 1523 // BEGIN Android-changed: Implement in terms of charAt(). 1524 /* 1525 byte v1[] = s1.value; 1526 byte v2[] = s2.value; 1527 if (s1.coder() == s2.coder()) { 1528 return s1.isLatin1() ? StringLatin1.compareToCI(v1, v2) 1529 : StringUTF16.compareToCI(v1, v2); 1530 } 1531 return s1.isLatin1() ? StringLatin1.compareToCI_UTF16(v1, v2) 1532 : StringUTF16.compareToCI_Latin1(v1, v2); 1533 */ 1534 int n1 = s1.length(); 1535 int n2 = s2.length(); 1536 int min = Math.min(n1, n2); 1537 for (int i = 0; i < min; i++) { 1538 char c1 = s1.charAt(i); 1539 char c2 = s2.charAt(i); 1540 if (c1 != c2) { 1541 c1 = Character.toUpperCase(c1); 1542 c2 = Character.toUpperCase(c2); 1543 if (c1 != c2) { 1544 c1 = Character.toLowerCase(c1); 1545 c2 = Character.toLowerCase(c2); 1546 if (c1 != c2) { 1547 // No overflow because of numeric promotion 1548 return c1 - c2; 1549 } 1550 } 1551 } 1552 } 1553 return n1 - n2; 1554 // END Android-changed: Implement in terms of charAt(). 1555 } 1556 1557 /** Replaces the de-serialized object. */ readResolve()1558 private Object readResolve() { return CASE_INSENSITIVE_ORDER; } 1559 } 1560 1561 /** 1562 * Compares two strings lexicographically, ignoring case 1563 * differences. This method returns an integer whose sign is that of 1564 * calling {@code compareTo} with normalized versions of the strings 1565 * where case differences have been eliminated by calling 1566 * {@code Character.toLowerCase(Character.toUpperCase(character))} on 1567 * each character. 1568 * <p> 1569 * Note that this method does <em>not</em> take locale into account, 1570 * and will result in an unsatisfactory ordering for certain locales. 1571 * The {@link java.text.Collator} class provides locale-sensitive comparison. 1572 * 1573 * @param str the {@code String} to be compared. 1574 * @return a negative integer, zero, or a positive integer as the 1575 * specified String is greater than, equal to, or less 1576 * than this String, ignoring case considerations. 1577 * @see java.text.Collator 1578 * @since 1.2 1579 */ compareToIgnoreCase(String str)1580 public int compareToIgnoreCase(String str) { 1581 return CASE_INSENSITIVE_ORDER.compare(this, str); 1582 } 1583 1584 /** 1585 * Tests if two string regions are equal. 1586 * <p> 1587 * A substring of this {@code String} object is compared to a substring 1588 * of the argument other. The result is true if these substrings 1589 * represent identical character sequences. The substring of this 1590 * {@code String} object to be compared begins at index {@code toffset} 1591 * and has length {@code len}. The substring of other to be compared 1592 * begins at index {@code ooffset} and has length {@code len}. The 1593 * result is {@code false} if and only if at least one of the following 1594 * is true: 1595 * <ul><li>{@code toffset} is negative. 1596 * <li>{@code ooffset} is negative. 1597 * <li>{@code toffset+len} is greater than the length of this 1598 * {@code String} object. 1599 * <li>{@code ooffset+len} is greater than the length of the other 1600 * argument. 1601 * <li>There is some nonnegative integer <i>k</i> less than {@code len} 1602 * such that: 1603 * {@code this.charAt(toffset + }<i>k</i>{@code ) != other.charAt(ooffset + } 1604 * <i>k</i>{@code )} 1605 * </ul> 1606 * 1607 * <p>Note that this method does <em>not</em> take locale into account. The 1608 * {@link java.text.Collator} class provides locale-sensitive comparison. 1609 * 1610 * @param toffset the starting offset of the subregion in this string. 1611 * @param other the string argument. 1612 * @param ooffset the starting offset of the subregion in the string 1613 * argument. 1614 * @param len the number of characters to compare. 1615 * @return {@code true} if the specified subregion of this string 1616 * exactly matches the specified subregion of the string argument; 1617 * {@code false} otherwise. 1618 */ regionMatches(int toffset, String other, int ooffset, int len)1619 public boolean regionMatches(int toffset, String other, int ooffset, int len) { 1620 // BEGIN Android-removed: Implement in terms of charAt(). 1621 /* 1622 byte tv[] = value; 1623 byte ov[] = other.value; 1624 */ 1625 // Note: toffset, ooffset, or len might be near -1>>>1. 1626 if ((ooffset < 0) || (toffset < 0) || 1627 (toffset > (long)length() - len) || 1628 (ooffset > (long)other.length() - len)) { 1629 return false; 1630 } 1631 // BEGIN Android-removed: Implement in terms of charAt(). 1632 /* 1633 if (coder() == other.coder()) { 1634 if (!isLatin1() && (len > 0)) { 1635 toffset = toffset << 1; 1636 ooffset = ooffset << 1; 1637 len = len << 1; 1638 } 1639 while (len-- > 0) { 1640 if (tv[toffset++] != ov[ooffset++]) { 1641 return false; 1642 } 1643 } 1644 } else { 1645 if (coder() == LATIN1) { 1646 while (len-- > 0) { 1647 if (StringLatin1.getChar(tv, toffset++) != 1648 StringUTF16.getChar(ov, ooffset++)) { 1649 return false; 1650 } 1651 } 1652 } else { 1653 while (len-- > 0) { 1654 if (StringUTF16.getChar(tv, toffset++) != 1655 StringLatin1.getChar(ov, ooffset++)) { 1656 return false; 1657 } 1658 } 1659 */ 1660 while (len-- > 0) { 1661 if (charAt(toffset++) != other.charAt(ooffset++)) { 1662 return false; 1663 // END Android-removed: Implement in terms of charAt(). 1664 } 1665 } 1666 return true; 1667 } 1668 1669 /** 1670 * Tests if two string regions are equal. 1671 * <p> 1672 * A substring of this {@code String} object is compared to a substring 1673 * of the argument {@code other}. The result is {@code true} if these 1674 * substrings represent character sequences that are the same, ignoring 1675 * case if and only if {@code ignoreCase} is true. The substring of 1676 * this {@code String} object to be compared begins at index 1677 * {@code toffset} and has length {@code len}. The substring of 1678 * {@code other} to be compared begins at index {@code ooffset} and 1679 * has length {@code len}. The result is {@code false} if and only if 1680 * at least one of the following is true: 1681 * <ul><li>{@code toffset} is negative. 1682 * <li>{@code ooffset} is negative. 1683 * <li>{@code toffset+len} is greater than the length of this 1684 * {@code String} object. 1685 * <li>{@code ooffset+len} is greater than the length of the other 1686 * argument. 1687 * <li>{@code ignoreCase} is {@code false} and there is some nonnegative 1688 * integer <i>k</i> less than {@code len} such that: 1689 * <blockquote><pre> 1690 * this.charAt(toffset+k) != other.charAt(ooffset+k) 1691 * </pre></blockquote> 1692 * <li>{@code ignoreCase} is {@code true} and there is some nonnegative 1693 * integer <i>k</i> less than {@code len} such that: 1694 * <blockquote><pre> 1695 * Character.toLowerCase(Character.toUpperCase(this.charAt(toffset+k))) != 1696 Character.toLowerCase(Character.toUpperCase(other.charAt(ooffset+k))) 1697 * </pre></blockquote> 1698 * </ul> 1699 * 1700 * <p>Note that this method does <em>not</em> take locale into account, 1701 * and will result in unsatisfactory results for certain locales when 1702 * {@code ignoreCase} is {@code true}. The {@link java.text.Collator} class 1703 * provides locale-sensitive comparison. 1704 * 1705 * @param ignoreCase if {@code true}, ignore case when comparing 1706 * characters. 1707 * @param toffset the starting offset of the subregion in this 1708 * string. 1709 * @param other the string argument. 1710 * @param ooffset the starting offset of the subregion in the string 1711 * argument. 1712 * @param len the number of characters to compare. 1713 * @return {@code true} if the specified subregion of this string 1714 * matches the specified subregion of the string argument; 1715 * {@code false} otherwise. Whether the matching is exact 1716 * or case insensitive depends on the {@code ignoreCase} 1717 * argument. 1718 */ regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)1719 public boolean regionMatches(boolean ignoreCase, int toffset, 1720 String other, int ooffset, int len) { 1721 if (!ignoreCase) { 1722 return regionMatches(toffset, other, ooffset, len); 1723 } 1724 // Note: toffset, ooffset, or len might be near -1>>>1. 1725 if ((ooffset < 0) || (toffset < 0) 1726 || (toffset > (long)length() - len) 1727 || (ooffset > (long)other.length() - len)) { 1728 return false; 1729 } 1730 // BEGIN Android-changed: Implement in terms of charAt(). 1731 /* 1732 byte tv[] = value; 1733 byte ov[] = other.value; 1734 if (coder() == other.coder()) { 1735 return isLatin1() 1736 ? StringLatin1.regionMatchesCI(tv, toffset, ov, ooffset, len) 1737 : StringUTF16.regionMatchesCI(tv, toffset, ov, ooffset, len); 1738 } 1739 return isLatin1() 1740 ? StringLatin1.regionMatchesCI_UTF16(tv, toffset, ov, ooffset, len) 1741 : StringUTF16.regionMatchesCI_Latin1(tv, toffset, ov, ooffset, len); 1742 */ 1743 while (len-- > 0) { 1744 char c1 = charAt(toffset++); 1745 char c2 = other.charAt(ooffset++); 1746 if (c1 == c2) { 1747 continue; 1748 } 1749 if (ignoreCase) { 1750 // If characters don't match but case may be ignored, 1751 // try converting both characters to uppercase. 1752 // If the results match, then the comparison scan should 1753 // continue. 1754 char u1 = Character.toUpperCase(c1); 1755 char u2 = Character.toUpperCase(c2); 1756 if (u1 == u2) { 1757 continue; 1758 } 1759 // Unfortunately, conversion to uppercase does not work properly 1760 // for the Georgian alphabet, which has strange rules about case 1761 // conversion. So we need to make one last check before 1762 // exiting. 1763 if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) { 1764 continue; 1765 } 1766 } 1767 return false; 1768 } 1769 return true; 1770 // END Android-changed: Implement in terms of charAt(). 1771 } 1772 1773 /** 1774 * Tests if the substring of this string beginning at the 1775 * specified index starts with the specified prefix. 1776 * 1777 * @param prefix the prefix. 1778 * @param toffset where to begin looking in this string. 1779 * @return {@code true} if the character sequence represented by the 1780 * argument is a prefix of the substring of this object starting 1781 * at index {@code toffset}; {@code false} otherwise. 1782 * The result is {@code false} if {@code toffset} is 1783 * negative or greater than the length of this 1784 * {@code String} object; otherwise the result is the same 1785 * as the result of the expression 1786 * <pre> 1787 * this.substring(toffset).startsWith(prefix) 1788 * </pre> 1789 */ startsWith(String prefix, int toffset)1790 public boolean startsWith(String prefix, int toffset) { 1791 // Android-added: Cache length() result so it's called once. 1792 int pc = prefix.length(); 1793 // Note: toffset might be near -1>>>1. 1794 if (toffset < 0 || toffset > length() - pc) { 1795 return false; 1796 } 1797 // BEGIN Android-changed: Implement in terms of charAt(). 1798 /* 1799 byte ta[] = value; 1800 byte pa[] = prefix.value; 1801 int po = 0; 1802 int pc = pa.length; 1803 if (coder() == prefix.coder()) { 1804 int to = isLatin1() ? toffset : toffset << 1; 1805 while (po < pc) { 1806 if (ta[to++] != pa[po++]) { 1807 return false; 1808 } 1809 } 1810 } else { 1811 if (isLatin1()) { // && pcoder == UTF16 1812 return false; 1813 } 1814 // coder == UTF16 && pcoder == LATIN1) 1815 while (po < pc) { 1816 if (StringUTF16.getChar(ta, toffset++) != (pa[po++] & 0xff)) { 1817 return false; 1818 } 1819 } 1820 */ 1821 int po = 0; 1822 while (--pc >= 0) { 1823 if (charAt(toffset++) != prefix.charAt(po++)) { 1824 return false; 1825 } 1826 // END Android-changed: Implement in terms of charAt(). 1827 } 1828 return true; 1829 } 1830 1831 /** 1832 * Tests if this string starts with the specified prefix. 1833 * 1834 * @param prefix the prefix. 1835 * @return {@code true} if the character sequence represented by the 1836 * argument is a prefix of the character sequence represented by 1837 * this string; {@code false} otherwise. 1838 * Note also that {@code true} will be returned if the 1839 * argument is an empty string or is equal to this 1840 * {@code String} object as determined by the 1841 * {@link #equals(Object)} method. 1842 * @since 1.0 1843 */ startsWith(String prefix)1844 public boolean startsWith(String prefix) { 1845 return startsWith(prefix, 0); 1846 } 1847 1848 /** 1849 * Tests if this string ends with the specified suffix. 1850 * 1851 * @param suffix the suffix. 1852 * @return {@code true} if the character sequence represented by the 1853 * argument is a suffix of the character sequence represented by 1854 * this object; {@code false} otherwise. Note that the 1855 * result will be {@code true} if the argument is the 1856 * empty string or is equal to this {@code String} object 1857 * as determined by the {@link #equals(Object)} method. 1858 */ endsWith(String suffix)1859 public boolean endsWith(String suffix) { 1860 return startsWith(suffix, length() - suffix.length()); 1861 } 1862 1863 /** 1864 * Returns a hash code for this string. The hash code for a 1865 * {@code String} object is computed as 1866 * <blockquote><pre> 1867 * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] 1868 * </pre></blockquote> 1869 * using {@code int} arithmetic, where {@code s[i]} is the 1870 * <i>i</i>th character of the string, {@code n} is the length of 1871 * the string, and {@code ^} indicates exponentiation. 1872 * (The hash value of the empty string is zero.) 1873 * 1874 * @return a hash code value for this object. 1875 */ hashCode()1876 public int hashCode() { 1877 int h = hash; 1878 // BEGIN Android-changed: Implement in terms of charAt(). 1879 /* 1880 if (h == 0 && value.length > 0) { 1881 hash = h = isLatin1() ? StringLatin1.hashCode(value) 1882 : StringUTF16.hashCode(value); 1883 */ 1884 final int len = length(); 1885 if (h == 0 && len > 0) { 1886 for (int i = 0; i < len; i++) { 1887 h = 31 * h + charAt(i); 1888 } 1889 hash = h; 1890 // END Android-changed: Implement in terms of charAt(). 1891 } 1892 return h; 1893 } 1894 1895 /** 1896 * Returns the index within this string of the first occurrence of 1897 * the specified character. If a character with value 1898 * {@code ch} occurs in the character sequence represented by 1899 * this {@code String} object, then the index (in Unicode 1900 * code units) of the first such occurrence is returned. For 1901 * values of {@code ch} in the range from 0 to 0xFFFF 1902 * (inclusive), this is the smallest value <i>k</i> such that: 1903 * <blockquote><pre> 1904 * this.charAt(<i>k</i>) == ch 1905 * </pre></blockquote> 1906 * is true. For other values of {@code ch}, it is the 1907 * smallest value <i>k</i> such that: 1908 * <blockquote><pre> 1909 * this.codePointAt(<i>k</i>) == ch 1910 * </pre></blockquote> 1911 * is true. In either case, if no such character occurs in this 1912 * string, then {@code -1} is returned. 1913 * 1914 * @param ch a character (Unicode code point). 1915 * @return the index of the first occurrence of the character in the 1916 * character sequence represented by this object, or 1917 * {@code -1} if the character does not occur. 1918 */ indexOf(int ch)1919 public int indexOf(int ch) { 1920 return indexOf(ch, 0); 1921 } 1922 1923 /** 1924 * Returns the index within this string of the first occurrence of the 1925 * specified character, starting the search at the specified index. 1926 * <p> 1927 * If a character with value {@code ch} occurs in the 1928 * character sequence represented by this {@code String} 1929 * object at an index no smaller than {@code fromIndex}, then 1930 * the index of the first such occurrence is returned. For values 1931 * of {@code ch} in the range from 0 to 0xFFFF (inclusive), 1932 * this is the smallest value <i>k</i> such that: 1933 * <blockquote><pre> 1934 * (this.charAt(<i>k</i>) == ch) {@code &&} (<i>k</i> >= fromIndex) 1935 * </pre></blockquote> 1936 * is true. For other values of {@code ch}, it is the 1937 * smallest value <i>k</i> such that: 1938 * <blockquote><pre> 1939 * (this.codePointAt(<i>k</i>) == ch) {@code &&} (<i>k</i> >= fromIndex) 1940 * </pre></blockquote> 1941 * is true. In either case, if no such character occurs in this 1942 * string at or after position {@code fromIndex}, then 1943 * {@code -1} is returned. 1944 * 1945 * <p> 1946 * There is no restriction on the value of {@code fromIndex}. If it 1947 * is negative, it has the same effect as if it were zero: this entire 1948 * string may be searched. If it is greater than the length of this 1949 * string, it has the same effect as if it were equal to the length of 1950 * this string: {@code -1} is returned. 1951 * 1952 * <p>All indices are specified in {@code char} values 1953 * (Unicode code units). 1954 * 1955 * @param ch a character (Unicode code point). 1956 * @param fromIndex the index to start the search from. 1957 * @return the index of the first occurrence of the character in the 1958 * character sequence represented by this object that is greater 1959 * than or equal to {@code fromIndex}, or {@code -1} 1960 * if the character does not occur. 1961 */ indexOf(int ch, int fromIndex)1962 public int indexOf(int ch, int fromIndex) { 1963 // BEGIN Android-changed: Implement in terms of charAt(). 1964 /* 1965 return isLatin1() ? StringLatin1.indexOf(value, ch, fromIndex) 1966 : StringUTF16.indexOf(value, ch, fromIndex); 1967 */ 1968 final int max = length(); 1969 if (fromIndex < 0) { 1970 fromIndex = 0; 1971 } else if (fromIndex >= max) { 1972 // Note: fromIndex might be near -1>>>1. 1973 return -1; 1974 } 1975 1976 if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) { 1977 // handle most cases here (ch is a BMP code point or a 1978 // negative value (invalid code point)) 1979 for (int i = fromIndex; i < max; i++) { 1980 if (charAt(i) == ch) { 1981 return i; 1982 } 1983 } 1984 return -1; 1985 } else { 1986 return indexOfSupplementary(ch, fromIndex); 1987 } 1988 } 1989 1990 /** 1991 * Handles (rare) calls of indexOf with a supplementary character. 1992 */ indexOfSupplementary(int ch, int fromIndex)1993 private int indexOfSupplementary(int ch, int fromIndex) { 1994 if (Character.isValidCodePoint(ch)) { 1995 final char hi = Character.highSurrogate(ch); 1996 final char lo = Character.lowSurrogate(ch); 1997 final int max = length() - 1; 1998 for (int i = fromIndex; i < max; i++) { 1999 if (charAt(i) == hi && charAt(i + 1) == lo) { 2000 return i; 2001 } 2002 } 2003 } 2004 return -1; 2005 // END Android-changed: Implement in terms of charAt(). 2006 } 2007 2008 /** 2009 * Returns the index within this string of the last occurrence of 2010 * the specified character. For values of {@code ch} in the 2011 * range from 0 to 0xFFFF (inclusive), the index (in Unicode code 2012 * units) returned is the largest value <i>k</i> such that: 2013 * <blockquote><pre> 2014 * this.charAt(<i>k</i>) == ch 2015 * </pre></blockquote> 2016 * is true. For other values of {@code ch}, it is the 2017 * largest value <i>k</i> such that: 2018 * <blockquote><pre> 2019 * this.codePointAt(<i>k</i>) == ch 2020 * </pre></blockquote> 2021 * is true. In either case, if no such character occurs in this 2022 * string, then {@code -1} is returned. The 2023 * {@code String} is searched backwards starting at the last 2024 * character. 2025 * 2026 * @param ch a character (Unicode code point). 2027 * @return the index of the last occurrence of the character in the 2028 * character sequence represented by this object, or 2029 * {@code -1} if the character does not occur. 2030 */ lastIndexOf(int ch)2031 public int lastIndexOf(int ch) { 2032 return lastIndexOf(ch, length() - 1); 2033 } 2034 2035 /** 2036 * Returns the index within this string of the last occurrence of 2037 * the specified character, searching backward starting at the 2038 * specified index. For values of {@code ch} in the range 2039 * from 0 to 0xFFFF (inclusive), the index returned is the largest 2040 * value <i>k</i> such that: 2041 * <blockquote><pre> 2042 * (this.charAt(<i>k</i>) == ch) {@code &&} (<i>k</i> <= fromIndex) 2043 * </pre></blockquote> 2044 * is true. For other values of {@code ch}, it is the 2045 * largest value <i>k</i> such that: 2046 * <blockquote><pre> 2047 * (this.codePointAt(<i>k</i>) == ch) {@code &&} (<i>k</i> <= fromIndex) 2048 * </pre></blockquote> 2049 * is true. In either case, if no such character occurs in this 2050 * string at or before position {@code fromIndex}, then 2051 * {@code -1} is returned. 2052 * 2053 * <p>All indices are specified in {@code char} values 2054 * (Unicode code units). 2055 * 2056 * @param ch a character (Unicode code point). 2057 * @param fromIndex the index to start the search from. There is no 2058 * restriction on the value of {@code fromIndex}. If it is 2059 * greater than or equal to the length of this string, it has 2060 * the same effect as if it were equal to one less than the 2061 * length of this string: this entire string may be searched. 2062 * If it is negative, it has the same effect as if it were -1: 2063 * -1 is returned. 2064 * @return the index of the last occurrence of the character in the 2065 * character sequence represented by this object that is less 2066 * than or equal to {@code fromIndex}, or {@code -1} 2067 * if the character does not occur before that point. 2068 */ lastIndexOf(int ch, int fromIndex)2069 public int lastIndexOf(int ch, int fromIndex) { 2070 // BEGIN Android-changed: Implement in terms of charAt(). 2071 /* 2072 return isLatin1() ? StringLatin1.lastIndexOf(value, ch, fromIndex) 2073 : StringUTF16.lastIndexOf(value, ch, fromIndex); 2074 */ 2075 if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) { 2076 // handle most cases here (ch is a BMP code point or a 2077 // negative value (invalid code point)) 2078 int i = Math.min(fromIndex, length() - 1); 2079 for (; i >= 0; i--) { 2080 if (charAt(i) == ch) { 2081 return i; 2082 } 2083 } 2084 return -1; 2085 } else { 2086 return lastIndexOfSupplementary(ch, fromIndex); 2087 } 2088 } 2089 2090 /** 2091 * Handles (rare) calls of lastIndexOf with a supplementary character. 2092 */ lastIndexOfSupplementary(int ch, int fromIndex)2093 private int lastIndexOfSupplementary(int ch, int fromIndex) { 2094 if (Character.isValidCodePoint(ch)) { 2095 char hi = Character.highSurrogate(ch); 2096 char lo = Character.lowSurrogate(ch); 2097 int i = Math.min(fromIndex, length() - 2); 2098 for (; i >= 0; i--) { 2099 if (charAt(i) == hi && charAt(i + 1) == lo) { 2100 return i; 2101 } 2102 } 2103 } 2104 return -1; 2105 // END Android-changed: Implement in terms of charAt(). 2106 } 2107 2108 /** 2109 * Returns the index within this string of the first occurrence of the 2110 * specified substring. 2111 * 2112 * <p>The returned index is the smallest value {@code k} for which: 2113 * <pre>{@code 2114 * this.startsWith(str, k) 2115 * }</pre> 2116 * If no such value of {@code k} exists, then {@code -1} is returned. 2117 * 2118 * @param str the substring to search for. 2119 * @return the index of the first occurrence of the specified substring, 2120 * or {@code -1} if there is no such occurrence. 2121 */ 2122 @NeverInline indexOf(String str)2123 public int indexOf(String str) { 2124 // BEGIN Android-changed: Implement with indexOf() method that takes String parameters. 2125 /* 2126 if (coder() == str.coder()) { 2127 return isLatin1() ? StringLatin1.indexOf(value, str.value) 2128 : StringUTF16.indexOf(value, str.value); 2129 } 2130 if (coder() == LATIN1) { // str.coder == UTF16 2131 return -1; 2132 } 2133 return StringUTF16.indexOfLatin1(value, str.value); 2134 */ 2135 return indexOf(str, 0); 2136 // END Android-changed: Implement with indexOf() method that takes String parameters. 2137 } 2138 2139 /** 2140 * Returns the index within this string of the first occurrence of the 2141 * specified substring, starting at the specified index. 2142 * 2143 * <p>The returned index is the smallest value {@code k} for which: 2144 * <pre>{@code 2145 * k >= Math.min(fromIndex, this.length()) && 2146 * this.startsWith(str, k) 2147 * }</pre> 2148 * If no such value of {@code k} exists, then {@code -1} is returned. 2149 * 2150 * @param str the substring to search for. 2151 * @param fromIndex the index from which to start the search. 2152 * @return the index of the first occurrence of the specified substring, 2153 * starting at the specified index, 2154 * or {@code -1} if there is no such occurrence. 2155 */ 2156 @NeverInline indexOf(String str, int fromIndex)2157 public int indexOf(String str, int fromIndex) { 2158 // BEGIN Android-changed: Implement with indexOf() method that takes String parameters. 2159 /* 2160 return indexOf(value, coder(), length(), str, fromIndex); 2161 */ 2162 return indexOf(this, str, fromIndex); 2163 // END Android-changed: Implement with indexOf() method that takes String parameters. 2164 } 2165 2166 // BEGIN Android-added: Private static indexOf method that takes String parameters. 2167 // The use of length(), charAt(), etc. makes it more efficient for compressed strings. 2168 /** 2169 * The source is the string being searched, and the target is the string being searched for. 2170 * 2171 * @param source the characters being searched. 2172 * @param target the characters being searched for. 2173 * @param fromIndex the index to begin searching from. 2174 */ indexOf(String source, String target, int fromIndex)2175 private static int indexOf(String source, String target, int fromIndex) { 2176 final int sourceLength = source.length(); 2177 final int targetLength = target.length(); 2178 if (fromIndex >= sourceLength) { 2179 return (targetLength == 0 ? sourceLength : -1); 2180 } 2181 if (fromIndex < 0) { 2182 fromIndex = 0; 2183 } 2184 if (targetLength == 0) { 2185 return fromIndex; 2186 } 2187 2188 char first = target.charAt(0); 2189 int max = (sourceLength - targetLength); 2190 2191 for (int i = fromIndex; i <= max; i++) { 2192 /* Look for first character. */ 2193 if (source.charAt(i)!= first) { 2194 while (++i <= max && source.charAt(i) != first); 2195 } 2196 2197 /* Found first character, now look at the rest of v2 */ 2198 if (i <= max) { 2199 int j = i + 1; 2200 int end = j + targetLength - 1; 2201 for (int k = 1; j < end && source.charAt(j) 2202 == target.charAt(k); j++, k++); 2203 2204 if (j == end) { 2205 /* Found whole string. */ 2206 return i; 2207 } 2208 } 2209 } 2210 return -1; 2211 } 2212 // END Android-added: Private static indexOf method that takes String parameters. 2213 2214 /** 2215 * Code shared by String and AbstractStringBuilder to do searches. The 2216 * source is the character array being searched, and the target 2217 * is the string being searched for. 2218 * 2219 * @param src the characters being searched. 2220 * @param srcCoder the coder of the source string. 2221 * @param srcCount length of the source string. 2222 * @param tgtStr the characters being searched for. 2223 * @param fromIndex the index to begin searching from. 2224 */ indexOf(byte[] src, byte srcCoder, int srcCount, String tgtStr, int fromIndex)2225 static int indexOf(byte[] src, byte srcCoder, int srcCount, 2226 String tgtStr, int fromIndex) { 2227 // byte[] tgt = tgtStr.value; 2228 byte tgtCoder = tgtStr.coder(); 2229 int tgtCount = tgtStr.length(); 2230 2231 if (fromIndex >= srcCount) { 2232 return (tgtCount == 0 ? srcCount : -1); 2233 } 2234 if (fromIndex < 0) { 2235 fromIndex = 0; 2236 } 2237 if (tgtCount == 0) { 2238 return fromIndex; 2239 } 2240 if (tgtCount > srcCount) { 2241 return -1; 2242 } 2243 if (srcCoder == tgtCoder) { 2244 return srcCoder == LATIN1 2245 // Android-changed: libcore doesn't store String as Latin1 or UTF16 byte[] field. 2246 // ? StringLatin1.indexOf(src, srcCount, tgt, tgtCount, fromIndex) 2247 // : StringUTF16.indexOf(src, srcCount, tgt, tgtCount, fromIndex); 2248 ? StringLatin1.indexOf(src, srcCount, tgtStr, tgtCount, fromIndex) 2249 : StringUTF16.indexOf(src, srcCount, tgtStr, tgtCount, fromIndex); 2250 } 2251 if (srcCoder == LATIN1) { // && tgtCoder == UTF16 2252 return -1; 2253 } 2254 // srcCoder == UTF16 && tgtCoder == LATIN1) { 2255 // return StringUTF16.indexOfLatin1(src, srcCount, tgt, tgtCount, fromIndex); 2256 return StringUTF16.indexOfLatin1(src, srcCount, tgtStr, tgtCount, fromIndex); 2257 } 2258 2259 /** 2260 * Returns the index within this string of the last occurrence of the 2261 * specified substring. The last occurrence of the empty string "" 2262 * is considered to occur at the index value {@code this.length()}. 2263 * 2264 * <p>The returned index is the largest value {@code k} for which: 2265 * <pre>{@code 2266 * this.startsWith(str, k) 2267 * }</pre> 2268 * If no such value of {@code k} exists, then {@code -1} is returned. 2269 * 2270 * @param str the substring to search for. 2271 * @return the index of the last occurrence of the specified substring, 2272 * or {@code -1} if there is no such occurrence. 2273 */ lastIndexOf(String str)2274 public int lastIndexOf(String str) { 2275 return lastIndexOf(str, length()); 2276 } 2277 2278 /** 2279 * Returns the index within this string of the last occurrence of the 2280 * specified substring, searching backward starting at the specified index. 2281 * 2282 * <p>The returned index is the largest value {@code k} for which: 2283 * <pre>{@code 2284 * k <= Math.min(fromIndex, this.length()) && 2285 * this.startsWith(str, k) 2286 * }</pre> 2287 * If no such value of {@code k} exists, then {@code -1} is returned. 2288 * 2289 * @param str the substring to search for. 2290 * @param fromIndex the index to start the search from. 2291 * @return the index of the last occurrence of the specified substring, 2292 * searching backward from the specified index, 2293 * or {@code -1} if there is no such occurrence. 2294 */ lastIndexOf(String str, int fromIndex)2295 public int lastIndexOf(String str, int fromIndex) { 2296 // BEGIN Android-changed: Implement with static lastIndexOf() that takes String parameters. 2297 /* 2298 return lastIndexOf(value, coder(), length(), str, fromIndex); 2299 */ 2300 return lastIndexOf(this, str, fromIndex); 2301 // END Android-changed: Implement with static lastIndexOf() that takes String parameters. 2302 } 2303 2304 // BEGIN Android-added: Private static lastIndexOf method that takes String parameters. 2305 // The use of length(), charAt(), etc. makes it more efficient for compressed strings. 2306 /** 2307 * The source is the string being searched, and the target is the string being searched for. 2308 * 2309 * @param source the characters being searched. 2310 * @param target the characters being searched for. 2311 * @param fromIndex the index to begin searching from. 2312 */ lastIndexOf(String source, String target, int fromIndex)2313 private static int lastIndexOf(String source, String target, int fromIndex) { 2314 /* 2315 * Check arguments; return immediately where possible. For 2316 * consistency, don't check for null str. 2317 */ 2318 final int sourceLength = source.length(); 2319 final int targetLength = target.length(); 2320 int rightIndex = sourceLength - targetLength; 2321 if (fromIndex < 0) { 2322 return -1; 2323 } 2324 if (fromIndex > rightIndex) { 2325 fromIndex = rightIndex; 2326 } 2327 /* Empty string always matches. */ 2328 if (targetLength == 0) { 2329 return fromIndex; 2330 } 2331 2332 int strLastIndex = targetLength - 1; 2333 char strLastChar = target.charAt(strLastIndex); 2334 int min = targetLength - 1; 2335 int i = min + fromIndex; 2336 2337 startSearchForLastChar: 2338 while (true) { 2339 while (i >= min && source.charAt(i) != strLastChar) { 2340 i--; 2341 } 2342 if (i < min) { 2343 return -1; 2344 } 2345 int j = i - 1; 2346 int start = j - (targetLength - 1); 2347 int k = strLastIndex - 1; 2348 2349 while (j > start) { 2350 if (source.charAt(j--) != target.charAt(k--)) { 2351 i--; 2352 continue startSearchForLastChar; 2353 } 2354 } 2355 return start + 1; 2356 } 2357 } 2358 // END Android-added: Private static lastIndexOf method that takes String parameters. 2359 2360 /** 2361 * Code shared by String and AbstractStringBuilder to do searches. The 2362 * source is the character array being searched, and the target 2363 * is the string being searched for. 2364 * 2365 * @param src the characters being searched. 2366 * @param srcCoder coder handles the mapping between bytes/chars 2367 * @param srcCount count of the source string. 2368 * @param tgtStr the characters being searched for. 2369 * @param fromIndex the index to begin searching from. 2370 */ lastIndexOf(byte[] src, byte srcCoder, int srcCount, String tgtStr, int fromIndex)2371 static int lastIndexOf(byte[] src, byte srcCoder, int srcCount, 2372 String tgtStr, int fromIndex) { 2373 // byte[] tgt = tgtStr.value; 2374 byte tgtCoder = tgtStr.coder(); 2375 int tgtCount = tgtStr.length(); 2376 /* 2377 * Check arguments; return immediately where possible. For 2378 * consistency, don't check for null str. 2379 */ 2380 int rightIndex = srcCount - tgtCount; 2381 if (fromIndex > rightIndex) { 2382 fromIndex = rightIndex; 2383 } 2384 if (fromIndex < 0) { 2385 return -1; 2386 } 2387 /* Empty string always matches. */ 2388 if (tgtCount == 0) { 2389 return fromIndex; 2390 } 2391 if (srcCoder == tgtCoder) { 2392 return srcCoder == LATIN1 2393 // Android-changed: libcore doesn't store String as Latin1 or UTF16 byte[] field. 2394 // ? StringLatin1.lastIndexOf(src, srcCount, tgt, tgtCount, fromIndex) 2395 // : StringUTF16.lastIndexOf(src, srcCount, tgt, tgtCount, fromIndex); 2396 ? StringLatin1.lastIndexOf(src, srcCount, tgtStr, tgtCount, fromIndex) 2397 : StringUTF16.lastIndexOf(src, srcCount, tgtStr, tgtCount, fromIndex); 2398 } 2399 if (srcCoder == LATIN1) { // && tgtCoder == UTF16 2400 return -1; 2401 } 2402 // srcCoder == UTF16 && tgtCoder == LATIN1 2403 // return StringUTF16.lastIndexOfLatin1(src, srcCount, tgt, tgtCount, fromIndex); 2404 return StringUTF16.lastIndexOfLatin1(src, srcCount, tgtStr, tgtCount, fromIndex); 2405 } 2406 2407 /** 2408 * Code shared by String and StringBuffer to do searches. The 2409 * source is the character array being searched, and the target 2410 * is the string being searched for. 2411 * 2412 * @param source the characters being searched. 2413 * @param sourceOffset offset of the source string. 2414 * @param sourceCount count of the source string. 2415 * @param target the characters being searched for. 2416 * @param targetOffset offset of the target string. 2417 * @param targetCount count of the target string. 2418 * @param fromIndex the index to begin searching from. 2419 */ lastIndexOf(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndex)2420 static int lastIndexOf(char[] source, int sourceOffset, int sourceCount, 2421 char[] target, int targetOffset, int targetCount, 2422 int fromIndex) { 2423 /* 2424 * Check arguments; return immediately where possible. For 2425 * consistency, don't check for null str. 2426 */ 2427 int rightIndex = sourceCount - targetCount; 2428 if (fromIndex < 0) { 2429 return -1; 2430 } 2431 if (fromIndex > rightIndex) { 2432 fromIndex = rightIndex; 2433 } 2434 /* Empty string always matches. */ 2435 if (targetCount == 0) { 2436 return fromIndex; 2437 } 2438 2439 int strLastIndex = targetOffset + targetCount - 1; 2440 char strLastChar = target[strLastIndex]; 2441 int min = sourceOffset + targetCount - 1; 2442 int i = min + fromIndex; 2443 2444 startSearchForLastChar: 2445 while (true) { 2446 while (i >= min && source[i] != strLastChar) { 2447 i--; 2448 } 2449 if (i < min) { 2450 return -1; 2451 } 2452 int j = i - 1; 2453 int start = j - (targetCount - 1); 2454 int k = strLastIndex - 1; 2455 2456 while (j > start) { 2457 if (source[j--] != target[k--]) { 2458 i--; 2459 continue startSearchForLastChar; 2460 } 2461 } 2462 return start - sourceOffset + 1; 2463 } 2464 } 2465 2466 /** 2467 * Returns a string that is a substring of this string. The 2468 * substring begins with the character at the specified index and 2469 * extends to the end of this string. <p> 2470 * Examples: 2471 * <blockquote><pre> 2472 * "unhappy".substring(2) returns "happy" 2473 * "Harbison".substring(3) returns "bison" 2474 * "emptiness".substring(9) returns "" (an empty string) 2475 * </pre></blockquote> 2476 * 2477 * @param beginIndex the beginning index, inclusive. 2478 * @return the specified substring. 2479 * @exception IndexOutOfBoundsException if 2480 * {@code beginIndex} is negative or larger than the 2481 * length of this {@code String} object. 2482 */ substring(int beginIndex)2483 public String substring(int beginIndex) { 2484 if (beginIndex < 0) { 2485 throw new StringIndexOutOfBoundsException(this, beginIndex); 2486 } 2487 int subLen = length() - beginIndex; 2488 if (subLen < 0) { 2489 throw new StringIndexOutOfBoundsException(this, beginIndex); 2490 } 2491 if (beginIndex == 0) { 2492 return this; 2493 } 2494 // BEGIN Android-changed: Use native fastSubstring instead of String constructor. 2495 /* 2496 return isLatin1() ? StringLatin1.newString(value, beginIndex, subLen) 2497 : StringUTF16.newString(value, beginIndex, subLen); 2498 */ 2499 return fastSubstring(beginIndex, subLen); 2500 // END Android-changed: Use native fastSubstring instead of String constructor. 2501 } 2502 2503 /** 2504 * Returns a string that is a substring of this string. The 2505 * substring begins at the specified {@code beginIndex} and 2506 * extends to the character at index {@code endIndex - 1}. 2507 * Thus the length of the substring is {@code endIndex-beginIndex}. 2508 * <p> 2509 * Examples: 2510 * <blockquote><pre> 2511 * "hamburger".substring(4, 8) returns "urge" 2512 * "smiles".substring(1, 5) returns "mile" 2513 * </pre></blockquote> 2514 * 2515 * @param beginIndex the beginning index, inclusive. 2516 * @param endIndex the ending index, exclusive. 2517 * @return the specified substring. 2518 * @exception IndexOutOfBoundsException if the 2519 * {@code beginIndex} is negative, or 2520 * {@code endIndex} is larger than the length of 2521 * this {@code String} object, or 2522 * {@code beginIndex} is larger than 2523 * {@code endIndex}. 2524 */ substring(int beginIndex, int endIndex)2525 public String substring(int beginIndex, int endIndex) { 2526 int length = length(); 2527 checkBoundsBeginEnd(beginIndex, endIndex, length); 2528 int subLen = endIndex - beginIndex; 2529 if (beginIndex == 0 && endIndex == length) { 2530 return this; 2531 } 2532 2533 // BEGIN Android-changed: Use native fastSubstring instead of String constructor. 2534 /* 2535 return isLatin1() ? StringLatin1.newString(value, beginIndex, subLen) 2536 : StringUTF16.newString(value, beginIndex, subLen); 2537 */ 2538 return fastSubstring(beginIndex, subLen); 2539 // END Android-changed: Use native fastSubstring instead of String constructor. 2540 } 2541 2542 // BEGIN Android-added: Native method to access char storage managed by runtime. 2543 @FastNative fastSubstring(int start, int length)2544 private native String fastSubstring(int start, int length); 2545 // END Android-added: Native method to access char storage managed by runtime. 2546 2547 /** 2548 * Returns a character sequence that is a subsequence of this sequence. 2549 * 2550 * <p> An invocation of this method of the form 2551 * 2552 * <blockquote><pre> 2553 * str.subSequence(begin, end)</pre></blockquote> 2554 * 2555 * behaves in exactly the same way as the invocation 2556 * 2557 * <blockquote><pre> 2558 * str.substring(begin, end)</pre></blockquote> 2559 * 2560 * @apiNote 2561 * This method is defined so that the {@code String} class can implement 2562 * the {@link CharSequence} interface. 2563 * 2564 * @param beginIndex the begin index, inclusive. 2565 * @param endIndex the end index, exclusive. 2566 * @return the specified subsequence. 2567 * 2568 * @throws IndexOutOfBoundsException 2569 * if {@code beginIndex} or {@code endIndex} is negative, 2570 * if {@code endIndex} is greater than {@code length()}, 2571 * or if {@code beginIndex} is greater than {@code endIndex} 2572 * 2573 * @since 1.4 2574 * @spec JSR-51 2575 */ subSequence(int beginIndex, int endIndex)2576 public CharSequence subSequence(int beginIndex, int endIndex) { 2577 return this.substring(beginIndex, endIndex); 2578 } 2579 2580 /** 2581 * Concatenates the specified string to the end of this string. 2582 * <p> 2583 * If the length of the argument string is {@code 0}, then this 2584 * {@code String} object is returned. Otherwise, a 2585 * {@code String} object is returned that represents a character 2586 * sequence that is the concatenation of the character sequence 2587 * represented by this {@code String} object and the character 2588 * sequence represented by the argument string.<p> 2589 * Examples: 2590 * <blockquote><pre> 2591 * "cares".concat("s") returns "caress" 2592 * "to".concat("get").concat("her") returns "together" 2593 * </pre></blockquote> 2594 * 2595 * @param str the {@code String} that is concatenated to the end 2596 * of this {@code String}. 2597 * @return a string that represents the concatenation of this object's 2598 * characters followed by the string argument's characters. 2599 */ 2600 // BEGIN Android-changed: Replace with implementation in runtime to access chars (see above). 2601 /* 2602 public String concat(String str) { 2603 if (str.isEmpty()) { 2604 return this; 2605 } 2606 if (coder() == str.coder()) { 2607 byte[] val = this.value; 2608 byte[] oval = str.value; 2609 int len = val.length + oval.length; 2610 byte[] buf = Arrays.copyOf(val, len); 2611 System.arraycopy(oval, 0, buf, val.length, oval.length); 2612 return new String(buf, coder); 2613 } 2614 int len = length(); 2615 int olen = str.length(); 2616 byte[] buf = StringUTF16.newBytesFor(len + olen); 2617 getBytes(buf, 0, UTF16); 2618 str.getBytes(buf, len, UTF16); 2619 return new String(buf, UTF16); 2620 } 2621 */ 2622 @FastNative concat(String str)2623 public native String concat(String str); 2624 // END Android-changed: Replace with implementation in runtime to access chars (see above). 2625 2626 /** 2627 * Returns a string resulting from replacing all occurrences of 2628 * {@code oldChar} in this string with {@code newChar}. 2629 * <p> 2630 * If the character {@code oldChar} does not occur in the 2631 * character sequence represented by this {@code String} object, 2632 * then a reference to this {@code String} object is returned. 2633 * Otherwise, a {@code String} object is returned that 2634 * represents a character sequence identical to the character sequence 2635 * represented by this {@code String} object, except that every 2636 * occurrence of {@code oldChar} is replaced by an occurrence 2637 * of {@code newChar}. 2638 * <p> 2639 * Examples: 2640 * <blockquote><pre> 2641 * "mesquite in your cellar".replace('e', 'o') 2642 * returns "mosquito in your collar" 2643 * "the war of baronets".replace('r', 'y') 2644 * returns "the way of bayonets" 2645 * "sparring with a purple porpoise".replace('p', 't') 2646 * returns "starring with a turtle tortoise" 2647 * "JonL".replace('q', 'x') returns "JonL" (no change) 2648 * </pre></blockquote> 2649 * 2650 * @param oldChar the old character. 2651 * @param newChar the new character. 2652 * @return a string derived from this string by replacing every 2653 * occurrence of {@code oldChar} with {@code newChar}. 2654 */ replace(char oldChar, char newChar)2655 public String replace(char oldChar, char newChar) { 2656 // BEGIN Android-changed: Replace with implementation using native doReplace method. 2657 if (oldChar != newChar) { 2658 /* 2659 String ret = isLatin1() ? StringLatin1.replace(value, oldChar, newChar) 2660 : StringUTF16.replace(value, oldChar, newChar); 2661 if (ret != null) { 2662 return ret; 2663 } 2664 */ 2665 final int len = length(); 2666 for (int i = 0; i < len; ++i) { 2667 if (charAt(i) == oldChar) { 2668 return doReplace(oldChar, newChar); 2669 } 2670 } 2671 } 2672 // END Android-changed: Replace with implementation using native doReplace method. 2673 return this; 2674 } 2675 2676 // BEGIN Android-added: Native method to access char storage managed by runtime. 2677 // Implementation of replace(char oldChar, char newChar) called when we found a match. 2678 @FastNative doReplace(char oldChar, char newChar)2679 private native String doReplace(char oldChar, char newChar); 2680 // END Android-added: Native method to access char storage managed by runtime. 2681 2682 /** 2683 * Tells whether or not this string matches the given <a 2684 * href="../util/regex/Pattern.html#sum">regular expression</a>. 2685 * 2686 * <p> An invocation of this method of the form 2687 * <i>str</i>{@code .matches(}<i>regex</i>{@code )} yields exactly the 2688 * same result as the expression 2689 * 2690 * <blockquote> 2691 * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#matches(String,CharSequence) 2692 * matches(<i>regex</i>, <i>str</i>)} 2693 * </blockquote> 2694 * 2695 * @param regex 2696 * the regular expression to which this string is to be matched 2697 * 2698 * @return {@code true} if, and only if, this string matches the 2699 * given regular expression 2700 * 2701 * @throws PatternSyntaxException 2702 * if the regular expression's syntax is invalid 2703 * 2704 * @see java.util.regex.Pattern 2705 * 2706 * @since 1.4 2707 * @spec JSR-51 2708 */ matches(String regex)2709 public boolean matches(String regex) { 2710 return Pattern.matches(regex, this); 2711 } 2712 2713 /** 2714 * Returns true if and only if this string contains the specified 2715 * sequence of char values. 2716 * 2717 * @param s the sequence to search for 2718 * @return true if this string contains {@code s}, false otherwise 2719 * @since 1.5 2720 */ contains(CharSequence s)2721 public boolean contains(CharSequence s) { 2722 return indexOf(s.toString()) >= 0; 2723 } 2724 2725 /** 2726 * Replaces the first substring of this string that matches the given <a 2727 * href="../util/regex/Pattern.html#sum">regular expression</a> with the 2728 * given replacement. 2729 * 2730 * <p> An invocation of this method of the form 2731 * <i>str</i>{@code .replaceFirst(}<i>regex</i>{@code ,} <i>repl</i>{@code )} 2732 * yields exactly the same result as the expression 2733 * 2734 * <blockquote> 2735 * <code> 2736 * {@link java.util.regex.Pattern}.{@link 2737 * java.util.regex.Pattern#compile compile}(<i>regex</i>).{@link 2738 * java.util.regex.Pattern#matcher(java.lang.CharSequence) matcher}(<i>str</i>).{@link 2739 * java.util.regex.Matcher#replaceFirst replaceFirst}(<i>repl</i>) 2740 * </code> 2741 * </blockquote> 2742 * 2743 *<p> 2744 * Note that backslashes ({@code \}) and dollar signs ({@code $}) in the 2745 * replacement string may cause the results to be different than if it were 2746 * being treated as a literal replacement string; see 2747 * {@link java.util.regex.Matcher#replaceFirst}. 2748 * Use {@link java.util.regex.Matcher#quoteReplacement} to suppress the special 2749 * meaning of these characters, if desired. 2750 * 2751 * @param regex 2752 * the regular expression to which this string is to be matched 2753 * @param replacement 2754 * the string to be substituted for the first match 2755 * 2756 * @return The resulting {@code String} 2757 * 2758 * @throws PatternSyntaxException 2759 * if the regular expression's syntax is invalid 2760 * 2761 * @see java.util.regex.Pattern 2762 * 2763 * @since 1.4 2764 * @spec JSR-51 2765 */ replaceFirst(String regex, String replacement)2766 public String replaceFirst(String regex, String replacement) { 2767 return Pattern.compile(regex).matcher(this).replaceFirst(replacement); 2768 } 2769 2770 /** 2771 * Replaces each substring of this string that matches the given <a 2772 * href="../util/regex/Pattern.html#sum">regular expression</a> with the 2773 * given replacement. 2774 * 2775 * <p> An invocation of this method of the form 2776 * <i>str</i>{@code .replaceAll(}<i>regex</i>{@code ,} <i>repl</i>{@code )} 2777 * yields exactly the same result as the expression 2778 * 2779 * <blockquote> 2780 * <code> 2781 * {@link java.util.regex.Pattern}.{@link 2782 * java.util.regex.Pattern#compile compile}(<i>regex</i>).{@link 2783 * java.util.regex.Pattern#matcher(java.lang.CharSequence) matcher}(<i>str</i>).{@link 2784 * java.util.regex.Matcher#replaceAll replaceAll}(<i>repl</i>) 2785 * </code> 2786 * </blockquote> 2787 * 2788 *<p> 2789 * Note that backslashes ({@code \}) and dollar signs ({@code $}) in the 2790 * replacement string may cause the results to be different than if it were 2791 * being treated as a literal replacement string; see 2792 * {@link java.util.regex.Matcher#replaceAll Matcher.replaceAll}. 2793 * Use {@link java.util.regex.Matcher#quoteReplacement} to suppress the special 2794 * meaning of these characters, if desired. 2795 * 2796 * @param regex 2797 * the regular expression to which this string is to be matched 2798 * @param replacement 2799 * the string to be substituted for each match 2800 * 2801 * @return The resulting {@code String} 2802 * 2803 * @throws PatternSyntaxException 2804 * if the regular expression's syntax is invalid 2805 * 2806 * @see java.util.regex.Pattern 2807 * 2808 * @since 1.4 2809 * @spec JSR-51 2810 */ replaceAll(String regex, String replacement)2811 public String replaceAll(String regex, String replacement) { 2812 return Pattern.compile(regex).matcher(this).replaceAll(replacement); 2813 } 2814 2815 /** 2816 * Replaces each substring of this string that matches the literal target 2817 * sequence with the specified literal replacement sequence. The 2818 * replacement proceeds from the beginning of the string to the end, for 2819 * example, replacing "aa" with "b" in the string "aaa" will result in 2820 * "ba" rather than "ab". 2821 * 2822 * @param target The sequence of char values to be replaced 2823 * @param replacement The replacement sequence of char values 2824 * @return The resulting string 2825 * @since 1.5 2826 */ replace(CharSequence target, CharSequence replacement)2827 public String replace(CharSequence target, CharSequence replacement) { 2828 // BEGIN Android-added: Additional null check for parameters. 2829 Objects.requireNonNull(target); 2830 Objects.requireNonNull(replacement); 2831 // END Android-added: Additional null check for parameters. 2832 2833 String tgtStr = target.toString(); 2834 String replStr = replacement.toString(); 2835 int j = indexOf(tgtStr); 2836 if (j < 0) { 2837 return this; 2838 } 2839 int tgtLen = tgtStr.length(); 2840 int tgtLen1 = Math.max(tgtLen, 1); 2841 int thisLen = length(); 2842 2843 int newLenHint = thisLen - tgtLen + replStr.length(); 2844 if (newLenHint < 0) { 2845 throw new OutOfMemoryError(); 2846 } 2847 StringBuilder sb = new StringBuilder(newLenHint); 2848 int i = 0; 2849 do { 2850 sb.append(this, i, j).append(replStr); 2851 i = j + tgtLen; 2852 } while (j < thisLen && (j = indexOf(tgtStr, j + tgtLen1)) > 0); 2853 return sb.append(this, i, thisLen).toString(); 2854 } 2855 2856 /** 2857 * Splits this string around matches of the given 2858 * <a href="../util/regex/Pattern.html#sum">regular expression</a>. 2859 * 2860 * <p> The array returned by this method contains each substring of this 2861 * string that is terminated by another substring that matches the given 2862 * expression or is terminated by the end of the string. The substrings in 2863 * the array are in the order in which they occur in this string. If the 2864 * expression does not match any part of the input then the resulting array 2865 * has just one element, namely this string. 2866 * 2867 * <p> When there is a positive-width match at the beginning of this 2868 * string then an empty leading substring is included at the beginning 2869 * of the resulting array. A zero-width match at the beginning however 2870 * never produces such empty leading substring. 2871 * 2872 * <p> The {@code limit} parameter controls the number of times the 2873 * pattern is applied and therefore affects the length of the resulting 2874 * array. 2875 * <ul> 2876 * <li><p> 2877 * If the <i>limit</i> is positive then the pattern will be applied 2878 * at most <i>limit</i> - 1 times, the array's length will be 2879 * no greater than <i>limit</i>, and the array's last entry will contain 2880 * all input beyond the last matched delimiter.</p></li> 2881 * 2882 * <li><p> 2883 * If the <i>limit</i> is zero then the pattern will be applied as 2884 * many times as possible, the array can have any length, and trailing 2885 * empty strings will be discarded.</p></li> 2886 * 2887 * <li><p> 2888 * If the <i>limit</i> is negative then the pattern will be applied 2889 * as many times as possible and the array can have any length.</p></li> 2890 * </ul> 2891 * 2892 * <p> The string {@code "boo:and:foo"}, for example, yields the 2893 * following results with these parameters: 2894 * 2895 * <blockquote><table class="plain"> 2896 * <caption style="display:none">Split example showing regex, limit, and result</caption> 2897 * <thead> 2898 * <tr> 2899 * <th scope="col">Regex</th> 2900 * <th scope="col">Limit</th> 2901 * <th scope="col">Result</th> 2902 * </tr> 2903 * </thead> 2904 * <tbody> 2905 * <tr><th scope="row" rowspan="3" style="font-weight:normal">:</th> 2906 * <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">2</th> 2907 * <td>{@code { "boo", "and:foo" }}</td></tr> 2908 * <tr><!-- : --> 2909 * <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">5</th> 2910 * <td>{@code { "boo", "and", "foo" }}</td></tr> 2911 * <tr><!-- : --> 2912 * <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">-2</th> 2913 * <td>{@code { "boo", "and", "foo" }}</td></tr> 2914 * <tr><th scope="row" rowspan="3" style="font-weight:normal">o</th> 2915 * <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">5</th> 2916 * <td>{@code { "b", "", ":and:f", "", "" }}</td></tr> 2917 * <tr><!-- o --> 2918 * <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">-2</th> 2919 * <td>{@code { "b", "", ":and:f", "", "" }}</td></tr> 2920 * <tr><!-- o --> 2921 * <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">0</th> 2922 * <td>{@code { "b", "", ":and:f" }}</td></tr> 2923 * </tbody> 2924 * </table></blockquote> 2925 * 2926 * <p> An invocation of this method of the form 2927 * <i>str.</i>{@code split(}<i>regex</i>{@code ,} <i>n</i>{@code )} 2928 * yields the same result as the expression 2929 * 2930 * <blockquote> 2931 * <code> 2932 * {@link java.util.regex.Pattern}.{@link 2933 * java.util.regex.Pattern#compile compile}(<i>regex</i>).{@link 2934 * java.util.regex.Pattern#split(java.lang.CharSequence,int) split}(<i>str</i>, <i>n</i>) 2935 * </code> 2936 * </blockquote> 2937 * 2938 * 2939 * @param regex 2940 * the delimiting regular expression 2941 * 2942 * @param limit 2943 * the result threshold, as described above 2944 * 2945 * @return the array of strings computed by splitting this string 2946 * around matches of the given regular expression 2947 * 2948 * @throws PatternSyntaxException 2949 * if the regular expression's syntax is invalid 2950 * 2951 * @see java.util.regex.Pattern 2952 * 2953 * @since 1.4 2954 * @spec JSR-51 2955 */ split(String regex, int limit)2956 public String[] split(String regex, int limit) { 2957 // BEGIN Android-changed: Replace custom fast-path with use of new Pattern.fastSplit method. 2958 // Try fast splitting without allocating Pattern object 2959 /* 2960 /* fastpath if the regex is a 2961 (1)one-char String and this character is not one of the 2962 RegEx's meta characters ".$|()[{^?*+\\", or 2963 (2)two-char String and the first char is the backslash and 2964 the second is not the ascii digit or ascii letter. 2965 * 2966 char ch = 0; 2967 if (((regex.length() == 1 && 2968 ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) || 2969 (regex.length() == 2 && 2970 regex.charAt(0) == '\\' && 2971 (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 && 2972 ((ch-'a')|('z'-ch)) < 0 && 2973 ((ch-'A')|('Z'-ch)) < 0)) && 2974 (ch < Character.MIN_HIGH_SURROGATE || 2975 ch > Character.MAX_LOW_SURROGATE)) 2976 { 2977 int off = 0; 2978 int next = 0; 2979 boolean limited = limit > 0; 2980 ArrayList<String> list = new ArrayList<>(); 2981 while ((next = indexOf(ch, off)) != -1) { 2982 if (!limited || list.size() < limit - 1) { 2983 list.add(substring(off, next)); 2984 off = next + 1; 2985 } else { // last one 2986 //assert (list.size() == limit - 1); 2987 int last = length(); 2988 list.add(substring(off, last)); 2989 off = last; 2990 break; 2991 } 2992 } 2993 // If no match was found, return this 2994 if (off == 0) 2995 return new String[]{this}; 2996 2997 // Add remaining segment 2998 if (!limited || list.size() < limit) 2999 list.add(substring(off, length())); 3000 3001 // Construct result 3002 int resultSize = list.size(); 3003 if (limit == 0) { 3004 while (resultSize > 0 && list.get(resultSize - 1).isEmpty()) { 3005 resultSize--; 3006 } 3007 } 3008 String[] result = new String[resultSize]; 3009 return list.subList(0, resultSize).toArray(result); 3010 } 3011 */ 3012 String[] fast = Pattern.fastSplit(regex, this, limit); 3013 if (fast != null) { 3014 return fast; 3015 } 3016 // END Android-changed: Replace custom fast-path with use of new Pattern.fastSplit method. 3017 return Pattern.compile(regex).split(this, limit); 3018 } 3019 3020 /** 3021 * Splits this string around matches of the given <a 3022 * href="../util/regex/Pattern.html#sum">regular expression</a>. 3023 * 3024 * <p> This method works as if by invoking the two-argument {@link 3025 * #split(String, int) split} method with the given expression and a limit 3026 * argument of zero. Trailing empty strings are therefore not included in 3027 * the resulting array. 3028 * 3029 * <p> The string {@code "boo:and:foo"}, for example, yields the following 3030 * results with these expressions: 3031 * 3032 * <blockquote><table class="plain"> 3033 * <caption style="display:none">Split examples showing regex and result</caption> 3034 * <thead> 3035 * <tr> 3036 * <th scope="col">Regex</th> 3037 * <th scope="col">Result</th> 3038 * </tr> 3039 * </thead> 3040 * <tbody> 3041 * <tr><th scope="row" style="text-weight:normal">:</th> 3042 * <td>{@code { "boo", "and", "foo" }}</td></tr> 3043 * <tr><th scope="row" style="text-weight:normal">o</th> 3044 * <td>{@code { "b", "", ":and:f" }}</td></tr> 3045 * </tbody> 3046 * </table></blockquote> 3047 * 3048 * 3049 * @param regex 3050 * the delimiting regular expression 3051 * 3052 * @return the array of strings computed by splitting this string 3053 * around matches of the given regular expression 3054 * 3055 * @throws PatternSyntaxException 3056 * if the regular expression's syntax is invalid 3057 * 3058 * @see java.util.regex.Pattern 3059 * 3060 * @since 1.4 3061 * @spec JSR-51 3062 */ split(String regex)3063 public String[] split(String regex) { 3064 return split(regex, 0); 3065 } 3066 3067 /** 3068 * Returns a new String composed of copies of the 3069 * {@code CharSequence elements} joined together with a copy of 3070 * the specified {@code delimiter}. 3071 * 3072 * <blockquote>For example, 3073 * <pre>{@code 3074 * String message = String.join("-", "Java", "is", "cool"); 3075 * // message returned is: "Java-is-cool" 3076 * }</pre></blockquote> 3077 * 3078 * Note that if an element is null, then {@code "null"} is added. 3079 * 3080 * @param delimiter the delimiter that separates each element 3081 * @param elements the elements to join together. 3082 * 3083 * @return a new {@code String} that is composed of the {@code elements} 3084 * separated by the {@code delimiter} 3085 * 3086 * @throws NullPointerException If {@code delimiter} or {@code elements} 3087 * is {@code null} 3088 * 3089 * @see java.util.StringJoiner 3090 * @since 1.8 3091 */ join(CharSequence delimiter, CharSequence... elements)3092 public static String join(CharSequence delimiter, CharSequence... elements) { 3093 Objects.requireNonNull(delimiter); 3094 Objects.requireNonNull(elements); 3095 // Number of elements not likely worth Arrays.stream overhead. 3096 StringJoiner joiner = new StringJoiner(delimiter); 3097 for (CharSequence cs: elements) { 3098 joiner.add(cs); 3099 } 3100 return joiner.toString(); 3101 } 3102 3103 /** 3104 * Returns a new {@code String} composed of copies of the 3105 * {@code CharSequence elements} joined together with a copy of the 3106 * specified {@code delimiter}. 3107 * 3108 * <blockquote>For example, 3109 * <pre>{@code 3110 * List<String> strings = List.of("Java", "is", "cool"); 3111 * String message = String.join(" ", strings); 3112 * //message returned is: "Java is cool" 3113 * 3114 * Set<String> strings = 3115 * new LinkedHashSet<>(List.of("Java", "is", "very", "cool")); 3116 * String message = String.join("-", strings); 3117 * //message returned is: "Java-is-very-cool" 3118 * }</pre></blockquote> 3119 * 3120 * Note that if an individual element is {@code null}, then {@code "null"} is added. 3121 * 3122 * @param delimiter a sequence of characters that is used to separate each 3123 * of the {@code elements} in the resulting {@code String} 3124 * @param elements an {@code Iterable} that will have its {@code elements} 3125 * joined together. 3126 * 3127 * @return a new {@code String} that is composed from the {@code elements} 3128 * argument 3129 * 3130 * @throws NullPointerException If {@code delimiter} or {@code elements} 3131 * is {@code null} 3132 * 3133 * @see #join(CharSequence,CharSequence...) 3134 * @see java.util.StringJoiner 3135 * @since 1.8 3136 */ join(CharSequence delimiter, Iterable<? extends CharSequence> elements)3137 public static String join(CharSequence delimiter, 3138 Iterable<? extends CharSequence> elements) { 3139 Objects.requireNonNull(delimiter); 3140 Objects.requireNonNull(elements); 3141 StringJoiner joiner = new StringJoiner(delimiter); 3142 for (CharSequence cs: elements) { 3143 joiner.add(cs); 3144 } 3145 return joiner.toString(); 3146 } 3147 3148 /** 3149 * Converts all of the characters in this {@code String} to lower 3150 * case using the rules of the given {@code Locale}. Case mapping is based 3151 * on the Unicode Standard version specified by the {@link java.lang.Character Character} 3152 * class. Since case mappings are not always 1:1 char mappings, the resulting 3153 * {@code String} may be a different length than the original {@code String}. 3154 * <p> 3155 * Examples of lowercase mappings are in the following table: 3156 * <table class="plain"> 3157 * <caption style="display:none">Lowercase mapping examples showing language code of locale, upper case, lower case, and description</caption> 3158 * <thead> 3159 * <tr> 3160 * <th scope="col">Language Code of Locale</th> 3161 * <th scope="col">Upper Case</th> 3162 * <th scope="col">Lower Case</th> 3163 * <th scope="col">Description</th> 3164 * </tr> 3165 * </thead> 3166 * <tbody> 3167 * <tr> 3168 * <td>tr (Turkish)</td> 3169 * <th scope="row" style="font-weight:normal; text-align:left">\u0130</th> 3170 * <td>\u0069</td> 3171 * <td>capital letter I with dot above -> small letter i</td> 3172 * </tr> 3173 * <tr> 3174 * <td>tr (Turkish)</td> 3175 * <th scope="row" style="font-weight:normal; text-align:left">\u0049</th> 3176 * <td>\u0131</td> 3177 * <td>capital letter I -> small letter dotless i </td> 3178 * </tr> 3179 * <tr> 3180 * <td>(all)</td> 3181 * <th scope="row" style="font-weight:normal; text-align:left">French Fries</th> 3182 * <td>french fries</td> 3183 * <td>lowercased all chars in String</td> 3184 * </tr> 3185 * <tr> 3186 * <td>(all)</td> 3187 * <th scope="row" style="font-weight:normal; text-align:left"> 3188 * ΙΧΘΥΣ</th> 3189 * <td>ιχθυσ</td> 3190 * <td>lowercased all chars in String</td> 3191 * </tr> 3192 * </tbody> 3193 * </table> 3194 * 3195 * @param locale use the case transformation rules for this locale 3196 * @return the {@code String}, converted to lowercase. 3197 * @see java.lang.String#toLowerCase() 3198 * @see java.lang.String#toUpperCase() 3199 * @see java.lang.String#toUpperCase(Locale) 3200 * @since 1.1 3201 */ toLowerCase(Locale locale)3202 public String toLowerCase(Locale locale) { 3203 // BEGIN Android-changed: Replace custom code with call to new CaseMapper class. 3204 /* 3205 return isLatin1() ? StringLatin1.toLowerCase(this, value, locale) 3206 : StringUTF16.toLowerCase(this, value, locale); 3207 */ 3208 return CaseMapper.toLowerCase(locale, this); 3209 // END Android-changed: Replace custom code with call to new CaseMapper class. 3210 } 3211 3212 /** 3213 * Converts all of the characters in this {@code String} to lower 3214 * case using the rules of the default locale. This is equivalent to calling 3215 * {@code toLowerCase(Locale.getDefault())}. 3216 * <p> 3217 * <b>Note:</b> This method is locale sensitive, and may produce unexpected 3218 * results if used for strings that are intended to be interpreted locale 3219 * independently. 3220 * Examples are programming language identifiers, protocol keys, and HTML 3221 * tags. 3222 * For instance, {@code "TITLE".toLowerCase()} in a Turkish locale 3223 * returns {@code "t\u005Cu0131tle"}, where '\u005Cu0131' is the 3224 * LATIN SMALL LETTER DOTLESS I character. 3225 * To obtain correct results for locale insensitive strings, use 3226 * {@code toLowerCase(Locale.ROOT)}. 3227 * 3228 * @return the {@code String}, converted to lowercase. 3229 * @see java.lang.String#toLowerCase(Locale) 3230 */ toLowerCase()3231 public String toLowerCase() { 3232 return toLowerCase(Locale.getDefault()); 3233 } 3234 3235 /** 3236 * Converts all of the characters in this {@code String} to upper 3237 * case using the rules of the given {@code Locale}. Case mapping is based 3238 * on the Unicode Standard version specified by the {@link java.lang.Character Character} 3239 * class. Since case mappings are not always 1:1 char mappings, the resulting 3240 * {@code String} may be a different length than the original {@code String}. 3241 * <p> 3242 * Examples of locale-sensitive and 1:M case mappings are in the following table. 3243 * 3244 * <table class="plain"> 3245 * <caption style="display:none">Examples of locale-sensitive and 1:M case mappings. Shows Language code of locale, lower case, upper case, and description.</caption> 3246 * <thead> 3247 * <tr> 3248 * <th scope="col">Language Code of Locale</th> 3249 * <th scope="col">Lower Case</th> 3250 * <th scope="col">Upper Case</th> 3251 * <th scope="col">Description</th> 3252 * </tr> 3253 * </thead> 3254 * <tbody> 3255 * <tr> 3256 * <td>tr (Turkish)</td> 3257 * <th scope="row" style="font-weight:normal; text-align:left">\u0069</th> 3258 * <td>\u0130</td> 3259 * <td>small letter i -> capital letter I with dot above</td> 3260 * </tr> 3261 * <tr> 3262 * <td>tr (Turkish)</td> 3263 * <th scope="row" style="font-weight:normal; text-align:left">\u0131</th> 3264 * <td>\u0049</td> 3265 * <td>small letter dotless i -> capital letter I</td> 3266 * </tr> 3267 * <tr> 3268 * <td>(all)</td> 3269 * <th scope="row" style="font-weight:normal; text-align:left">\u00df</th> 3270 * <td>\u0053 \u0053</td> 3271 * <td>small letter sharp s -> two letters: SS</td> 3272 * </tr> 3273 * <tr> 3274 * <td>(all)</td> 3275 * <th scope="row" style="font-weight:normal; text-align:left">Fahrvergnügen</th> 3276 * <td>FAHRVERGNÜGEN</td> 3277 * <td></td> 3278 * </tr> 3279 * </tbody> 3280 * </table> 3281 * @param locale use the case transformation rules for this locale 3282 * @return the {@code String}, converted to uppercase. 3283 * @see java.lang.String#toUpperCase() 3284 * @see java.lang.String#toLowerCase() 3285 * @see java.lang.String#toLowerCase(Locale) 3286 * @since 1.1 3287 */ toUpperCase(Locale locale)3288 public String toUpperCase(Locale locale) { 3289 // BEGIN Android-changed: Replace custom code with call to new CaseMapper class. 3290 /* 3291 return isLatin1() ? StringLatin1.toUpperCase(this, value, locale) 3292 : StringUTF16.toUpperCase(this, value, locale); 3293 */ 3294 return CaseMapper.toUpperCase(locale, this, length()); 3295 // END Android-changed: Replace custom code with call to new CaseMapper class. 3296 } 3297 3298 /** 3299 * Converts all of the characters in this {@code String} to upper 3300 * case using the rules of the default locale. This method is equivalent to 3301 * {@code toUpperCase(Locale.getDefault())}. 3302 * <p> 3303 * <b>Note:</b> This method is locale sensitive, and may produce unexpected 3304 * results if used for strings that are intended to be interpreted locale 3305 * independently. 3306 * Examples are programming language identifiers, protocol keys, and HTML 3307 * tags. 3308 * For instance, {@code "title".toUpperCase()} in a Turkish locale 3309 * returns {@code "T\u005Cu0130TLE"}, where '\u005Cu0130' is the 3310 * LATIN CAPITAL LETTER I WITH DOT ABOVE character. 3311 * To obtain correct results for locale insensitive strings, use 3312 * {@code toUpperCase(Locale.ROOT)}. 3313 * 3314 * @return the {@code String}, converted to uppercase. 3315 * @see java.lang.String#toUpperCase(Locale) 3316 */ toUpperCase()3317 public String toUpperCase() { 3318 return toUpperCase(Locale.getDefault()); 3319 } 3320 3321 /** 3322 * Returns a string whose value is this string, with all leading 3323 * and trailing space removed, where space is defined 3324 * as any character whose codepoint is less than or equal to 3325 * {@code 'U+0020'} (the space character). 3326 * <p> 3327 * If this {@code String} object represents an empty character 3328 * sequence, or the first and last characters of character sequence 3329 * represented by this {@code String} object both have codes 3330 * that are not space (as defined above), then a 3331 * reference to this {@code String} object is returned. 3332 * <p> 3333 * Otherwise, if all characters in this string are space (as 3334 * defined above), then a {@code String} object representing an 3335 * empty string is returned. 3336 * <p> 3337 * Otherwise, let <i>k</i> be the index of the first character in the 3338 * string whose code is not a space (as defined above) and let 3339 * <i>m</i> be the index of the last character in the string whose code 3340 * is not a space (as defined above). A {@code String} 3341 * object is returned, representing the substring of this string that 3342 * begins with the character at index <i>k</i> and ends with the 3343 * character at index <i>m</i>-that is, the result of 3344 * {@code this.substring(k, m + 1)}. 3345 * <p> 3346 * This method may be used to trim space (as defined above) from 3347 * the beginning and end of a string. 3348 * 3349 * @return a string whose value is this string, with all leading 3350 * and trailing space removed, or this string if it 3351 * has no leading or trailing space. 3352 */ trim()3353 public String trim() { 3354 // BEGIN Android-changed: Implement in terms of charAt(). 3355 /* 3356 String ret = isLatin1() ? StringLatin1.trim(value) 3357 : StringUTF16.trim(value); 3358 return ret == null ? this : ret; 3359 */ 3360 int len = length(); 3361 int st = 0; 3362 3363 while ((st < len) && (charAt(st) <= ' ')) { 3364 st++; 3365 } 3366 while ((st < len) && (charAt(len - 1) <= ' ')) { 3367 len--; 3368 } 3369 return ((st > 0) || (len < length())) ? substring(st, len) : this; 3370 // END Android-changed: Implement in terms of charAt(). 3371 } 3372 3373 /** 3374 * Returns a string whose value is this string, with all leading 3375 * and trailing {@link Character#isWhitespace(int) white space} 3376 * removed. 3377 * <p> 3378 * If this {@code String} object represents an empty string, 3379 * or if all code points in this string are 3380 * {@link Character#isWhitespace(int) white space}, then an empty string 3381 * is returned. 3382 * <p> 3383 * Otherwise, returns a substring of this string beginning with the first 3384 * code point that is not a {@link Character#isWhitespace(int) white space} 3385 * up to and including the last code point that is not a 3386 * {@link Character#isWhitespace(int) white space}. 3387 * <p> 3388 * This method may be used to strip 3389 * {@link Character#isWhitespace(int) white space} from 3390 * the beginning and end of a string. 3391 * 3392 * @return a string whose value is this string, with all leading 3393 * and trailing white space removed 3394 * 3395 * @see Character#isWhitespace(int) 3396 * 3397 * @since 11 3398 */ strip()3399 public String strip() { 3400 // BEGIN Android-changed: Delegate to StringUTF16. 3401 /* 3402 String ret = isLatin1() ? StringLatin1.strip(value) 3403 : StringUTF16.strip(value); 3404 */ 3405 String ret = StringUTF16.strip(this); 3406 // END Android-changed: Delegate to StringUTF16. 3407 return ret == null ? this : ret; 3408 } 3409 3410 /** 3411 * Returns a string whose value is this string, with all leading 3412 * {@link Character#isWhitespace(int) white space} removed. 3413 * <p> 3414 * If this {@code String} object represents an empty string, 3415 * or if all code points in this string are 3416 * {@link Character#isWhitespace(int) white space}, then an empty string 3417 * is returned. 3418 * <p> 3419 * Otherwise, returns a substring of this string beginning with the first 3420 * code point that is not a {@link Character#isWhitespace(int) white space} 3421 * up to to and including the last code point of this string. 3422 * <p> 3423 * This method may be used to trim 3424 * {@link Character#isWhitespace(int) white space} from 3425 * the beginning of a string. 3426 * 3427 * @return a string whose value is this string, with all leading white 3428 * space removed 3429 * 3430 * @see Character#isWhitespace(int) 3431 * 3432 * @since 11 3433 */ stripLeading()3434 public String stripLeading() { 3435 // BEGIN Android-changed: Delegate to StringUTF16. 3436 /* 3437 String ret = isLatin1() ? StringLatin1.stripLeading(value) 3438 : StringUTF16.stripLeading(value); 3439 */ 3440 String ret = StringUTF16.stripLeading(this); 3441 // END Android-changed: Delegate to StringUTF16. 3442 return ret == null ? this : ret; 3443 } 3444 3445 /** 3446 * Returns a string whose value is this string, with all trailing 3447 * {@link Character#isWhitespace(int) white space} removed. 3448 * <p> 3449 * If this {@code String} object represents an empty string, 3450 * or if all characters in this string are 3451 * {@link Character#isWhitespace(int) white space}, then an empty string 3452 * is returned. 3453 * <p> 3454 * Otherwise, returns a substring of this string beginning with the first 3455 * code point of this string up to and including the last code point 3456 * that is not a {@link Character#isWhitespace(int) white space}. 3457 * <p> 3458 * This method may be used to trim 3459 * {@link Character#isWhitespace(int) white space} from 3460 * the end of a string. 3461 * 3462 * @return a string whose value is this string, with all trailing white 3463 * space removed 3464 * 3465 * @see Character#isWhitespace(int) 3466 * 3467 * @since 11 3468 */ stripTrailing()3469 public String stripTrailing() { 3470 // BEGIN Android-changed: Delegate to StringUTF16. 3471 /* 3472 String ret = isLatin1() ? StringLatin1.stripTrailing(value) 3473 : StringUTF16.stripTrailing(value); 3474 */ 3475 String ret = StringUTF16.stripTrailing(this); 3476 // END Android-changed: Delegate to StringUTF16. 3477 return ret == null ? this : ret; 3478 } 3479 3480 /** 3481 * Returns {@code true} if the string is empty or contains only 3482 * {@link Character#isWhitespace(int) white space} codepoints, 3483 * otherwise {@code false}. 3484 * 3485 * @return {@code true} if the string is empty or contains only 3486 * {@link Character#isWhitespace(int) white space} codepoints, 3487 * otherwise {@code false} 3488 * 3489 * @see Character#isWhitespace(int) 3490 * 3491 * @since 11 3492 */ isBlank()3493 public boolean isBlank() { 3494 return indexOfNonWhitespace() == length(); 3495 } 3496 3497 /** 3498 * Returns a stream of lines extracted from this string, 3499 * separated by line terminators. 3500 * <p> 3501 * A <i>line terminator</i> is one of the following: 3502 * a line feed character {@code "\n"} (U+000A), 3503 * a carriage return character {@code "\r"} (U+000D), 3504 * or a carriage return followed immediately by a line feed 3505 * {@code "\r\n"} (U+000D U+000A). 3506 * <p> 3507 * A <i>line</i> is either a sequence of zero or more characters 3508 * followed by a line terminator, or it is a sequence of one or 3509 * more characters followed by the end of the string. A 3510 * line does not include the line terminator. 3511 * <p> 3512 * The stream returned by this method contains the lines from 3513 * this string in the order in which they occur. 3514 * 3515 * @apiNote This definition of <i>line</i> implies that an empty 3516 * string has zero lines and that there is no empty line 3517 * following a line terminator at the end of a string. 3518 * 3519 * @implNote This method provides better performance than 3520 * split("\R") by supplying elements lazily and 3521 * by faster search of new line terminators. 3522 * 3523 * @return the stream of lines extracted from this string 3524 * 3525 * @since 11 3526 */ lines()3527 public Stream<String> lines() { 3528 // BEGIN Android-removed: Delegate to StringUTF16. 3529 /* 3530 return isLatin1() ? StringLatin1.lines(value) 3531 : StringUTF16.lines(value); 3532 */ 3533 return StringUTF16.lines(this); 3534 // END Android-removed: Delegate to StringUTF16. 3535 } 3536 3537 /** 3538 * Adjusts the indentation of each line of this string based on the value of 3539 * {@code n}, and normalizes line termination characters. 3540 * <p> 3541 * This string is conceptually separated into lines using 3542 * {@link String#lines()}. Each line is then adjusted as described below 3543 * and then suffixed with a line feed {@code "\n"} (U+000A). The resulting 3544 * lines are then concatenated and returned. 3545 * <p> 3546 * If {@code n > 0} then {@code n} spaces (U+0020) are inserted at the 3547 * beginning of each line. 3548 * <p> 3549 * If {@code n < 0} then up to {@code n} 3550 * {@linkplain Character#isWhitespace(int) white space characters} are removed 3551 * from the beginning of each line. If a given line does not contain 3552 * sufficient white space then all leading 3553 * {@linkplain Character#isWhitespace(int) white space characters} are removed. 3554 * Each white space character is treated as a single character. In 3555 * particular, the tab character {@code "\t"} (U+0009) is considered a 3556 * single character; it is not expanded. 3557 * <p> 3558 * If {@code n == 0} then the line remains unchanged. However, line 3559 * terminators are still normalized. 3560 * 3561 * @param n number of leading 3562 * {@linkplain Character#isWhitespace(int) white space characters} 3563 * to add or remove 3564 * 3565 * @return string with indentation adjusted and line endings normalized 3566 * 3567 * @see String#lines() 3568 * @see String#isBlank() 3569 * @see Character#isWhitespace(int) 3570 * 3571 * @since 12 3572 */ indent(int n)3573 public String indent(int n) { 3574 if (isEmpty()) { 3575 return ""; 3576 } 3577 Stream<String> stream = lines(); 3578 if (n > 0) { 3579 final String spaces = " ".repeat(n); 3580 stream = stream.map(s -> spaces + s); 3581 } else if (n == Integer.MIN_VALUE) { 3582 stream = stream.map(s -> s.stripLeading()); 3583 } else if (n < 0) { 3584 stream = stream.map(s -> s.substring(Math.min(-n, s.indexOfNonWhitespace()))); 3585 } 3586 return stream.collect(Collectors.joining("\n", "", "\n")); 3587 } 3588 indexOfNonWhitespace()3589 private int indexOfNonWhitespace() { 3590 // BEGIN Android-removed: Delegate to StringUTF16. 3591 /* 3592 return isLatin1() ? StringLatin1.indexOfNonWhitespace(value) 3593 : StringUTF16.indexOfNonWhitespace(value); 3594 */ 3595 return StringUTF16.indexOfNonWhitespace(this); 3596 // END Android-removed: Delegate to StringUTF16. 3597 } 3598 lastIndexOfNonWhitespace()3599 private int lastIndexOfNonWhitespace() { 3600 // BEGIN Android-changed: Delegate to StringUTF16. 3601 /* 3602 return isLatin1() ? StringLatin1.lastIndexOfNonWhitespace(value) 3603 : StringUTF16.lastIndexOfNonWhitespace(value); 3604 */ 3605 return StringUTF16.lastIndexOfNonWhitespace(this); 3606 // END Android-changed: Delegate to StringUTF16. 3607 } 3608 3609 /** 3610 * Returns a string whose value is this string, with incidental 3611 * {@linkplain Character#isWhitespace(int) white space} removed from 3612 * the beginning and end of every line. 3613 * <p> 3614 * Incidental {@linkplain Character#isWhitespace(int) white space} 3615 * is often present in a text block to align the content with the opening 3616 * delimiter. For example, in the following code, dots represent incidental 3617 * {@linkplain Character#isWhitespace(int) white space}: 3618 * <blockquote><pre> 3619 * String html = """ 3620 * ..............<html> 3621 * .............. <body> 3622 * .............. <p>Hello, world</p> 3623 * .............. </body> 3624 * ..............</html> 3625 * .............."""; 3626 * </pre></blockquote> 3627 * This method treats the incidental 3628 * {@linkplain Character#isWhitespace(int) white space} as indentation to be 3629 * stripped, producing a string that preserves the relative indentation of 3630 * the content. Using | to visualize the start of each line of the string: 3631 * <blockquote><pre> 3632 * |<html> 3633 * | <body> 3634 * | <p>Hello, world</p> 3635 * | </body> 3636 * |</html> 3637 * </pre></blockquote> 3638 * First, the individual lines of this string are extracted. A <i>line</i> 3639 * is a sequence of zero or more characters followed by either a line 3640 * terminator or the end of the string. 3641 * If the string has at least one line terminator, the last line consists 3642 * of the characters between the last terminator and the end of the string. 3643 * Otherwise, if the string has no terminators, the last line is the start 3644 * of the string to the end of the string, in other words, the entire 3645 * string. 3646 * A line does not include the line terminator. 3647 * <p> 3648 * Then, the <i>minimum indentation</i> (min) is determined as follows: 3649 * <ul> 3650 * <li><p>For each non-blank line (as defined by {@link String#isBlank()}), 3651 * the leading {@linkplain Character#isWhitespace(int) white space} 3652 * characters are counted.</p> 3653 * </li> 3654 * <li><p>The leading {@linkplain Character#isWhitespace(int) white space} 3655 * characters on the last line are also counted even if 3656 * {@linkplain String#isBlank() blank}.</p> 3657 * </li> 3658 * </ul> 3659 * <p>The <i>min</i> value is the smallest of these counts. 3660 * <p> 3661 * For each {@linkplain String#isBlank() non-blank} line, <i>min</i> leading 3662 * {@linkplain Character#isWhitespace(int) white space} characters are 3663 * removed, and any trailing {@linkplain Character#isWhitespace(int) white 3664 * space} characters are removed. {@linkplain String#isBlank() Blank} lines 3665 * are replaced with the empty string. 3666 * 3667 * <p> 3668 * Finally, the lines are joined into a new string, using the LF character 3669 * {@code "\n"} (U+000A) to separate lines. 3670 * 3671 * @apiNote 3672 * This method's primary purpose is to shift a block of lines as far as 3673 * possible to the left, while preserving relative indentation. Lines 3674 * that were indented the least will thus have no leading 3675 * {@linkplain Character#isWhitespace(int) white space}. 3676 * The result will have the same number of line terminators as this string. 3677 * If this string ends with a line terminator then the result will end 3678 * with a line terminator. 3679 * 3680 * @implSpec 3681 * This method treats all {@linkplain Character#isWhitespace(int) white space} 3682 * characters as having equal width. As long as the indentation on every 3683 * line is consistently composed of the same character sequences, then the 3684 * result will be as described above. 3685 * 3686 * @return string with incidental indentation removed and line 3687 * terminators normalized 3688 * 3689 * @see String#lines() 3690 * @see String#isBlank() 3691 * @see String#indent(int) 3692 * @see Character#isWhitespace(int) 3693 * 3694 * @since 15 3695 * 3696 */ stripIndent()3697 public String stripIndent() { 3698 int length = length(); 3699 if (length == 0) { 3700 return ""; 3701 } 3702 char lastChar = charAt(length - 1); 3703 boolean optOut = lastChar == '\n' || lastChar == '\r'; 3704 List<String> lines = lines().toList(); 3705 final int outdent = optOut ? 0 : outdent(lines); 3706 return lines.stream() 3707 .map(line -> { 3708 int firstNonWhitespace = line.indexOfNonWhitespace(); 3709 int lastNonWhitespace = line.lastIndexOfNonWhitespace(); 3710 int incidentalWhitespace = Math.min(outdent, firstNonWhitespace); 3711 return firstNonWhitespace > lastNonWhitespace 3712 ? "" : line.substring(incidentalWhitespace, lastNonWhitespace); 3713 }) 3714 .collect(Collectors.joining("\n", "", optOut ? "\n" : "")); 3715 } 3716 outdent(List<String> lines)3717 private static int outdent(List<String> lines) { 3718 // Note: outdent is guaranteed to be zero or positive number. 3719 // If there isn't a non-blank line then the last must be blank 3720 int outdent = Integer.MAX_VALUE; 3721 for (String line : lines) { 3722 int leadingWhitespace = line.indexOfNonWhitespace(); 3723 if (leadingWhitespace != line.length()) { 3724 outdent = Integer.min(outdent, leadingWhitespace); 3725 } 3726 } 3727 String lastLine = lines.get(lines.size() - 1); 3728 if (lastLine.isBlank()) { 3729 outdent = Integer.min(outdent, lastLine.length()); 3730 } 3731 return outdent; 3732 } 3733 3734 /** 3735 * Returns a string whose value is this string, with escape sequences 3736 * translated as if in a string literal. 3737 * <p> 3738 * Escape sequences are translated as follows; 3739 * <table class="striped"> 3740 * <caption style="display:none">Translation</caption> 3741 * <thead> 3742 * <tr> 3743 * <th scope="col">Escape</th> 3744 * <th scope="col">Name</th> 3745 * <th scope="col">Translation</th> 3746 * </tr> 3747 * </thead> 3748 * <tbody> 3749 * <tr> 3750 * <th scope="row">{@code \u005Cb}</th> 3751 * <td>backspace</td> 3752 * <td>{@code U+0008}</td> 3753 * </tr> 3754 * <tr> 3755 * <th scope="row">{@code \u005Ct}</th> 3756 * <td>horizontal tab</td> 3757 * <td>{@code U+0009}</td> 3758 * </tr> 3759 * <tr> 3760 * <th scope="row">{@code \u005Cn}</th> 3761 * <td>line feed</td> 3762 * <td>{@code U+000A}</td> 3763 * </tr> 3764 * <tr> 3765 * <th scope="row">{@code \u005Cf}</th> 3766 * <td>form feed</td> 3767 * <td>{@code U+000C}</td> 3768 * </tr> 3769 * <tr> 3770 * <th scope="row">{@code \u005Cr}</th> 3771 * <td>carriage return</td> 3772 * <td>{@code U+000D}</td> 3773 * </tr> 3774 * <tr> 3775 * <th scope="row">{@code \u005Cs}</th> 3776 * <td>space</td> 3777 * <td>{@code U+0020}</td> 3778 * </tr> 3779 * <tr> 3780 * <th scope="row">{@code \u005C"}</th> 3781 * <td>double quote</td> 3782 * <td>{@code U+0022}</td> 3783 * </tr> 3784 * <tr> 3785 * <th scope="row">{@code \u005C'}</th> 3786 * <td>single quote</td> 3787 * <td>{@code U+0027}</td> 3788 * </tr> 3789 * <tr> 3790 * <th scope="row">{@code \u005C\u005C}</th> 3791 * <td>backslash</td> 3792 * <td>{@code U+005C}</td> 3793 * </tr> 3794 * <tr> 3795 * <th scope="row">{@code \u005C0 - \u005C377}</th> 3796 * <td>octal escape</td> 3797 * <td>code point equivalents</td> 3798 * </tr> 3799 * <tr> 3800 * <th scope="row">{@code \u005C<line-terminator>}</th> 3801 * <td>continuation</td> 3802 * <td>discard</td> 3803 * </tr> 3804 * </tbody> 3805 * </table> 3806 * 3807 * @implNote 3808 * This method does <em>not</em> translate Unicode escapes such as "{@code \u005cu2022}". 3809 * Unicode escapes are translated by the Java compiler when reading input characters and 3810 * are not part of the string literal specification. 3811 * 3812 * @throws IllegalArgumentException when an escape sequence is malformed. 3813 * 3814 * @return String with escape sequences translated. 3815 * 3816 * @jls 3.10.7 Escape Sequences 3817 * 3818 * @since 15 3819 */ translateEscapes()3820 public String translateEscapes() { 3821 if (isEmpty()) { 3822 return ""; 3823 } 3824 char[] chars = toCharArray(); 3825 int length = chars.length; 3826 int from = 0; 3827 int to = 0; 3828 while (from < length) { 3829 char ch = chars[from++]; 3830 if (ch == '\\') { 3831 ch = from < length ? chars[from++] : '\0'; 3832 switch (ch) { 3833 case 'b': 3834 ch = '\b'; 3835 break; 3836 case 'f': 3837 ch = '\f'; 3838 break; 3839 case 'n': 3840 ch = '\n'; 3841 break; 3842 case 'r': 3843 ch = '\r'; 3844 break; 3845 case 's': 3846 ch = ' '; 3847 break; 3848 case 't': 3849 ch = '\t'; 3850 break; 3851 case '\'': 3852 case '\"': 3853 case '\\': 3854 // as is 3855 break; 3856 case '0': case '1': case '2': case '3': 3857 case '4': case '5': case '6': case '7': 3858 int limit = Integer.min(from + (ch <= '3' ? 2 : 1), length); 3859 int code = ch - '0'; 3860 while (from < limit) { 3861 ch = chars[from]; 3862 if (ch < '0' || '7' < ch) { 3863 break; 3864 } 3865 from++; 3866 code = (code << 3) | (ch - '0'); 3867 } 3868 ch = (char)code; 3869 break; 3870 case '\n': 3871 continue; 3872 case '\r': 3873 if (from < length && chars[from] == '\n') { 3874 from++; 3875 } 3876 continue; 3877 default: { 3878 String msg = String.format( 3879 "Invalid escape sequence: \\%c \\\\u%04X", 3880 ch, (int)ch); 3881 throw new IllegalArgumentException(msg); 3882 } 3883 } 3884 } 3885 3886 chars[to++] = ch; 3887 } 3888 3889 return new String(chars, 0, to); 3890 } 3891 3892 /** 3893 * This method allows the application of a function to {@code this} 3894 * string. The function should expect a single String argument 3895 * and produce an {@code R} result. 3896 * <p> 3897 * Any exception thrown by {@code f.apply()} will be propagated to the 3898 * caller. 3899 * 3900 * @param f a function to apply 3901 * 3902 * @param <R> the type of the result 3903 * 3904 * @return the result of applying the function to this string 3905 * 3906 * @see java.util.function.Function 3907 * 3908 * @since 12 3909 */ 3910 public <R> R transform(Function<? super String, ? extends R> f) { 3911 return f.apply(this); 3912 } 3913 3914 /** 3915 * This object (which is already a string!) is itself returned. 3916 * 3917 * @return the string itself. 3918 */ 3919 public String toString() { 3920 return this; 3921 } 3922 3923 /** 3924 * Returns a stream of {@code int} zero-extending the {@code char} values 3925 * from this sequence. Any char which maps to a <a 3926 * href="{@docRoot}/java.base/java/lang/Character.html#unicode">surrogate code 3927 * point</a> is passed through uninterpreted. 3928 * 3929 * @return an IntStream of char values from this sequence 3930 * @since 9 3931 */ 3932 @Override 3933 public IntStream chars() { 3934 return StreamSupport.intStream( 3935 // BEGIN Android-removed: Delegate to StringUTF16. 3936 /* 3937 isLatin1() ? new StringLatin1.CharsSpliterator(value, Spliterator.IMMUTABLE) 3938 : new StringUTF16.CharsSpliterator(value, Spliterator.IMMUTABLE), 3939 */ 3940 new StringUTF16.CharsSpliteratorForString(this, Spliterator.IMMUTABLE), 3941 // END Android-removed: Delegate to StringUTF16. 3942 false); 3943 } 3944 3945 3946 /** 3947 * Returns a stream of code point values from this sequence. Any surrogate 3948 * pairs encountered in the sequence are combined as if by {@linkplain 3949 * Character#toCodePoint Character.toCodePoint} and the result is passed 3950 * to the stream. Any other code units, including ordinary BMP characters, 3951 * unpaired surrogates, and undefined code units, are zero-extended to 3952 * {@code int} values which are then passed to the stream. 3953 * 3954 * @return an IntStream of Unicode code points from this sequence 3955 * @since 9 3956 */ 3957 @Override 3958 public IntStream codePoints() { 3959 return StreamSupport.intStream( 3960 // BEGIN Android-removed: Delegate to StringUTF16. 3961 /* 3962 isLatin1() ? new StringLatin1.CharsSpliterator(value, Spliterator.IMMUTABLE) 3963 : new StringUTF16.CodePointsSpliterator(value, Spliterator.IMMUTABLE), 3964 */ 3965 new StringUTF16.CodePointsSpliteratorForString(this, Spliterator.IMMUTABLE), 3966 // END Android-removed: Delegate to StringUTF16. 3967 false); 3968 } 3969 3970 /** 3971 * Converts this string to a new character array. 3972 * 3973 * @return a newly allocated character array whose length is the length 3974 * of this string and whose contents are initialized to contain 3975 * the character sequence represented by this string. 3976 */ 3977 // BEGIN Android-changed: Replace with implementation in runtime to access chars (see above). 3978 /* 3979 public char[] toCharArray() { 3980 return isLatin1() ? StringLatin1.toChars(value) 3981 : StringUTF16.toChars(value); 3982 } 3983 */ 3984 @FastNative 3985 public native char[] toCharArray(); 3986 // END Android-changed: Replace with implementation in runtime to access chars (see above). 3987 3988 3989 /** 3990 * Returns a formatted string using the specified format string and 3991 * arguments. 3992 * 3993 * <p> The locale always used is the one returned by {@link 3994 * java.util.Locale#getDefault(java.util.Locale.Category) 3995 * Locale.getDefault(Locale.Category)} with 3996 * {@link java.util.Locale.Category#FORMAT FORMAT} category specified. 3997 * 3998 * @param format 3999 * A <a href="../util/Formatter.html#syntax">format string</a> 4000 * 4001 * @param args 4002 * Arguments referenced by the format specifiers in the format 4003 * string. If there are more arguments than format specifiers, the 4004 * extra arguments are ignored. The number of arguments is 4005 * variable and may be zero. The maximum number of arguments is 4006 * limited by the maximum dimension of a Java array as defined by 4007 * <cite>The Java™ Virtual Machine Specification</cite>. 4008 * The behaviour on a 4009 * {@code null} argument depends on the <a 4010 * href="../util/Formatter.html#syntax">conversion</a>. 4011 * 4012 * @throws java.util.IllegalFormatException 4013 * If a format string contains an illegal syntax, a format 4014 * specifier that is incompatible with the given arguments, 4015 * insufficient arguments given the format string, or other 4016 * illegal conditions. For specification of all possible 4017 * formatting errors, see the <a 4018 * href="../util/Formatter.html#detail">Details</a> section of the 4019 * formatter class specification. 4020 * 4021 * @return A formatted string 4022 * 4023 * @see java.util.Formatter 4024 * @since 1.5 4025 */ 4026 public static String format(String format, Object... args) { 4027 return new Formatter().format(format, args).toString(); 4028 } 4029 4030 /** 4031 * Returns a formatted string using the specified locale, format string, 4032 * and arguments. 4033 * 4034 * @param l 4035 * The {@linkplain java.util.Locale locale} to apply during 4036 * formatting. If {@code l} is {@code null} then no localization 4037 * is applied. 4038 * 4039 * @param format 4040 * A <a href="../util/Formatter.html#syntax">format string</a> 4041 * 4042 * @param args 4043 * Arguments referenced by the format specifiers in the format 4044 * string. If there are more arguments than format specifiers, the 4045 * extra arguments are ignored. The number of arguments is 4046 * variable and may be zero. The maximum number of arguments is 4047 * limited by the maximum dimension of a Java array as defined by 4048 * <cite>The Java™ Virtual Machine Specification</cite>. 4049 * The behaviour on a 4050 * {@code null} argument depends on the 4051 * <a href="../util/Formatter.html#syntax">conversion</a>. 4052 * 4053 * @throws java.util.IllegalFormatException 4054 * If a format string contains an illegal syntax, a format 4055 * specifier that is incompatible with the given arguments, 4056 * insufficient arguments given the format string, or other 4057 * illegal conditions. For specification of all possible 4058 * formatting errors, see the <a 4059 * href="../util/Formatter.html#detail">Details</a> section of the 4060 * formatter class specification 4061 * 4062 * @return A formatted string 4063 * 4064 * @see java.util.Formatter 4065 * @since 1.5 4066 */ 4067 public static String format(Locale l, String format, Object... args) { 4068 return new Formatter(l).format(format, args).toString(); 4069 } 4070 4071 /** 4072 * Formats using this string as the format string, and the supplied 4073 * arguments. 4074 * 4075 * @implSpec This method is equivalent to {@code String.format(this, args)}. 4076 * 4077 * @param args 4078 * Arguments referenced by the format specifiers in this string. 4079 * 4080 * @return A formatted string 4081 * 4082 * @see java.lang.String#format(String,Object...) 4083 * @see java.util.Formatter 4084 * 4085 * @since 15 4086 * 4087 */ 4088 public String formatted(Object... args) { 4089 return new Formatter().format(this, args).toString(); 4090 } 4091 4092 /** 4093 * Returns the string representation of the {@code Object} argument. 4094 * 4095 * @param obj an {@code Object}. 4096 * @return if the argument is {@code null}, then a string equal to 4097 * {@code "null"}; otherwise, the value of 4098 * {@code obj.toString()} is returned. 4099 * @see java.lang.Object#toString() 4100 */ 4101 public static String valueOf(Object obj) { 4102 return (obj == null) ? "null" : obj.toString(); 4103 } 4104 4105 /** 4106 * Returns the string representation of the {@code char} array 4107 * argument. The contents of the character array are copied; subsequent 4108 * modification of the character array does not affect the returned 4109 * string. 4110 * 4111 * @param data the character array. 4112 * @return a {@code String} that contains the characters of the 4113 * character array. 4114 */ 4115 public static String valueOf(char data[]) { 4116 return new String(data); 4117 } 4118 4119 /** 4120 * Returns the string representation of a specific subarray of the 4121 * {@code char} array argument. 4122 * <p> 4123 * The {@code offset} argument is the index of the first 4124 * character of the subarray. The {@code count} argument 4125 * specifies the length of the subarray. The contents of the subarray 4126 * are copied; subsequent modification of the character array does not 4127 * affect the returned string. 4128 * 4129 * @param data the character array. 4130 * @param offset initial offset of the subarray. 4131 * @param count length of the subarray. 4132 * @return a {@code String} that contains the characters of the 4133 * specified subarray of the character array. 4134 * @exception IndexOutOfBoundsException if {@code offset} is 4135 * negative, or {@code count} is negative, or 4136 * {@code offset+count} is larger than 4137 * {@code data.length}. 4138 */ 4139 public static String valueOf(char data[], int offset, int count) { 4140 return new String(data, offset, count); 4141 } 4142 4143 /** 4144 * Equivalent to {@link #valueOf(char[], int, int)}. 4145 * 4146 * @param data the character array. 4147 * @param offset initial offset of the subarray. 4148 * @param count length of the subarray. 4149 * @return a {@code String} that contains the characters of the 4150 * specified subarray of the character array. 4151 * @exception IndexOutOfBoundsException if {@code offset} is 4152 * negative, or {@code count} is negative, or 4153 * {@code offset+count} is larger than 4154 * {@code data.length}. 4155 */ 4156 public static String copyValueOf(char data[], int offset, int count) { 4157 return new String(data, offset, count); 4158 } 4159 4160 /** 4161 * Equivalent to {@link #valueOf(char[])}. 4162 * 4163 * @param data the character array. 4164 * @return a {@code String} that contains the characters of the 4165 * character array. 4166 */ 4167 public static String copyValueOf(char data[]) { 4168 return new String(data); 4169 } 4170 4171 /** 4172 * Returns the string representation of the {@code boolean} argument. 4173 * 4174 * @param b a {@code boolean}. 4175 * @return if the argument is {@code true}, a string equal to 4176 * {@code "true"} is returned; otherwise, a string equal to 4177 * {@code "false"} is returned. 4178 */ 4179 public static String valueOf(boolean b) { 4180 return b ? "true" : "false"; 4181 } 4182 4183 /** 4184 * Returns the string representation of the {@code char} 4185 * argument. 4186 * 4187 * @param c a {@code char}. 4188 * @return a string of length {@code 1} containing 4189 * as its single character the argument {@code c}. 4190 */ 4191 public static String valueOf(char c) { 4192 // BEGIN Android-changed: Replace constructor call with call to StringFactory class. 4193 // There is currently no String(char[], boolean) on Android to call. http://b/79902155 4194 /* 4195 if (COMPACT_STRINGS && StringLatin1.canEncode(c)) { 4196 return new String(StringLatin1.toBytes(c), LATIN1); 4197 } 4198 return new String(StringUTF16.toBytes(c), UTF16); 4199 */ 4200 return StringFactory.newStringFromChars(0, 1, new char[] { c }); 4201 // END Android-changed: Replace constructor call with call to StringFactory class. 4202 } 4203 4204 /** 4205 * Returns the string representation of the {@code int} argument. 4206 * <p> 4207 * The representation is exactly the one returned by the 4208 * {@code Integer.toString} method of one argument. 4209 * 4210 * @param i an {@code int}. 4211 * @return a string representation of the {@code int} argument. 4212 * @see java.lang.Integer#toString(int, int) 4213 */ 4214 public static String valueOf(int i) { 4215 return Integer.toString(i); 4216 } 4217 4218 /** 4219 * Returns the string representation of the {@code long} argument. 4220 * <p> 4221 * The representation is exactly the one returned by the 4222 * {@code Long.toString} method of one argument. 4223 * 4224 * @param l a {@code long}. 4225 * @return a string representation of the {@code long} argument. 4226 * @see java.lang.Long#toString(long) 4227 */ 4228 public static String valueOf(long l) { 4229 return Long.toString(l); 4230 } 4231 4232 /** 4233 * Returns the string representation of the {@code float} argument. 4234 * <p> 4235 * The representation is exactly the one returned by the 4236 * {@code Float.toString} method of one argument. 4237 * 4238 * @param f a {@code float}. 4239 * @return a string representation of the {@code float} argument. 4240 * @see java.lang.Float#toString(float) 4241 */ 4242 public static String valueOf(float f) { 4243 return Float.toString(f); 4244 } 4245 4246 /** 4247 * Returns the string representation of the {@code double} argument. 4248 * <p> 4249 * The representation is exactly the one returned by the 4250 * {@code Double.toString} method of one argument. 4251 * 4252 * @param d a {@code double}. 4253 * @return a string representation of the {@code double} argument. 4254 * @see java.lang.Double#toString(double) 4255 */ 4256 public static String valueOf(double d) { 4257 return Double.toString(d); 4258 } 4259 4260 /** 4261 * Returns a canonical representation for the string object. 4262 * <p> 4263 * A pool of strings, initially empty, is maintained privately by the 4264 * class {@code String}. 4265 * <p> 4266 * When the intern method is invoked, if the pool already contains a 4267 * string equal to this {@code String} object as determined by 4268 * the {@link #equals(Object)} method, then the string from the pool is 4269 * returned. Otherwise, this {@code String} object is added to the 4270 * pool and a reference to this {@code String} object is returned. 4271 * <p> 4272 * It follows that for any two strings {@code s} and {@code t}, 4273 * {@code s.intern() == t.intern()} is {@code true} 4274 * if and only if {@code s.equals(t)} is {@code true}. 4275 * <p> 4276 * All literal strings and string-valued constant expressions are 4277 * interned. String literals are defined in section 3.10.5 of the 4278 * <cite>The Java™ Language Specification</cite>. 4279 * 4280 * @return a string that has the same contents as this string, but is 4281 * guaranteed to be from a pool of unique strings. 4282 * @jls 3.10.5 String Literals 4283 */ 4284 // Android-added: Annotate native method as @FastNative. 4285 @FastNative 4286 public native String intern(); 4287 4288 /** 4289 * Returns a string whose value is the concatenation of this 4290 * string repeated {@code count} times. 4291 * <p> 4292 * If this string is empty or count is zero then the empty 4293 * string is returned. 4294 * 4295 * @param count number of times to repeat 4296 * 4297 * @return A string composed of this string repeated 4298 * {@code count} times or the empty string if this 4299 * string is empty or count is zero 4300 * 4301 * @throws IllegalArgumentException if the {@code count} is 4302 * negative. 4303 * 4304 * @since 11 4305 */ 4306 public String repeat(int count) { 4307 if (count < 0) { 4308 throw new IllegalArgumentException("count is negative: " + count); 4309 } 4310 if (count == 1) { 4311 return this; 4312 } 4313 // Android-changed: Replace with implementation in runtime. 4314 // final int len = value.length; 4315 final int len = length(); 4316 if (len == 0 || count == 0) { 4317 return ""; 4318 } 4319 // BEGIN Android-changed: Replace with implementation in runtime. 4320 /* 4321 if (len == 1) { 4322 final byte[] single = new byte[count]; 4323 Arrays.fill(single, value[0]); 4324 return new String(single, coder); 4325 } 4326 */ 4327 // END Android-changed: Replace with implementation in runtime. 4328 if (Integer.MAX_VALUE / count < len) { 4329 throw new OutOfMemoryError("Repeating " + len + " bytes String " + count + 4330 " times will produce a String exceeding maximum size."); 4331 } 4332 // BEGIN Android-changed: Replace with implementation in runtime. 4333 /* 4334 final int limit = len * count; 4335 final byte[] multiple = new byte[limit]; 4336 System.arraycopy(value, 0, multiple, 0, len); 4337 int copied = len; 4338 for (; copied < limit - copied; copied <<= 1) { 4339 System.arraycopy(multiple, 0, multiple, copied, copied); 4340 } 4341 System.arraycopy(multiple, 0, multiple, copied, limit - copied); 4342 return new String(multiple, coder); 4343 */ 4344 // END Android-changed: Replace with implementation in runtime. 4345 return doRepeat(count); 4346 } 4347 4348 @FastNative 4349 private native String doRepeat(int count); 4350 4351 //////////////////////////////////////////////////////////////// 4352 4353 /** 4354 * Copy character bytes from this string into dst starting at dstBegin. 4355 * This method doesn't perform any range checking. 4356 * 4357 * Invoker guarantees: dst is in UTF16 (inflate itself for asb), if two 4358 * coders are different, and dst is big enough (range check) 4359 * 4360 * @param dstBegin the char index, not offset of byte[] 4361 * @param coder the coder of dst[] 4362 */ 4363 void getBytes(byte dst[], int dstBegin, byte coder) { 4364 // Android-changed: libcore doesn't store String as Latin1 or UTF16 byte[] field. 4365 /* 4366 if (coder() == coder) { 4367 System.arraycopy(value, 0, dst, dstBegin << coder, value.length); 4368 } else { // this.coder == LATIN && coder == UTF16 4369 StringLatin1.inflate(value, 0, dst, dstBegin, value.length); 4370 } 4371 */ 4372 // We do bound check here before the native calls, because the upstream implementation does 4373 // the bound check in System.arraycopy and StringLatin1.inflate or throws an exception. 4374 if (coder == UTF16) { 4375 int fromIndex = dstBegin << 1; 4376 checkBoundsOffCount(fromIndex, length() << 1, dst.length); 4377 fillBytesUTF16(dst, fromIndex); 4378 } else { 4379 if (coder() != LATIN1) { 4380 // Do not concat String in the error message. 4381 throw new StringIndexOutOfBoundsException("Expect Latin-1 coder."); 4382 } 4383 checkBoundsOffCount(dstBegin, length(), dst.length); 4384 fillBytesLatin1(dst, dstBegin); 4385 } 4386 } 4387 4388 // BEGIN Android-added: Implement fillBytes*() method natively. 4389 4390 /** 4391 * Fill the underlying characters into the byte buffer. No range check. 4392 * The caller should guarantee that dst is big enough for this operation. 4393 */ 4394 @FastNative 4395 private native void fillBytesLatin1(byte[] dst, int byteIndex); 4396 4397 /** 4398 * Fill the underlying characters into the byte buffer. No range check. 4399 * The caller should guarantee that dst is big enough for this operation. 4400 */ 4401 @FastNative 4402 private native void fillBytesUTF16(byte[] dst, int byteIndex); 4403 // END Android-added: Implement fillBytes*() method natively. 4404 4405 /* 4406 * Package private constructor which shares value array for speed. 4407 */ 4408 String(byte[] value, byte coder) { 4409 // BEGIN Android-changed: Implemented as compiler and runtime intrinsics. 4410 // this.value = value; 4411 // this.coder = coder; 4412 throw new UnsupportedOperationException("Use StringFactory instead."); 4413 // END Android-changed: Implemented as compiler and runtime intrinsics. 4414 } 4415 4416 /** 4417 * Android note: It returns UTF16 if the string has any 0x00 char. 4418 * See the difference between {@link StringLatin1#canEncode(int)} and 4419 * art::mirror::String::IsASCII(uint16_t) in string.h. 4420 */ 4421 byte coder() { 4422 // Android-changed: ART stores the flag in the count field. 4423 // return COMPACT_STRINGS ? coder : UTF16; 4424 // We assume that STRING_COMPRESSION_ENABLED is enabled here. 4425 // The flag has been true for 6+ years. 4426 return COMPACT_STRINGS ? ((byte) (count & 1)) : UTF16; 4427 } 4428 4429 /* 4430 * StringIndexOutOfBoundsException if {@code index} is 4431 * negative or greater than or equal to {@code length}. 4432 */ 4433 static void checkIndex(int index, int length) { 4434 if (index < 0 || index >= length) { 4435 throw new StringIndexOutOfBoundsException("index " + index + 4436 ",length " + length); 4437 } 4438 } 4439 4440 /* 4441 * StringIndexOutOfBoundsException if {@code offset} 4442 * is negative or greater than {@code length}. 4443 */ 4444 static void checkOffset(int offset, int length) { 4445 if (offset < 0 || offset > length) { 4446 throw new StringIndexOutOfBoundsException("offset " + offset + 4447 ",length " + length); 4448 } 4449 } 4450 4451 /* 4452 * Check {@code offset}, {@code count} against {@code 0} and {@code length} 4453 * bounds. 4454 * 4455 * @throws StringIndexOutOfBoundsException 4456 * If {@code offset} is negative, {@code count} is negative, 4457 * or {@code offset} is greater than {@code length - count} 4458 */ 4459 static void checkBoundsOffCount(int offset, int count, int length) { 4460 if (offset < 0 || count < 0 || offset > length - count) { 4461 throw new StringIndexOutOfBoundsException( 4462 "offset " + offset + ", count " + count + ", length " + length); 4463 } 4464 } 4465 4466 /** 4467 * Returns the string representation of the {@code codePoint} 4468 * argument. 4469 * 4470 * @param codePoint a {@code codePoint}. 4471 * @return a string of length {@code 1} or {@code 2} containing 4472 * as its single character the argument {@code codePoint}. 4473 * @throws IllegalArgumentException if the specified 4474 * {@code codePoint} is not a {@linkplain Character#isValidCodePoint 4475 * valid Unicode code point}. 4476 */ 4477 static String valueOfCodePoint(int codePoint) { 4478 if (COMPACT_STRINGS && StringLatin1.canEncode(codePoint)) { 4479 return new String(StringLatin1.toBytes((char)codePoint), LATIN1); 4480 } else if (Character.isBmpCodePoint(codePoint)) { 4481 return new String(StringUTF16.toBytes((char)codePoint), UTF16); 4482 } else if (Character.isSupplementaryCodePoint(codePoint)) { 4483 return new String(StringUTF16.toBytesSupplementary(codePoint), UTF16); 4484 } 4485 4486 throw new IllegalArgumentException( 4487 format("Not a valid Unicode code point: 0x%X", codePoint)); 4488 } 4489 4490 /* 4491 * Check {@code begin}, {@code end} against {@code 0} and {@code length} 4492 * bounds. 4493 * 4494 * @throws StringIndexOutOfBoundsException 4495 * If {@code begin} is negative, {@code begin} is greater than 4496 * {@code end}, or {@code end} is greater than {@code length}. 4497 */ 4498 static void checkBoundsBeginEnd(int begin, int end, int length) { 4499 if (begin < 0 || begin > end || end > length) { 4500 throw new StringIndexOutOfBoundsException( 4501 "begin " + begin + ", end " + end + ", length " + length); 4502 } 4503 } 4504 4505 /** 4506 * Returns an {@link Optional} containing the nominal descriptor for this 4507 * instance, which is the instance itself. 4508 * 4509 * @return an {@link Optional} describing the {@linkplain String} instance 4510 * @since 12 4511 * @hide 4512 */ 4513 @Override 4514 public Optional<String> describeConstable() { 4515 return Optional.of(this); 4516 } 4517 4518 /** 4519 * Resolves this instance as a {@link ConstantDesc}, the result of which is 4520 * the instance itself. 4521 * 4522 * @param lookup ignored 4523 * @return the {@linkplain String} instance 4524 * @since 12 4525 * @hide 4526 */ 4527 @Override 4528 public String resolveConstantDesc(MethodHandles.Lookup lookup) { 4529 return this; 4530 } 4531 } 4532