src/share/classes/java/lang/String.java

Print this page
rev 5382 : 6924259: Remove offset and count fields from java.lang.String
Summary: Removes the use of shared character array buffers by String along with the two fields needed to support the use of shared buffers.
Contributed-by: brian.doherty@oracle.com

@@ -20,19 +20,16 @@
  *
  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
-
 package java.lang;
 
-import java.io.ObjectStreamClass;
 import java.io.ObjectStreamField;
 import java.io.UnsupportedEncodingException;
 import java.nio.charset.Charset;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Comparator;
 import java.util.Formatter;
 import java.util.Locale;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;

@@ -104,23 +101,16 @@
  * @see     java.lang.StringBuffer
  * @see     java.lang.StringBuilder
  * @see     java.nio.charset.Charset
  * @since   JDK1.0
  */
-
 public final class String
-    implements java.io.Serializable, Comparable<String>, CharSequence
-{
+        implements java.io.Serializable, Comparable<String>, CharSequence {
+
     /** The value is used for character storage. */
     private final char value[];
 
-    /** The offset is the first index of the storage that is used. */
-    private final int offset;
-
-    /** The count is the number of characters in the String. */
-    private final int count;
-
     /** Cache the hash code for the string */
     private int hash; // Default to 0
 
     /** use serialVersionUID from JDK 1.0.2 for interoperability */
     private static final long serialVersionUID = -6849794470754667710L;

@@ -139,18 +129,21 @@
      */
     private static final ObjectStreamField[] serialPersistentFields =
         new ObjectStreamField[0];
 
     /**
+     * a shared instance of the string value for empty strings.
+     */
+    private static final char[] EMPTY_STRING_VALUE = new char[0];
+
+    /**
      * Initializes a newly created {@code String} object so that it represents
      * an empty character sequence.  Note that use of this constructor is
      * unnecessary since Strings are immutable.
      */
     public String() {
-        this.offset = 0;
-        this.count = 0;
-        this.value = new char[0];
+        this.value = EMPTY_STRING_VALUE;
     }
 
     /**
      * Initializes a newly created {@code String} object so that it represents
      * the same sequence of characters as the argument; in other words, the

@@ -160,27 +153,14 @@
      *
      * @param  original
      *         A {@code String}
      */
     public String(String original) {
-        int size = original.count;
-        char[] originalValue = original.value;
-        char[] v;
-        if (originalValue.length > size) {
-            // The array representing the String is bigger than the new
-            // String itself.  Perhaps this constructor is being called
-            // in order to trim the baggage, so make a copy of the array.
-            int off = original.offset;
-            v = Arrays.copyOfRange(originalValue, off, off+size);
-        } else {
-            // The array representing the String is the same
-            // size as the String, so no point in making a copy.
-            v = originalValue;
-        }
-        this.offset = 0;
-        this.count = size;
-        this.value = v;
+        this.value = (original.value.length > 0)
+                ? original.value
+                : EMPTY_STRING_VALUE;
+        this.hash = original.hash;
     }
 
     /**
      * Allocates a new {@code String} so that it represents the sequence of
      * characters currently contained in the character array argument. The

@@ -189,14 +169,17 @@
      *
      * @param  value
      *         The initial value of the string
      */
     public String(char value[]) {
-        int size = value.length;
-        this.offset = 0;
-        this.count = size;
-        this.value = Arrays.copyOf(value, size);
+        if (value.length > 0) {
+            int len = value.length;
+            this.value = new char[len];
+            System.arraycopy(value, 0, this.value, 0, len);
+        } else {
+            this.value = EMPTY_STRING_VALUE;
+        }
     }
 
     /**
      * Allocates a new {@code String} that contains characters from a subarray
      * of the character array argument. The {@code offset} argument is the

@@ -227,13 +210,16 @@
         }
         // Note: offset or count might be near -1>>>1.
         if (offset > value.length - count) {
             throw new StringIndexOutOfBoundsException(offset + count);
         }
-        this.offset = 0;
-        this.count = count;
-        this.value = Arrays.copyOfRange(value, offset, offset+count);
+        if (count > 0) {
+            this.value = new char[count];
+            System.arraycopy(value, offset, this.value, 0, count);
+        } else {
+            this.value = EMPTY_STRING_VALUE;
+        }
     }
 
     /**
      * Allocates a new {@code String} that contains characters from a subarray
      * of the <a href="Character.html#unicode">Unicode code point</a> array

@@ -286,23 +272,21 @@
                 n++;
             else throw new IllegalArgumentException(Integer.toString(c));
         }
 
         // Pass 2: Allocate and fill in char[]
-        final char[] v = new char[n];
+        final char[] v = (n != 0) ? new char[n] : EMPTY_STRING_VALUE;
 
         for (int i = offset, j = 0; i < end; i++, j++) {
             int c = codePoints[i];
             if (Character.isBmpCodePoint(c))
                 v[j] = (char) c;
             else
                 Character.toSurrogates(c, v, j++);
         }
 
         this.value  = v;
-        this.count  = n;
-        this.offset = 0;
     }
 
     /**
      * Allocates a new {@code String} constructed from a subarray of an array
      * of 8-bit integer values.

@@ -343,24 +327,22 @@
      * @see  #String(byte[])
      */
     @Deprecated
     public String(byte ascii[], int hibyte, int offset, int count) {
         checkBounds(ascii, offset, count);
-        char value[] = new char[count];
+        char value[] = (count != 0) ? new char[count] : EMPTY_STRING_VALUE;
 
         if (hibyte == 0) {
-            for (int i = count ; i-- > 0 ;) {
+            for (int i = count; i-- > 0;) {
                 value[i] = (char) (ascii[i + offset] & 0xff);
             }
         } else {
             hibyte <<= 8;
-            for (int i = count ; i-- > 0 ;) {
+            for (int i = count; i-- > 0;) {
                 value[i] = (char) (hibyte | (ascii[i + offset] & 0xff));
             }
         }
-        this.offset = 0;
-        this.count = count;
         this.value = value;
     }
 
     /**
      * Allocates a new {@code String} containing characters constructed from

@@ -442,19 +424,16 @@
      *          characters outside the bounds of the {@code bytes} array
      *
      * @since  JDK1.1
      */
     public String(byte bytes[], int offset, int length, String charsetName)
-        throws UnsupportedEncodingException
-    {
+            throws UnsupportedEncodingException {
         if (charsetName == null)
             throw new NullPointerException("charsetName");
         checkBounds(bytes, offset, length);
         char[] v = StringCoding.decode(charsetName, bytes, offset, length);
-        this.offset = 0;
-        this.count = v.length;
-        this.value = v;
+        this.value = (v.length != 0) ? v : EMPTY_STRING_VALUE;
     }
 
     /**
      * Constructs a new {@code String} by decoding the specified subarray of
      * bytes using the specified {@linkplain java.nio.charset.Charset charset}.

@@ -488,13 +467,11 @@
     public String(byte bytes[], int offset, int length, Charset charset) {
         if (charset == null)
             throw new NullPointerException("charset");
         checkBounds(bytes, offset, length);
         char[] v = StringCoding.decode(charset, bytes, offset, length);
-        this.offset = 0;
-        this.count = v.length;
-        this.value = v;
+        this.value = (v.length != 0) ? v : EMPTY_STRING_VALUE;
     }
 
     /**
      * Constructs a new {@code String} by decoding the specified array of bytes
      * using the specified {@linkplain java.nio.charset.Charset charset}.  The

@@ -517,12 +494,11 @@
      *          If the named charset is not supported
      *
      * @since  JDK1.1
      */
     public String(byte bytes[], String charsetName)
-        throws UnsupportedEncodingException
-    {
+            throws UnsupportedEncodingException {
         this(bytes, 0, bytes.length, charsetName);
     }
 
     /**
      * Constructs a new {@code String} by decoding the specified array of

@@ -575,13 +551,11 @@
      * @since  JDK1.1
      */
     public String(byte bytes[], int offset, int length) {
         checkBounds(bytes, offset, length);
         char[] v  = StringCoding.decode(bytes, offset, length);
-        this.offset = 0;
-        this.count = v.length;
-        this.value = v;
+        this.value = (v.length != 0) ? v : EMPTY_STRING_VALUE;
     }
 
     /**
      * Constructs a new {@code String} by decoding the specified array of bytes
      * using the platform's default charset.  The length of the new {@code

@@ -610,14 +584,11 @@
      *
      * @param  buffer
      *         A {@code StringBuffer}
      */
     public String(StringBuffer buffer) {
-        String result = buffer.toString();
-        this.value = result.value;
-        this.count = result.count;
-        this.offset = result.offset;
+        this(buffer.toString());
     }
 
     /**
      * Allocates a new string that contains the sequence of characters
      * currently contained in the string builder argument. The contents of the

@@ -632,22 +603,23 @@
      *          A {@code StringBuilder}
      *
      * @since  1.5
      */
     public String(StringBuilder builder) {
-        String result = builder.toString();
-        this.value = result.value;
-        this.count = result.count;
-        this.offset = result.offset;
+        this(builder.toString());
     }
 
-
     // Package private constructor which shares value array for speed.
-    String(int offset, int count, char value[]) {
-        this.value = value;
-        this.offset = offset;
-        this.count = count;
+    // this constructor is always expected to be called with share==true.
+    // a separate constructor is needed because we already have a public
+    // String(char[]) constructor that makes a copy of the given char[].
+    //
+    String(char value[], boolean share) {
+        // assert share : "unshared not supported";
+        this.value = (value.length > 0)
+                ? value
+                : EMPTY_STRING_VALUE;
     }
 
     /**
      * Returns the length of this string.
      * The length is equal to the number of <a href="Character.html#unicode">Unicode

@@ -655,11 +627,11 @@
      *
      * @return  the length of the sequence of characters represented by this
      *          object.
      */
     public int length() {
-        return count;
+        return value.length;
     }
 
     /**
      * Returns <tt>true</tt> if, and only if, {@link #length()} is <tt>0</tt>.
      *

@@ -667,11 +639,11 @@
      * <tt>false</tt>
      *
      * @since 1.6
      */
     public boolean isEmpty() {
-        return count == 0;
+        return value.length == 0;
     }
 
     /**
      * Returns the {@code char} value at the
      * specified index. An index ranges from {@code 0} to

@@ -689,14 +661,14 @@
      * @exception  IndexOutOfBoundsException  if the {@code index}
      *             argument is negative or not less than the length of this
      *             string.
      */
     public char charAt(int index) {
-        if ((index < 0) || (index >= count)) {
+        if ((index < 0) || (index >= value.length)) {
             throw new StringIndexOutOfBoundsException(index);
         }
-        return value[index + offset];
+        return value[index];
     }
 
     /**
      * Returns the character (Unicode code point) at the specified
      * index. The index refers to {@code char} values

@@ -718,14 +690,14 @@
      *             argument is negative or not less than the length of this
      *             string.
      * @since      1.5
      */
     public int codePointAt(int index) {
-        if ((index < 0) || (index >= count)) {
+        if ((index < 0) || (index >= value.length)) {
             throw new StringIndexOutOfBoundsException(index);
         }
-        return Character.codePointAtImpl(value, offset + index, offset + count);
+        return Character.codePointAtImpl(value, index, value.length);
     }
 
     /**
      * Returns the character (Unicode code point) before the specified
      * index. The index refers to {@code char} values

@@ -748,14 +720,14 @@
      *            of this string.
      * @since     1.5
      */
     public int codePointBefore(int index) {
         int i = index - 1;
-        if ((i < 0) || (i >= count)) {
+        if ((i < 0) || (i >= value.length)) {
             throw new StringIndexOutOfBoundsException(index);
         }
-        return Character.codePointBeforeImpl(value, offset + index, offset);
+        return Character.codePointBeforeImpl(value, index, 0);
     }
 
     /**
      * Returns the number of Unicode code points in the specified text
      * range of this {@code String}. The text range begins at the

@@ -776,14 +748,14 @@
      * is larger than the length of this {@code String}, or
      * {@code beginIndex} is larger than {@code endIndex}.
      * @since  1.5
      */
     public int codePointCount(int beginIndex, int endIndex) {
-        if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {
+        if (beginIndex < 0 || endIndex > value.length || beginIndex > endIndex) {
             throw new IndexOutOfBoundsException();
         }
-        return Character.codePointCountImpl(value, offset+beginIndex, endIndex-beginIndex);
+        return Character.codePointCountImpl(value, beginIndex, endIndex - beginIndex);
     }
 
     /**
      * Returns the index within this {@code String} that is
      * offset from the given {@code index} by

@@ -803,23 +775,23 @@
      *   before {@code index} has fewer than the absolute value
      *   of {@code codePointOffset} code points.
      * @since 1.5
      */
     public int offsetByCodePoints(int index, int codePointOffset) {
-        if (index < 0 || index > count) {
+        if (index < 0 || index > value.length) {
             throw new IndexOutOfBoundsException();
         }
-        return Character.offsetByCodePointsImpl(value, offset, count,
-                                                offset+index, codePointOffset) - offset;
+        return Character.offsetByCodePointsImpl(value, 0, value.length,
+                index, codePointOffset);
     }
 
     /**
      * Copy characters from this string into dst starting at dstBegin.
      * This method doesn't perform any range checking.
      */
     void getChars(char dst[], int dstBegin) {
-        System.arraycopy(value, offset, dst, dstBegin, count);
+        System.arraycopy(value, 0, dst, dstBegin, value.length);
     }
 
     /**
      * Copies characters from this string into the destination character
      * array.

@@ -852,18 +824,17 @@
      */
     public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
         if (srcBegin < 0) {
             throw new StringIndexOutOfBoundsException(srcBegin);
         }
-        if (srcEnd > count) {
+        if (srcEnd > value.length) {
             throw new StringIndexOutOfBoundsException(srcEnd);
         }
         if (srcBegin > srcEnd) {
             throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
         }
-        System.arraycopy(value, offset + srcBegin, dst, dstBegin,
-             srcEnd - srcBegin);
+        System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
     }
 
     /**
      * Copies characters from this string into the destination byte array. Each
      * byte receives the 8 low-order bits of the corresponding character. The

@@ -910,23 +881,23 @@
     @Deprecated
     public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) {
         if (srcBegin < 0) {
             throw new StringIndexOutOfBoundsException(srcBegin);
         }
-        if (srcEnd > count) {
+        if (srcEnd > value.length) {
             throw new StringIndexOutOfBoundsException(srcEnd);
         }
         if (srcBegin > srcEnd) {
             throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
         }
         int j = dstBegin;
-        int n = offset + srcEnd;
-        int i = offset + srcBegin;
+        int n = srcEnd;
+        int i = srcBegin;
         char[] val = value;   /* avoid getfield opcode */
 
         while (i < n) {
-            dst[j++] = (byte)val[i++];
+            dst[j++] = (byte) val[i++];
         }
     }
 
     /**
      * Encodes this {@code String} into a sequence of bytes using the named

@@ -947,14 +918,13 @@
      *          If the named charset is not supported
      *
      * @since  JDK1.1
      */
     public byte[] getBytes(String charsetName)
-        throws UnsupportedEncodingException
-    {
+            throws UnsupportedEncodingException {
         if (charsetName == null) throw new NullPointerException();
-        return StringCoding.encode(charsetName, value, offset, count);
+        return StringCoding.encode(charsetName, value);
     }
 
     /**
      * Encodes this {@code String} into a sequence of bytes using the given
      * {@linkplain java.nio.charset.Charset charset}, storing the result into a

@@ -973,11 +943,11 @@
      *
      * @since  1.6
      */
     public byte[] getBytes(Charset charset) {
         if (charset == null) throw new NullPointerException();
-        return StringCoding.encode(charset, value, offset, count);
+        return StringCoding.encode(charset, value);
     }
 
     /**
      * Encodes this {@code String} into a sequence of bytes using the
      * platform's default charset, storing the result into a new byte array.

@@ -990,11 +960,11 @@
      * @return  The resultant byte array
      *
      * @since      JDK1.1
      */
     public byte[] getBytes() {
-        return StringCoding.encode(value, offset, count);
+        return StringCoding.encode(value);
     }
 
     /**
      * Compares this string to the specified object.  The result is {@code
      * true} if and only if the argument is not {@code null} and is a {@code

@@ -1013,20 +983,20 @@
     public boolean equals(Object anObject) {
         if (this == anObject) {
             return true;
         }
         if (anObject instanceof String) {
-            String anotherString = (String)anObject;
-            int n = count;
-            if (n == anotherString.count) {
+            String anotherString = (String) anObject;
+            int n = value.length;
+            if (n == anotherString.value.length) {
                 char v1[] = value;
                 char v2[] = anotherString.value;
-                int i = offset;
-                int j = anotherString.offset;
+                int i = 0;
                 while (n-- != 0) {
-                    if (v1[i++] != v2[j++])
+                    if (v1[i] != v2[i])
                         return false;
+                    i++;
                 }
                 return true;
             }
         }
         return false;

@@ -1045,12 +1015,12 @@
      *          {@code false} otherwise
      *
      * @since  1.4
      */
     public boolean contentEquals(StringBuffer sb) {
-        synchronized(sb) {
-            return contentEquals((CharSequence)sb);
+        synchronized (sb) {
+            return contentEquals((CharSequence) sb);
         }
     }
 
     /**
      * Compares this string to the specified {@code CharSequence}.  The result

@@ -1065,36 +1035,36 @@
      *          false} otherwise
      *
      * @since  1.5
      */
     public boolean contentEquals(CharSequence cs) {
-        if (count != cs.length())
+        if (value.length != cs.length())
             return false;
         // Argument is a StringBuffer, StringBuilder
         if (cs instanceof AbstractStringBuilder) {
             char v1[] = value;
-            char v2[] = ((AbstractStringBuilder)cs).getValue();
-            int i = offset;
-            int j = 0;
-            int n = count;
+            char v2[] = ((AbstractStringBuilder) cs).getValue();
+            int i = 0;
+            int n = value.length;
             while (n-- != 0) {
-                if (v1[i++] != v2[j++])
+                if (v1[i] != v2[i])
                     return false;
+                i++;
             }
             return true;
         }
         // Argument is a String
         if (cs.equals(this))
             return true;
         // Argument is a generic CharSequence
         char v1[] = value;
-        int i = offset;
-        int j = 0;
-        int n = count;
+        int i = 0;
+        int n = value.length;
         while (n-- != 0) {
-            if (v1[i++] != cs.charAt(j++))
+            if (v1[i] != cs.charAt(i))
                 return false;
+            i++;
         }
         return true;
     }
 
     /**

@@ -1124,13 +1094,14 @@
      *          false} otherwise
      *
      * @see  #equals(Object)
      */
     public boolean equalsIgnoreCase(String anotherString) {
-        return (this == anotherString) ? true :
-               (anotherString != null) && (anotherString.count == count) &&
-               regionMatches(true, 0, anotherString, 0, count);
+        return (this == anotherString) ? true
+                : (anotherString != null)
+                && (anotherString.value.length == value.length)
+                && regionMatches(true, 0, anotherString, 0, value.length);
     }
 
     /**
      * Compares two strings lexicographically.
      * The comparison is based on the Unicode value of each character in

@@ -1171,38 +1142,25 @@
      *          is lexicographically less than the string argument; and a
      *          value greater than {@code 0} if this string is
      *          lexicographically greater than the string argument.
      */
     public int compareTo(String anotherString) {
-        int len1 = count;
-        int len2 = anotherString.count;
-        int n = Math.min(len1, len2);
+        int len1 = value.length;
+        int len2 = anotherString.value.length;
+        int lim = Math.min(len1, len2);
         char v1[] = value;
         char v2[] = anotherString.value;
-        int i = offset;
-        int j = anotherString.offset;
 
-        if (i == j) {
-            int k = i;
-            int lim = n + i;
+        int k = 0;
             while (k < lim) {
                 char c1 = v1[k];
                 char c2 = v2[k];
                 if (c1 != c2) {
                     return c1 - c2;
                 }
                 k++;
             }
-        } else {
-            while (n-- != 0) {
-                char c1 = v1[i++];
-                char c2 = v2[j++];
-                if (c1 != c2) {
-                    return c1 - c2;
-                }
-            }
-        }
         return len1 - len2;
     }
 
     /**
      * A Comparator that orders {@code String} objects as by

@@ -1306,16 +1264,17 @@
      *          {@code false} otherwise.
      */
     public boolean regionMatches(int toffset, String other, int ooffset,
                                  int len) {
         char ta[] = value;
-        int to = offset + toffset;
+        int to = toffset;
         char pa[] = other.value;
-        int po = other.offset + ooffset;
+        int po = ooffset;
         // Note: toffset, ooffset, or len might be near -1>>>1.
-        if ((ooffset < 0) || (toffset < 0) || (toffset > (long)count - len)
-            || (ooffset > (long)other.count - len)) {
+        if ((ooffset < 0) || (toffset < 0)
+                || (toffset > (long) value.length - len)
+                || (ooffset > (long) other.value.length - len)) {
             return false;
         }
         while (len-- > 0) {
             if (ta[to++] != pa[po++]) {
                 return false;

@@ -1375,16 +1334,17 @@
      *          argument.
      */
     public boolean regionMatches(boolean ignoreCase, int toffset,
                            String other, int ooffset, int len) {
         char ta[] = value;
-        int to = offset + toffset;
+        int to = toffset;
         char pa[] = other.value;
-        int po = other.offset + ooffset;
+        int po = ooffset;
         // Note: toffset, ooffset, or len might be near -1>>>1.
-        if ((ooffset < 0) || (toffset < 0) || (toffset > (long)count - len) ||
-                (ooffset > (long)other.count - len)) {
+        if ((ooffset < 0) || (toffset < 0)
+                || (toffset > (long) value.length - len)
+                || (ooffset > (long) other.value.length - len)) {
             return false;
         }
         while (len-- > 0) {
             char c1 = ta[to++];
             char c2 = pa[po++];

@@ -1431,16 +1391,16 @@
      *          this.substring(toffset).startsWith(prefix)
      *          </pre>
      */
     public boolean startsWith(String prefix, int toffset) {
         char ta[] = value;
-        int to = offset + toffset;
+        int to = toffset;
         char pa[] = prefix.value;
-        int po = prefix.offset;
-        int pc = prefix.count;
+        int po = 0;
+        int pc = prefix.value.length;
         // Note: toffset might be near -1>>>1.
-        if ((toffset < 0) || (toffset > count - pc)) {
+        if ((toffset < 0) || (toffset > value.length - pc)) {
             return false;
         }
         while (--pc >= 0) {
             if (ta[to++] != pa[po++]) {
                 return false;

@@ -1476,11 +1436,11 @@
      *          result will be {@code true} if the argument is the
      *          empty string or is equal to this {@code String} object
      *          as determined by the {@link #equals(Object)} method.
      */
     public boolean endsWith(String suffix) {
-        return startsWith(suffix, count - suffix.count);
+        return startsWith(suffix, value.length - suffix.value.length);
     }
 
     /**
      * Returns a hash code for this string. The hash code for a
      * {@code String} object is computed as

@@ -1494,17 +1454,15 @@
      *
      * @return  a hash code value for this object.
      */
     public int hashCode() {
         int h = hash;
-        if (h == 0 && count > 0) {
-            int off = offset;
+        if (h == 0 && value.length > 0) {
             char val[] = value;
-            int len = count;
 
-            for (int i = 0; i < len; i++) {
-                h = 31*h + val[off++];
+            for (int i = 0; i < value.length; i++) {
+                h = 31 * h + val[i];
             }
             hash = h;
         }
         return h;
     }

@@ -1575,26 +1533,25 @@
      *          character sequence represented by this object that is greater
      *          than or equal to {@code fromIndex}, or {@code -1}
      *          if the character does not occur.
      */
     public int indexOf(int ch, int fromIndex) {
+        final int max = value.length;
         if (fromIndex < 0) {
             fromIndex = 0;
-        } else if (fromIndex >= count) {
+        } else if (fromIndex >= max) {
             // Note: fromIndex might be near -1>>>1.
             return -1;
         }
 
         if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
             // handle most cases here (ch is a BMP code point or a
             // negative value (invalid code point))
             final char[] value = this.value;
-            final int offset = this.offset;
-            final int max = offset + count;
-            for (int i = offset + fromIndex; i < max ; i++) {
+            for (int i = fromIndex; i < max; i++) {
                 if (value[i] == ch) {
-                    return i - offset;
+                    return i;
                 }
             }
             return -1;
         } else {
             return indexOfSupplementary(ch, fromIndex);

@@ -1605,17 +1562,16 @@
      * Handles (rare) calls of indexOf with a supplementary character.
      */
     private int indexOfSupplementary(int ch, int fromIndex) {
         if (Character.isValidCodePoint(ch)) {
             final char[] value = this.value;
-            final int offset = this.offset;
             final char hi = Character.highSurrogate(ch);
             final char lo = Character.lowSurrogate(ch);
-            final int max = offset + count - 1;
-            for (int i = offset + fromIndex; i < max; i++) {
-                if (value[i] == hi && value[i+1] == lo) {
-                    return i - offset;
+            final int max = value.length - 1;
+            for (int i = fromIndex; i < max; i++) {
+                if (value[i] == hi && value[i + 1] == lo) {
+                    return i;
                 }
             }
         }
         return -1;
     }

@@ -1642,11 +1598,11 @@
      * @return  the index of the last occurrence of the character in the
      *          character sequence represented by this object, or
      *          {@code -1} if the character does not occur.
      */
     public int lastIndexOf(int ch) {
-        return lastIndexOf(ch, count - 1);
+        return lastIndexOf(ch, value.length - 1);
     }
 
     /**
      * Returns the index within this string of the last occurrence of
      * the specified character, searching backward starting at the

@@ -1684,15 +1640,14 @@
     public int lastIndexOf(int ch, int fromIndex) {
         if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
             // handle most cases here (ch is a BMP code point or a
             // negative value (invalid code point))
             final char[] value = this.value;
-            final int offset = this.offset;
-            int i = offset + Math.min(fromIndex, count - 1);
-            for (; i >= offset ; i--) {
+            int i = Math.min(fromIndex, value.length - 1);
+            for (; i >= 0; i--) {
                 if (value[i] == ch) {
-                    return i - offset;
+                    return i;
                 }
             }
             return -1;
         } else {
             return lastIndexOfSupplementary(ch, fromIndex);

@@ -1703,17 +1658,16 @@
      * Handles (rare) calls of lastIndexOf with a supplementary character.
      */
     private int lastIndexOfSupplementary(int ch, int fromIndex) {
         if (Character.isValidCodePoint(ch)) {
             final char[] value = this.value;
-            final int offset = this.offset;
             char hi = Character.highSurrogate(ch);
             char lo = Character.lowSurrogate(ch);
-            int i = offset + Math.min(fromIndex, count - 2);
-            for (; i >= offset; i--) {
-                if (value[i] == hi && value[i+1] == lo) {
-                    return i - offset;
+            int i = Math.min(fromIndex, value.length - 2);
+            for (; i >= 0; i--) {
+                if (value[i] == hi && value[i + 1] == lo) {
+                    return i;
                 }
             }
         }
         return -1;
     }

@@ -1751,12 +1705,12 @@
      * @return  the index of the first occurrence of the specified substring,
      *          starting at the specified index,
      *          or {@code -1} if there is no such occurrence.
      */
     public int indexOf(String str, int fromIndex) {
-        return indexOf(value, offset, count,
-                       str.value, str.offset, str.count, fromIndex);
+        return indexOf(value, 0, value.length,
+                str.value, 0, str.value.length, fromIndex);
     }
 
     /**
      * Code shared by String and StringBuffer to do searches. The
      * source is the character array being searched, and the target

@@ -1794,12 +1748,12 @@
 
             /* Found first character, now look at the rest of v2 */
             if (i <= max) {
                 int j = i + 1;
                 int end = j + targetCount - 1;
-                for (int k = targetOffset + 1; j < end && source[j] ==
-                         target[k]; j++, k++);
+                for (int k = targetOffset + 1; j < end && source[j]
+                        == target[k]; j++, k++);
 
                 if (j == end) {
                     /* Found whole string. */
                     return i - sourceOffset;
                 }

@@ -1822,11 +1776,11 @@
      * @param   str   the substring to search for.
      * @return  the index of the last occurrence of the specified substring,
      *          or {@code -1} if there is no such occurrence.
      */
     public int lastIndexOf(String str) {
-        return lastIndexOf(str, count);
+        return lastIndexOf(str, value.length);
     }
 
     /**
      * Returns the index within this string of the last occurrence of the
      * specified substring, searching backward starting at the specified index.

@@ -1842,12 +1796,12 @@
      * @return  the index of the last occurrence of the specified substring,
      *          searching backward from the specified index,
      *          or {@code -1} if there is no such occurrence.
      */
     public int lastIndexOf(String str, int fromIndex) {
-        return lastIndexOf(value, offset, count,
-                           str.value, str.offset, str.count, fromIndex);
+        return lastIndexOf(value, 0, value.length,
+                str.value, 0, str.value.length, fromIndex);
     }
 
     /**
      * Code shared by String and StringBuffer to do searches. The
      * source is the character array being searched, and the target

@@ -1923,11 +1877,18 @@
      * @exception  IndexOutOfBoundsException  if
      *             {@code beginIndex} is negative or larger than the
      *             length of this {@code String} object.
      */
     public String substring(int beginIndex) {
-        return substring(beginIndex, count);
+        if (beginIndex < 0) {
+            throw new StringIndexOutOfBoundsException(beginIndex);
+        }
+        int subLen = value.length - beginIndex;
+        if (subLen < 0) {
+            throw new StringIndexOutOfBoundsException(subLen);
+        }
+        return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
     }
 
     /**
      * Returns a new string that is a substring of this string. The
      * substring begins at the specified {@code beginIndex} and

@@ -1952,18 +1913,19 @@
      */
     public String substring(int beginIndex, int endIndex) {
         if (beginIndex < 0) {
             throw new StringIndexOutOfBoundsException(beginIndex);
         }
-        if (endIndex > count) {
+        if (endIndex > value.length) {
             throw new StringIndexOutOfBoundsException(endIndex);
         }
-        if (beginIndex > endIndex) {
-            throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
+        int subLen = endIndex - beginIndex;
+        if (subLen < 0) {
+            throw new StringIndexOutOfBoundsException(subLen);
         }
-        return ((beginIndex == 0) && (endIndex == count)) ? this :
-            new String(offset + beginIndex, endIndex - beginIndex, value);
+        return ((beginIndex == 0) && (endIndex == value.length)) ? this
+                : new String(value, beginIndex, subLen);
     }
 
     /**
      * Returns a new character sequence that is a subsequence of this sequence.
      *

@@ -2019,14 +1981,15 @@
     public String concat(String str) {
         int otherLen = str.length();
         if (otherLen == 0) {
             return this;
         }
-        char buf[] = new char[count + otherLen];
-        getChars(0, count, buf, 0);
-        str.getChars(0, otherLen, buf, count);
-        return new String(0, count + otherLen, buf);
+        int len = value.length;
+        char buf[] = new char[len + otherLen];
+        getChars(buf, 0);
+        str.getChars(buf, len);
+        return new String(buf, true);
     }
 
     /**
      * Returns a new string resulting from replacing all occurrences of
      * {@code oldChar} in this string with {@code newChar}.

@@ -2056,31 +2019,30 @@
      * @return  a string derived from this string by replacing every
      *          occurrence of {@code oldChar} with {@code newChar}.
      */
     public String replace(char oldChar, char newChar) {
         if (oldChar != newChar) {
-            int len = count;
+            int len = value.length;
             int i = -1;
             char[] val = value; /* avoid getfield opcode */
-            int off = offset;   /* avoid getfield opcode */
 
             while (++i < len) {
-                if (val[off + i] == oldChar) {
+                if (val[i] == oldChar) {
                     break;
                 }
             }
             if (i < len) {
                 char buf[] = new char[len];
-                for (int j = 0 ; j < i ; j++) {
-                    buf[j] = val[off+j];
+                for (int j = 0; j < i; j++) {
+                    buf[j] = val[j];
                 }
                 while (i < len) {
-                    char c = val[off + i];
+                    char c = val[i];
                     buf[i] = (c == oldChar) ? newChar : c;
                     i++;
                 }
-                return new String(0, len, buf);
+                return new String(buf, true);
             }
         }
         return this;
     }
 

@@ -2318,11 +2280,11 @@
               RegEx's meta characters ".$|()[{^?*+\\", or
            (2)two-char String and the first char is the backslash and
               the second is not the ascii digit or ascii letter.
         */
         char ch = 0;
-        if (((regex.count == 1 &&
+        if (((regex.value.length == 1 &&
              ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
              (regex.length() == 2 &&
               regex.charAt(0) == '\\' &&
               (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
               ((ch-'a')|('z'-ch)) < 0 &&

@@ -2338,27 +2300,27 @@
                 if (!limited || list.size() < limit - 1) {
                     list.add(substring(off, next));
                     off = next + 1;
                 } else {    // last one
                     //assert (list.size() == limit - 1);
-                    list.add(substring(off, count));
-                    off = count;
+                    list.add(substring(off, value.length));
+                    off = value.length;
                     break;
                 }
             }
             // If no match was found, return this
             if (off == 0)
-                return new String[] { this };
+                return new String[]{this};
 
             // Add remaining segment
             if (!limited || list.size() < limit)
-                list.add(substring(off, count));
+                list.add(substring(off, value.length));
 
             // Construct result
             int resultSize = list.size();
             if (limit == 0)
-                while (resultSize > 0 && list.get(resultSize-1).length() == 0)
+                while (resultSize > 0 && list.get(resultSize - 1).length() == 0)
                     resultSize--;
             String[] result = new String[resultSize];
             return list.subList(0, resultSize).toArray(result);
         }
         return Pattern.compile(regex).split(this, limit);

@@ -2462,17 +2424,18 @@
         if (locale == null) {
             throw new NullPointerException();
         }
 
         int     firstUpper;
+        final int len = value.length;
 
         /* Now check if there are any characters that need to be changed. */
         scan: {
-            for (firstUpper = 0 ; firstUpper < count; ) {
-                char c = value[offset+firstUpper];
-                if ((c >= Character.MIN_HIGH_SURROGATE) &&
-                    (c <= Character.MAX_HIGH_SURROGATE)) {
+            for (firstUpper = 0 ; firstUpper < len; ) {
+                char c = value[firstUpper];
+                if ((c >= Character.MIN_HIGH_SURROGATE)
+                        && (c <= Character.MAX_HIGH_SURROGATE)) {
                     int supplChar = codePointAt(firstUpper);
                     if (supplChar != Character.toLowerCase(supplChar)) {
                         break scan;
                     }
                     firstUpper += Character.charCount(supplChar);

@@ -2484,28 +2447,28 @@
                 }
             }
             return this;
         }
 
-        char[]  result = new char[count];
+        char[] result = new char[len];
         int     resultOffset = 0;  /* result may grow, so i+resultOffset
                                     * is the write location in result */
 
         /* Just copy the first few lowerCase characters. */
-        System.arraycopy(value, offset, result, 0, firstUpper);
+        System.arraycopy(value, 0, result, 0, firstUpper);
 
         String lang = locale.getLanguage();
         boolean localeDependent =
             (lang == "tr" || lang == "az" || lang == "lt");
         char[] lowerCharArray;
         int lowerChar;
         int srcChar;
         int srcCount;
-        for (int i = firstUpper; i < count; i += srcCount) {
-            srcChar = (int)value[offset+i];
-            if ((char)srcChar >= Character.MIN_HIGH_SURROGATE &&
-                (char)srcChar <= Character.MAX_HIGH_SURROGATE) {
+        for (int i = firstUpper; i < len; i += srcCount) {
+            srcChar = (int) value[i];
+            if ((char) srcChar >= Character.MIN_HIGH_SURROGATE
+                    && (char) srcChar <= Character.MAX_HIGH_SURROGATE) {
                 srcChar = codePointAt(i);
                 srcCount = Character.charCount(srcChar);
             } else {
                 srcCount = 1;
             }

@@ -2514,12 +2477,12 @@
             } else if (srcChar == '\u0130') { // LATIN CAPITAL LETTER I DOT
                 lowerChar = Character.ERROR;
             } else {
                 lowerChar = Character.toLowerCase(srcChar);
             }
-            if ((lowerChar == Character.ERROR) ||
-                (lowerChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
+            if ((lowerChar == Character.ERROR)
+                    || (lowerChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
                 if (lowerChar == Character.ERROR) {
                      if (!localeDependent && srcChar == '\u0130') {
                          lowerCharArray =
                              ConditionalSpecialCasing.toLowerCaseCharArray(this, i, Locale.ENGLISH);
                      } else {

@@ -2539,19 +2502,19 @@
                     char[] result2 = new char[result.length + mapLen - srcCount];
                     System.arraycopy(result, 0, result2, 0,
                         i + resultOffset);
                     result = result2;
                 }
-                for (int x=0; x<mapLen; ++x) {
-                    result[i+resultOffset+x] = lowerCharArray[x];
+                for (int x = 0; x < mapLen; ++x) {
+                    result[i + resultOffset + x] = lowerCharArray[x];
                 }
                 resultOffset += (mapLen - srcCount);
             } else {
-                result[i+resultOffset] = (char)lowerChar;
+                result[i + resultOffset] = (char) lowerChar;
             }
         }
-        return new String(0, count+resultOffset, result);
+        return new String(result, 0, len + resultOffset);
     }
 
     /**
      * Converts all of the characters in this {@code String} to lower
      * case using the rules of the default locale. This is equivalent to calling

@@ -2627,49 +2590,50 @@
         if (locale == null) {
             throw new NullPointerException();
         }
 
         int     firstLower;
+        final int len = value.length;
 
         /* Now check if there are any characters that need to be changed. */
         scan: {
-            for (firstLower = 0 ; firstLower < count; ) {
-                int c = (int)value[offset+firstLower];
+            for (firstLower = 0 ; firstLower < len; ) {
+                int c = (int)value[firstLower];
                 int srcCount;
-                if ((c >= Character.MIN_HIGH_SURROGATE) &&
-                    (c <= Character.MAX_HIGH_SURROGATE)) {
+                if ((c >= Character.MIN_HIGH_SURROGATE)
+                        && (c <= Character.MAX_HIGH_SURROGATE)) {
                     c = codePointAt(firstLower);
                     srcCount = Character.charCount(c);
                 } else {
                     srcCount = 1;
                 }
                 int upperCaseChar = Character.toUpperCaseEx(c);
-                if ((upperCaseChar == Character.ERROR) ||
-                    (c != upperCaseChar)) {
+                if ((upperCaseChar == Character.ERROR)
+                        || (c != upperCaseChar)) {
                     break scan;
                 }
                 firstLower += srcCount;
             }
             return this;
         }
 
-        char[]  result       = new char[count]; /* may grow */
+        char[] result = new char[len]; /* may grow */
         int     resultOffset = 0;  /* result may grow, so i+resultOffset
                                     * is the write location in result */
 
         /* Just copy the first few upperCase characters. */
-        System.arraycopy(value, offset, result, 0, firstLower);
+        System.arraycopy(value, 0, result, 0, firstLower);
 
         String lang = locale.getLanguage();
         boolean localeDependent =
             (lang == "tr" || lang == "az" || lang == "lt");
         char[] upperCharArray;
         int upperChar;
         int srcChar;
         int srcCount;
-        for (int i = firstLower; i < count; i += srcCount) {
-            srcChar = (int)value[offset+i];
+        for (int i = firstLower; i < len; i += srcCount) {
+            srcChar = (int) value[i];
             if ((char)srcChar >= Character.MIN_HIGH_SURROGATE &&
                 (char)srcChar <= Character.MAX_HIGH_SURROGATE) {
                 srcChar = codePointAt(i);
                 srcCount = Character.charCount(srcChar);
             } else {

@@ -2678,12 +2642,12 @@
             if (localeDependent) {
                 upperChar = ConditionalSpecialCasing.toUpperCaseEx(this, i, locale);
             } else {
                 upperChar = Character.toUpperCaseEx(srcChar);
             }
-            if ((upperChar == Character.ERROR) ||
-                (upperChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
+            if ((upperChar == Character.ERROR)
+                    || (upperChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
                 if (upperChar == Character.ERROR) {
                     if (localeDependent) {
                         upperCharArray =
                             ConditionalSpecialCasing.toUpperCaseCharArray(this, i, locale);
                     } else {

@@ -2702,19 +2666,19 @@
                     char[] result2 = new char[result.length + mapLen - srcCount];
                     System.arraycopy(result, 0, result2, 0,
                         i + resultOffset);
                     result = result2;
                 }
-                for (int x=0; x<mapLen; ++x) {
-                    result[i+resultOffset+x] = upperCharArray[x];
+                for (int x = 0; x < mapLen; ++x) {
+                    result[i + resultOffset + x] = upperCharArray[x];
                 }
                 resultOffset += (mapLen - srcCount);
             } else {
-                result[i+resultOffset] = (char)upperChar;
+                result[i + resultOffset] = (char) upperChar;
             }
         }
-        return new String(0, count+resultOffset, result);
+        return new String(result, 0, len + resultOffset);
     }
 
     /**
      * Converts all of the characters in this {@code String} to upper
      * case using the rules of the default locale. This method is equivalent to

@@ -2768,22 +2732,21 @@
      * @return  A copy of this string with leading and trailing white
      *          space removed, or this string if it has no leading or
      *          trailing white space.
      */
     public String trim() {
-        int len = count;
+        int len = value.length;
         int st = 0;
-        int off = offset;      /* avoid getfield opcode */
         char[] val = value;    /* avoid getfield opcode */
 
-        while ((st < len) && (val[off + st] <= ' ')) {
+        while ((st < len) && (val[st] <= ' ')) {
             st++;
         }
-        while ((st < len) && (val[off + len - 1] <= ' ')) {
+        while ((st < len) && (val[len - 1] <= ' ')) {
             len--;
         }
-        return ((st > 0) || (len < count)) ? substring(st, len) : this;
+        return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
     }
 
     /**
      * This object (which is already a string!) is itself returned.
      *

@@ -2799,12 +2762,12 @@
      * @return  a newly allocated character array whose length is the length
      *          of this string and whose contents are initialized to contain
      *          the character sequence represented by this string.
      */
     public char[] toCharArray() {
-        char result[] = new char[count];
-        getChars(0, count, result, 0);
+        char result[] = new char[value.length];
+        getChars(result, 0);
         return result;
     }
 
     /**
      * Returns a formatted string using the specified format string and

@@ -2842,11 +2805,11 @@
      * @return  A formatted string
      *
      * @see  java.util.Formatter
      * @since  1.5
      */
-    public static String format(String format, Object ... args) {
+    public static String format(String format, Object... args) {
         return new Formatter().format(format, args).toString();
     }
 
     /**
      * Returns a formatted string using the specified locale, format string,

@@ -2886,11 +2849,11 @@
      * @return  A formatted string
      *
      * @see  java.util.Formatter
      * @since  1.5
      */
-    public static String format(Locale l, String format, Object ... args) {
+    public static String format(Locale l, String format, Object... args) {
         return new Formatter(l).format(format, args).toString();
     }
 
     /**
      * Returns the string representation of the {@code Object} argument.

@@ -2991,11 +2954,11 @@
      * @return  a string of length {@code 1} containing
      *          as its single character the argument {@code c}.
      */
     public static String valueOf(char c) {
         char data[] = {c};
-        return new String(0, 1, data);
+        return new String(data, true);
     }
 
     /**
      * Returns the string representation of the {@code int} argument.
      * <p>

@@ -3074,7 +3037,6 @@
      *
      * @return  a string that has the same contents as this string, but is
      *          guaranteed to be from a pool of unique strings.
      */
     public native String intern();
-
 }