src/java.base/share/classes/java/lang/String.java
author jlahoda
Wed, 02 Oct 2019 10:59:40 +0200
branchJDK-8226585-branch
changeset 58440 4c8a6d1d066c
parent 58109 ee07de0d2c16
permissions -rw-r--r--
Improving appearance of javadoc of the APIs associated with preview features.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53563
a4b7ea85d668 8218022: Repeated words typos in java.base
prappo
parents: 53245
diff changeset
     2
 * Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5469
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5469
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5469
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5469
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5469
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.ObjectStreamField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.UnsupportedEncodingException;
45572
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 45138
diff changeset
    30
import java.lang.annotation.Native;
52914
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
    31
import java.lang.invoke.MethodHandles;
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
    32
import java.lang.constant.Constable;
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
    33
import java.lang.constant.ConstantDesc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.charset.Charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Formatter;
55260
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
    39
import java.util.List;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Locale;
15312
4b57f9da8192 4247235: (spec str) StringBuffer.insert(int, char[]) specification is inconsistent
jgish
parents: 14997
diff changeset
    41
import java.util.Objects;
52914
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
    42
import java.util.Optional;
28667
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
    43
import java.util.Spliterator;
17181
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
    44
import java.util.StringJoiner;
52682
c93e62e7fa3c 8203442: String::transform
jlaskey
parents: 52312
diff changeset
    45
import java.util.function.Function;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.regex.Matcher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.regex.Pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.regex.PatternSyntaxException;
51714
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
    49
import java.util.stream.Collectors;
28667
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
    50
import java.util.stream.IntStream;
50215
2fb27c352cae 8200380: String::lines
jlaskey
parents: 50175
diff changeset
    51
import java.util.stream.Stream;
28667
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
    52
import java.util.stream.StreamSupport;
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 30913
diff changeset
    53
import jdk.internal.HotSpotIntrinsicCandidate;
36413
0870841a16ce 8150180: String.value contents should be trusted
shade
parents: 35302
diff changeset
    54
import jdk.internal.vm.annotation.Stable;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
51714
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
    56
import static java.util.function.Predicate.not;
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
    57
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
/**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
    59
 * The {@code String} class represents character strings. All
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
    60
 * string literals in Java programs, such as {@code "abc"}, are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * implemented as instances of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * Strings are constant; their values cannot be changed after they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * are created. String buffers support mutable strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * Because String objects are immutable they can be shared. For example:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 21308
diff changeset
    66
 * <blockquote><pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *     String str = "abc";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * </pre></blockquote><p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * is equivalent to:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 21308
diff changeset
    70
 * <blockquote><pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *     char data[] = {'a', 'b', 'c'};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *     String str = new String(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * </pre></blockquote><p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * Here are some more examples of how strings can be used:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 21308
diff changeset
    75
 * <blockquote><pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *     System.out.println("abc");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *     String cde = "cde";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *     System.out.println("abc" + cde);
54728
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
    79
 *     String c = "abc".substring(2, 3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *     String d = cde.substring(1, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
    83
 * The class {@code String} includes methods for examining
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * individual characters of the sequence, for comparing strings, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * searching strings, for extracting substrings, and for creating a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * copy of a string with all characters translated to uppercase or to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * lowercase. Case mapping is based on the Unicode Standard version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * specified by the {@link java.lang.Character Character} class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * The Java language provides special support for the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * concatenation operator (&nbsp;+&nbsp;), and for conversion of
37718
79b158dae903 8155215: java.lang.String concatenation spec is unnecessarily strong
shade
parents: 37521
diff changeset
    92
 * other objects to strings. For additional information on string
79b158dae903 8155215: java.lang.String concatenation spec is unnecessarily strong
shade
parents: 37521
diff changeset
    93
 * concatenation and conversion, see <i>The Java&trade; Language Specification</i>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31671
diff changeset
    95
 * <p> Unless otherwise noted, passing a {@code null} argument to a constructor
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * or method in this class will cause a {@link NullPointerException} to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
    99
 * <p>A {@code String} represents a string in the UTF-16 format
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * in which <em>supplementary characters</em> are represented by <em>surrogate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * pairs</em> (see the section <a href="Character.html#unicode">Unicode
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   102
 * Character Representations</a> in the {@code Character} class for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * more information).
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   104
 * Index values refer to {@code char} code units, so a supplementary
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   105
 * character uses two positions in a {@code String}.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   106
 * <p>The {@code String} class provides methods for dealing with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * Unicode code points (i.e., characters), in addition to those for
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   108
 * dealing with Unicode code units (i.e., {@code char} values).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
33314
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
   110
 * <p>Unless otherwise noted, methods for comparing Strings do not take locale
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
   111
 * into account.  The {@link java.text.Collator} class provides methods for
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
   112
 * finer-grain, locale-sensitive String comparison.
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
   113
 *
37718
79b158dae903 8155215: java.lang.String concatenation spec is unnecessarily strong
shade
parents: 37521
diff changeset
   114
 * @implNote The implementation of the string concatenation operator is left to
79b158dae903 8155215: java.lang.String concatenation spec is unnecessarily strong
shade
parents: 37521
diff changeset
   115
 * the discretion of a Java compiler, as long as the compiler ultimately conforms
79b158dae903 8155215: java.lang.String concatenation spec is unnecessarily strong
shade
parents: 37521
diff changeset
   116
 * to <i>The Java&trade; Language Specification</i>. For example, the {@code javac} compiler
79b158dae903 8155215: java.lang.String concatenation spec is unnecessarily strong
shade
parents: 37521
diff changeset
   117
 * may implement the operator with {@code StringBuffer}, {@code StringBuilder},
79b158dae903 8155215: java.lang.String concatenation spec is unnecessarily strong
shade
parents: 37521
diff changeset
   118
 * or {@code java.lang.invoke.StringConcatFactory} depending on the JDK version. The
79b158dae903 8155215: java.lang.String concatenation spec is unnecessarily strong
shade
parents: 37521
diff changeset
   119
 * implementation of string conversion is typically through the method {@code toString},
79b158dae903 8155215: java.lang.String concatenation spec is unnecessarily strong
shade
parents: 37521
diff changeset
   120
 * defined by {@code Object} and inherited by all classes in Java.
79b158dae903 8155215: java.lang.String concatenation spec is unnecessarily strong
shade
parents: 37521
diff changeset
   121
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * @author  Lee Boynton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * @author  Arthur van Hoff
5986
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   124
 * @author  Martin Buchholz
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   125
 * @author  Ulf Zibis
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * @see     java.lang.Object#toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * @see     java.lang.StringBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * @see     java.lang.StringBuilder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * @see     java.nio.charset.Charset
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24374
diff changeset
   130
 * @since   1.0
37723
1b9abe91485d 8155792: Add @jls citations to java.lang.String
darcy
parents: 37718
diff changeset
   131
 * @jls     15.18.1 String Concatenation Operator +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
public final class String
52914
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
   135
    implements java.io.Serializable, Comparable<String>, CharSequence,
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
   136
               Constable, ConstantDesc {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   137
36413
0870841a16ce 8150180: String.value contents should be trusted
shade
parents: 35302
diff changeset
   138
    /**
0870841a16ce 8150180: String.value contents should be trusted
shade
parents: 35302
diff changeset
   139
     * The value is used for character storage.
0870841a16ce 8150180: String.value contents should be trusted
shade
parents: 35302
diff changeset
   140
     *
0870841a16ce 8150180: String.value contents should be trusted
shade
parents: 35302
diff changeset
   141
     * @implNote This field is trusted by the VM, and is a subject to
0870841a16ce 8150180: String.value contents should be trusted
shade
parents: 35302
diff changeset
   142
     * constant folding if String instance is constant. Overwriting this
0870841a16ce 8150180: String.value contents should be trusted
shade
parents: 35302
diff changeset
   143
     * field after construction will cause problems.
0870841a16ce 8150180: String.value contents should be trusted
shade
parents: 35302
diff changeset
   144
     *
0870841a16ce 8150180: String.value contents should be trusted
shade
parents: 35302
diff changeset
   145
     * Additionally, it is marked with {@link Stable} to trust the contents
0870841a16ce 8150180: String.value contents should be trusted
shade
parents: 35302
diff changeset
   146
     * of the array. No other facility in JDK provides this functionality (yet).
0870841a16ce 8150180: String.value contents should be trusted
shade
parents: 35302
diff changeset
   147
     * {@link Stable} is safe here, because value is never null.
0870841a16ce 8150180: String.value contents should be trusted
shade
parents: 35302
diff changeset
   148
     */
0870841a16ce 8150180: String.value contents should be trusted
shade
parents: 35302
diff changeset
   149
    @Stable
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   150
    private final byte[] value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   151
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   152
    /**
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   153
     * The identifier of the encoding used to encode the bytes in
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   154
     * {@code value}. The supported values in this implementation are
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   155
     *
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   156
     * LATIN1
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   157
     * UTF16
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   158
     *
36413
0870841a16ce 8150180: String.value contents should be trusted
shade
parents: 35302
diff changeset
   159
     * @implNote This field is trusted by the VM, and is a subject to
0870841a16ce 8150180: String.value contents should be trusted
shade
parents: 35302
diff changeset
   160
     * constant folding if String instance is constant. Overwriting this
0870841a16ce 8150180: String.value contents should be trusted
shade
parents: 35302
diff changeset
   161
     * field after construction will cause problems.
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   162
     */
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   163
    private final byte coder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /** Cache the hash code for the string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private int hash; // Default to 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
54486
7fd299216e97 8221836: Avoid recalculating String.hash when zero
redestad
parents: 54378
diff changeset
   168
    /**
7fd299216e97 8221836: Avoid recalculating String.hash when zero
redestad
parents: 54378
diff changeset
   169
     * Cache if the hash has been calculated as actually being zero, enabling
7fd299216e97 8221836: Avoid recalculating String.hash when zero
redestad
parents: 54378
diff changeset
   170
     * us to avoid recalculating this.
7fd299216e97 8221836: Avoid recalculating String.hash when zero
redestad
parents: 54378
diff changeset
   171
     */
7fd299216e97 8221836: Avoid recalculating String.hash when zero
redestad
parents: 54378
diff changeset
   172
    private boolean hashIsZero; // Default to false;
7fd299216e97 8221836: Avoid recalculating String.hash when zero
redestad
parents: 54378
diff changeset
   173
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 57783
diff changeset
   175
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    private static final long serialVersionUID = -6849794470754667710L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   179
     * If String compaction is disabled, the bytes in {@code value} are
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   180
     * always encoded in UTF16.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   181
     *
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   182
     * For methods with several possible implementation paths, when String
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   183
     * compaction is disabled, only one code path is taken.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   184
     *
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   185
     * The instance field value is generally opaque to optimizing JIT
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   186
     * compilers. Therefore, in performance-sensitive place, an explicit
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   187
     * check of the static boolean {@code COMPACT_STRINGS} is done first
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   188
     * before checking the {@code coder} field since the static boolean
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   189
     * {@code COMPACT_STRINGS} would be constant folded away by an
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   190
     * optimizing JIT compiler. The idioms for these cases are as follows.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   191
     *
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   192
     * For code such as:
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   193
     *
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   194
     *    if (coder == LATIN1) { ... }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   195
     *
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   196
     * can be written more optimally as
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   197
     *
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   198
     *    if (coder() == LATIN1) { ... }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   199
     *
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   200
     * or:
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   201
     *
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   202
     *    if (COMPACT_STRINGS && coder == LATIN1) { ... }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   203
     *
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   204
     * An optimizing JIT compiler can fold the above conditional as:
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   205
     *
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   206
     *    COMPACT_STRINGS == true  => if (coder == LATIN1) { ... }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   207
     *    COMPACT_STRINGS == false => if (false)           { ... }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   208
     *
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   209
     * @implNote
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   210
     * The actual value for this field is injected by JVM. The static
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   211
     * initialization block is used to set the value here to communicate
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   212
     * that this static final field is not statically foldable, and to
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   213
     * avoid any possible circular dependency during vm initialization.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   214
     */
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   215
    static final boolean COMPACT_STRINGS;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   216
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   217
    static {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   218
        COMPACT_STRINGS = true;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   219
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   220
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   221
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Class String is special cased within the Serialization Stream Protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
21636
fcec9002d5f4 8028041: Serialized Form description of j.l.String is not consistent with the implementation
rriggs
parents: 21334
diff changeset
   224
     * A String instance is written into an ObjectOutputStream according to
45138
ddcafe0d0ea3 8180082: Broken javadoc links
rriggs
parents: 45124
diff changeset
   225
     * <a href="{@docRoot}/../specs/serialization/protocol.html#stream-elements">
21636
fcec9002d5f4 8028041: Serialized Form description of j.l.String is not consistent with the implementation
rriggs
parents: 21334
diff changeset
   226
     * Object Serialization Specification, Section 6.2, "Stream Elements"</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 57783
diff changeset
   228
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    private static final ObjectStreamField[] serialPersistentFields =
17181
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
   230
        new ObjectStreamField[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Initializes a newly created {@code String} object so that it represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * an empty character sequence.  Note that use of this constructor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * unnecessary since Strings are immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public String() {
28423
0bf78b38bc0b 8067471: Use private static final char[0] for empty Strings
lpriima
parents: 27955
diff changeset
   238
        this.value = "".value;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   239
        this.coder = "".coder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Initializes a newly created {@code String} object so that it represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * the same sequence of characters as the argument; in other words, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * newly created string is a copy of the argument string. Unless an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * explicit copy of {@code original} is needed, use of this constructor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * unnecessary since Strings are immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @param  original
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *         A {@code String}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 30913
diff changeset
   252
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public String(String original) {
12858
97e3f3f77254 6924259: Remove offset and count fields from java.lang.String
mduigou
parents: 11676
diff changeset
   254
        this.value = original.value;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   255
        this.coder = original.coder;
12858
97e3f3f77254 6924259: Remove offset and count fields from java.lang.String
mduigou
parents: 11676
diff changeset
   256
        this.hash = original.hash;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Allocates a new {@code String} so that it represents the sequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * characters currently contained in the character array argument. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * contents of the character array are copied; subsequent modification of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * the character array does not affect the newly created string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @param  value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *         The initial value of the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public String(char value[]) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   269
        this(value, 0, value.length, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Allocates a new {@code String} that contains characters from a subarray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * of the character array argument. The {@code offset} argument is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * index of the first character of the subarray and the {@code count}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * argument specifies the length of the subarray. The contents of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * subarray are copied; subsequent modification of the character array does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * not affect the newly created string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @param  value
30642
d9891f85d583 8071571: Move substring of same string to slow path
igerasim
parents: 30640
diff changeset
   281
     *         Array that is the source of characters
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *         The initial offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @param  count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *         The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @throws  IndexOutOfBoundsException
27955
8d98ed07bc28 8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents: 27087
diff changeset
   290
     *          If {@code offset} is negative, {@code count} is negative, or
8d98ed07bc28 8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents: 27087
diff changeset
   291
     *          {@code offset} is greater than {@code value.length - count}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public String(char value[], int offset, int count) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   294
        this(value, offset, count, rangeCheck(value, offset, count));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   295
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   296
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   297
    private static Void rangeCheck(char[] value, int offset, int count) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   298
        checkBoundsOffCount(offset, count, value.length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   299
        return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Allocates a new {@code String} that contains characters from a subarray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * of the <a href="Character.html#unicode">Unicode code point</a> array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * argument.  The {@code offset} argument is the index of the first code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * point of the subarray and the {@code count} argument specifies the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * length of the subarray.  The contents of the subarray are converted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * {@code char}s; subsequent modification of the {@code int} array does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * affect the newly created string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param  codePoints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *         Array that is the source of Unicode code points
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *         The initial offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @param  count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *         The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *          If any invalid Unicode code point is found in {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *          codePoints}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @throws  IndexOutOfBoundsException
27955
8d98ed07bc28 8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents: 27087
diff changeset
   325
     *          If {@code offset} is negative, {@code count} is negative, or
8d98ed07bc28 8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents: 27087
diff changeset
   326
     *          {@code offset} is greater than {@code codePoints.length - count}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public String(int[] codePoints, int offset, int count) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   331
        checkBoundsOffCount(offset, count, codePoints.length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   332
        if (count == 0) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   333
            this.value = "".value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   334
            this.coder = "".coder;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   335
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   337
        if (COMPACT_STRINGS) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   338
            byte[] val = StringLatin1.toBytes(codePoints, offset, count);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   339
            if (val != null) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   340
                this.coder = LATIN1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   341
                this.value = val;
28423
0bf78b38bc0b 8067471: Use private static final char[0] for empty Strings
lpriima
parents: 27955
diff changeset
   342
                return;
0bf78b38bc0b 8067471: Use private static final char[0] for empty Strings
lpriima
parents: 27955
diff changeset
   343
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   345
        this.coder = UTF16;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   346
        this.value = StringUTF16.toBytes(codePoints, offset, count);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Allocates a new {@code String} constructed from a subarray of an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * of 8-bit integer values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * <p> The {@code offset} argument is the index of the first byte of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * subarray, and the {@code count} argument specifies the length of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * subarray.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * <p> Each {@code byte} in the subarray is converted to a {@code char} as
45121
5bf18ccab99e 8180128: small errors in String javadoc
smarks
parents: 44846
diff changeset
   358
     * specified in the {@link #String(byte[],int) String(byte[],int)} constructor.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @deprecated This method does not properly convert bytes into characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * As of JDK&nbsp;1.1, the preferred way to do this is via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * {@code String} constructors that take a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * java.nio.charset.Charset}, charset name, or that use the platform's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * default charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @param  ascii
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *         The bytes to be converted to characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @param  hibyte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *         The top 8 bits of each 16-bit Unicode code unit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *         The initial offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @param  count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *         The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @throws  IndexOutOfBoundsException
27955
8d98ed07bc28 8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents: 27087
diff changeset
   378
     *          If {@code offset} is negative, {@code count} is negative, or
8d98ed07bc28 8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents: 27087
diff changeset
   379
     *          {@code offset} is greater than {@code ascii.length - count}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @see  #String(byte[], int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @see  #String(byte[], int, int, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @see  #String(byte[], int, int, java.nio.charset.Charset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @see  #String(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @see  #String(byte[], java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @see  #String(byte[], java.nio.charset.Charset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @see  #String(byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     */
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 36431
diff changeset
   389
    @Deprecated(since="1.1")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    public String(byte ascii[], int hibyte, int offset, int count) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   391
        checkBoundsOffCount(offset, count, ascii.length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   392
        if (count == 0) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   393
            this.value = "".value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   394
            this.coder = "".coder;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   395
            return;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   396
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   397
        if (COMPACT_STRINGS && (byte)hibyte == 0) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   398
            this.value = Arrays.copyOfRange(ascii, offset, offset + count);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   399
            this.coder = LATIN1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            hibyte <<= 8;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   402
            byte[] val = StringUTF16.newBytesFor(count);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   403
            for (int i = 0; i < count; i++) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   404
                StringUTF16.putChar(val, i, hibyte | (ascii[offset++] & 0xff));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   406
            this.value = val;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   407
            this.coder = UTF16;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * Allocates a new {@code String} containing characters constructed from
45121
5bf18ccab99e 8180128: small errors in String javadoc
smarks
parents: 44846
diff changeset
   413
     * an array of 8-bit integer values. Each character <i>c</i> in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * resulting string is constructed from the corresponding component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * <i>b</i> in the byte array such that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *     <b><i>c</i></b> == (char)(((hibyte &amp; 0xff) &lt;&lt; 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *                         | (<b><i>b</i></b> &amp; 0xff))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @deprecated  This method does not properly convert bytes into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * characters.  As of JDK&nbsp;1.1, the preferred way to do this is via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * {@code String} constructors that take a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * java.nio.charset.Charset}, charset name, or that use the platform's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * default charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @param  ascii
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *         The bytes to be converted to characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @param  hibyte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *         The top 8 bits of each 16-bit Unicode code unit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @see  #String(byte[], int, int, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @see  #String(byte[], int, int, java.nio.charset.Charset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @see  #String(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @see  #String(byte[], java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @see  #String(byte[], java.nio.charset.Charset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @see  #String(byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 36431
diff changeset
   441
    @Deprecated(since="1.1")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    public String(byte ascii[], int hibyte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        this(ascii, hibyte, 0, ascii.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * Constructs a new {@code String} by decoding the specified subarray of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * bytes using the specified charset.  The length of the new {@code String}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * is a function of the charset, and hence may not be equal to the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * of the subarray.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * <p> The behavior of this constructor when the given bytes are not valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * in the given charset is unspecified.  The {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * java.nio.charset.CharsetDecoder} class should be used when more control
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * over the decoding process is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @param  bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *         The bytes to be decoded into characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *         The index of the first byte to decode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @param  length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *         The number of bytes to decode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @param  charsetName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *         The name of a supported {@linkplain java.nio.charset.Charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *         charset}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @throws  UnsupportedEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *          If the named charset is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @throws  IndexOutOfBoundsException
27955
8d98ed07bc28 8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents: 27087
diff changeset
   474
     *          If {@code offset} is negative, {@code length} is negative, or
8d98ed07bc28 8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents: 27087
diff changeset
   475
     *          {@code offset} is greater than {@code bytes.length - length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24374
diff changeset
   477
     * @since  1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    public String(byte bytes[], int offset, int length, String charsetName)
12858
97e3f3f77254 6924259: Remove offset and count fields from java.lang.String
mduigou
parents: 11676
diff changeset
   480
            throws UnsupportedEncodingException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        if (charsetName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            throw new NullPointerException("charsetName");
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   483
        checkBoundsOffCount(offset, length, bytes.length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   484
        StringCoding.Result ret =
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   485
            StringCoding.decode(charsetName, bytes, offset, length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   486
        this.value = ret.value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   487
        this.coder = ret.coder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * Constructs a new {@code String} by decoding the specified subarray of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * bytes using the specified {@linkplain java.nio.charset.Charset charset}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * The length of the new {@code String} is a function of the charset, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * hence may not be equal to the length of the subarray.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * <p> This method always replaces malformed-input and unmappable-character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * sequences with this charset's default replacement string.  The {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * java.nio.charset.CharsetDecoder} class should be used when more control
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * over the decoding process is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @param  bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *         The bytes to be decoded into characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *         The index of the first byte to decode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @param  length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *         The number of bytes to decode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @param  charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *         The {@linkplain java.nio.charset.Charset charset} to be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *         decode the {@code bytes}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @throws  IndexOutOfBoundsException
27955
8d98ed07bc28 8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents: 27087
diff changeset
   515
     *          If {@code offset} is negative, {@code length} is negative, or
8d98ed07bc28 8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents: 27087
diff changeset
   516
     *          {@code offset} is greater than {@code bytes.length - length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @since  1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    public String(byte bytes[], int offset, int length, Charset charset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        if (charset == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            throw new NullPointerException("charset");
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   523
        checkBoundsOffCount(offset, length, bytes.length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   524
        StringCoding.Result ret =
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   525
            StringCoding.decode(charset, bytes, offset, length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   526
        this.value = ret.value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   527
        this.coder = ret.coder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * Constructs a new {@code String} by decoding the specified array of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * using the specified {@linkplain java.nio.charset.Charset charset}.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * length of the new {@code String} is a function of the charset, and hence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * may not be equal to the length of the byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * <p> The behavior of this constructor when the given bytes are not valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * in the given charset is unspecified.  The {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * java.nio.charset.CharsetDecoder} class should be used when more control
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * over the decoding process is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @param  bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *         The bytes to be decoded into characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @param  charsetName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *         The name of a supported {@linkplain java.nio.charset.Charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *         charset}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @throws  UnsupportedEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *          If the named charset is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24374
diff changeset
   551
     * @since  1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    public String(byte bytes[], String charsetName)
12858
97e3f3f77254 6924259: Remove offset and count fields from java.lang.String
mduigou
parents: 11676
diff changeset
   554
            throws UnsupportedEncodingException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        this(bytes, 0, bytes.length, charsetName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * Constructs a new {@code String} by decoding the specified array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * bytes using the specified {@linkplain java.nio.charset.Charset charset}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * The length of the new {@code String} is a function of the charset, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * hence may not be equal to the length of the byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * <p> This method always replaces malformed-input and unmappable-character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * sequences with this charset's default replacement string.  The {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * java.nio.charset.CharsetDecoder} class should be used when more control
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * over the decoding process is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @param  bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *         The bytes to be decoded into characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * @param  charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *         The {@linkplain java.nio.charset.Charset charset} to be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *         decode the {@code bytes}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @since  1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    public String(byte bytes[], Charset charset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        this(bytes, 0, bytes.length, charset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * Constructs a new {@code String} by decoding the specified subarray of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * bytes using the platform's default charset.  The length of the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * {@code String} is a function of the charset, and hence may not be equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * to the length of the subarray.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * <p> The behavior of this constructor when the given bytes are not valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * in the default charset is unspecified.  The {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * java.nio.charset.CharsetDecoder} class should be used when more control
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * over the decoding process is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @param  bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *         The bytes to be decoded into characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *         The index of the first byte to decode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @param  length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *         The number of bytes to decode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * @throws  IndexOutOfBoundsException
27955
8d98ed07bc28 8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents: 27087
diff changeset
   603
     *          If {@code offset} is negative, {@code length} is negative, or
8d98ed07bc28 8046219: (str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown
sherman
parents: 27087
diff changeset
   604
     *          {@code offset} is greater than {@code bytes.length - length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24374
diff changeset
   606
     * @since  1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    public String(byte bytes[], int offset, int length) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   609
        checkBoundsOffCount(offset, length, bytes.length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   610
        StringCoding.Result ret = StringCoding.decode(bytes, offset, length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   611
        this.value = ret.value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   612
        this.coder = ret.coder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * Constructs a new {@code String} by decoding the specified array of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * using the platform's default charset.  The length of the new {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * String} is a function of the charset, and hence may not be equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * length of the byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * <p> The behavior of this constructor when the given bytes are not valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * in the default charset is unspecified.  The {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * java.nio.charset.CharsetDecoder} class should be used when more control
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * over the decoding process is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @param  bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *         The bytes to be decoded into characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24374
diff changeset
   629
     * @since  1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     */
30642
d9891f85d583 8071571: Move substring of same string to slow path
igerasim
parents: 30640
diff changeset
   631
    public String(byte[] bytes) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        this(bytes, 0, bytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * Allocates a new string that contains the sequence of characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * currently contained in the string buffer argument. The contents of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * string buffer are copied; subsequent modification of the string buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * does not affect the newly created string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @param  buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *         A {@code StringBuffer}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    public String(StringBuffer buffer) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   645
        this(buffer.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * Allocates a new string that contains the sequence of characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * currently contained in the string builder argument. The contents of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * string builder are copied; subsequent modification of the string builder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * does not affect the newly created string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * <p> This constructor is provided to ease migration to {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * StringBuilder}. Obtaining a string from a string builder via the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * toString} method is likely to run faster and is generally preferred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * @param   builder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *          A {@code StringBuilder}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    public String(StringBuilder builder) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   664
        this(builder, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * Returns the length of this string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * The length is equal to the number of <a href="Character.html#unicode">Unicode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * code units</a> in the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @return  the length of the sequence of characters represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     *          object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    public int length() {
53049
c74f074c532a 8215380: Backout accidental change to String::length
redestad
parents: 53019
diff changeset
   676
        return value.length >> coder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    /**
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
   680
     * Returns {@code true} if, and only if, {@link #length()} is {@code 0}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
   682
     * @return {@code true} if {@link #length()} is {@code 0}, otherwise
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
   683
     * {@code false}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    public boolean isEmpty() {
12858
97e3f3f77254 6924259: Remove offset and count fields from java.lang.String
mduigou
parents: 11676
diff changeset
   688
        return value.length == 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   692
     * Returns the {@code char} value at the
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   693
     * specified index. An index ranges from {@code 0} to
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   694
     * {@code length() - 1}. The first {@code char} value of the sequence
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   695
     * is at index {@code 0}, the next at index {@code 1},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * and so on, as for array indexing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   698
     * <p>If the {@code char} value specified by the index is a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * <a href="Character.html#unicode">surrogate</a>, the surrogate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * value is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   702
     * @param      index   the index of the {@code char} value.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   703
     * @return     the {@code char} value at the specified index of this string.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   704
     *             The first {@code char} value is at index {@code 0}.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   705
     * @exception  IndexOutOfBoundsException  if the {@code index}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     *             argument is negative or not less than the length of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     *             string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    public char charAt(int index) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   710
        if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   711
            return StringLatin1.charAt(value, index);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   712
        } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   713
            return StringUTF16.charAt(value, index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * Returns the character (Unicode code point) at the specified
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   719
     * index. The index refers to {@code char} values
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   720
     * (Unicode code units) and ranges from {@code 0} to
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   721
     * {@link #length()}{@code  - 1}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   723
     * <p> If the {@code char} value specified at the given index
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * is in the high-surrogate range, the following index is less
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   725
     * than the length of this {@code String}, and the
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   726
     * {@code char} value at the following index is in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * low-surrogate range, then the supplementary code point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * corresponding to this surrogate pair is returned. Otherwise,
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   729
     * the {@code char} value at the given index is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   731
     * @param      index the index to the {@code char} values
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * @return     the code point value of the character at the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   733
     *             {@code index}
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   734
     * @exception  IndexOutOfBoundsException  if the {@code index}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     *             argument is negative or not less than the length of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     *             string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * @since      1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    public int codePointAt(int index) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   740
        if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   741
            checkIndex(index, value.length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   742
            return value[index] & 0xff;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   744
        int length = value.length >> 1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   745
        checkIndex(index, length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   746
        return StringUTF16.codePointAt(value, index, length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * Returns the character (Unicode code point) before the specified
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   751
     * index. The index refers to {@code char} values
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   752
     * (Unicode code units) and ranges from {@code 1} to {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * CharSequence#length() length}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   755
     * <p> If the {@code char} value at {@code (index - 1)}
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   756
     * is in the low-surrogate range, {@code (index - 2)} is not
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   757
     * negative, and the {@code char} value at {@code (index -
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   758
     * 2)} is in the high-surrogate range, then the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * supplementary code point value of the surrogate pair is
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   760
     * returned. If the {@code char} value at {@code index -
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   761
     * 1} is an unpaired low-surrogate or a high-surrogate, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * surrogate value is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * @param     index the index following the code point that should be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * @return    the Unicode code point value before the given index.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   766
     * @exception IndexOutOfBoundsException if the {@code index}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     *            argument is less than 1 or greater than the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     *            of this string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * @since     1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    public int codePointBefore(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        int i = index - 1;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   773
        if (i < 0 || i >= length()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            throw new StringIndexOutOfBoundsException(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   776
        if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   777
            return (value[i] & 0xff);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   778
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   779
        return StringUTF16.codePointBefore(value, index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * Returns the number of Unicode code points in the specified text
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   784
     * range of this {@code String}. The text range begins at the
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   785
     * specified {@code beginIndex} and extends to the
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   786
     * {@code char} at index {@code endIndex - 1}. Thus the
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   787
     * length (in {@code char}s) of the text range is
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   788
     * {@code endIndex-beginIndex}. Unpaired surrogates within
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * the text range count as one code point each.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   791
     * @param beginIndex the index to the first {@code char} of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * the text range.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   793
     * @param endIndex the index after the last {@code char} of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * the text range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * @return the number of Unicode code points in the specified text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * @exception IndexOutOfBoundsException if the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   798
     * {@code beginIndex} is negative, or {@code endIndex}
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   799
     * is larger than the length of this {@code String}, or
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   800
     * {@code beginIndex} is larger than {@code endIndex}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    public int codePointCount(int beginIndex, int endIndex) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   804
        if (beginIndex < 0 || beginIndex > endIndex ||
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   805
            endIndex > length()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   808
        if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   809
            return endIndex - beginIndex;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   810
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   811
        return StringUTF16.codePointCount(value, beginIndex, endIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   815
     * Returns the index within this {@code String} that is
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   816
     * offset from the given {@code index} by
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   817
     * {@code codePointOffset} code points. Unpaired surrogates
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   818
     * within the text range given by {@code index} and
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   819
     * {@code codePointOffset} count as one code point each.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @param index the index to be offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * @param codePointOffset the offset in code points
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   823
     * @return the index within this {@code String}
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   824
     * @exception IndexOutOfBoundsException if {@code index}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     *   is negative or larger then the length of this
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   826
     *   {@code String}, or if {@code codePointOffset} is positive
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   827
     *   and the substring starting with {@code index} has fewer
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   828
     *   than {@code codePointOffset} code points,
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   829
     *   or if {@code codePointOffset} is negative and the substring
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   830
     *   before {@code index} has fewer than the absolute value
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   831
     *   of {@code codePointOffset} code points.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    public int offsetByCodePoints(int index, int codePointOffset) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   835
        if (index < 0 || index > length()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   838
        return Character.offsetByCodePoints(this, index, codePointOffset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * Copies characters from this string into the destination character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   845
     * The first character to be copied is at index {@code srcBegin};
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   846
     * the last character to be copied is at index {@code srcEnd-1}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * (thus the total number of characters to be copied is
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   848
     * {@code srcEnd-srcBegin}). The characters are copied into the
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   849
     * subarray of {@code dst} starting at index {@code dstBegin}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * and ending at index:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 21308
diff changeset
   851
     * <blockquote><pre>
28423
0bf78b38bc0b 8067471: Use private static final char[0] for empty Strings
lpriima
parents: 27955
diff changeset
   852
     *     dstBegin + (srcEnd-srcBegin) - 1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @param      srcBegin   index of the first character in the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     *                        to copy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * @param      srcEnd     index after the last character in the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     *                        to copy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * @param      dst        the destination array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * @param      dstBegin   the start offset in the destination array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * @exception IndexOutOfBoundsException If any of the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *            is true:
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   863
     *            <ul><li>{@code srcBegin} is negative.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   864
     *            <li>{@code srcBegin} is greater than {@code srcEnd}
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   865
     *            <li>{@code srcEnd} is greater than the length of this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     *                string
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   867
     *            <li>{@code dstBegin} is negative
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   868
     *            <li>{@code dstBegin+(srcEnd-srcBegin)} is larger than
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
   869
     *                {@code dst.length}</ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   872
        checkBoundsBeginEnd(srcBegin, srcEnd, length());
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   873
        checkBoundsOffCount(dstBegin, srcEnd - srcBegin, dst.length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   874
        if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   875
            StringLatin1.getChars(value, srcBegin, srcEnd, dst, dstBegin);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   876
        } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   877
            StringUTF16.getChars(value, srcBegin, srcEnd, dst, dstBegin);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * Copies characters from this string into the destination byte array. Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * byte receives the 8 low-order bits of the corresponding character. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * eight high-order bits of each character are not copied and do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * participate in the transfer in any way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * <p> The first character to be copied is at index {@code srcBegin}; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * last character to be copied is at index {@code srcEnd-1}.  The total
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * number of characters to be copied is {@code srcEnd-srcBegin}. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * characters, converted to bytes, are copied into the subarray of {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * dst} starting at index {@code dstBegin} and ending at index:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * <blockquote><pre>
28423
0bf78b38bc0b 8067471: Use private static final char[0] for empty Strings
lpriima
parents: 27955
diff changeset
   894
     *     dstBegin + (srcEnd-srcBegin) - 1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * @deprecated  This method does not properly convert characters into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * bytes.  As of JDK&nbsp;1.1, the preferred way to do this is via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * {@link #getBytes()} method, which uses the platform's default charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * @param  srcBegin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     *         Index of the first character in the string to copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * @param  srcEnd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     *         Index after the last character in the string to copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * @param  dst
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     *         The destination array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * @param  dstBegin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     *         The start offset in the destination array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     *          If any of the following is true:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     *          <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     *            <li> {@code srcBegin} is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     *            <li> {@code srcBegin} is greater than {@code srcEnd}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     *            <li> {@code srcEnd} is greater than the length of this String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     *            <li> {@code dstBegin} is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     *            <li> {@code dstBegin+(srcEnd-srcBegin)} is larger than {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     *                 dst.length}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     *          </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     */
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 36431
diff changeset
   924
    @Deprecated(since="1.1")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   926
        checkBoundsBeginEnd(srcBegin, srcEnd, length());
15312
4b57f9da8192 4247235: (spec str) StringBuffer.insert(int, char[]) specification is inconsistent
jgish
parents: 14997
diff changeset
   927
        Objects.requireNonNull(dst);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   928
        checkBoundsOffCount(dstBegin, srcEnd - srcBegin, dst.length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   929
        if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   930
            StringLatin1.getBytes(value, srcBegin, srcEnd, dst, dstBegin);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   931
        } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   932
            StringUTF16.getBytes(value, srcBegin, srcEnd, dst, dstBegin);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * Encodes this {@code String} into a sequence of bytes using the named
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * charset, storing the result into a new byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * <p> The behavior of this method when this string cannot be encoded in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * the given charset is unspecified.  The {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * java.nio.charset.CharsetEncoder} class should be used when more control
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * over the encoding process is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * @param  charsetName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     *         The name of a supported {@linkplain java.nio.charset.Charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     *         charset}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * @return  The resultant byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * @throws  UnsupportedEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     *          If the named charset is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24374
diff changeset
   954
     * @since  1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    public byte[] getBytes(String charsetName)
12858
97e3f3f77254 6924259: Remove offset and count fields from java.lang.String
mduigou
parents: 11676
diff changeset
   957
            throws UnsupportedEncodingException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        if (charsetName == null) throw new NullPointerException();
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   959
        return StringCoding.encode(charsetName, coder(), value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * Encodes this {@code String} into a sequence of bytes using the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * {@linkplain java.nio.charset.Charset charset}, storing the result into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * new byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * <p> This method always replaces malformed-input and unmappable-character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * sequences with this charset's default replacement byte array.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * {@link java.nio.charset.CharsetEncoder} class should be used when more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * control over the encoding process is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * @param  charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     *         The {@linkplain java.nio.charset.Charset} to be used to encode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     *         the {@code String}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * @return  The resultant byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * @since  1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    public byte[] getBytes(Charset charset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        if (charset == null) throw new NullPointerException();
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   982
        return StringCoding.encode(charset, coder(), value);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   983
     }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * Encodes this {@code String} into a sequence of bytes using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * platform's default charset, storing the result into a new byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * <p> The behavior of this method when this string cannot be encoded in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * the default charset is unspecified.  The {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * java.nio.charset.CharsetEncoder} class should be used when more control
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * over the encoding process is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * @return  The resultant byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24374
diff changeset
   996
     * @since      1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    public byte[] getBytes() {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
   999
        return StringCoding.encode(coder(), value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * Compares this string to the specified object.  The result is {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * true} if and only if the argument is not {@code null} and is a {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * String} object that represents the same sequence of characters as this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     *
33314
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1008
     * <p>For finer-grained String comparison, refer to
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1009
     * {@link java.text.Collator}.
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1010
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * @param  anObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     *         The object to compare this {@code String} against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * @return  {@code true} if the given object represents a {@code String}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     *          equivalent to this string, {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * @see  #compareTo(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * @see  #equalsIgnoreCase(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    public boolean equals(Object anObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        if (this == anObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        if (anObject instanceof String) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1025
            String aString = (String)anObject;
54597
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1026
            if (!COMPACT_STRINGS || this.coder == aString.coder) {
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1027
                return StringLatin1.equals(value, aString.value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * Compares this string to the specified {@code StringBuffer}.  The result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * is {@code true} if and only if this {@code String} represents the same
13404
8e63aa2e956c 6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents: 13156
diff changeset
  1036
     * sequence of characters as the specified {@code StringBuffer}. This method
8e63aa2e956c 6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents: 13156
diff changeset
  1037
     * synchronizes on the {@code StringBuffer}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     *
33314
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1039
     * <p>For finer-grained String comparison, refer to
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1040
     * {@link java.text.Collator}.
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1041
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * @param  sb
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     *         The {@code StringBuffer} to compare this {@code String} against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * @return  {@code true} if this {@code String} represents the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     *          sequence of characters as the specified {@code StringBuffer},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     *          {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    public boolean contentEquals(StringBuffer sb) {
17181
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  1052
        return contentEquals((CharSequence)sb);
13404
8e63aa2e956c 6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents: 13156
diff changeset
  1053
    }
8e63aa2e956c 6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents: 13156
diff changeset
  1054
8e63aa2e956c 6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents: 13156
diff changeset
  1055
    private boolean nonSyncContentEquals(AbstractStringBuilder sb) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1056
        int len = length();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1057
        if (len != sb.length()) {
17698
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents: 17181
diff changeset
  1058
            return false;
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents: 17181
diff changeset
  1059
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1060
        byte v1[] = value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1061
        byte v2[] = sb.getValue();
54597
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1062
        byte coder = coder();
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1063
        if (coder == sb.getCoder()) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1064
            int n = v1.length;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1065
            for (int i = 0; i < n; i++) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1066
                if (v1[i] != v2[i]) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1067
                    return false;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1068
                }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1069
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1070
        } else {
54597
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1071
            if (coder != LATIN1) {  // utf16 str and latin1 abs can never be "equal"
13404
8e63aa2e956c 6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents: 13156
diff changeset
  1072
                return false;
8e63aa2e956c 6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents: 13156
diff changeset
  1073
            }
44642
331e669007f7 8158168: Missing bounds checks for some String intrinsics
dlong
parents: 44369
diff changeset
  1074
            return StringUTF16.contentEquals(v1, v2, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        }
13404
8e63aa2e956c 6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents: 13156
diff changeset
  1076
        return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    /**
13404
8e63aa2e956c 6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents: 13156
diff changeset
  1080
     * Compares this string to the specified {@code CharSequence}.  The
8e63aa2e956c 6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents: 13156
diff changeset
  1081
     * result is {@code true} if and only if this {@code String} represents the
8e63aa2e956c 6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents: 13156
diff changeset
  1082
     * same sequence of char values as the specified sequence. Note that if the
8e63aa2e956c 6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents: 13156
diff changeset
  1083
     * {@code CharSequence} is a {@code StringBuffer} then the method
8e63aa2e956c 6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents: 13156
diff changeset
  1084
     * synchronizes on it.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     *
33314
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1086
     * <p>For finer-grained String comparison, refer to
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1087
     * {@link java.text.Collator}.
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1088
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * @param  cs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     *         The sequence to compare this {@code String} against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * @return  {@code true} if this {@code String} represents the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     *          sequence of char values as the specified sequence, {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     *          false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    public boolean contentEquals(CharSequence cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        // Argument is a StringBuffer, StringBuilder
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        if (cs instanceof AbstractStringBuilder) {
13404
8e63aa2e956c 6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents: 13156
diff changeset
  1101
            if (cs instanceof StringBuffer) {
8e63aa2e956c 6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents: 13156
diff changeset
  1102
                synchronized(cs) {
8e63aa2e956c 6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents: 13156
diff changeset
  1103
                   return nonSyncContentEquals((AbstractStringBuilder)cs);
8e63aa2e956c 6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents: 13156
diff changeset
  1104
                }
8e63aa2e956c 6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents: 13156
diff changeset
  1105
            } else {
8e63aa2e956c 6914123: (str) Missing synchronization in java.lang.String#contentEquals(CharSequence)
jgish
parents: 13156
diff changeset
  1106
                return nonSyncContentEquals((AbstractStringBuilder)cs);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        // Argument is a String
27087
de850fa3be4d 8060485: (str) contentEquals checks the String contents twice on mismatch
shade
parents: 26731
diff changeset
  1110
        if (cs instanceof String) {
de850fa3be4d 8060485: (str) contentEquals checks the String contents twice on mismatch
shade
parents: 26731
diff changeset
  1111
            return equals(cs);
de850fa3be4d 8060485: (str) contentEquals checks the String contents twice on mismatch
shade
parents: 26731
diff changeset
  1112
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        // Argument is a generic CharSequence
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1114
        int n = cs.length();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1115
        if (n != length()) {
17698
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents: 17181
diff changeset
  1116
            return false;
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents: 17181
diff changeset
  1117
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1118
        byte[] val = this.value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1119
        if (isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1120
            for (int i = 0; i < n; i++) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1121
                if ((val[i] & 0xff) != cs.charAt(i)) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1122
                    return false;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1123
                }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1124
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1125
        } else {
44642
331e669007f7 8158168: Missing bounds checks for some String intrinsics
dlong
parents: 44369
diff changeset
  1126
            if (!StringUTF16.contentEquals(val, cs, n)) {
331e669007f7 8158168: Missing bounds checks for some String intrinsics
dlong
parents: 44369
diff changeset
  1127
                return false;
17698
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents: 17181
diff changeset
  1128
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * Compares this {@code String} to another {@code String}, ignoring case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * considerations.  Two strings are considered equal ignoring case if they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * are of the same length and corresponding characters in the two strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * are equal ignoring case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * <p> Two characters {@code c1} and {@code c2} are considered the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * ignoring case if at least one of the following is true:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     *   <li> The two characters are the same (as compared by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     *        {@code ==} operator)
33314
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1144
     *   <li> Calling {@code Character.toLowerCase(Character.toUpperCase(char))}
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1145
     *        on each character produces the same result
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     *
33314
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1148
     * <p>Note that this method does <em>not</em> take locale into account, and
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1149
     * will result in unsatisfactory results for certain locales.  The
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1150
     * {@link java.text.Collator} class provides locale-sensitive comparison.
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1151
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * @param  anotherString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     *         The {@code String} to compare this {@code String} against
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * @return  {@code true} if the argument is not {@code null} and it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     *          represents an equivalent {@code String} ignoring case; {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     *          false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * @see  #equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    public boolean equalsIgnoreCase(String anotherString) {
12858
97e3f3f77254 6924259: Remove offset and count fields from java.lang.String
mduigou
parents: 11676
diff changeset
  1162
        return (this == anotherString) ? true
97e3f3f77254 6924259: Remove offset and count fields from java.lang.String
mduigou
parents: 11676
diff changeset
  1163
                : (anotherString != null)
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1164
                && (anotherString.length() == length())
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1165
                && regionMatches(true, 0, anotherString, 0, length());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * Compares two strings lexicographically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * The comparison is based on the Unicode value of each character in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * the strings. The character sequence represented by this
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1172
     * {@code String} object is compared lexicographically to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * character sequence represented by the argument string. The result is
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1174
     * a negative integer if this {@code String} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * lexicographically precedes the argument string. The result is a
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1176
     * positive integer if this {@code String} object lexicographically
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * follows the argument string. The result is zero if the strings
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1178
     * are equal; {@code compareTo} returns {@code 0} exactly when
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1179
     * the {@link #equals(Object)} method would return {@code true}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * This is the definition of lexicographic ordering. If two strings are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * different, then either they have different characters at some index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * that is a valid index for both strings, or their lengths are different,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * or both. If they have different characters at one or more index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * positions, let <i>k</i> be the smallest such index; then the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * whose character at position <i>k</i> has the smaller value, as
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31671
diff changeset
  1187
     * determined by using the {@code <} operator, lexicographically precedes the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1188
     * other string. In this case, {@code compareTo} returns the
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1189
     * difference of the two character values at position {@code k} in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * the two string -- that is, the value:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * this.charAt(k)-anotherString.charAt(k)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * If there is no index position at which they differ, then the shorter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * string lexicographically precedes the longer string. In this case,
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1196
     * {@code compareTo} returns the difference of the lengths of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * strings -- that is, the value:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * this.length()-anotherString.length()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     *
33314
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1202
     * <p>For finer-grained String comparison, refer to
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1203
     * {@link java.text.Collator}.
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1204
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1205
     * @param   anotherString   the {@code String} to be compared.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1206
     * @return  the value {@code 0} if the argument string is equal to
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1207
     *          this string; a value less than {@code 0} if this string
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     *          is lexicographically less than the string argument; and a
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1209
     *          value greater than {@code 0} if this string is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     *          lexicographically greater than the string argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    public int compareTo(String anotherString) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1213
        byte v1[] = value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1214
        byte v2[] = anotherString.value;
54597
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1215
        byte coder = coder();
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1216
        if (coder == anotherString.coder()) {
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1217
            return coder == LATIN1 ? StringLatin1.compareTo(v1, v2)
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1218
                                   : StringUTF16.compareTo(v1, v2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        }
54597
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1220
        return coder == LATIN1 ? StringLatin1.compareToUTF16(v1, v2)
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1221
                               : StringUTF16.compareToLatin1(v1, v2);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1222
     }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1225
     * A Comparator that orders {@code String} objects as by
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1226
     * {@code compareToIgnoreCase}. This comparator is serializable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * Note that this Comparator does <em>not</em> take locale into account,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * and will result in an unsatisfactory ordering for certain locales.
33314
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1230
     * The {@link java.text.Collator} class provides locale-sensitive comparison.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     *
33314
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1232
     * @see     java.text.Collator
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
    public static final Comparator<String> CASE_INSENSITIVE_ORDER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                                         = new CaseInsensitiveComparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    private static class CaseInsensitiveComparator
12858
97e3f3f77254 6924259: Remove offset and count fields from java.lang.String
mduigou
parents: 11676
diff changeset
  1238
            implements Comparator<String>, java.io.Serializable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        // use serialVersionUID from JDK 1.2.2 for interoperability
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 57783
diff changeset
  1240
        @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        private static final long serialVersionUID = 8575799808933029326L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        public int compare(String s1, String s2) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1244
            byte v1[] = s1.value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1245
            byte v2[] = s2.value;
54597
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1246
            byte coder = s1.coder();
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1247
            if (coder == s2.coder()) {
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1248
                return coder == LATIN1 ? StringLatin1.compareToCI(v1, v2)
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1249
                                       : StringUTF16.compareToCI(v1, v2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            }
54597
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1251
            return coder == LATIN1 ? StringLatin1.compareToCI_UTF16(v1, v2)
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1252
                                   : StringUTF16.compareToCI_Latin1(v1, v2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        }
11127
6d29e4d16530 5035850: (str) String.CASE_INSENSITIVE_ORDER should override readResolve()
sherman
parents: 9266
diff changeset
  1254
6d29e4d16530 5035850: (str) String.CASE_INSENSITIVE_ORDER should override readResolve()
sherman
parents: 9266
diff changeset
  1255
        /** Replaces the de-serialized object. */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 57783
diff changeset
  1256
        @java.io.Serial
11127
6d29e4d16530 5035850: (str) String.CASE_INSENSITIVE_ORDER should override readResolve()
sherman
parents: 9266
diff changeset
  1257
        private Object readResolve() { return CASE_INSENSITIVE_ORDER; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * Compares two strings lexicographically, ignoring case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * differences. This method returns an integer whose sign is that of
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1263
     * calling {@code compareTo} with normalized versions of the strings
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * where case differences have been eliminated by calling
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1265
     * {@code Character.toLowerCase(Character.toUpperCase(character))} on
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * each character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * Note that this method does <em>not</em> take locale into account,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * and will result in an unsatisfactory ordering for certain locales.
33314
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1270
     * The {@link java.text.Collator} class provides locale-sensitive comparison.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1272
     * @param   str   the {@code String} to be compared.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * @return  a negative integer, zero, or a positive integer as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     *          specified String is greater than, equal to, or less
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     *          than this String, ignoring case considerations.
33314
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1276
     * @see     java.text.Collator
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
    public int compareToIgnoreCase(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        return CASE_INSENSITIVE_ORDER.compare(this, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * Tests if two string regions are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * <p>
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1286
     * A substring of this {@code String} object is compared to a substring
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     * of the argument other. The result is true if these substrings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     * represent identical character sequences. The substring of this
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1289
     * {@code String} object to be compared begins at index {@code toffset}
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1290
     * and has length {@code len}. The substring of other to be compared
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1291
     * begins at index {@code ooffset} and has length {@code len}. The
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1292
     * result is {@code false} if and only if at least one of the following
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * is true:
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1294
     * <ul><li>{@code toffset} is negative.
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1295
     * <li>{@code ooffset} is negative.
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1296
     * <li>{@code toffset+len} is greater than the length of this
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1297
     * {@code String} object.
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1298
     * <li>{@code ooffset+len} is greater than the length of the other
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * argument.
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1300
     * <li>There is some nonnegative integer <i>k</i> less than {@code len}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     * such that:
17181
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  1302
     * {@code this.charAt(toffset + }<i>k</i>{@code ) != other.charAt(ooffset + }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  1303
     * <i>k</i>{@code )}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     *
33314
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1306
     * <p>Note that this method does <em>not</em> take locale into account.  The
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1307
     * {@link java.text.Collator} class provides locale-sensitive comparison.
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1308
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * @param   toffset   the starting offset of the subregion in this string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     * @param   other     the string argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * @param   ooffset   the starting offset of the subregion in the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     *                    argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * @param   len       the number of characters to compare.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1314
     * @return  {@code true} if the specified subregion of this string
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     *          exactly matches the specified subregion of the string argument;
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1316
     *          {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     */
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1318
    public boolean regionMatches(int toffset, String other, int ooffset, int len) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1319
        byte tv[] = value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1320
        byte ov[] = other.value;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        // Note: toffset, ooffset, or len might be near -1>>>1.
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1322
        if ((ooffset < 0) || (toffset < 0) ||
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1323
             (toffset > (long)length() - len) ||
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1324
             (ooffset > (long)other.length() - len)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        }
54597
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1327
        byte coder = coder();
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1328
        if (coder == other.coder()) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1329
            if (!isLatin1() && (len > 0)) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1330
                toffset = toffset << 1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1331
                ooffset = ooffset << 1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1332
                len = len << 1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1333
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1334
            while (len-- > 0) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1335
                if (tv[toffset++] != ov[ooffset++]) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1336
                    return false;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1337
                }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1338
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1339
        } else {
54597
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1340
            if (coder == LATIN1) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1341
                while (len-- > 0) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1342
                    if (StringLatin1.getChar(tv, toffset++) !=
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1343
                        StringUTF16.getChar(ov, ooffset++)) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1344
                        return false;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1345
                    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1346
                }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1347
            } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1348
                while (len-- > 0) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1349
                    if (StringUTF16.getChar(tv, toffset++) !=
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1350
                        StringLatin1.getChar(ov, ooffset++)) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1351
                        return false;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1352
                    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1353
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     * Tests if two string regions are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     * <p>
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1362
     * A substring of this {@code String} object is compared to a substring
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1363
     * of the argument {@code other}. The result is {@code true} if these
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     * substrings represent character sequences that are the same, ignoring
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1365
     * case if and only if {@code ignoreCase} is true. The substring of
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1366
     * this {@code String} object to be compared begins at index
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1367
     * {@code toffset} and has length {@code len}. The substring of
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1368
     * {@code other} to be compared begins at index {@code ooffset} and
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1369
     * has length {@code len}. The result is {@code false} if and only if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * at least one of the following is true:
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1371
     * <ul><li>{@code toffset} is negative.
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1372
     * <li>{@code ooffset} is negative.
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1373
     * <li>{@code toffset+len} is greater than the length of this
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1374
     * {@code String} object.
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1375
     * <li>{@code ooffset+len} is greater than the length of the other
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * argument.
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1377
     * <li>{@code ignoreCase} is {@code false} and there is some nonnegative
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1378
     * integer <i>k</i> less than {@code len} such that:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
     * this.charAt(toffset+k) != other.charAt(ooffset+k)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
     * </pre></blockquote>
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1382
     * <li>{@code ignoreCase} is {@code true} and there is some nonnegative
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1383
     * integer <i>k</i> less than {@code len} such that:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * <blockquote><pre>
33314
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1385
     * Character.toLowerCase(Character.toUpperCase(this.charAt(toffset+k))) !=
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1386
     Character.toLowerCase(Character.toUpperCase(other.charAt(ooffset+k)))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     *
33314
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1390
     * <p>Note that this method does <em>not</em> take locale into account,
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1391
     * and will result in unsatisfactory results for certain locales when
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1392
     * {@code ignoreCase} is {@code true}.  The {@link java.text.Collator} class
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1393
     * provides locale-sensitive comparison.
777bf87e5050 8138824: java.lang.String: spec doesn't match impl when ignoring case - equalsIgnoreCase(), regionMatches()
bchristi
parents: 32033
diff changeset
  1394
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1395
     * @param   ignoreCase   if {@code true}, ignore case when comparing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     *                       characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * @param   toffset      the starting offset of the subregion in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     *                       string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * @param   other        the string argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * @param   ooffset      the starting offset of the subregion in the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     *                       argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * @param   len          the number of characters to compare.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1403
     * @return  {@code true} if the specified subregion of this string
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     *          matches the specified subregion of the string argument;
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1405
     *          {@code false} otherwise. Whether the matching is exact
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1406
     *          or case insensitive depends on the {@code ignoreCase}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     *          argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    public boolean regionMatches(boolean ignoreCase, int toffset,
12858
97e3f3f77254 6924259: Remove offset and count fields from java.lang.String
mduigou
parents: 11676
diff changeset
  1410
            String other, int ooffset, int len) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1411
        if (!ignoreCase) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1412
            return regionMatches(toffset, other, ooffset, len);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1413
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        // Note: toffset, ooffset, or len might be near -1>>>1.
12858
97e3f3f77254 6924259: Remove offset and count fields from java.lang.String
mduigou
parents: 11676
diff changeset
  1415
        if ((ooffset < 0) || (toffset < 0)
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1416
                || (toffset > (long)length() - len)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1417
                || (ooffset > (long)other.length() - len)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1420
        byte tv[] = value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1421
        byte ov[] = other.value;
54597
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1422
        byte coder = coder();
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1423
        if (coder == other.coder()) {
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1424
            return coder == LATIN1
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1425
              ? StringLatin1.regionMatchesCI(tv, toffset, ov, ooffset, len)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1426
              : StringUTF16.regionMatchesCI(tv, toffset, ov, ooffset, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        }
54597
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1428
        return coder == LATIN1
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1429
              ? StringLatin1.regionMatchesCI_UTF16(tv, toffset, ov, ooffset, len)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1430
              : StringUTF16.regionMatchesCI_Latin1(tv, toffset, ov, ooffset, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     * Tests if the substring of this string beginning at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * specified index starts with the specified prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     * @param   prefix    the prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     * @param   toffset   where to begin looking in this string.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1439
     * @return  {@code true} if the character sequence represented by the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     *          argument is a prefix of the substring of this object starting
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1441
     *          at index {@code toffset}; {@code false} otherwise.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1442
     *          The result is {@code false} if {@code toffset} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     *          negative or greater than the length of this
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1444
     *          {@code String} object; otherwise the result is the same
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
     *          as the result of the expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     *          <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     *          this.substring(toffset).startsWith(prefix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     *          </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
    public boolean startsWith(String prefix, int toffset) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1451
        // Note: toffset might be near -1>>>1.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1452
        if (toffset < 0 || toffset > length() - prefix.length()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1453
            return false;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1454
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1455
        byte ta[] = value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1456
        byte pa[] = prefix.value;
12858
97e3f3f77254 6924259: Remove offset and count fields from java.lang.String
mduigou
parents: 11676
diff changeset
  1457
        int po = 0;
30642
d9891f85d583 8071571: Move substring of same string to slow path
igerasim
parents: 30640
diff changeset
  1458
        int pc = pa.length;
54597
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1459
        byte coder = coder();
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1460
        if (coder == prefix.coder()) {
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1461
            int to = (coder == LATIN1) ? toffset : toffset << 1;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1462
            while (po < pc) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1463
                if (ta[to++] != pa[po++]) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1464
                    return false;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1465
                }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1466
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1467
        } else {
54597
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1468
            if (coder == LATIN1) {  // && pcoder == UTF16
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
            }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1471
            // coder == UTF16 && pcoder == LATIN1)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1472
            while (po < pc) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1473
                if (StringUTF16.getChar(ta, toffset++) != (pa[po++] & 0xff)) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1474
                    return false;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1475
               }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1476
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     * Tests if this string starts with the specified prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     * @param   prefix   the prefix.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1485
     * @return  {@code true} if the character sequence represented by the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     *          argument is a prefix of the character sequence represented by
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1487
     *          this string; {@code false} otherwise.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1488
     *          Note also that {@code true} will be returned if the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     *          argument is an empty string or is equal to this
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1490
     *          {@code String} object as determined by the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
     *          {@link #equals(Object)} method.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24374
diff changeset
  1492
     * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
    public boolean startsWith(String prefix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        return startsWith(prefix, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     * Tests if this string ends with the specified suffix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     * @param   suffix   the suffix.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1502
     * @return  {@code true} if the character sequence represented by the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
     *          argument is a suffix of the character sequence represented by
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1504
     *          this object; {@code false} otherwise. Note that the
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1505
     *          result will be {@code true} if the argument is the
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1506
     *          empty string or is equal to this {@code String} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     *          as determined by the {@link #equals(Object)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
    public boolean endsWith(String suffix) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1510
        return startsWith(suffix, length() - suffix.length());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     * Returns a hash code for this string. The hash code for a
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1515
     * {@code String} object is computed as
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     * </pre></blockquote>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1519
     * using {@code int} arithmetic, where {@code s[i]} is the
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1520
     * <i>i</i>th character of the string, {@code n} is the length of
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1521
     * the string, and {@code ^} indicates exponentiation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     * (The hash value of the empty string is zero.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     * @return  a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
    public int hashCode() {
54486
7fd299216e97 8221836: Avoid recalculating String.hash when zero
redestad
parents: 54378
diff changeset
  1527
        // The hash or hashIsZero fields are subject to a benign data race,
7fd299216e97 8221836: Avoid recalculating String.hash when zero
redestad
parents: 54378
diff changeset
  1528
        // making it crucial to ensure that any observable result of the
7fd299216e97 8221836: Avoid recalculating String.hash when zero
redestad
parents: 54378
diff changeset
  1529
        // calculation in this method stays correct under any possible read of
7fd299216e97 8221836: Avoid recalculating String.hash when zero
redestad
parents: 54378
diff changeset
  1530
        // these fields. Necessary restrictions to allow this to be correct
7fd299216e97 8221836: Avoid recalculating String.hash when zero
redestad
parents: 54378
diff changeset
  1531
        // without explicit memory fences or similar concurrency primitives is
7fd299216e97 8221836: Avoid recalculating String.hash when zero
redestad
parents: 54378
diff changeset
  1532
        // that we can ever only write to one of these two fields for a given
7fd299216e97 8221836: Avoid recalculating String.hash when zero
redestad
parents: 54378
diff changeset
  1533
        // String instance, and that the computation is idempotent and derived
7fd299216e97 8221836: Avoid recalculating String.hash when zero
redestad
parents: 54378
diff changeset
  1534
        // from immutable state
41219
eab7a5086e95 8166842: String.hashCode() has a non-benign data race
plevart
parents: 38786
diff changeset
  1535
        int h = hash;
54486
7fd299216e97 8221836: Avoid recalculating String.hash when zero
redestad
parents: 54378
diff changeset
  1536
        if (h == 0 && !hashIsZero) {
54378
7c576e4d0afa 8221723: Avoid storing zero to String.hash
redestad
parents: 53563
diff changeset
  1537
            h = isLatin1() ? StringLatin1.hashCode(value)
7c576e4d0afa 8221723: Avoid storing zero to String.hash
redestad
parents: 53563
diff changeset
  1538
                           : StringUTF16.hashCode(value);
54486
7fd299216e97 8221836: Avoid recalculating String.hash when zero
redestad
parents: 54378
diff changeset
  1539
            if (h == 0) {
7fd299216e97 8221836: Avoid recalculating String.hash when zero
redestad
parents: 54378
diff changeset
  1540
                hashIsZero = true;
7fd299216e97 8221836: Avoid recalculating String.hash when zero
redestad
parents: 54378
diff changeset
  1541
            } else {
54378
7c576e4d0afa 8221723: Avoid storing zero to String.hash
redestad
parents: 53563
diff changeset
  1542
                hash = h;
7c576e4d0afa 8221723: Avoid storing zero to String.hash
redestad
parents: 53563
diff changeset
  1543
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        }
41219
eab7a5086e95 8166842: String.hashCode() has a non-benign data race
plevart
parents: 38786
diff changeset
  1545
        return h;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     * Returns the index within this string of the first occurrence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     * the specified character. If a character with value
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1551
     * {@code ch} occurs in the character sequence represented by
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1552
     * this {@code String} object, then the index (in Unicode
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     * code units) of the first such occurrence is returned. For
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1554
     * values of {@code ch} in the range from 0 to 0xFFFF
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * (inclusive), this is the smallest value <i>k</i> such that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     * this.charAt(<i>k</i>) == ch
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * </pre></blockquote>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1559
     * is true. For other values of {@code ch}, it is the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     * smallest value <i>k</i> such that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     * this.codePointAt(<i>k</i>) == ch
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * is true. In either case, if no such character occurs in this
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1565
     * string, then {@code -1} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     * @param   ch   a character (Unicode code point).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     * @return  the index of the first occurrence of the character in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     *          character sequence represented by this object, or
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1570
     *          {@code -1} if the character does not occur.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
    public int indexOf(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        return indexOf(ch, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     * Returns the index within this string of the first occurrence of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     * specified character, starting the search at the specified index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
     * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1580
     * If a character with value {@code ch} occurs in the
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1581
     * character sequence represented by this {@code String}
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1582
     * object at an index no smaller than {@code fromIndex}, then
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
     * the index of the first such occurrence is returned. For values
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1584
     * of {@code ch} in the range from 0 to 0xFFFF (inclusive),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
     * this is the smallest value <i>k</i> such that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
     * <blockquote><pre>
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1587
     * (this.charAt(<i>k</i>) == ch) {@code &&} (<i>k</i> &gt;= fromIndex)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     * </pre></blockquote>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1589
     * is true. For other values of {@code ch}, it is the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     * smallest value <i>k</i> such that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     * <blockquote><pre>
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1592
     * (this.codePointAt(<i>k</i>) == ch) {@code &&} (<i>k</i> &gt;= fromIndex)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     * is true. In either case, if no such character occurs in this
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1595
     * string at or after position {@code fromIndex}, then
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1596
     * {@code -1} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
     * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1599
     * There is no restriction on the value of {@code fromIndex}. If it
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
     * is negative, it has the same effect as if it were zero: this entire
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
     * string may be searched. If it is greater than the length of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
     * string, it has the same effect as if it were equal to the length of
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1603
     * this string: {@code -1} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1605
     * <p>All indices are specified in {@code char} values
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     * (Unicode code units).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     * @param   ch          a character (Unicode code point).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     * @param   fromIndex   the index to start the search from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     * @return  the index of the first occurrence of the character in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     *          character sequence represented by this object that is greater
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1612
     *          than or equal to {@code fromIndex}, or {@code -1}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
     *          if the character does not occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
    public int indexOf(int ch, int fromIndex) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1616
        return isLatin1() ? StringLatin1.indexOf(value, ch, fromIndex)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1617
                          : StringUTF16.indexOf(value, ch, fromIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
     * Returns the index within this string of the last occurrence of
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1622
     * the specified character. For values of {@code ch} in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
     * range from 0 to 0xFFFF (inclusive), the index (in Unicode code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
     * units) returned is the largest value <i>k</i> such that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
     * this.charAt(<i>k</i>) == ch
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
     * </pre></blockquote>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1628
     * is true. For other values of {@code ch}, it is the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     * largest value <i>k</i> such that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     * this.codePointAt(<i>k</i>) == ch
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     * is true.  In either case, if no such character occurs in this
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1634
     * string, then {@code -1} is returned.  The
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1635
     * {@code String} is searched backwards starting at the last
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     * character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     * @param   ch   a character (Unicode code point).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     * @return  the index of the last occurrence of the character in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
     *          character sequence represented by this object, or
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1641
     *          {@code -1} if the character does not occur.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
    public int lastIndexOf(int ch) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1644
        return lastIndexOf(ch, length() - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
     * Returns the index within this string of the last occurrence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
     * the specified character, searching backward starting at the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1650
     * specified index. For values of {@code ch} in the range
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     * from 0 to 0xFFFF (inclusive), the index returned is the largest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     * value <i>k</i> such that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     * <blockquote><pre>
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1654
     * (this.charAt(<i>k</i>) == ch) {@code &&} (<i>k</i> &lt;= fromIndex)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
     * </pre></blockquote>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1656
     * is true. For other values of {@code ch}, it is the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
     * largest value <i>k</i> such that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
     * <blockquote><pre>
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  1659
     * (this.codePointAt(<i>k</i>) == ch) {@code &&} (<i>k</i> &lt;= fromIndex)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     * is true. In either case, if no such character occurs in this
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1662
     * string at or before position {@code fromIndex}, then
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1663
     * {@code -1} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1665
     * <p>All indices are specified in {@code char} values
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     * (Unicode code units).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
     * @param   ch          a character (Unicode code point).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     * @param   fromIndex   the index to start the search from. There is no
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1670
     *          restriction on the value of {@code fromIndex}. If it is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
     *          greater than or equal to the length of this string, it has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
     *          the same effect as if it were equal to one less than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     *          length of this string: this entire string may be searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     *          If it is negative, it has the same effect as if it were -1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     *          -1 is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     * @return  the index of the last occurrence of the character in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
     *          character sequence represented by this object that is less
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1678
     *          than or equal to {@code fromIndex}, or {@code -1}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
     *          if the character does not occur before that point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
    public int lastIndexOf(int ch, int fromIndex) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1682
        return isLatin1() ? StringLatin1.lastIndexOf(value, ch, fromIndex)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1683
                          : StringUTF16.lastIndexOf(value, ch, fromIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     * Returns the index within this string of the first occurrence of the
5988
2c984724db38 6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents: 5987
diff changeset
  1688
     * specified substring.
2c984724db38 6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents: 5987
diff changeset
  1689
     *
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 22943
diff changeset
  1690
     * <p>The returned index is the smallest value {@code k} for which:
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 22943
diff changeset
  1691
     * <pre>{@code
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 22943
diff changeset
  1692
     * this.startsWith(str, k)
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 22943
diff changeset
  1693
     * }</pre>
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 22943
diff changeset
  1694
     * If no such value of {@code k} exists, then {@code -1} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
     *
5988
2c984724db38 6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents: 5987
diff changeset
  1696
     * @param   str   the substring to search for.
2c984724db38 6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents: 5987
diff changeset
  1697
     * @return  the index of the first occurrence of the specified substring,
2c984724db38 6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents: 5987
diff changeset
  1698
     *          or {@code -1} if there is no such occurrence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
    public int indexOf(String str) {
54597
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1701
        byte coder = coder();
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1702
        if (coder == str.coder()) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1703
            return isLatin1() ? StringLatin1.indexOf(value, str.value)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1704
                              : StringUTF16.indexOf(value, str.value);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1705
        }
54597
ab57d6bebed8 8215017: Improve String::equals warmup characteristics
redestad
parents: 54550
diff changeset
  1706
        if (coder == LATIN1) {  // str.coder == UTF16
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1707
            return -1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1708
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1709
        return StringUTF16.indexOfLatin1(value, str.value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     * Returns the index within this string of the first occurrence of the
5988
2c984724db38 6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents: 5987
diff changeset
  1714
     * specified substring, starting at the specified index.
2c984724db38 6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents: 5987
diff changeset
  1715
     *
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 22943
diff changeset
  1716
     * <p>The returned index is the smallest value {@code k} for which:
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 22943
diff changeset
  1717
     * <pre>{@code
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 22943
diff changeset
  1718
     *     k >= Math.min(fromIndex, this.length()) &&
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 22943
diff changeset
  1719
     *                   this.startsWith(str, k)
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 22943
diff changeset
  1720
     * }</pre>
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 22943
diff changeset
  1721
     * If no such value of {@code k} exists, then {@code -1} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
     *
5988
2c984724db38 6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents: 5987
diff changeset
  1723
     * @param   str         the substring to search for.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
     * @param   fromIndex   the index from which to start the search.
5988
2c984724db38 6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents: 5987
diff changeset
  1725
     * @return  the index of the first occurrence of the specified substring,
2c984724db38 6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents: 5987
diff changeset
  1726
     *          starting at the specified index,
2c984724db38 6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents: 5987
diff changeset
  1727
     *          or {@code -1} if there is no such occurrence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
    public int indexOf(String str, int fromIndex) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1730
        return indexOf(value, coder(), length(), str, fromIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
    /**
14686
fb59583d33b2 6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents: 14014
diff changeset
  1734
     * Code shared by String and AbstractStringBuilder to do searches. The
fb59583d33b2 6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents: 14014
diff changeset
  1735
     * source is the character array being searched, and the target
fb59583d33b2 6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents: 14014
diff changeset
  1736
     * is the string being searched for.
fb59583d33b2 6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents: 14014
diff changeset
  1737
     *
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1738
     * @param   src       the characters being searched.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1739
     * @param   srcCoder  the coder of the source string.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1740
     * @param   srcCount  length of the source string.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1741
     * @param   tgtStr    the characters being searched for.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1742
     * @param   fromIndex the index to begin searching from.
14686
fb59583d33b2 6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents: 14014
diff changeset
  1743
     */
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1744
    static int indexOf(byte[] src, byte srcCoder, int srcCount,
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1745
                       String tgtStr, int fromIndex) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1746
        byte[] tgt    = tgtStr.value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1747
        byte tgtCoder = tgtStr.coder();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1748
        int tgtCount  = tgtStr.length();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1749
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1750
        if (fromIndex >= srcCount) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1751
            return (tgtCount == 0 ? srcCount : -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
        if (fromIndex < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
            fromIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1756
        if (tgtCount == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
            return fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
        }
44642
331e669007f7 8158168: Missing bounds checks for some String intrinsics
dlong
parents: 44369
diff changeset
  1759
        if (tgtCount > srcCount) {
331e669007f7 8158168: Missing bounds checks for some String intrinsics
dlong
parents: 44369
diff changeset
  1760
            return -1;
331e669007f7 8158168: Missing bounds checks for some String intrinsics
dlong
parents: 44369
diff changeset
  1761
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1762
        if (srcCoder == tgtCoder) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1763
            return srcCoder == LATIN1
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1764
                ? StringLatin1.indexOf(src, srcCount, tgt, tgtCount, fromIndex)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1765
                : StringUTF16.indexOf(src, srcCount, tgt, tgtCount, fromIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1767
        if (srcCoder == LATIN1) {    //  && tgtCoder == UTF16
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1768
            return -1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1769
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1770
        // srcCoder == UTF16 && tgtCoder == LATIN1) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1771
        return StringUTF16.indexOfLatin1(src, srcCount, tgt, tgtCount, fromIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
    /**
5988
2c984724db38 6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents: 5987
diff changeset
  1775
     * Returns the index within this string of the last occurrence of the
2c984724db38 6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents: 5987
diff changeset
  1776
     * specified substring.  The last occurrence of the empty string ""
2c984724db38 6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents: 5987
diff changeset
  1777
     * is considered to occur at the index value {@code this.length()}.
2c984724db38 6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents: 5987
diff changeset
  1778
     *
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 22943
diff changeset
  1779
     * <p>The returned index is the largest value {@code k} for which:
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 22943
diff changeset
  1780
     * <pre>{@code
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 22943
diff changeset
  1781
     * this.startsWith(str, k)
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 22943
diff changeset
  1782
     * }</pre>
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 22943
diff changeset
  1783
     * If no such value of {@code k} exists, then {@code -1} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     * @param   str   the substring to search for.
5988
2c984724db38 6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents: 5987
diff changeset
  1786
     * @return  the index of the last occurrence of the specified substring,
2c984724db38 6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents: 5987
diff changeset
  1787
     *          or {@code -1} if there is no such occurrence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
    public int lastIndexOf(String str) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1790
        return lastIndexOf(str, length());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
     * Returns the index within this string of the last occurrence of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
     * specified substring, searching backward starting at the specified index.
5988
2c984724db38 6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents: 5987
diff changeset
  1796
     *
23024
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 22943
diff changeset
  1797
     * <p>The returned index is the largest value {@code k} for which:
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 22943
diff changeset
  1798
     * <pre>{@code
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 22943
diff changeset
  1799
     *     k <= Math.min(fromIndex, this.length()) &&
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 22943
diff changeset
  1800
     *                   this.startsWith(str, k)
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 22943
diff changeset
  1801
     * }</pre>
7d5ecb31115f 8027640: String.indexOf(String,int) for the empty string case not specified
bchristi
parents: 22943
diff changeset
  1802
     * If no such value of {@code k} exists, then {@code -1} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
     * @param   str         the substring to search for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
     * @param   fromIndex   the index to start the search from.
5988
2c984724db38 6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents: 5987
diff changeset
  1806
     * @return  the index of the last occurrence of the specified substring,
2c984724db38 6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents: 5987
diff changeset
  1807
     *          searching backward from the specified index,
2c984724db38 6940381: Wording improvements for String.indexOf, String.lastIndexOf
martin
parents: 5987
diff changeset
  1808
     *          or {@code -1} if there is no such occurrence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
    public int lastIndexOf(String str, int fromIndex) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1811
        return lastIndexOf(value, coder(), length(), str, fromIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
    /**
14686
fb59583d33b2 6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents: 14014
diff changeset
  1815
     * Code shared by String and AbstractStringBuilder to do searches. The
fb59583d33b2 6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents: 14014
diff changeset
  1816
     * source is the character array being searched, and the target
fb59583d33b2 6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents: 14014
diff changeset
  1817
     * is the string being searched for.
fb59583d33b2 6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents: 14014
diff changeset
  1818
     *
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1819
     * @param   src         the characters being searched.
44642
331e669007f7 8158168: Missing bounds checks for some String intrinsics
dlong
parents: 44369
diff changeset
  1820
     * @param   srcCoder    coder handles the mapping between bytes/chars
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1821
     * @param   srcCount    count of the source string.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1822
     * @param   tgt         the characters being searched for.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1823
     * @param   fromIndex   the index to begin searching from.
14686
fb59583d33b2 6553074: String{Buffer,Builder}.indexOf(Str, int) contains unnecessary allocation
mduigou
parents: 14014
diff changeset
  1824
     */
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1825
    static int lastIndexOf(byte[] src, byte srcCoder, int srcCount,
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1826
                           String tgtStr, int fromIndex) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1827
        byte[] tgt = tgtStr.value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1828
        byte tgtCoder = tgtStr.coder();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1829
        int tgtCount = tgtStr.length();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
         * Check arguments; return immediately where possible. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
         * consistency, don't check for null str.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
         */
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1834
        int rightIndex = srcCount - tgtCount;
44642
331e669007f7 8158168: Missing bounds checks for some String intrinsics
dlong
parents: 44369
diff changeset
  1835
        if (fromIndex > rightIndex) {
331e669007f7 8158168: Missing bounds checks for some String intrinsics
dlong
parents: 44369
diff changeset
  1836
            fromIndex = rightIndex;
331e669007f7 8158168: Missing bounds checks for some String intrinsics
dlong
parents: 44369
diff changeset
  1837
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
        if (fromIndex < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
        /* Empty string always matches. */
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1842
        if (tgtCount == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
            return fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1845
        if (srcCoder == tgtCoder) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1846
            return srcCoder == LATIN1
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1847
                ? StringLatin1.lastIndexOf(src, srcCount, tgt, tgtCount, fromIndex)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1848
                : StringUTF16.lastIndexOf(src, srcCount, tgt, tgtCount, fromIndex);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1849
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1850
        if (srcCoder == LATIN1) {    // && tgtCoder == UTF16
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1851
            return -1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1852
        }
44642
331e669007f7 8158168: Missing bounds checks for some String intrinsics
dlong
parents: 44369
diff changeset
  1853
        // srcCoder == UTF16 && tgtCoder == LATIN1
331e669007f7 8158168: Missing bounds checks for some String intrinsics
dlong
parents: 44369
diff changeset
  1854
        return StringUTF16.lastIndexOfLatin1(src, srcCount, tgt, tgtCount, fromIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
    /**
21841
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  1858
     * Returns a string that is a substring of this string. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
     * substring begins with the character at the specified index and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
     * extends to the end of this string. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
     * Examples:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     * "unhappy".substring(2) returns "happy"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     * "Harbison".substring(3) returns "bison"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
     * "emptiness".substring(9) returns "" (an empty string)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
     * @param      beginIndex   the beginning index, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
     * @return     the specified substring.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
     * @exception  IndexOutOfBoundsException  if
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1871
     *             {@code beginIndex} is negative or larger than the
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1872
     *             length of this {@code String} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
    public String substring(int beginIndex) {
57783
a0257f1458aa 8221307: String.substring() OOB exception on start index reports improper information
igerasim
parents: 55290
diff changeset
  1875
        return substring(beginIndex, length());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
    /**
21841
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  1879
     * Returns a string that is a substring of this string. The
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1880
     * substring begins at the specified {@code beginIndex} and
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1881
     * extends to the character at index {@code endIndex - 1}.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1882
     * Thus the length of the substring is {@code endIndex-beginIndex}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
     * Examples:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     * "hamburger".substring(4, 8) returns "urge"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     * "smiles".substring(1, 5) returns "mile"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     * @param      beginIndex   the beginning index, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     * @param      endIndex     the ending index, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     * @return     the specified substring.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     * @exception  IndexOutOfBoundsException  if the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1894
     *             {@code beginIndex} is negative, or
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1895
     *             {@code endIndex} is larger than the length of
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1896
     *             this {@code String} object, or
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1897
     *             {@code beginIndex} is larger than
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1898
     *             {@code endIndex}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
    public String substring(int beginIndex, int endIndex) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1901
        int length = length();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1902
        checkBoundsBeginEnd(beginIndex, endIndex, length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1903
        int subLen = endIndex - beginIndex;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1904
        if (beginIndex == 0 && endIndex == length) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1905
            return this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1907
        return isLatin1() ? StringLatin1.newString(value, beginIndex, subLen)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  1908
                          : StringUTF16.newString(value, beginIndex, subLen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
    /**
21959
0767c3d7c550 8028757: CharSequence.subSequence improperly requires a "new" CharSequence be returned
smarks
parents: 21841
diff changeset
  1912
     * Returns a character sequence that is a subsequence of this sequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
     * <p> An invocation of this method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
     * str.subSequence(begin,&nbsp;end)</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
     * behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
     * str.substring(begin,&nbsp;end)</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
     *
21841
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  1924
     * @apiNote
13156
e88d9099b6f0 7170938: (str) incorrect wording in doc for String.subSequence
smarks
parents: 12859
diff changeset
  1925
     * This method is defined so that the {@code String} class can implement
17181
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  1926
     * the {@link CharSequence} interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
     *
13156
e88d9099b6f0 7170938: (str) incorrect wording in doc for String.subSequence
smarks
parents: 12859
diff changeset
  1928
     * @param   beginIndex   the begin index, inclusive.
e88d9099b6f0 7170938: (str) incorrect wording in doc for String.subSequence
smarks
parents: 12859
diff changeset
  1929
     * @param   endIndex     the end index, exclusive.
e88d9099b6f0 7170938: (str) incorrect wording in doc for String.subSequence
smarks
parents: 12859
diff changeset
  1930
     * @return  the specified subsequence.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
     * @throws  IndexOutOfBoundsException
13156
e88d9099b6f0 7170938: (str) incorrect wording in doc for String.subSequence
smarks
parents: 12859
diff changeset
  1933
     *          if {@code beginIndex} or {@code endIndex} is negative,
e88d9099b6f0 7170938: (str) incorrect wording in doc for String.subSequence
smarks
parents: 12859
diff changeset
  1934
     *          if {@code endIndex} is greater than {@code length()},
e88d9099b6f0 7170938: (str) incorrect wording in doc for String.subSequence
smarks
parents: 12859
diff changeset
  1935
     *          or if {@code beginIndex} is greater than {@code endIndex}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
    public CharSequence subSequence(int beginIndex, int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
        return this.substring(beginIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     * Concatenates the specified string to the end of this string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1947
     * If the length of the argument string is {@code 0}, then this
21841
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  1948
     * {@code String} object is returned. Otherwise, a
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  1949
     * {@code String} object is returned that represents a character
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
     * sequence that is the concatenation of the character sequence
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1951
     * represented by this {@code String} object and the character
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
     * sequence represented by the argument string.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
     * Examples:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
     * "cares".concat("s") returns "caress"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
     * "to".concat("get").concat("her") returns "together"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1959
     * @param   str   the {@code String} that is concatenated to the end
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1960
     *                of this {@code String}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
     * @return  a string that represents the concatenation of this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
     *          characters followed by the string argument's characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
    public String concat(String str) {
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52914
diff changeset
  1965
        if (str.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
        }
54550
5fa7fbddfe9d 8222484: Specialize generation of simple String concatenation expressions
redestad
parents: 54486
diff changeset
  1968
        return StringConcatHelper.simpleConcat(this, str);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
    /**
21841
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  1972
     * Returns a string resulting from replacing all occurrences of
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1973
     * {@code oldChar} in this string with {@code newChar}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
     * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1975
     * If the character {@code oldChar} does not occur in the
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1976
     * character sequence represented by this {@code String} object,
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1977
     * then a reference to this {@code String} object is returned.
21841
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  1978
     * Otherwise, a {@code String} object is returned that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
     * represents a character sequence identical to the character sequence
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1980
     * represented by this {@code String} object, except that every
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1981
     * occurrence of {@code oldChar} is replaced by an occurrence
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1982
     * of {@code newChar}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
     * Examples:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
     * "mesquite in your cellar".replace('e', 'o')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
     *         returns "mosquito in your collar"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
     * "the war of baronets".replace('r', 'y')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
     *         returns "the way of bayonets"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
     * "sparring with a purple porpoise".replace('p', 't')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
     *         returns "starring with a turtle tortoise"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
     * "JonL".replace('q', 'x') returns "JonL" (no change)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
     * @param   oldChar   the old character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
     * @param   newChar   the new character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
     * @return  a string derived from this string by replacing every
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  1998
     *          occurrence of {@code oldChar} with {@code newChar}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
    public String replace(char oldChar, char newChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
        if (oldChar != newChar) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  2002
            String ret = isLatin1() ? StringLatin1.replace(value, oldChar, newChar)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  2003
                                    : StringUTF16.replace(value, oldChar, newChar);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  2004
            if (ret != null) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  2005
                return ret;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
     * Tells whether or not this string matches the given <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
     * href="../util/regex/Pattern.html#sum">regular expression</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
     * <p> An invocation of this method of the form
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2016
     * <i>str</i>{@code .matches(}<i>regex</i>{@code )} yields exactly the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
     * same result as the expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
     *
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2019
     * <blockquote>
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2020
     * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#matches(String,CharSequence)
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2021
     * matches(<i>regex</i>, <i>str</i>)}
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2022
     * </blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
     * @param   regex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
     *          the regular expression to which this string is to be matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
     *
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2027
     * @return  {@code true} if, and only if, this string matches the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
     *          given regular expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
     * @throws  PatternSyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
     *          if the regular expression's syntax is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
     * @see java.util.regex.Pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
    public boolean matches(String regex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
        return Pattern.matches(regex, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
     * Returns true if and only if this string contains the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
     * sequence of char values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
     * @param s the sequence to search for
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  2047
     * @return true if this string contains {@code s}, false otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
    public boolean contains(CharSequence s) {
26451
f86e59f18322 8056951: pico-optimize contains(Object) methods
martin
parents: 25859
diff changeset
  2051
        return indexOf(s.toString()) >= 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
     * Replaces the first substring of this string that matches the given <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
     * href="../util/regex/Pattern.html#sum">regular expression</a> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
     * given replacement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
     * <p> An invocation of this method of the form
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2060
     * <i>str</i>{@code .replaceFirst(}<i>regex</i>{@code ,} <i>repl</i>{@code )}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
     * yields exactly the same result as the expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     *
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2063
     * <blockquote>
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2064
     * <code>
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2065
     * {@link java.util.regex.Pattern}.{@link
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2066
     * java.util.regex.Pattern#compile compile}(<i>regex</i>).{@link
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2067
     * java.util.regex.Pattern#matcher(java.lang.CharSequence) matcher}(<i>str</i>).{@link
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2068
     * java.util.regex.Matcher#replaceFirst replaceFirst}(<i>repl</i>)
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2069
     * </code>
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2070
     * </blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
     *<p>
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2073
     * Note that backslashes ({@code \}) and dollar signs ({@code $}) in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
     * replacement string may cause the results to be different than if it were
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
     * being treated as a literal replacement string; see
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
     * {@link java.util.regex.Matcher#replaceFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
     * Use {@link java.util.regex.Matcher#quoteReplacement} to suppress the special
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
     * meaning of these characters, if desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
     * @param   regex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
     *          the regular expression to which this string is to be matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
     * @param   replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
     *          the string to be substituted for the first match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
     *
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2085
     * @return  The resulting {@code String}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
     * @throws  PatternSyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
     *          if the regular expression's syntax is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
     * @see java.util.regex.Pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
    public String replaceFirst(String regex, String replacement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
        return Pattern.compile(regex).matcher(this).replaceFirst(replacement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
     * Replaces each substring of this string that matches the given <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
     * href="../util/regex/Pattern.html#sum">regular expression</a> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
     * given replacement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
     * <p> An invocation of this method of the form
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2105
     * <i>str</i>{@code .replaceAll(}<i>regex</i>{@code ,} <i>repl</i>{@code )}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
     * yields exactly the same result as the expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
     *
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2108
     * <blockquote>
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2109
     * <code>
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2110
     * {@link java.util.regex.Pattern}.{@link
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2111
     * java.util.regex.Pattern#compile compile}(<i>regex</i>).{@link
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2112
     * java.util.regex.Pattern#matcher(java.lang.CharSequence) matcher}(<i>str</i>).{@link
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2113
     * java.util.regex.Matcher#replaceAll replaceAll}(<i>repl</i>)
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2114
     * </code>
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2115
     * </blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
     *<p>
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2118
     * Note that backslashes ({@code \}) and dollar signs ({@code $}) in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
     * replacement string may cause the results to be different than if it were
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
     * being treated as a literal replacement string; see
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
     * {@link java.util.regex.Matcher#replaceAll Matcher.replaceAll}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
     * Use {@link java.util.regex.Matcher#quoteReplacement} to suppress the special
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
     * meaning of these characters, if desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
     * @param   regex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
     *          the regular expression to which this string is to be matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
     * @param   replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
     *          the string to be substituted for each match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
     *
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2130
     * @return  The resulting {@code String}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
     * @throws  PatternSyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
     *          if the regular expression's syntax is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
     * @see java.util.regex.Pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
    public String replaceAll(String regex, String replacement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
        return Pattern.compile(regex).matcher(this).replaceAll(replacement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
     * Replaces each substring of this string that matches the literal target
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
     * sequence with the specified literal replacement sequence. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
     * replacement proceeds from the beginning of the string to the end, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
     * example, replacing "aa" with "b" in the string "aaa" will result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
     * "ba" rather than "ab".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
     * @param  target The sequence of char values to be replaced
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
     * @param  replacement The replacement sequence of char values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
     * @return  The resulting string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
    public String replace(CharSequence target, CharSequence replacement) {
54728
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2157
        String trgtStr = target.toString();
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  2158
        String replStr = replacement.toString();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  2159
        int thisLen = length();
54728
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2160
        int trgtLen = trgtStr.length();
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2161
        int replLen = replStr.length();
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2162
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2163
        if (trgtLen > 0) {
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2164
            if (trgtLen == 1 && replLen == 1) {
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2165
                return replace(trgtStr.charAt(0), replStr.charAt(0));
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2166
            }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  2167
54728
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2168
            boolean thisIsLatin1 = this.isLatin1();
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2169
            boolean trgtIsLatin1 = trgtStr.isLatin1();
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2170
            boolean replIsLatin1 = replStr.isLatin1();
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2171
            String ret = (thisIsLatin1 && trgtIsLatin1 && replIsLatin1)
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2172
                    ? StringLatin1.replace(value, thisLen,
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2173
                                           trgtStr.value, trgtLen,
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2174
                                           replStr.value, replLen)
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2175
                    : StringUTF16.replace(value, thisLen, thisIsLatin1,
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2176
                                          trgtStr.value, trgtLen, trgtIsLatin1,
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2177
                                          replStr.value, replLen, replIsLatin1);
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2178
            if (ret != null) {
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2179
                return ret;
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2180
            }
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2181
            return this;
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2182
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2183
        } else { // trgtLen == 0
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2184
            int resultLen;
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2185
            try {
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2186
                resultLen = Math.addExact(thisLen, Math.multiplyExact(
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2187
                        Math.addExact(thisLen, 1), replLen));
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2188
            } catch (ArithmeticException ignored) {
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2189
                throw new OutOfMemoryError();
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2190
            }
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2191
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2192
            StringBuilder sb = new StringBuilder(resultLen);
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2193
            sb.append(replStr);
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2194
            for (int i = 0; i < thisLen; ++i) {
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2195
                sb.append(charAt(i)).append(replStr);
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2196
            }
6188582d58b5 8222955: Optimize String.replace(CharSequence, CharSequence) for common cases
igerasim
parents: 54597
diff changeset
  2197
            return sb.toString();
30913
bb5c454d451e 8058779: Faster implementation of String.replace(CharSequence, CharSequence)
igerasim
parents: 30642
diff changeset
  2198
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
     * Splits this string around matches of the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
     * <a href="../util/regex/Pattern.html#sum">regular expression</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
     * <p> The array returned by this method contains each substring of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
     * string that is terminated by another substring that matches the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
     * expression or is terminated by the end of the string.  The substrings in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
     * the array are in the order in which they occur in this string.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
     * expression does not match any part of the input then the resulting array
21670
ca3553133ede 8028321: Fix for String.split() empty input sequence/JDK-6559590 triggers regression
sherman
parents: 21668
diff changeset
  2210
     * has just one element, namely this string.
21668
b62ce4a9635f 8027645: Pattern.split() with positive lookahead
sherman
parents: 21636
diff changeset
  2211
     *
b62ce4a9635f 8027645: Pattern.split() with positive lookahead
sherman
parents: 21636
diff changeset
  2212
     * <p> When there is a positive-width match at the beginning of this
b62ce4a9635f 8027645: Pattern.split() with positive lookahead
sherman
parents: 21636
diff changeset
  2213
     * string then an empty leading substring is included at the beginning
b62ce4a9635f 8027645: Pattern.split() with positive lookahead
sherman
parents: 21636
diff changeset
  2214
     * of the resulting array. A zero-width match at the beginning however
b62ce4a9635f 8027645: Pattern.split() with positive lookahead
sherman
parents: 21636
diff changeset
  2215
     * never produces such empty leading substring.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
     *
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2217
     * <p> The {@code limit} parameter controls the number of times the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
     * pattern is applied and therefore affects the length of the resulting
50237
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 50215
diff changeset
  2219
     * array.
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 50215
diff changeset
  2220
     * <ul>
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 50215
diff changeset
  2221
     *    <li><p>
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 50215
diff changeset
  2222
     *    If the <i>limit</i> is positive then the pattern will be applied
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 50215
diff changeset
  2223
     *    at most <i>limit</i>&nbsp;-&nbsp;1 times, the array's length will be
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 50215
diff changeset
  2224
     *    no greater than <i>limit</i>, and the array's last entry will contain
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 50215
diff changeset
  2225
     *    all input beyond the last matched delimiter.</p></li>
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 50215
diff changeset
  2226
     *
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 50215
diff changeset
  2227
     *    <li><p>
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 50215
diff changeset
  2228
     *    If the <i>limit</i> is zero then the pattern will be applied as
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 50215
diff changeset
  2229
     *    many times as possible, the array can have any length, and trailing
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 50215
diff changeset
  2230
     *    empty strings will be discarded.</p></li>
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 50215
diff changeset
  2231
     *
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 50215
diff changeset
  2232
     *    <li><p>
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 50215
diff changeset
  2233
     *    If the <i>limit</i> is negative then the pattern will be applied
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 50215
diff changeset
  2234
     *    as many times as possible and the array can have any length.</p></li>
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 50215
diff changeset
  2235
     * </ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
     *
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2237
     * <p> The string {@code "boo:and:foo"}, for example, yields the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
     * following results with these parameters:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
     *
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2240
     * <blockquote><table class="plain">
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2241
     * <caption style="display:none">Split example showing regex, limit, and result</caption>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2242
     * <thead>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
     * <tr>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2244
     *     <th scope="col">Regex</th>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2245
     *     <th scope="col">Limit</th>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2246
     *     <th scope="col">Result</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
     * </tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2248
     * </thead>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2249
     * <tbody>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2250
     * <tr><th scope="row" rowspan="3" style="font-weight:normal">:</th>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2251
     *     <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">2</th>
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2252
     *     <td>{@code { "boo", "and:foo" }}</td></tr>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2253
     * <tr><!-- : -->
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2254
     *     <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">5</th>
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2255
     *     <td>{@code { "boo", "and", "foo" }}</td></tr>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2256
     * <tr><!-- : -->
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2257
     *     <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">-2</th>
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2258
     *     <td>{@code { "boo", "and", "foo" }}</td></tr>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2259
     * <tr><th scope="row" rowspan="3" style="font-weight:normal">o</th>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2260
     *     <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">5</th>
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2261
     *     <td>{@code { "b", "", ":and:f", "", "" }}</td></tr>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2262
     * <tr><!-- o -->
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2263
     *     <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">-2</th>
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2264
     *     <td>{@code { "b", "", ":and:f", "", "" }}</td></tr>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2265
     * <tr><!-- o -->
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2266
     *     <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">0</th>
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2267
     *     <td>{@code { "b", "", ":and:f" }}</td></tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2268
     * </tbody>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
     * </table></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
     * <p> An invocation of this method of the form
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2272
     * <i>str.</i>{@code split(}<i>regex</i>{@code ,}&nbsp;<i>n</i>{@code )}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
     * yields the same result as the expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
     * <blockquote>
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2276
     * <code>
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2277
     * {@link java.util.regex.Pattern}.{@link
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2278
     * java.util.regex.Pattern#compile compile}(<i>regex</i>).{@link
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2279
     * java.util.regex.Pattern#split(java.lang.CharSequence,int) split}(<i>str</i>,&nbsp;<i>n</i>)
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2280
     * </code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
     * @param  regex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
     *         the delimiting regular expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
     * @param  limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
     *         the result threshold, as described above
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
     * @return  the array of strings computed by splitting this string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
     *          around matches of the given regular expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
     * @throws  PatternSyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
     *          if the regular expression's syntax is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
     * @see java.util.regex.Pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
    public String[] split(String regex, int limit) {
3617
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2302
        /* fastpath if the regex is a
12858
97e3f3f77254 6924259: Remove offset and count fields from java.lang.String
mduigou
parents: 11676
diff changeset
  2303
         (1)one-char String and this character is not one of the
97e3f3f77254 6924259: Remove offset and count fields from java.lang.String
mduigou
parents: 11676
diff changeset
  2304
            RegEx's meta characters ".$|()[{^?*+\\", or
97e3f3f77254 6924259: Remove offset and count fields from java.lang.String
mduigou
parents: 11676
diff changeset
  2305
         (2)two-char String and the first char is the backslash and
97e3f3f77254 6924259: Remove offset and count fields from java.lang.String
mduigou
parents: 11676
diff changeset
  2306
            the second is not the ascii digit or ascii letter.
97e3f3f77254 6924259: Remove offset and count fields from java.lang.String
mduigou
parents: 11676
diff changeset
  2307
         */
3617
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2308
        char ch = 0;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  2309
        if (((regex.length() == 1 &&
3617
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2310
             ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2311
             (regex.length() == 2 &&
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2312
              regex.charAt(0) == '\\' &&
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2313
              (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2314
              ((ch-'a')|('z'-ch)) < 0 &&
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2315
              ((ch-'A')|('Z'-ch)) < 0)) &&
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2316
            (ch < Character.MIN_HIGH_SURROGATE ||
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2317
             ch > Character.MAX_LOW_SURROGATE))
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2318
        {
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2319
            int off = 0;
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2320
            int next = 0;
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2321
            boolean limited = limit > 0;
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5991
diff changeset
  2322
            ArrayList<String> list = new ArrayList<>();
3617
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2323
            while ((next = indexOf(ch, off)) != -1) {
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2324
                if (!limited || list.size() < limit - 1) {
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2325
                    list.add(substring(off, next));
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2326
                    off = next + 1;
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2327
                } else {    // last one
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2328
                    //assert (list.size() == limit - 1);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  2329
                    int last = length();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  2330
                    list.add(substring(off, last));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  2331
                    off = last;
3617
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2332
                    break;
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2333
                }
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2334
            }
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2335
            // If no match was found, return this
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2336
            if (off == 0)
12858
97e3f3f77254 6924259: Remove offset and count fields from java.lang.String
mduigou
parents: 11676
diff changeset
  2337
                return new String[]{this};
3617
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2338
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2339
            // Add remaining segment
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2340
            if (!limited || list.size() < limit)
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  2341
                list.add(substring(off, length()));
3617
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2342
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2343
            // Construct result
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2344
            int resultSize = list.size();
17181
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2345
            if (limit == 0) {
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52914
diff changeset
  2346
                while (resultSize > 0 && list.get(resultSize - 1).isEmpty()) {
3617
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2347
                    resultSize--;
17181
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2348
                }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2349
            }
3617
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2350
            String[] result = new String[resultSize];
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2351
            return list.subList(0, resultSize).toArray(result);
422105639b62 6840246: Lightweight implementation of String.split for simple use case
sherman
parents: 2497
diff changeset
  2352
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
        return Pattern.compile(regex).split(this, limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
     * Splits this string around matches of the given <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
     * href="../util/regex/Pattern.html#sum">regular expression</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
     * <p> This method works as if by invoking the two-argument {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
     * #split(String, int) split} method with the given expression and a limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
     * argument of zero.  Trailing empty strings are therefore not included in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
     * the resulting array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
     *
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2365
     * <p> The string {@code "boo:and:foo"}, for example, yields the following
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
     * results with these expressions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
     *
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2368
     * <blockquote><table class="plain">
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2369
     * <caption style="display:none">Split examples showing regex and result</caption>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2370
     * <thead>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
     * <tr>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2372
     *  <th scope="col">Regex</th>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2373
     *  <th scope="col">Result</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
     * </tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2375
     * </thead>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2376
     * <tbody>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2377
     * <tr><th scope="row" style="text-weight:normal">:</th>
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2378
     *     <td>{@code { "boo", "and", "foo" }}</td></tr>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2379
     * <tr><th scope="row" style="text-weight:normal">o</th>
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  2380
     *     <td>{@code { "b", "", ":and:f" }}</td></tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2381
     * </tbody>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
     * </table></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
     * @param  regex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
     *         the delimiting regular expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
     * @return  the array of strings computed by splitting this string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
     *          around matches of the given regular expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
     * @throws  PatternSyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
     *          if the regular expression's syntax is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
     * @see java.util.regex.Pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
    public String[] split(String regex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
        return split(regex, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
    /**
17181
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2404
     * Returns a new String composed of copies of the
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2405
     * {@code CharSequence elements} joined together with a copy of
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2406
     * the specified {@code delimiter}.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2407
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2408
     * <blockquote>For example,
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2409
     * <pre>{@code
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2410
     *     String message = String.join("-", "Java", "is", "cool");
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2411
     *     // message returned is: "Java-is-cool"
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2412
     * }</pre></blockquote>
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2413
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2414
     * Note that if an element is null, then {@code "null"} is added.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2415
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2416
     * @param  delimiter the delimiter that separates each element
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2417
     * @param  elements the elements to join together.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2418
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2419
     * @return a new {@code String} that is composed of the {@code elements}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2420
     *         separated by the {@code delimiter}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2421
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2422
     * @throws NullPointerException If {@code delimiter} or {@code elements}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2423
     *         is {@code null}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2424
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2425
     * @see java.util.StringJoiner
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2426
     * @since 1.8
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2427
     */
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2428
    public static String join(CharSequence delimiter, CharSequence... elements) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2429
        Objects.requireNonNull(delimiter);
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2430
        Objects.requireNonNull(elements);
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2431
        // Number of elements not likely worth Arrays.stream overhead.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2432
        StringJoiner joiner = new StringJoiner(delimiter);
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2433
        for (CharSequence cs: elements) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2434
            joiner.add(cs);
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2435
        }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2436
        return joiner.toString();
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2437
    }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2438
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2439
    /**
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2440
     * Returns a new {@code String} composed of copies of the
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2441
     * {@code CharSequence elements} joined together with a copy of the
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2442
     * specified {@code delimiter}.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2443
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2444
     * <blockquote>For example,
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2445
     * <pre>{@code
38765
b64f3ec3420e 8158312: Update String.join example code to use List convenience factory methods
darcy
parents: 37723
diff changeset
  2446
     *     List<String> strings = List.of("Java", "is", "cool");
17181
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2447
     *     String message = String.join(" ", strings);
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2448
     *     //message returned is: "Java is cool"
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2449
     *
38765
b64f3ec3420e 8158312: Update String.join example code to use List convenience factory methods
darcy
parents: 37723
diff changeset
  2450
     *     Set<String> strings =
b64f3ec3420e 8158312: Update String.join example code to use List convenience factory methods
darcy
parents: 37723
diff changeset
  2451
     *         new LinkedHashSet<>(List.of("Java", "is", "very", "cool"));
17181
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2452
     *     String message = String.join("-", strings);
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2453
     *     //message returned is: "Java-is-very-cool"
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2454
     * }</pre></blockquote>
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2455
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2456
     * Note that if an individual element is {@code null}, then {@code "null"} is added.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2457
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2458
     * @param  delimiter a sequence of characters that is used to separate each
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2459
     *         of the {@code elements} in the resulting {@code String}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2460
     * @param  elements an {@code Iterable} that will have its {@code elements}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2461
     *         joined together.
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2462
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2463
     * @return a new {@code String} that is composed from the {@code elements}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2464
     *         argument
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2465
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2466
     * @throws NullPointerException If {@code delimiter} or {@code elements}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2467
     *         is {@code null}
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2468
     *
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2469
     * @see    #join(CharSequence,CharSequence...)
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2470
     * @see    java.util.StringJoiner
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2471
     * @since 1.8
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2472
     */
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2473
    public static String join(CharSequence delimiter,
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2474
            Iterable<? extends CharSequence> elements) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2475
        Objects.requireNonNull(delimiter);
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2476
        Objects.requireNonNull(elements);
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2477
        StringJoiner joiner = new StringJoiner(delimiter);
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2478
        for (CharSequence cs: elements) {
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2479
            joiner.add(cs);
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2480
        }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2481
        return joiner.toString();
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2482
    }
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2483
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2484
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  2485
     * Converts all of the characters in this {@code String} to lower
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  2486
     * case using the rules of the given {@code Locale}.  Case mapping is based
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
     * on the Unicode Standard version specified by the {@link java.lang.Character Character}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
     * class. Since case mappings are not always 1:1 char mappings, the resulting
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  2489
     * {@code String} may be a different length than the original {@code String}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
     * Examples of lowercase  mappings are in the following table:
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2492
     * <table class="plain">
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2493
     * <caption style="display:none">Lowercase mapping examples showing language code of locale, upper case, lower case, and description</caption>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2494
     * <thead>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
     * <tr>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2496
     *   <th scope="col">Language Code of Locale</th>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2497
     *   <th scope="col">Upper Case</th>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2498
     *   <th scope="col">Lower Case</th>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2499
     *   <th scope="col">Description</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
     * </tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2501
     * </thead>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2502
     * <tbody>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
     *   <td>tr (Turkish)</td>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2505
     *   <th scope="row" style="font-weight:normal; text-align:left">&#92;u0130</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
     *   <td>&#92;u0069</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
     *   <td>capital letter I with dot above -&gt; small letter i</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
     *   <td>tr (Turkish)</td>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2511
     *   <th scope="row" style="font-weight:normal; text-align:left">&#92;u0049</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
     *   <td>&#92;u0131</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
     *   <td>capital letter I -&gt; small letter dotless i </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
     *   <td>(all)</td>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2517
     *   <th scope="row" style="font-weight:normal; text-align:left">French Fries</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
     *   <td>french fries</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
     *   <td>lowercased all chars in String</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
     *   <td>(all)</td>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2523
     *   <th scope="row" style="font-weight:normal; text-align:left">
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46148
diff changeset
  2524
     *       &Iota;&Chi;&Theta;&Upsilon;&Sigma;</th>
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46148
diff changeset
  2525
     *   <td>&iota;&chi;&theta;&upsilon;&sigma;</td>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
     *   <td>lowercased all chars in String</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
     * </tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2528
     * </tbody>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
     * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
     * @param locale use the case transformation rules for this locale
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  2532
     * @return the {@code String}, converted to lowercase.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
     * @see     java.lang.String#toLowerCase()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
     * @see     java.lang.String#toUpperCase()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
     * @see     java.lang.String#toUpperCase(Locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
     * @since   1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
    public String toLowerCase(Locale locale) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  2539
        return isLatin1() ? StringLatin1.toLowerCase(this, value, locale)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  2540
                          : StringUTF16.toLowerCase(this, value, locale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  2544
     * Converts all of the characters in this {@code String} to lower
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
     * case using the rules of the default locale. This is equivalent to calling
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  2546
     * {@code toLowerCase(Locale.getDefault())}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
     * <b>Note:</b> This method is locale sensitive, and may produce unexpected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
     * results if used for strings that are intended to be interpreted locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
     * independently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
     * Examples are programming language identifiers, protocol keys, and HTML
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
     * tags.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  2553
     * For instance, {@code "TITLE".toLowerCase()} in a Turkish locale
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  2554
     * returns {@code "t\u005Cu0131tle"}, where '\u005Cu0131' is the
5469
18ea05ea56b9 6937842: Unreadable \uXXXX in javadoc
martin
parents: 4982
diff changeset
  2555
     * LATIN SMALL LETTER DOTLESS I character.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
     * To obtain correct results for locale insensitive strings, use
19815
5b15fa787fab 8023943: Method description fix for String.toLower/UpperCase() methods
naoto
parents: 19802
diff changeset
  2557
     * {@code toLowerCase(Locale.ROOT)}.
24367
705490680527 8030709: Tidy warnings cleanup for java.lang package; minor cleanup in java.math, javax.script
yan
parents: 23024
diff changeset
  2558
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  2559
     * @return  the {@code String}, converted to lowercase.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
     * @see     java.lang.String#toLowerCase(Locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
    public String toLowerCase() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
        return toLowerCase(Locale.getDefault());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  2567
     * Converts all of the characters in this {@code String} to upper
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  2568
     * case using the rules of the given {@code Locale}. Case mapping is based
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
     * on the Unicode Standard version specified by the {@link java.lang.Character Character}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
     * class. Since case mappings are not always 1:1 char mappings, the resulting
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  2571
     * {@code String} may be a different length than the original {@code String}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
     * Examples of locale-sensitive and 1:M case mappings are in the following table.
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 21308
diff changeset
  2574
     *
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2575
     * <table class="plain">
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2576
     * <caption style="display:none">Examples of locale-sensitive and 1:M case mappings. Shows Language code of locale, lower case, upper case, and description.</caption>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2577
     * <thead>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
     * <tr>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2579
     *   <th scope="col">Language Code of Locale</th>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2580
     *   <th scope="col">Lower Case</th>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2581
     *   <th scope="col">Upper Case</th>
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2582
     *   <th scope="col">Description</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
     * </tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2584
     * </thead>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2585
     * <tbody>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
     *   <td>tr (Turkish)</td>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2588
     *   <th scope="row" style="font-weight:normal; text-align:left">&#92;u0069</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
     *   <td>&#92;u0130</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
     *   <td>small letter i -&gt; capital letter I with dot above</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
     *   <td>tr (Turkish)</td>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2594
     *   <th scope="row" style="font-weight:normal; text-align:left">&#92;u0131</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
     *   <td>&#92;u0049</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
     *   <td>small letter dotless i -&gt; capital letter I</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
     *   <td>(all)</td>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2600
     *   <th scope="row" style="font-weight:normal; text-align:left">&#92;u00df</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
     *   <td>&#92;u0053 &#92;u0053</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
     *   <td>small letter sharp s -&gt; two letters: SS</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
     *   <td>(all)</td>
46148
6d8e27cd2f1e 8186052: Fix a11y and HTML issues in the java.base/java.lang[.*] packages
jjg
parents: 45572
diff changeset
  2606
     *   <th scope="row" style="font-weight:normal; text-align:left">Fahrvergn&uuml;gen</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
     *   <td>FAHRVERGN&Uuml;GEN</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
     *   <td></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
     * </tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 45121
diff changeset
  2610
     * </tbody>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
     * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
     * @param locale use the case transformation rules for this locale
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  2613
     * @return the {@code String}, converted to uppercase.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
     * @see     java.lang.String#toUpperCase()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
     * @see     java.lang.String#toLowerCase()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
     * @see     java.lang.String#toLowerCase(Locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
     * @since   1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
    public String toUpperCase(Locale locale) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  2620
        return isLatin1() ? StringLatin1.toUpperCase(this, value, locale)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  2621
                          : StringUTF16.toUpperCase(this, value, locale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  2625
     * Converts all of the characters in this {@code String} to upper
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
     * case using the rules of the default locale. This method is equivalent to
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  2627
     * {@code toUpperCase(Locale.getDefault())}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
     * <b>Note:</b> This method is locale sensitive, and may produce unexpected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
     * results if used for strings that are intended to be interpreted locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
     * independently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
     * Examples are programming language identifiers, protocol keys, and HTML
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
     * tags.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  2634
     * For instance, {@code "title".toUpperCase()} in a Turkish locale
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  2635
     * returns {@code "T\u005Cu0130TLE"}, where '\u005Cu0130' is the
5469
18ea05ea56b9 6937842: Unreadable \uXXXX in javadoc
martin
parents: 4982
diff changeset
  2636
     * LATIN CAPITAL LETTER I WITH DOT ABOVE character.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
     * To obtain correct results for locale insensitive strings, use
19815
5b15fa787fab 8023943: Method description fix for String.toLower/UpperCase() methods
naoto
parents: 19802
diff changeset
  2638
     * {@code toUpperCase(Locale.ROOT)}.
24367
705490680527 8030709: Tidy warnings cleanup for java.lang package; minor cleanup in java.math, javax.script
yan
parents: 23024
diff changeset
  2639
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  2640
     * @return  the {@code String}, converted to uppercase.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
     * @see     java.lang.String#toUpperCase(Locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
    public String toUpperCase() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
        return toUpperCase(Locale.getDefault());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
    /**
50051
f5231f5762fc 8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents: 49433
diff changeset
  2648
     * Returns a string whose value is this string, with all leading
f5231f5762fc 8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents: 49433
diff changeset
  2649
     * and trailing space removed, where space is defined
f5231f5762fc 8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents: 49433
diff changeset
  2650
     * as any character whose codepoint is less than or equal to
50098
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2651
     * {@code 'U+0020'} (the space character).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
     * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  2653
     * If this {@code String} object represents an empty character
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
     * sequence, or the first and last characters of character sequence
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  2655
     * represented by this {@code String} object both have codes
50051
f5231f5762fc 8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents: 49433
diff changeset
  2656
     * that are not space (as defined above), then a
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  2657
     * reference to this {@code String} object is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
     * <p>
50051
f5231f5762fc 8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents: 49433
diff changeset
  2659
     * Otherwise, if all characters in this string are space (as
f5231f5762fc 8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents: 49433
diff changeset
  2660
     * defined above), then a  {@code String} object representing an
f5231f5762fc 8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents: 49433
diff changeset
  2661
     * empty string is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
     * Otherwise, let <i>k</i> be the index of the first character in the
50051
f5231f5762fc 8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents: 49433
diff changeset
  2664
     * string whose code is not a space (as defined above) and let
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
     * <i>m</i> be the index of the last character in the string whose code
50051
f5231f5762fc 8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents: 49433
diff changeset
  2666
     * is not a space (as defined above). A {@code String}
21841
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  2667
     * object is returned, representing the substring of this string that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
     * begins with the character at index <i>k</i> and ends with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
     * character at index <i>m</i>-that is, the result of
17181
e3d13a15c5c0 5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents: 15312
diff changeset
  2670
     * {@code this.substring(k, m + 1)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
     * <p>
50051
f5231f5762fc 8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents: 49433
diff changeset
  2672
     * This method may be used to trim space (as defined above) from
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
     * the beginning and end of a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
     *
50051
f5231f5762fc 8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents: 49433
diff changeset
  2675
     * @return  a string whose value is this string, with all leading
f5231f5762fc 8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents: 49433
diff changeset
  2676
     *          and trailing space removed, or this string if it
f5231f5762fc 8200372: String::trim JavaDoc should clarify meaning of space
jlaskey
parents: 49433
diff changeset
  2677
     *          has no leading or trailing space.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
    public String trim() {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  2680
        String ret = isLatin1() ? StringLatin1.trim(value)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  2681
                                : StringUTF16.trim(value);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  2682
        return ret == null ? this : ret;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
    /**
50098
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2686
     * Returns a string whose value is this string, with all leading
55062
45128070fd15 8224783: Javadoc of String strip methods uses link where linkplain would be better
darcy
parents: 54728
diff changeset
  2687
     * and trailing {@linkplain Character#isWhitespace(int) white space}
50098
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2688
     * removed.
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2689
     * <p>
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2690
     * If this {@code String} object represents an empty string,
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2691
     * or if all code points in this string are
55062
45128070fd15 8224783: Javadoc of String strip methods uses link where linkplain would be better
darcy
parents: 54728
diff changeset
  2692
     * {@linkplain Character#isWhitespace(int) white space}, then an empty string
50098
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2693
     * is returned.
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2694
     * <p>
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2695
     * Otherwise, returns a substring of this string beginning with the first
55062
45128070fd15 8224783: Javadoc of String strip methods uses link where linkplain would be better
darcy
parents: 54728
diff changeset
  2696
     * code point that is not a {@linkplain Character#isWhitespace(int) white space}
50098
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2697
     * up to and including the last code point that is not a
55062
45128070fd15 8224783: Javadoc of String strip methods uses link where linkplain would be better
darcy
parents: 54728
diff changeset
  2698
     * {@linkplain Character#isWhitespace(int) white space}.
50098
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2699
     * <p>
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2700
     * This method may be used to strip
55062
45128070fd15 8224783: Javadoc of String strip methods uses link where linkplain would be better
darcy
parents: 54728
diff changeset
  2701
     * {@linkplain Character#isWhitespace(int) white space} from
50098
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2702
     * the beginning and end of a string.
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2703
     *
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2704
     * @return  a string whose value is this string, with all leading
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2705
     *          and trailing white space removed
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2706
     *
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2707
     * @see Character#isWhitespace(int)
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2708
     *
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2709
     * @since 11
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2710
     */
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2711
    public String strip() {
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2712
        String ret = isLatin1() ? StringLatin1.strip(value)
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2713
                                : StringUTF16.strip(value);
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2714
        return ret == null ? this : ret;
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2715
    }
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2716
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2717
    /**
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2718
     * Returns a string whose value is this string, with all leading
55062
45128070fd15 8224783: Javadoc of String strip methods uses link where linkplain would be better
darcy
parents: 54728
diff changeset
  2719
     * {@linkplain Character#isWhitespace(int) white space} removed.
50098
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2720
     * <p>
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2721
     * If this {@code String} object represents an empty string,
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2722
     * or if all code points in this string are
55062
45128070fd15 8224783: Javadoc of String strip methods uses link where linkplain would be better
darcy
parents: 54728
diff changeset
  2723
     * {@linkplain Character#isWhitespace(int) white space}, then an empty string
50098
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2724
     * is returned.
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2725
     * <p>
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2726
     * Otherwise, returns a substring of this string beginning with the first
55062
45128070fd15 8224783: Javadoc of String strip methods uses link where linkplain would be better
darcy
parents: 54728
diff changeset
  2727
     * code point that is not a {@linkplain Character#isWhitespace(int) white space}
53563
a4b7ea85d668 8218022: Repeated words typos in java.base
prappo
parents: 53245
diff changeset
  2728
     * up to and including the last code point of this string.
50098
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2729
     * <p>
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2730
     * This method may be used to trim
55062
45128070fd15 8224783: Javadoc of String strip methods uses link where linkplain would be better
darcy
parents: 54728
diff changeset
  2731
     * {@linkplain Character#isWhitespace(int) white space} from
50098
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2732
     * the beginning of a string.
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2733
     *
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2734
     * @return  a string whose value is this string, with all leading white
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2735
     *          space removed
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2736
     *
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2737
     * @see Character#isWhitespace(int)
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2738
     *
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2739
     * @since 11
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2740
     */
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2741
    public String stripLeading() {
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2742
        String ret = isLatin1() ? StringLatin1.stripLeading(value)
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2743
                                : StringUTF16.stripLeading(value);
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2744
        return ret == null ? this : ret;
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2745
    }
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2746
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2747
    /**
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2748
     * Returns a string whose value is this string, with all trailing
55062
45128070fd15 8224783: Javadoc of String strip methods uses link where linkplain would be better
darcy
parents: 54728
diff changeset
  2749
     * {@linkplain Character#isWhitespace(int) white space} removed.
50098
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2750
     * <p>
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2751
     * If this {@code String} object represents an empty string,
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2752
     * or if all characters in this string are
55062
45128070fd15 8224783: Javadoc of String strip methods uses link where linkplain would be better
darcy
parents: 54728
diff changeset
  2753
     * {@linkplain Character#isWhitespace(int) white space}, then an empty string
50098
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2754
     * is returned.
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2755
     * <p>
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2756
     * Otherwise, returns a substring of this string beginning with the first
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2757
     * code point of this string up to and including the last code point
55062
45128070fd15 8224783: Javadoc of String strip methods uses link where linkplain would be better
darcy
parents: 54728
diff changeset
  2758
     * that is not a {@linkplain Character#isWhitespace(int) white space}.
50098
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2759
     * <p>
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2760
     * This method may be used to trim
55062
45128070fd15 8224783: Javadoc of String strip methods uses link where linkplain would be better
darcy
parents: 54728
diff changeset
  2761
     * {@linkplain Character#isWhitespace(int) white space} from
50098
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2762
     * the end of a string.
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2763
     *
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2764
     * @return  a string whose value is this string, with all trailing white
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2765
     *          space removed
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2766
     *
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2767
     * @see Character#isWhitespace(int)
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2768
     *
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2769
     * @since 11
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2770
     */
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2771
    public String stripTrailing() {
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2772
        String ret = isLatin1() ? StringLatin1.stripTrailing(value)
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2773
                                : StringUTF16.stripTrailing(value);
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2774
        return ret == null ? this : ret;
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2775
    }
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2776
92560438d306 8200377: String::strip, String::stripLeading, String::stripTrailing
jlaskey
parents: 50051
diff changeset
  2777
    /**
50175
589ed2770141 8200436: String::isBlank
jlaskey
parents: 50098
diff changeset
  2778
     * Returns {@code true} if the string is empty or contains only
55062
45128070fd15 8224783: Javadoc of String strip methods uses link where linkplain would be better
darcy
parents: 54728
diff changeset
  2779
     * {@linkplain Character#isWhitespace(int) white space} codepoints,
50175
589ed2770141 8200436: String::isBlank
jlaskey
parents: 50098
diff changeset
  2780
     * otherwise {@code false}.
589ed2770141 8200436: String::isBlank
jlaskey
parents: 50098
diff changeset
  2781
     *
589ed2770141 8200436: String::isBlank
jlaskey
parents: 50098
diff changeset
  2782
     * @return {@code true} if the string is empty or contains only
55062
45128070fd15 8224783: Javadoc of String strip methods uses link where linkplain would be better
darcy
parents: 54728
diff changeset
  2783
     *         {@linkplain Character#isWhitespace(int) white space} codepoints,
50175
589ed2770141 8200436: String::isBlank
jlaskey
parents: 50098
diff changeset
  2784
     *         otherwise {@code false}
589ed2770141 8200436: String::isBlank
jlaskey
parents: 50098
diff changeset
  2785
     *
589ed2770141 8200436: String::isBlank
jlaskey
parents: 50098
diff changeset
  2786
     * @see Character#isWhitespace(int)
589ed2770141 8200436: String::isBlank
jlaskey
parents: 50098
diff changeset
  2787
     *
589ed2770141 8200436: String::isBlank
jlaskey
parents: 50098
diff changeset
  2788
     * @since 11
589ed2770141 8200436: String::isBlank
jlaskey
parents: 50098
diff changeset
  2789
     */
589ed2770141 8200436: String::isBlank
jlaskey
parents: 50098
diff changeset
  2790
    public boolean isBlank() {
589ed2770141 8200436: String::isBlank
jlaskey
parents: 50098
diff changeset
  2791
        return indexOfNonWhitespace() == length();
589ed2770141 8200436: String::isBlank
jlaskey
parents: 50098
diff changeset
  2792
    }
589ed2770141 8200436: String::isBlank
jlaskey
parents: 50098
diff changeset
  2793
589ed2770141 8200436: String::isBlank
jlaskey
parents: 50098
diff changeset
  2794
    /**
51249
3fe0329588b8 8208164: (str) improve specification of String::lines
jlaskey
parents: 50237
diff changeset
  2795
     * Returns a stream of lines extracted from this string,
3fe0329588b8 8208164: (str) improve specification of String::lines
jlaskey
parents: 50237
diff changeset
  2796
     * separated by line terminators.
3fe0329588b8 8208164: (str) improve specification of String::lines
jlaskey
parents: 50237
diff changeset
  2797
     * <p>
3fe0329588b8 8208164: (str) improve specification of String::lines
jlaskey
parents: 50237
diff changeset
  2798
     * A <i>line terminator</i> is one of the following:
3fe0329588b8 8208164: (str) improve specification of String::lines
jlaskey
parents: 50237
diff changeset
  2799
     * a line feed character {@code "\n"} (U+000A),
3fe0329588b8 8208164: (str) improve specification of String::lines
jlaskey
parents: 50237
diff changeset
  2800
     * a carriage return character {@code "\r"} (U+000D),
3fe0329588b8 8208164: (str) improve specification of String::lines
jlaskey
parents: 50237
diff changeset
  2801
     * or a carriage return followed immediately by a line feed
3fe0329588b8 8208164: (str) improve specification of String::lines
jlaskey
parents: 50237
diff changeset
  2802
     * {@code "\r\n"} (U+000D U+000A).
50215
2fb27c352cae 8200380: String::lines
jlaskey
parents: 50175
diff changeset
  2803
     * <p>
51249
3fe0329588b8 8208164: (str) improve specification of String::lines
jlaskey
parents: 50237
diff changeset
  2804
     * A <i>line</i> is either a sequence of zero or more characters
3fe0329588b8 8208164: (str) improve specification of String::lines
jlaskey
parents: 50237
diff changeset
  2805
     * followed by a line terminator, or it is a sequence of one or
3fe0329588b8 8208164: (str) improve specification of String::lines
jlaskey
parents: 50237
diff changeset
  2806
     * more characters followed by the end of the string. A
3fe0329588b8 8208164: (str) improve specification of String::lines
jlaskey
parents: 50237
diff changeset
  2807
     * line does not include the line terminator.
50215
2fb27c352cae 8200380: String::lines
jlaskey
parents: 50175
diff changeset
  2808
     * <p>
51249
3fe0329588b8 8208164: (str) improve specification of String::lines
jlaskey
parents: 50237
diff changeset
  2809
     * The stream returned by this method contains the lines from
3fe0329588b8 8208164: (str) improve specification of String::lines
jlaskey
parents: 50237
diff changeset
  2810
     * this string in the order in which they occur.
3fe0329588b8 8208164: (str) improve specification of String::lines
jlaskey
parents: 50237
diff changeset
  2811
     *
3fe0329588b8 8208164: (str) improve specification of String::lines
jlaskey
parents: 50237
diff changeset
  2812
     * @apiNote This definition of <i>line</i> implies that an empty
3fe0329588b8 8208164: (str) improve specification of String::lines
jlaskey
parents: 50237
diff changeset
  2813
     *          string has zero lines and that there is no empty line
3fe0329588b8 8208164: (str) improve specification of String::lines
jlaskey
parents: 50237
diff changeset
  2814
     *          following a line terminator at the end of a string.
50215
2fb27c352cae 8200380: String::lines
jlaskey
parents: 50175
diff changeset
  2815
     *
2fb27c352cae 8200380: String::lines
jlaskey
parents: 50175
diff changeset
  2816
     * @implNote This method provides better performance than
2fb27c352cae 8200380: String::lines
jlaskey
parents: 50175
diff changeset
  2817
     *           split("\R") by supplying elements lazily and
2fb27c352cae 8200380: String::lines
jlaskey
parents: 50175
diff changeset
  2818
     *           by faster search of new line terminators.
2fb27c352cae 8200380: String::lines
jlaskey
parents: 50175
diff changeset
  2819
     *
51249
3fe0329588b8 8208164: (str) improve specification of String::lines
jlaskey
parents: 50237
diff changeset
  2820
     * @return  the stream of lines extracted from this string
50215
2fb27c352cae 8200380: String::lines
jlaskey
parents: 50175
diff changeset
  2821
     *
2fb27c352cae 8200380: String::lines
jlaskey
parents: 50175
diff changeset
  2822
     * @since 11
2fb27c352cae 8200380: String::lines
jlaskey
parents: 50175
diff changeset
  2823
     */
2fb27c352cae 8200380: String::lines
jlaskey
parents: 50175
diff changeset
  2824
    public Stream<String> lines() {
55260
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2825
        return isLatin1() ? StringLatin1.lines(value) : StringUTF16.lines(value);
51714
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2826
    }
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2827
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2828
    /**
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2829
     * Adjusts the indentation of each line of this string based on the value of
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2830
     * {@code n}, and normalizes line termination characters.
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2831
     * <p>
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2832
     * This string is conceptually separated into lines using
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2833
     * {@link String#lines()}. Each line is then adjusted as described below
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2834
     * and then suffixed with a line feed {@code "\n"} (U+000A). The resulting
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2835
     * lines are then concatenated and returned.
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2836
     * <p>
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2837
     * If {@code n > 0} then {@code n} spaces (U+0020) are inserted at the
53245
05c5c27b5a54 8215493: String::indent inconsistency with blank lines
jlaskey
parents: 53231
diff changeset
  2838
     * beginning of each line.
51714
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2839
     * <p>
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2840
     * If {@code n < 0} then up to {@code n}
55062
45128070fd15 8224783: Javadoc of String strip methods uses link where linkplain would be better
darcy
parents: 54728
diff changeset
  2841
     * {@linkplain Character#isWhitespace(int) white space characters} are removed
51714
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2842
     * from the beginning of each line. If a given line does not contain
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2843
     * sufficient white space then all leading
55062
45128070fd15 8224783: Javadoc of String strip methods uses link where linkplain would be better
darcy
parents: 54728
diff changeset
  2844
     * {@linkplain Character#isWhitespace(int) white space characters} are removed.
51714
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2845
     * Each white space character is treated as a single character. In
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2846
     * particular, the tab character {@code "\t"} (U+0009) is considered a
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2847
     * single character; it is not expanded.
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2848
     * <p>
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2849
     * If {@code n == 0} then the line remains unchanged. However, line
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2850
     * terminators are still normalized.
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2851
     *
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2852
     * @param n  number of leading
55062
45128070fd15 8224783: Javadoc of String strip methods uses link where linkplain would be better
darcy
parents: 54728
diff changeset
  2853
     *           {@linkplain Character#isWhitespace(int) white space characters}
51714
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2854
     *           to add or remove
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2855
     *
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2856
     * @return string with indentation adjusted and line endings normalized
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2857
     *
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2858
     * @see String#lines()
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2859
     * @see String#isBlank()
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2860
     * @see Character#isWhitespace(int)
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2861
     *
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2862
     * @since 12
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2863
     */
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2864
    public String indent(int n) {
55260
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2865
        if (isEmpty()) {
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2866
            return "";
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2867
        }
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2868
        Stream<String> stream = lines();
51714
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2869
        if (n > 0) {
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2870
            final String spaces = " ".repeat(n);
53245
05c5c27b5a54 8215493: String::indent inconsistency with blank lines
jlaskey
parents: 53231
diff changeset
  2871
            stream = stream.map(s -> spaces + s);
51714
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2872
        } else if (n == Integer.MIN_VALUE) {
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2873
            stream = stream.map(s -> s.stripLeading());
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2874
        } else if (n < 0) {
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2875
            stream = stream.map(s -> s.substring(Math.min(-n, s.indexOfNonWhitespace())));
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2876
        }
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2877
        return stream.collect(Collectors.joining("\n", "", "\n"));
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2878
    }
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2879
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2880
    private int indexOfNonWhitespace() {
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2881
        return isLatin1() ? StringLatin1.indexOfNonWhitespace(value)
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2882
                          : StringUTF16.indexOfNonWhitespace(value);
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2883
    }
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2884
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2885
    private int lastIndexOfNonWhitespace() {
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2886
        return isLatin1() ? StringLatin1.lastIndexOfNonWhitespace(value)
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2887
                          : StringUTF16.lastIndexOfNonWhitespace(value);
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2888
    }
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2889
975d3636a2f9 8200434: String::align, String::indent
jlaskey
parents: 51249
diff changeset
  2890
    /**
58440
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  2891
     * {@preview {@previewSummary Associated with text blocks,
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  2892
     *                            a preview feature of the Java language.}
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  2893
     *           This method is associated with <i>text blocks</i>, a preview
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  2894
     *           feature of the Java language. Programs can only use this
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  2895
     *           method when preview features are enabled. Preview features
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  2896
     *           may be removed in a future release, or upgraded to permanent
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  2897
     *           features of the Java language.}
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  2898
     *
55260
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2899
     * Returns a string whose value is this string, with incidental
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2900
     * {@linkplain Character#isWhitespace(int) white space} removed from
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2901
     * the beginning and end of every line.
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2902
     * <p>
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2903
     * Incidental {@linkplain Character#isWhitespace(int) white space}
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2904
     * is often present in a text block to align the content with the opening
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2905
     * delimiter. For example, in the following code, dots represent incidental
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2906
     * {@linkplain Character#isWhitespace(int) white space}:
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2907
     * <blockquote><pre>
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2908
     * String html = """
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2909
     * ..............&lt;html&gt;
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2910
     * ..............    &lt;body&gt;
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2911
     * ..............        &lt;p&gt;Hello, world&lt;/p&gt;
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2912
     * ..............    &lt;/body&gt;
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2913
     * ..............&lt;/html&gt;
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2914
     * ..............""";
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2915
     * </pre></blockquote>
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2916
     * This method treats the incidental
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2917
     * {@linkplain Character#isWhitespace(int) white space} as indentation to be
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2918
     * stripped, producing a string that preserves the relative indentation of
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2919
     * the content. Using | to visualize the start of each line of the string:
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2920
     * <blockquote><pre>
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2921
     * |&lt;html&gt;
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2922
     * |    &lt;body&gt;
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2923
     * |        &lt;p&gt;Hello, world&lt;/p&gt;
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2924
     * |    &lt;/body&gt;
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2925
     * |&lt;/html&gt;
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2926
     * </pre></blockquote>
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2927
     * First, the individual lines of this string are extracted as if by using
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2928
     * {@link String#lines()}.
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2929
     * <p>
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2930
     * Then, the <i>minimum indentation</i> (min) is determined as follows.
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2931
     * For each non-blank line (as defined by {@link String#isBlank()}), the
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2932
     * leading {@linkplain Character#isWhitespace(int) white space} characters are
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2933
     * counted. The leading {@linkplain Character#isWhitespace(int) white space}
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2934
     * characters on the last line are also counted even if
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2935
     * {@linkplain String#isBlank() blank}. The <i>min</i> value is the smallest
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2936
     * of these counts.
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2937
     * <p>
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2938
     * For each {@linkplain String#isBlank() non-blank} line, <i>min</i> leading
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2939
     * {@linkplain Character#isWhitespace(int) white space} characters are removed,
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2940
     * and any trailing {@linkplain Character#isWhitespace(int) white space}
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2941
     * characters are removed. {@linkplain String#isBlank() Blank} lines are
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2942
     * replaced with the empty string.
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2943
     *
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2944
     * <p>
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2945
     * Finally, the lines are joined into a new string, using the LF character
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2946
     * {@code "\n"} (U+000A) to separate lines.
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2947
     *
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2948
     * @apiNote
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2949
     * This method's primary purpose is to shift a block of lines as far as
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2950
     * possible to the left, while preserving relative indentation. Lines
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2951
     * that were indented the least will thus have no leading
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2952
     * {@linkplain Character#isWhitespace(int) white space}.
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2953
     * The line count of the result will be the same as line count of this
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2954
     * string.
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2955
     * If this string ends with a line terminator then the result will end
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2956
     * with a line terminator.
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2957
     *
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2958
     * @implNote
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2959
     * This method treats all {@linkplain Character#isWhitespace(int) white space}
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2960
     * characters as having equal width. As long as the indentation on every
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2961
     * line is consistently composed of the same character sequences, then the
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2962
     * result will be as described above.
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2963
     *
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2964
     * @return string with incidental indentation removed and line
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2965
     *         terminators normalized
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2966
     *
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2967
     * @see String#lines()
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2968
     * @see String#isBlank()
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2969
     * @see String#indent(int)
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2970
     * @see Character#isWhitespace(int)
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2971
     *
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2972
     * @since 13
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2973
     *
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2974
     */
58109
ee07de0d2c16 Updating javac behavior w.r.t. preview API. Based on darcy's patch.
jlahoda
parents: 57956
diff changeset
  2975
    @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.TEXT_BLOCKS,
ee07de0d2c16 Updating javac behavior w.r.t. preview API. Based on darcy's patch.
jlahoda
parents: 57956
diff changeset
  2976
                                 essentialAPI=true)
55260
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2977
    public String stripIndent() {
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2978
        int length = length();
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2979
        if (length == 0) {
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2980
            return "";
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2981
        }
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2982
        char lastChar = charAt(length - 1);
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2983
        boolean optOut = lastChar == '\n' || lastChar == '\r';
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2984
        List<String> lines = lines().collect(Collectors.toList());
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2985
        final int outdent = optOut ? 0 : outdent(lines);
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2986
        return lines.stream()
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2987
            .map(line -> {
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2988
                int firstNonWhitespace = line.indexOfNonWhitespace();
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2989
                int lastNonWhitespace = line.lastIndexOfNonWhitespace();
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2990
                int incidentalWhitespace = Math.min(outdent, firstNonWhitespace);
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2991
                return firstNonWhitespace > lastNonWhitespace
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2992
                    ? "" : line.substring(incidentalWhitespace, lastNonWhitespace);
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2993
            })
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2994
            .collect(Collectors.joining("\n", "", optOut ? "\n" : ""));
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2995
    }
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2996
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2997
    private static int outdent(List<String> lines) {
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2998
        // Note: outdent is guaranteed to be zero or positive number.
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  2999
        // If there isn't a non-blank line then the last must be blank
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  3000
        int outdent = Integer.MAX_VALUE;
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  3001
        for (String line : lines) {
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  3002
            int leadingWhitespace = line.indexOfNonWhitespace();
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  3003
            if (leadingWhitespace != line.length()) {
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  3004
                outdent = Integer.min(outdent, leadingWhitespace);
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  3005
            }
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  3006
        }
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  3007
        String lastLine = lines.get(lines.size() - 1);
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  3008
        if (lastLine.isBlank()) {
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  3009
            outdent = Integer.min(outdent, lastLine.length());
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  3010
        }
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  3011
        return outdent;
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  3012
    }
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  3013
cc0f117f4405 8223775: String::stripIndent (Preview)
jlaskey
parents: 55124
diff changeset
  3014
    /**
58440
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  3015
     * {@preview {@previewSummary Associated with text blocks,
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  3016
     *                            a preview feature of the Java language.}
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  3017
     *           This method is associated with <i>text blocks</i>, a preview
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  3018
     *           feature of the Java language. Programs can only use this
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  3019
     *           method when preview features are enabled. Preview features
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  3020
     *           may be removed in a future release, or upgraded to permanent
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  3021
     *           features of the Java language.}
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  3022
     *
55261
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3023
     * Returns a string whose value is this string, with escape sequences
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3024
     * translated as if in a string literal.
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3025
     * <p>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3026
     * Escape sequences are translated as follows;
55290
ceabad800468 8225448: String::translateEscapes javadoc has accessibility issues
jlaskey
parents: 55262
diff changeset
  3027
     * <table class="striped">
55261
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3028
     *   <caption style="display:none">Translation</caption>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3029
     *   <thead>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3030
     *   <tr>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3031
     *     <th scope="col">Escape</th>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3032
     *     <th scope="col">Name</th>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3033
     *     <th scope="col">Translation</th>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3034
     *   </tr>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3035
     *   </thead>
55290
ceabad800468 8225448: String::translateEscapes javadoc has accessibility issues
jlaskey
parents: 55262
diff changeset
  3036
     *   <tbody>
55261
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3037
     *   <tr>
55290
ceabad800468 8225448: String::translateEscapes javadoc has accessibility issues
jlaskey
parents: 55262
diff changeset
  3038
     *     <th scope="row">{@code \u005Cb}</th>
55261
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3039
     *     <td>backspace</td>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3040
     *     <td>{@code U+0008}</td>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3041
     *   </tr>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3042
     *   <tr>
55290
ceabad800468 8225448: String::translateEscapes javadoc has accessibility issues
jlaskey
parents: 55262
diff changeset
  3043
     *     <th scope="row">{@code \u005Ct}</th>
55261
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3044
     *     <td>horizontal tab</td>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3045
     *     <td>{@code U+0009}</td>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3046
     *   </tr>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3047
     *   <tr>
55290
ceabad800468 8225448: String::translateEscapes javadoc has accessibility issues
jlaskey
parents: 55262
diff changeset
  3048
     *     <th scope="row">{@code \u005Cn}</th>
55261
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3049
     *     <td>line feed</td>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3050
     *     <td>{@code U+000A}</td>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3051
     *   </tr>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3052
     *   <tr>
55290
ceabad800468 8225448: String::translateEscapes javadoc has accessibility issues
jlaskey
parents: 55262
diff changeset
  3053
     *     <th scope="row">{@code \u005Cf}</th>
55261
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3054
     *     <td>form feed</td>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3055
     *     <td>{@code U+000C}</td>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3056
     *   </tr>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3057
     *   <tr>
55290
ceabad800468 8225448: String::translateEscapes javadoc has accessibility issues
jlaskey
parents: 55262
diff changeset
  3058
     *     <th scope="row">{@code \u005Cr}</th>
55261
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3059
     *     <td>carriage return</td>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3060
     *     <td>{@code U+000D}</td>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3061
     *   </tr>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3062
     *   <tr>
55290
ceabad800468 8225448: String::translateEscapes javadoc has accessibility issues
jlaskey
parents: 55262
diff changeset
  3063
     *     <th scope="row">{@code \u005C"}</th>
55261
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3064
     *     <td>double quote</td>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3065
     *     <td>{@code U+0022}</td>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3066
     *   </tr>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3067
     *   <tr>
55290
ceabad800468 8225448: String::translateEscapes javadoc has accessibility issues
jlaskey
parents: 55262
diff changeset
  3068
     *     <th scope="row">{@code \u005C'}</th>
55261
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3069
     *     <td>single quote</td>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3070
     *     <td>{@code U+0027}</td>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3071
     *   </tr>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3072
     *   <tr>
55290
ceabad800468 8225448: String::translateEscapes javadoc has accessibility issues
jlaskey
parents: 55262
diff changeset
  3073
     *     <th scope="row">{@code \u005C\u005C}</th>
55261
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3074
     *     <td>backslash</td>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3075
     *     <td>{@code U+005C}</td>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3076
     *   </tr>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3077
     *   <tr>
55290
ceabad800468 8225448: String::translateEscapes javadoc has accessibility issues
jlaskey
parents: 55262
diff changeset
  3078
     *     <th scope="row">{@code \u005C0 - \u005C377}</th>
55261
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3079
     *     <td>octal escape</td>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3080
     *     <td>code point equivalents</td>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3081
     *   </tr>
55290
ceabad800468 8225448: String::translateEscapes javadoc has accessibility issues
jlaskey
parents: 55262
diff changeset
  3082
     *   </tbody>
55261
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3083
     * </table>
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3084
     *
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3085
     * @implNote
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3086
     * This method does <em>not</em> translate Unicode escapes such as "{@code \u005cu2022}".
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3087
     * Unicode escapes are translated by the Java compiler when reading input characters and
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3088
     * are not part of the string literal specification.
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3089
     *
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3090
     * @throws IllegalArgumentException when an escape sequence is malformed.
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3091
     *
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3092
     * @return String with escape sequences translated.
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3093
     *
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3094
     * @jls 3.10.7 Escape Sequences
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3095
     *
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3096
     * @since 13
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3097
     */
58109
ee07de0d2c16 Updating javac behavior w.r.t. preview API. Based on darcy's patch.
jlahoda
parents: 57956
diff changeset
  3098
    @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.TEXT_BLOCKS,
ee07de0d2c16 Updating javac behavior w.r.t. preview API. Based on darcy's patch.
jlahoda
parents: 57956
diff changeset
  3099
                                 essentialAPI=true)
55261
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3100
    public String translateEscapes() {
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3101
        if (isEmpty()) {
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3102
            return "";
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3103
        }
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3104
        char[] chars = toCharArray();
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3105
        int length = chars.length;
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3106
        int from = 0;
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3107
        int to = 0;
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3108
        while (from < length) {
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3109
            char ch = chars[from++];
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3110
            if (ch == '\\') {
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3111
                ch = from < length ? chars[from++] : '\0';
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3112
                switch (ch) {
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3113
                case 'b':
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3114
                    ch = '\b';
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3115
                    break;
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3116
                case 'f':
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3117
                    ch = '\f';
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3118
                    break;
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3119
                case 'n':
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3120
                    ch = '\n';
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3121
                    break;
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3122
                case 'r':
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3123
                    ch = '\r';
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3124
                    break;
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3125
                case 't':
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3126
                    ch = '\t';
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3127
                    break;
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3128
                case '\'':
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3129
                case '\"':
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3130
                case '\\':
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3131
                    // as is
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3132
                    break;
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3133
                case '0': case '1': case '2': case '3':
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3134
                case '4': case '5': case '6': case '7':
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3135
                    int limit = Integer.min(from + (ch <= '3' ? 2 : 1), length);
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3136
                    int code = ch - '0';
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3137
                    while (from < limit) {
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3138
                        ch = chars[from];
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3139
                        if (ch < '0' || '7' < ch) {
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3140
                            break;
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3141
                        }
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3142
                        from++;
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3143
                        code = (code << 3) | (ch - '0');
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3144
                    }
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3145
                    ch = (char)code;
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3146
                    break;
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3147
                default: {
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3148
                    String msg = String.format(
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3149
                        "Invalid escape sequence: \\%c \\\\u%04X",
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3150
                        ch, (int)ch);
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3151
                    throw new IllegalArgumentException(msg);
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3152
                }
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3153
                }
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3154
            }
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3155
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3156
            chars[to++] = ch;
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3157
        }
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3158
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3159
        return new String(chars, 0, to);
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3160
    }
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3161
dff30b1557ee 8223780: String::translateEscapes (Preview)
jlaskey
parents: 55260
diff changeset
  3162
    /**
52682
c93e62e7fa3c 8203442: String::transform
jlaskey
parents: 52312
diff changeset
  3163
     * This method allows the application of a function to {@code this}
c93e62e7fa3c 8203442: String::transform
jlaskey
parents: 52312
diff changeset
  3164
     * string. The function should expect a single String argument
c93e62e7fa3c 8203442: String::transform
jlaskey
parents: 52312
diff changeset
  3165
     * and produce an {@code R} result.
53230
b5f085197234 8215112: String::transform spec clarification
jlaskey
parents: 53049
diff changeset
  3166
     * <p>
b5f085197234 8215112: String::transform spec clarification
jlaskey
parents: 53049
diff changeset
  3167
     * Any exception thrown by {@code f()} will be propagated to the
b5f085197234 8215112: String::transform spec clarification
jlaskey
parents: 53049
diff changeset
  3168
     * caller.
52682
c93e62e7fa3c 8203442: String::transform
jlaskey
parents: 52312
diff changeset
  3169
     *
c93e62e7fa3c 8203442: String::transform
jlaskey
parents: 52312
diff changeset
  3170
     * @param f    functional interface to a apply
c93e62e7fa3c 8203442: String::transform
jlaskey
parents: 52312
diff changeset
  3171
     *
c93e62e7fa3c 8203442: String::transform
jlaskey
parents: 52312
diff changeset
  3172
     * @param <R>  class of the result
c93e62e7fa3c 8203442: String::transform
jlaskey
parents: 52312
diff changeset
  3173
     *
c93e62e7fa3c 8203442: String::transform
jlaskey
parents: 52312
diff changeset
  3174
     * @return     the result of applying the function to this string
c93e62e7fa3c 8203442: String::transform
jlaskey
parents: 52312
diff changeset
  3175
     *
c93e62e7fa3c 8203442: String::transform
jlaskey
parents: 52312
diff changeset
  3176
     * @see java.util.function.Function
c93e62e7fa3c 8203442: String::transform
jlaskey
parents: 52312
diff changeset
  3177
     *
c93e62e7fa3c 8203442: String::transform
jlaskey
parents: 52312
diff changeset
  3178
     * @since 12
c93e62e7fa3c 8203442: String::transform
jlaskey
parents: 52312
diff changeset
  3179
     */
c93e62e7fa3c 8203442: String::transform
jlaskey
parents: 52312
diff changeset
  3180
    public <R> R transform(Function<? super String, ? extends R> f) {
c93e62e7fa3c 8203442: String::transform
jlaskey
parents: 52312
diff changeset
  3181
        return f.apply(this);
c93e62e7fa3c 8203442: String::transform
jlaskey
parents: 52312
diff changeset
  3182
    }
c93e62e7fa3c 8203442: String::transform
jlaskey
parents: 52312
diff changeset
  3183
c93e62e7fa3c 8203442: String::transform
jlaskey
parents: 52312
diff changeset
  3184
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
     * This object (which is already a string!) is itself returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
     * @return  the string itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
28667
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3193
    /**
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3194
     * Returns a stream of {@code int} zero-extending the {@code char} values
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3195
     * from this sequence.  Any char which maps to a <a
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 49211
diff changeset
  3196
     * href="{@docRoot}/java.base/java/lang/Character.html#unicode">surrogate code
28667
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3197
     * point</a> is passed through uninterpreted.
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3198
     *
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3199
     * @return an IntStream of char values from this sequence
47463
30186b6741b8 8189952: New methods on String: chars() and codePoints() should be marked since 9
darcy
parents: 47216
diff changeset
  3200
     * @since 9
28667
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3201
     */
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3202
    @Override
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3203
    public IntStream chars() {
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3204
        return StreamSupport.intStream(
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3205
            isLatin1() ? new StringLatin1.CharsSpliterator(value, Spliterator.IMMUTABLE)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3206
                       : new StringUTF16.CharsSpliterator(value, Spliterator.IMMUTABLE),
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3207
            false);
28667
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3208
    }
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3209
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3210
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3211
    /**
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3212
     * Returns a stream of code point values from this sequence.  Any surrogate
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3213
     * pairs encountered in the sequence are combined as if by {@linkplain
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3214
     * Character#toCodePoint Character.toCodePoint} and the result is passed
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3215
     * to the stream. Any other code units, including ordinary BMP characters,
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3216
     * unpaired surrogates, and undefined code units, are zero-extended to
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3217
     * {@code int} values which are then passed to the stream.
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3218
     *
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3219
     * @return an IntStream of Unicode code points from this sequence
47463
30186b6741b8 8189952: New methods on String: chars() and codePoints() should be marked since 9
darcy
parents: 47216
diff changeset
  3220
     * @since 9
28667
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3221
     */
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3222
    @Override
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3223
    public IntStream codePoints() {
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3224
        return StreamSupport.intStream(
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3225
            isLatin1() ? new StringLatin1.CharsSpliterator(value, Spliterator.IMMUTABLE)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3226
                       : new StringUTF16.CodePointsSpliterator(value, Spliterator.IMMUTABLE),
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3227
            false);
28667
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3228
    }
2245cc40bf5d 8071477: Better Spliterator implementations for String.chars() and String.codePoints()
psandoz
parents: 28423
diff changeset
  3229
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
     * Converts this string to a new character array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
     * @return  a newly allocated character array whose length is the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
     *          of this string and whose contents are initialized to contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
     *          the character sequence represented by this string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
    public char[] toCharArray() {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3238
        return isLatin1() ? StringLatin1.toChars(value)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3239
                          : StringUTF16.toChars(value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
     * Returns a formatted string using the specified format string and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
     * arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
     * <p> The locale always used is the one returned by {@link
38786
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents: 38765
diff changeset
  3247
     * java.util.Locale#getDefault(java.util.Locale.Category)
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents: 38765
diff changeset
  3248
     * Locale.getDefault(Locale.Category)} with
8e7b0ac05815 8146156: Inconsistent default locale in string formatter methods
naoto
parents: 38765
diff changeset
  3249
     * {@link java.util.Locale.Category#FORMAT FORMAT} category specified.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
     * @param  format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
     *         A <a href="../util/Formatter.html#syntax">format string</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
     * @param  args
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
     *         Arguments referenced by the format specifiers in the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
     *         string.  If there are more arguments than format specifiers, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
     *         extra arguments are ignored.  The number of arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
     *         variable and may be zero.  The maximum number of arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
     *         limited by the maximum dimension of a Java array as defined by
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7816
diff changeset
  3260
     *         <cite>The Java&trade; Virtual Machine Specification</cite>.
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7816
diff changeset
  3261
     *         The behaviour on a
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  3262
     *         {@code null} argument depends on the <a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
     *         href="../util/Formatter.html#syntax">conversion</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
     *
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13404
diff changeset
  3265
     * @throws  java.util.IllegalFormatException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
     *          If a format string contains an illegal syntax, a format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
     *          specifier that is incompatible with the given arguments,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
     *          insufficient arguments given the format string, or other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
     *          illegal conditions.  For specification of all possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
     *          formatting errors, see the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
     *          href="../util/Formatter.html#detail">Details</a> section of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
     *          formatter class specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
     * @return  A formatted string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
     * @see  java.util.Formatter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
     */
12858
97e3f3f77254 6924259: Remove offset and count fields from java.lang.String
mduigou
parents: 11676
diff changeset
  3279
    public static String format(String format, Object... args) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
        return new Formatter().format(format, args).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
     * Returns a formatted string using the specified locale, format string,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
     * and arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
     * @param  l
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
     *         The {@linkplain java.util.Locale locale} to apply during
14997
97d6098fd419 8005118: Javadoc styles are inconsistent
jgish
parents: 14686
diff changeset
  3289
     *         formatting.  If {@code l} is {@code null} then no localization
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
     *         is applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
     * @param  format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
     *         A <a href="../util/Formatter.html#syntax">format string</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
     * @param  args
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
     *         Arguments referenced by the format specifiers in the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
     *         string.  If there are more arguments than format specifiers, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
     *         extra arguments are ignored.  The number of arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
     *         variable and may be zero.  The maximum number of arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
     *         limited by the maximum dimension of a Java array as defined by
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7816
diff changeset
  3301
     *         <cite>The Java&trade; Virtual Machine Specification</cite>.
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7816
diff changeset
  3302
     *         The behaviour on a
15312
4b57f9da8192 4247235: (spec str) StringBuffer.insert(int, char[]) specification is inconsistent
jgish
parents: 14997
diff changeset
  3303
     *         {@code null} argument depends on the
4b57f9da8192 4247235: (spec str) StringBuffer.insert(int, char[]) specification is inconsistent
jgish
parents: 14997
diff changeset
  3304
     *         <a href="../util/Formatter.html#syntax">conversion</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
     *
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13404
diff changeset
  3306
     * @throws  java.util.IllegalFormatException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
     *          If a format string contains an illegal syntax, a format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
     *          specifier that is incompatible with the given arguments,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
     *          insufficient arguments given the format string, or other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
     *          illegal conditions.  For specification of all possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
     *          formatting errors, see the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
     *          href="../util/Formatter.html#detail">Details</a> section of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
     *          formatter class specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
     * @return  A formatted string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
     * @see  java.util.Formatter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
     */
12858
97e3f3f77254 6924259: Remove offset and count fields from java.lang.String
mduigou
parents: 11676
diff changeset
  3320
    public static String format(Locale l, String format, Object... args) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
        return new Formatter(l).format(format, args).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
    /**
58440
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  3325
     * {@preview Associated with text blocks, a preview feature of
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  3326
     *           the Java language.
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  3327
     *
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  3328
     *           This method is associated with <i>text blocks</i>, a preview
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  3329
     *           feature of the Java language. Programs can only use this
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  3330
     *           method when preview features are enabled. Preview features
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  3331
     *           may be removed in a future release, or upgraded to permanent
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  3332
     *           features of the Java language.}
4c8a6d1d066c Improving appearance of javadoc of the APIs associated with preview features.
jlahoda
parents: 58109
diff changeset
  3333
     *
55262
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents: 55261
diff changeset
  3334
     * Formats using this string as the format string, and the supplied
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents: 55261
diff changeset
  3335
     * arguments.
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents: 55261
diff changeset
  3336
     *
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents: 55261
diff changeset
  3337
     * @implSpec This method is equivalent to {@code String.format(this, args)}.
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents: 55261
diff changeset
  3338
     *
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents: 55261
diff changeset
  3339
     * @param  args
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents: 55261
diff changeset
  3340
     *         Arguments referenced by the format specifiers in this string.
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents: 55261
diff changeset
  3341
     *
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents: 55261
diff changeset
  3342
     * @return  A formatted string
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents: 55261
diff changeset
  3343
     *
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents: 55261
diff changeset
  3344
     * @see  java.lang.String#format(String,Object...)
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents: 55261
diff changeset
  3345
     * @see  java.util.Formatter
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents: 55261
diff changeset
  3346
     *
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents: 55261
diff changeset
  3347
     * @since 13
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents: 55261
diff changeset
  3348
     *
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents: 55261
diff changeset
  3349
     */
58109
ee07de0d2c16 Updating javac behavior w.r.t. preview API. Based on darcy's patch.
jlahoda
parents: 57956
diff changeset
  3350
    @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.TEXT_BLOCKS,
ee07de0d2c16 Updating javac behavior w.r.t. preview API. Based on darcy's patch.
jlahoda
parents: 57956
diff changeset
  3351
                                 essentialAPI=true)
55262
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents: 55261
diff changeset
  3352
    public String formatted(Object... args) {
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents: 55261
diff changeset
  3353
        return new Formatter().format(this, args).toString();
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents: 55261
diff changeset
  3354
    }
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents: 55261
diff changeset
  3355
7d83cf1cfa74 8203444: String::formatted (Preview)
jlaskey
parents: 55261
diff changeset
  3356
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3357
     * Returns the string representation of the {@code Object} argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3359
     * @param   obj   an {@code Object}.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3360
     * @return  if the argument is {@code null}, then a string equal to
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3361
     *          {@code "null"}; otherwise, the value of
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3362
     *          {@code obj.toString()} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
     * @see     java.lang.Object#toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
    public static String valueOf(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
        return (obj == null) ? "null" : obj.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3370
     * Returns the string representation of the {@code char} array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
     * argument. The contents of the character array are copied; subsequent
21841
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  3372
     * modification of the character array does not affect the returned
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  3373
     * string.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
     *
21841
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  3375
     * @param   data     the character array.
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  3376
     * @return  a {@code String} that contains the characters of the
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  3377
     *          character array.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
    public static String valueOf(char data[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
        return new String(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
     * Returns the string representation of a specific subarray of the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3385
     * {@code char} array argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
     * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3387
     * The {@code offset} argument is the index of the first
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3388
     * character of the subarray. The {@code count} argument
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
     * specifies the length of the subarray. The contents of the subarray
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
     * are copied; subsequent modification of the character array does not
21841
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  3391
     * affect the returned string.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
     * @param   data     the character array.
21841
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  3394
     * @param   offset   initial offset of the subarray.
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  3395
     * @param   count    length of the subarray.
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  3396
     * @return  a {@code String} that contains the characters of the
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  3397
     *          specified subarray of the character array.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3398
     * @exception IndexOutOfBoundsException if {@code offset} is
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3399
     *          negative, or {@code count} is negative, or
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3400
     *          {@code offset+count} is larger than
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3401
     *          {@code data.length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
    public static String valueOf(char data[], int offset, int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
        return new String(data, offset, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
    /**
21841
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  3408
     * Equivalent to {@link #valueOf(char[], int, int)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
     * @param   data     the character array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
     * @param   offset   initial offset of the subarray.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
     * @param   count    length of the subarray.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3413
     * @return  a {@code String} that contains the characters of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
     *          specified subarray of the character array.
21841
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  3415
     * @exception IndexOutOfBoundsException if {@code offset} is
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  3416
     *          negative, or {@code count} is negative, or
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  3417
     *          {@code offset+count} is larger than
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  3418
     *          {@code data.length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
    public static String copyValueOf(char data[], int offset, int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
        return new String(data, offset, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
    /**
21841
5e5571b9a6a2 7174936: several String methods claim to always create new String
smarks
parents: 21670
diff changeset
  3425
     * Equivalent to {@link #valueOf(char[])}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
     * @param   data   the character array.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3428
     * @return  a {@code String} that contains the characters of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
     *          character array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
    public static String copyValueOf(char data[]) {
12858
97e3f3f77254 6924259: Remove offset and count fields from java.lang.String
mduigou
parents: 11676
diff changeset
  3432
        return new String(data);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3436
     * Returns the string representation of the {@code boolean} argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3438
     * @param   b   a {@code boolean}.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3439
     * @return  if the argument is {@code true}, a string equal to
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3440
     *          {@code "true"} is returned; otherwise, a string equal to
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3441
     *          {@code "false"} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
    public static String valueOf(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
        return b ? "true" : "false";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3448
     * Returns the string representation of the {@code char}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
     * argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3451
     * @param   c   a {@code char}.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3452
     * @return  a string of length {@code 1} containing
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3453
     *          as its single character the argument {@code c}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
    public static String valueOf(char c) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3456
        if (COMPACT_STRINGS && StringLatin1.canEncode(c)) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3457
            return new String(StringLatin1.toBytes(c), LATIN1);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3458
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3459
        return new String(StringUTF16.toBytes(c), UTF16);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3463
     * Returns the string representation of the {@code int} argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
     * The representation is exactly the one returned by the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3466
     * {@code Integer.toString} method of one argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3468
     * @param   i   an {@code int}.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3469
     * @return  a string representation of the {@code int} argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
     * @see     java.lang.Integer#toString(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
    public static String valueOf(int i) {
3964
cf913644be58 6480728: Byte.valueOf(byte) returns a cached value but Byte.valueOf(String)
darcy
parents: 3617
diff changeset
  3473
        return Integer.toString(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3477
     * Returns the string representation of the {@code long} argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
     * The representation is exactly the one returned by the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3480
     * {@code Long.toString} method of one argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3482
     * @param   l   a {@code long}.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3483
     * @return  a string representation of the {@code long} argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
     * @see     java.lang.Long#toString(long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
    public static String valueOf(long l) {
3964
cf913644be58 6480728: Byte.valueOf(byte) returns a cached value but Byte.valueOf(String)
darcy
parents: 3617
diff changeset
  3487
        return Long.toString(l);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3491
     * Returns the string representation of the {@code float} argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
     * The representation is exactly the one returned by the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3494
     * {@code Float.toString} method of one argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3496
     * @param   f   a {@code float}.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3497
     * @return  a string representation of the {@code float} argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
     * @see     java.lang.Float#toString(float)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
    public static String valueOf(float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
        return Float.toString(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3505
     * Returns the string representation of the {@code double} argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
     * The representation is exactly the one returned by the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3508
     * {@code Double.toString} method of one argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3510
     * @param   d   a {@code double}.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3511
     * @return  a  string representation of the {@code double} argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
     * @see     java.lang.Double#toString(double)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
    public static String valueOf(double d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
        return Double.toString(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
     * Returns a canonical representation for the string object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
     * A pool of strings, initially empty, is maintained privately by the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3522
     * class {@code String}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
     * When the intern method is invoked, if the pool already contains a
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3525
     * string equal to this {@code String} object as determined by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
     * the {@link #equals(Object)} method, then the string from the pool is
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3527
     * returned. Otherwise, this {@code String} object is added to the
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3528
     * pool and a reference to this {@code String} object is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
     * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3530
     * It follows that for any two strings {@code s} and {@code t},
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3531
     * {@code s.intern() == t.intern()} is {@code true}
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11127
diff changeset
  3532
     * if and only if {@code s.equals(t)} is {@code true}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
     * All literal strings and string-valued constant expressions are
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7816
diff changeset
  3535
     * interned. String literals are defined in section 3.10.5 of the
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7816
diff changeset
  3536
     * <cite>The Java&trade; Language Specification</cite>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
     * @return  a string that has the same contents as this string, but is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
     *          guaranteed to be from a pool of unique strings.
37723
1b9abe91485d 8155792: Add @jls citations to java.lang.String
darcy
parents: 37718
diff changeset
  3540
     * @jls 3.10.5 String Literals
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
    public native String intern();
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3543
49109
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3544
    /**
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3545
     * Returns a string whose value is the concatenation of this
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3546
     * string repeated {@code count} times.
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3547
     * <p>
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3548
     * If this string is empty or count is zero then the empty
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3549
     * string is returned.
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3550
     *
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3551
     * @param   count number of times to repeat
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3552
     *
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3553
     * @return  A string composed of this string repeated
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3554
     *          {@code count} times or the empty string if this
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3555
     *          string is empty or count is zero
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3556
     *
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3557
     * @throws  IllegalArgumentException if the {@code count} is
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3558
     *          negative.
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3559
     *
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3560
     * @since 11
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3561
     */
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3562
    public String repeat(int count) {
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3563
        if (count < 0) {
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3564
            throw new IllegalArgumentException("count is negative: " + count);
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3565
        }
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3566
        if (count == 1) {
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3567
            return this;
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3568
        }
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3569
        final int len = value.length;
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3570
        if (len == 0 || count == 0) {
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3571
            return "";
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3572
        }
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3573
        if (len == 1) {
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3574
            final byte[] single = new byte[count];
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3575
            Arrays.fill(single, value[0]);
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3576
            return new String(single, coder);
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3577
        }
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3578
        if (Integer.MAX_VALUE / count < len) {
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3579
            throw new OutOfMemoryError("Repeating " + len + " bytes String " + count +
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3580
                    " times will produce a String exceeding maximum size.");
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3581
        }
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3582
        final int limit = len * count;
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3583
        final byte[] multiple = new byte[limit];
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3584
        System.arraycopy(value, 0, multiple, 0, len);
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3585
        int copied = len;
49122
fc16b5f193c7 8198955: String#repeat loop optimization
jlaskey
parents: 49109
diff changeset
  3586
        for (; copied < limit - copied; copied <<= 1) {
49109
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3587
            System.arraycopy(multiple, 0, multiple, copied, copied);
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3588
        }
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3589
        System.arraycopy(multiple, 0, multiple, copied, limit - copied);
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3590
        return new String(multiple, coder);
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3591
    }
f2804cb922ce 8197594: String#repeat
jlaskey
parents: 48262
diff changeset
  3592
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3593
    ////////////////////////////////////////////////////////////////
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3594
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3595
    /**
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3596
     * Copy character bytes from this string into dst starting at dstBegin.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3597
     * This method doesn't perform any range checking.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3598
     *
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3599
     * Invoker guarantees: dst is in UTF16 (inflate itself for asb), if two
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3600
     * coders are different, and dst is big enough (range check)
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3601
     *
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3602
     * @param dstBegin  the char index, not offset of byte[]
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3603
     * @param coder     the coder of dst[]
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3604
     */
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3605
    void getBytes(byte dst[], int dstBegin, byte coder) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3606
        if (coder() == coder) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3607
            System.arraycopy(value, 0, dst, dstBegin << coder, value.length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3608
        } else {    // this.coder == LATIN && coder == UTF16
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3609
            StringLatin1.inflate(value, 0, dst, dstBegin, value.length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3610
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3611
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3612
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3613
    /*
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3614
     * Package private constructor. Trailing Void argument is there for
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3615
     * disambiguating it against other (public) constructors.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3616
     *
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3617
     * Stores the char[] value into a byte[] that each byte represents
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3618
     * the8 low-order bits of the corresponding character, if the char[]
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3619
     * contains only latin1 character. Or a byte[] that stores all
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3620
     * characters in their byte sequences defined by the {@code StringUTF16}.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3621
     */
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3622
    String(char[] value, int off, int len, Void sig) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3623
        if (len == 0) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3624
            this.value = "".value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3625
            this.coder = "".coder;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3626
            return;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3627
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3628
        if (COMPACT_STRINGS) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3629
            byte[] val = StringUTF16.compress(value, off, len);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3630
            if (val != null) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3631
                this.value = val;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3632
                this.coder = LATIN1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3633
                return;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3634
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3635
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3636
        this.coder = UTF16;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3637
        this.value = StringUTF16.toBytes(value, off, len);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3638
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3639
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3640
    /*
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3641
     * Package private constructor. Trailing Void argument is there for
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3642
     * disambiguating it against other (public) constructors.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3643
     */
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3644
    String(AbstractStringBuilder asb, Void sig) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3645
        byte[] val = asb.getValue();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3646
        int length = asb.length();
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3647
        if (asb.isLatin1()) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3648
            this.coder = LATIN1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3649
            this.value = Arrays.copyOfRange(val, 0, length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3650
        } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3651
            if (COMPACT_STRINGS) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3652
                byte[] buf = StringUTF16.compress(val, 0, length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3653
                if (buf != null) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3654
                    this.coder = LATIN1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3655
                    this.value = buf;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3656
                    return;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3657
                }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3658
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3659
            this.coder = UTF16;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3660
            this.value = Arrays.copyOfRange(val, 0, length << 1);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3661
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3662
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3664
   /*
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3665
    * Package private constructor which shares value array for speed.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3666
    */
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3667
    String(byte[] value, byte coder) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3668
        this.value = value;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3669
        this.coder = coder;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3670
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3671
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3672
    byte coder() {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3673
        return COMPACT_STRINGS ? coder : UTF16;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3674
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3675
48262
daf3b49f4839 8184947: ZipCoder performance improvements
sherman
parents: 48065
diff changeset
  3676
    byte[] value() {
daf3b49f4839 8184947: ZipCoder performance improvements
sherman
parents: 48065
diff changeset
  3677
        return value;
daf3b49f4839 8184947: ZipCoder performance improvements
sherman
parents: 48065
diff changeset
  3678
    }
daf3b49f4839 8184947: ZipCoder performance improvements
sherman
parents: 48065
diff changeset
  3679
55124
f91999057a5a 8224986: (str) optimize StringBuilder.append(CharSequence, int, int) for String arguments
redestad
parents: 55062
diff changeset
  3680
    boolean isLatin1() {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3681
        return COMPACT_STRINGS && coder == LATIN1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3682
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3683
45572
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 45138
diff changeset
  3684
    @Native static final byte LATIN1 = 0;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 45138
diff changeset
  3685
    @Native static final byte UTF16  = 1;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3686
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3687
    /*
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3688
     * StringIndexOutOfBoundsException  if {@code index} is
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3689
     * negative or greater than or equal to {@code length}.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3690
     */
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3691
    static void checkIndex(int index, int length) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3692
        if (index < 0 || index >= length) {
44642
331e669007f7 8158168: Missing bounds checks for some String intrinsics
dlong
parents: 44369
diff changeset
  3693
            throw new StringIndexOutOfBoundsException("index " + index +
57783
a0257f1458aa 8221307: String.substring() OOB exception on start index reports improper information
igerasim
parents: 55290
diff changeset
  3694
                                                      ", length " + length);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3695
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3696
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3697
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3698
    /*
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3699
     * StringIndexOutOfBoundsException  if {@code offset}
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3700
     * is negative or greater than {@code length}.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3701
     */
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3702
    static void checkOffset(int offset, int length) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3703
        if (offset < 0 || offset > length) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3704
            throw new StringIndexOutOfBoundsException("offset " + offset +
57783
a0257f1458aa 8221307: String.substring() OOB exception on start index reports improper information
igerasim
parents: 55290
diff changeset
  3705
                                                      ", length " + length);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3706
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3707
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3708
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3709
    /*
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3710
     * Check {@code offset}, {@code count} against {@code 0} and {@code length}
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3711
     * bounds.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3712
     *
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3713
     * @throws  StringIndexOutOfBoundsException
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3714
     *          If {@code offset} is negative, {@code count} is negative,
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3715
     *          or {@code offset} is greater than {@code length - count}
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3716
     */
34517
c6e795a80c80 8142303: C2 compilation fails with "bad AD file"
thartmann
parents: 33663
diff changeset
  3717
    static void checkBoundsOffCount(int offset, int count, int length) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3718
        if (offset < 0 || count < 0 || offset > length - count) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3719
            throw new StringIndexOutOfBoundsException(
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3720
                "offset " + offset + ", count " + count + ", length " + length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3721
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3722
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3723
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3724
    /*
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3725
     * Check {@code begin}, {@code end} against {@code 0} and {@code length}
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3726
     * bounds.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3727
     *
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3728
     * @throws  StringIndexOutOfBoundsException
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3729
     *          If {@code begin} is negative, {@code begin} is greater than
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3730
     *          {@code end}, or {@code end} is greater than {@code length}.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3731
     */
44642
331e669007f7 8158168: Missing bounds checks for some String intrinsics
dlong
parents: 44369
diff changeset
  3732
    static void checkBoundsBeginEnd(int begin, int end, int length) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3733
        if (begin < 0 || begin > end || end > length) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3734
            throw new StringIndexOutOfBoundsException(
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3735
                "begin " + begin + ", end " + end + ", length " + length);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3736
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 33314
diff changeset
  3737
    }
49129
fb9f590b9eee 4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents: 49122
diff changeset
  3738
fb9f590b9eee 4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents: 49122
diff changeset
  3739
    /**
fb9f590b9eee 4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents: 49122
diff changeset
  3740
     * Returns the string representation of the {@code codePoint}
fb9f590b9eee 4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents: 49122
diff changeset
  3741
     * argument.
fb9f590b9eee 4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents: 49122
diff changeset
  3742
     *
fb9f590b9eee 4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents: 49122
diff changeset
  3743
     * @param   codePoint a {@code codePoint}.
fb9f590b9eee 4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents: 49122
diff changeset
  3744
     * @return  a string of length {@code 1} or {@code 2} containing
fb9f590b9eee 4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents: 49122
diff changeset
  3745
     *          as its single character the argument {@code codePoint}.
fb9f590b9eee 4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents: 49122
diff changeset
  3746
     * @throws IllegalArgumentException if the specified
fb9f590b9eee 4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents: 49122
diff changeset
  3747
     *          {@code codePoint} is not a {@linkplain Character#isValidCodePoint
fb9f590b9eee 4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents: 49122
diff changeset
  3748
     *          valid Unicode code point}.
fb9f590b9eee 4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents: 49122
diff changeset
  3749
     */
fb9f590b9eee 4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents: 49122
diff changeset
  3750
    static String valueOfCodePoint(int codePoint) {
fb9f590b9eee 4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents: 49122
diff changeset
  3751
        if (COMPACT_STRINGS && StringLatin1.canEncode(codePoint)) {
fb9f590b9eee 4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents: 49122
diff changeset
  3752
            return new String(StringLatin1.toBytes((char)codePoint), LATIN1);
fb9f590b9eee 4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents: 49122
diff changeset
  3753
        } else if (Character.isBmpCodePoint(codePoint)) {
fb9f590b9eee 4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents: 49122
diff changeset
  3754
            return new String(StringUTF16.toBytes((char)codePoint), UTF16);
fb9f590b9eee 4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents: 49122
diff changeset
  3755
        } else if (Character.isSupplementaryCodePoint(codePoint)) {
fb9f590b9eee 4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents: 49122
diff changeset
  3756
            return new String(StringUTF16.toBytesSupplementary(codePoint), UTF16);
fb9f590b9eee 4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents: 49122
diff changeset
  3757
        }
fb9f590b9eee 4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents: 49122
diff changeset
  3758
49211
948ece16567b 8198989: Provide more diagnostic IAE messages
naoto
parents: 49129
diff changeset
  3759
        throw new IllegalArgumentException(
948ece16567b 8198989: Provide more diagnostic IAE messages
naoto
parents: 49129
diff changeset
  3760
            format("Not a valid Unicode code point: 0x%X", codePoint));
49129
fb9f590b9eee 4993841: (str) java.lang.Character should have a toString(int) method
naoto
parents: 49122
diff changeset
  3761
    }
52914
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3762
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3763
    /**
53019
4ddd3c410a85 8215300: additional changes to constants API
vromero
parents: 53018
diff changeset
  3764
     * Returns an {@link Optional} containing the nominal descriptor for this
4ddd3c410a85 8215300: additional changes to constants API
vromero
parents: 53018
diff changeset
  3765
     * instance, which is the instance itself.
52914
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3766
     *
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3767
     * @return an {@link Optional} describing the {@linkplain String} instance
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3768
     * @since 12
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3769
     */
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3770
    @Override
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3771
    public Optional<String> describeConstable() {
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3772
        return Optional.of(this);
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3773
    }
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3774
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3775
    /**
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3776
     * Resolves this instance as a {@link ConstantDesc}, the result of which is
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3777
     * the instance itself.
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3778
     *
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3779
     * @param lookup ignored
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3780
     * @return the {@linkplain String} instance
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3781
     * @since 12
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3782
     */
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3783
    @Override
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3784
    public String resolveConstantDesc(MethodHandles.Lookup lookup) {
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3785
        return this;
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3786
    }
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52682
diff changeset
  3787
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3788
}