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