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