home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 October / Chip_1997-10_cd.bin / tema / sybase / powerj / java.z / String.java < prev    next >
Text File  |  1996-05-03  |  28KB  |  922 lines

  1. /*
  2.  * @(#)String.java    1.60 96/03/28  
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.lang;
  21.  
  22. import java.util.Hashtable;
  23.  
  24. /**
  25.  * A general class of objects to represent character Strings.
  26.  * Strings are constant, their values cannot be changed after creation.
  27.  * The compiler makes sure that each String constant actually results
  28.  * in a String object. Because String objects are immutable they can
  29.  * be shared. For example:
  30.  * <pre>
  31.  *    String str = "abc";
  32.  * </pre>
  33.  * is equivalent to:
  34.  * <pre>
  35.  *    char data[] = {'a', 'b', 'c'};
  36.  *    String str = new String(data);
  37.  * </pre>
  38.  * Here are some more examples of how strings can be used:
  39.  * <pre>
  40.  *     System.out.println("abc");
  41.  *     String cde = "cde";
  42.  *     System.out.println("abc" + cde);
  43.  *    String c = "abc".substring(2,3);
  44.  *    String d = cde.substring(1, 2);
  45.  * </pre>
  46.  * @see        StringBuffer
  47.  * @version     1.60, 28 Mar 1996
  48.  * @author     Lee Boynton
  49.  * @author    Arthur van Hoff
  50.  */
  51. public final
  52. class String {
  53.     /** The value is used for character storage. */
  54.     private char value[];
  55.  
  56.     /** The offset is the first index of the storage that is used. */
  57.     private int offset;
  58.  
  59.     /** The count is the number of characters in the String. */
  60.     private int count;
  61.  
  62.     /**
  63.      * Constructs a new empty String.
  64.      */
  65.     public String() {
  66.     value = new char[0];
  67.     }
  68.  
  69.     /**
  70.      * Constructs a new String that is a copy of the specified String.
  71.      * @param value the initial value of the String
  72.      */
  73.     public String(String value) {
  74.     count = value.length();
  75.     this.value = new char[count];
  76.     value.getChars(0, count, this.value, 0);
  77.     }
  78.  
  79.     /**
  80.      * Constructs a new String whose initial value is the specified array 
  81.      * of characters.
  82.      * @param value the initial value of the String
  83.      */
  84.     public String(char value[]) {
  85.     this.count = value.length;
  86.     this.value = new char[count];
  87.     System.arraycopy(value, 0, this.value, 0, count);
  88.     }
  89.  
  90.     /**
  91.      * Constructs a new String whose initial value is the specified sub array of characters.
  92.      * The length of the new string will be count characters
  93.      * starting at offset within the specified character array.
  94.      * @param value    the initial value of the String, an array of characters
  95.      * @param offset    the offset into the value of the String
  96.      * @param count     the length of the value of the String
  97.      * @exception StringIndexOutOfBoundsException If the offset and count arguments are invalid.
  98.      */
  99.     public String(char value[], int offset, int count) {
  100.     if (offset < 0) {
  101.         throw new StringIndexOutOfBoundsException(offset);
  102.     }
  103.     if (count < 0) {
  104.         throw new StringIndexOutOfBoundsException(count);
  105.     }
  106.     if (offset + count > value.length) {
  107.         throw new StringIndexOutOfBoundsException(offset + count);
  108.     }
  109.  
  110.     this.value = new char[count];
  111.     this.count = count;
  112.     System.arraycopy(value, offset, this.value, 0, count);
  113.     }
  114.  
  115.     /**
  116.      * Constructs a new String whose initial value is the specified subarray of bytes.
  117.      * The high-byte of each character can be specified, it should usually be 0.
  118.      * The length of the new String will be count characters
  119.      * starting at offset within the specified character array.  
  120.      * @param ascii    the bytes that will be converted to characters
  121.      * @param hibyte    the high byte of each Unicode character
  122.      * @param offset    the offset into the ascii array
  123.      * @param count     the length of the String
  124.      * @exception StringIndexOutOfBoundsException If the offset and count arguments are invalid.
  125.      */
  126.     public String(byte ascii[], int hibyte, int offset, int count) {
  127.     if (offset < 0) {
  128.         throw new StringIndexOutOfBoundsException(offset);
  129.     }
  130.     if (count < 0) {
  131.         throw new StringIndexOutOfBoundsException(count);
  132.     }
  133.     if (offset + count > ascii.length) {
  134.         throw new StringIndexOutOfBoundsException(offset + count);
  135.     }
  136.  
  137.     char value[] = new char[count];
  138.     this.count = count;
  139.     this.value = value;
  140.  
  141.     if (hibyte == 0) {
  142.         for (int i = count ; i-- > 0 ;) {
  143.         value[i] = (char) (ascii[i + offset] & 0xff);
  144.         }
  145.     } else {
  146.         hibyte <<= 8;
  147.         for (int i = count ; i-- > 0 ;) {
  148.         value[i] = (char) (hibyte | (ascii[i + offset] & 0xff));
  149.         }
  150.     }
  151.     }
  152.  
  153.     /**
  154.      * Constructs a new String whose value is the specified array of bytes.
  155.      * The byte array is transformed into Unicode chars using hibyte
  156.      * as the upper byte of each character.
  157.      * @param ascii    the byte that will be converted to characters
  158.      * @param hibyte    the top 8 bits of each 16 bit Unicode character
  159.      */
  160.     public String(byte ascii[], int hibyte) {
  161.     this(ascii, hibyte, 0, ascii.length);
  162.     }
  163.  
  164.      
  165.     /**
  166.      * Construct a new string whose value is the current contents of the
  167.      * given string buffer
  168.      * @param buffer     the stringbuffer to be converted
  169.      */
  170.     public String (StringBuffer buffer) { 
  171.     synchronized(buffer) { 
  172.         buffer.setShared();
  173.         this.value = buffer.getValue();
  174.         this.offset = 0;
  175.         this.count = buffer.length();
  176.     }
  177.     }
  178.     
  179.  
  180.     // Private constructor which shares value array for speed.
  181.     private String(int offset, int count, char value[]) {
  182.     this.value = value;
  183.     this.offset = offset;
  184.     this.count = count;
  185.     }
  186.  
  187.  
  188.     /**
  189.      * Returns the length of the String.
  190.      * The length of the String is equal to the number of 16 bit
  191.      * Unicode characters in the String.
  192.      */
  193.     public int length() {
  194.     return count;
  195.     }
  196.  
  197.     /**
  198.      * Returns the character at the specified index. An index ranges
  199.      * from <tt>0</tt> to <tt>length() - 1</tt>.
  200.      * @param index    the index of the desired character
  201.      * @exception    StringIndexOutOfBoundsException If the index is not
  202.      *            in the range <tt>0</tt> to <tt>length()-1</tt>.
  203.      */
  204.     public char charAt(int index) {
  205.     if ((index < 0) || (index >= count)) {
  206.         throw new StringIndexOutOfBoundsException(index);
  207.     }
  208.     return value[index + offset];
  209.     }
  210.  
  211.     /**
  212.      * Copies characters from this String into the specified character array.
  213.      * The characters of the specified substring (determined by
  214.      * srcBegin and srcEnd) are copied into the character array,
  215.      * starting at the array's dstBegin location.
  216.      * @param srcBegin    index of the first character in the string
  217.      * @param srcEnd    end of the characters that are copied
  218.      * @param dst        the destination array
  219.      * @param dstBegin    the start offset in the destination array
  220.      */
  221.     public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
  222.     System.arraycopy(value, offset + srcBegin, dst, dstBegin, srcEnd - srcBegin);
  223.     }
  224.  
  225.     /**
  226.      * Copies characters from this String into the specified byte array.
  227.      * Copies the characters of the specified substring (determined by
  228.      * srcBegin and srcEnd) into the byte array, starting at the
  229.      * array's dstBegin location.
  230.      * @param srcBegin    index of the first character in the String
  231.      * @param srcEnd    end of the characters that are copied
  232.      * @param dst        the destination array
  233.      * @param dstBegin    the start offset in the destination array
  234.      */
  235.     public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) {
  236.     int j = dstBegin;
  237.     int n = offset + srcEnd;
  238.     int i = offset + srcBegin;
  239.     while (i < n) {
  240.         dst[j++] = (byte)value[i++];
  241.     }
  242.     }
  243.  
  244.     /**
  245.      * Compares this String to the specified object.
  246.      * Returns true if the object is equal to this String; that is,
  247.      * has the same length and the same characters in the same sequence.
  248.      * @param anObject    the object to compare this String against
  249.      * @return     true if the Strings are equal; false otherwise.
  250.      */
  251.     public boolean equals(Object anObject) {
  252.     if ((anObject != null) && (anObject instanceof String)) {
  253.         String anotherString = (String)anObject;
  254.         int n = count;
  255.         if (n == anotherString.count) {
  256.         char v1[] = value;
  257.         char v2[] = anotherString.value;;
  258.         int i = offset;
  259.         int j = anotherString.offset;
  260.         while (n-- != 0) {
  261.             if (v1[i++] != v2[j++]) {
  262.             return false;
  263.             }
  264.         }
  265.         return true;
  266.         }
  267.     }
  268.     return false;
  269.     }
  270.  
  271.     /**
  272.      * Compares this String to another object.
  273.      * Returns true if the object is equal to this String; that is,
  274.      * has the same length and the same characters in the same sequence.
  275.      * Upper case characters are folded to lower case before
  276.      * they are compared.
  277.      * @param anotherString    the String to compare this String against
  278.      * @return     true if the Strings are equal, ignoring case; false otherwise.
  279.      */
  280.     public boolean equalsIgnoreCase(String anotherString) {
  281.     return (anotherString != null) && (anotherString.count == count) &&
  282.         regionMatches(true, 0, anotherString, 0, count);
  283.     }
  284.  
  285.     /**
  286.      * Compares this String to another specified String.
  287.      * Returns an integer that is less than, equal to, or greater than zero.
  288.      * The integer's value depends on whether this String is less than, equal to, or greater
  289.      * than anotherString.
  290.      * @param anotherString the String to be compared
  291.      */
  292.     public int compareTo(String anotherString) {
  293.     int len1 = count;
  294.     int len2 = anotherString.count;
  295.     int n = Math.min(len1, len2);
  296.     char v1[] = value;
  297.     char v2[] = anotherString.value;
  298.     int i = offset;
  299.     int j = anotherString.offset;
  300.  
  301.     while (n-- != 0) {
  302.         char c1 = v1[i++];
  303.         char c2 = v2[j++];
  304.         if (c1 != c2) {
  305.         return c1 - c2;
  306.         }
  307.     }
  308.     return len1 - len2;
  309.     }
  310.  
  311.     /**
  312.      * Determines whether a region of this String matches the specified region
  313.      * of the specified String.
  314.      * @param toffset    where to start looking in this String
  315.      * @param other     the other String
  316.      * @param ooffset    where to start looking in the other String
  317.      * @param len       the number of characters to compare
  318.      * @return          true if the region matches with the other; false otherwise.
  319.      */
  320.     public boolean regionMatches(int toffset, String other, int ooffset, int len) {
  321.     char ta[] = value;
  322.     int to = offset + toffset;
  323.     int tlim = offset + count;
  324.     char pa[] = other.value;
  325.     int po = other.offset + ooffset;
  326.     int plim = po + other.count;
  327.     if ((ooffset < 0) || (toffset < 0) || (to + len > tlim) || (po + len > plim)) {
  328.         return false;
  329.     }
  330.     while (len-- > 0) {
  331.         if (ta[to++] != pa[po++]) {
  332.             return false;
  333.         }
  334.     }
  335.     return true;
  336.     }
  337.  
  338.     /**
  339.      * Determines whether a region of this String matches the specified region
  340.      * of the specified String.  If the boolean ignoreCase is true, upper case characters are 
  341.      * considered equivalent to lower case letters.
  342.      * @param ignoreCase if true, case is ignored
  343.      * @param toffset    where to start looking in this String
  344.      * @param other     the other String
  345.      * @param ooffset    where to start looking in the other String
  346.      * @param len       the number of characters to compare
  347.      * @return          true if the region matches with the other; false otherwise.
  348.      */
  349.     public boolean regionMatches(boolean ignoreCase,
  350.                          int toffset,
  351.                            String other, int ooffset, int len) {
  352.     char ta[] = value;
  353.     int to = offset + toffset;
  354.     int tlim = offset + count;
  355.     char pa[] = other.value;
  356.     int po = other.offset + ooffset;
  357.     int plim = po + other.count;
  358.     if ((ooffset < 0) || (toffset < 0) || (to + len > tlim) || (po + len > plim)) {
  359.         return false;
  360.     }
  361.     while (len-- > 0) {
  362.         char c1 = ta[to++];
  363.         char c2 = pa[po++];
  364.         if (c1 == c2)
  365.         continue;
  366.         if (ignoreCase) {
  367.         // If characters don't match but case may be ignored,
  368.         // try converting both characters to uppercase.
  369.         // If the results match, then the comparison scan should
  370.         // continue. 
  371.         char u1 = Character.toUpperCase(c1);
  372.         char u2 = Character.toUpperCase(c2);
  373.         if (u1 == u2)
  374.             continue;
  375.         // Unfortunately, conversion to uppercase does not work properly
  376.         // for the Georgian alphabet, which has strange rules about case
  377.         // conversion.  So we need to make one last check before 
  378.         // exiting.
  379.         if (Character.toLowerCase(u1) == Character.toLowerCase(u2))
  380.             continue;
  381.         }
  382.         return false;
  383.     }
  384.     return true;
  385.     }
  386.  
  387.  
  388.     /**
  389.      * Determines whether this String starts with some prefix.
  390.      * @param prefix    the prefix
  391.      * @param toffset    where to begin looking in the the String
  392.      * @return         true if the String starts with the specified prefix; false otherwise.
  393.      */
  394.     public boolean startsWith(String prefix, int toffset) {
  395.     char ta[] = value;
  396.     int to = offset + toffset;
  397.     int tlim = offset + count;
  398.     char pa[] = prefix.value;
  399.     int po = prefix.offset;
  400.     int pc = prefix.count;
  401.     int plim = po + pc;
  402.     if ((toffset < 0) || (to + pc > tlim)) {
  403.         return false;
  404.     }
  405.     while (--pc >= 0) {
  406.         if (ta[to++] != pa[po++]) {
  407.             return false;
  408.         }
  409.     }
  410.     return true;
  411.     }
  412.  
  413.     /**
  414.      * Determines whether this String starts with some prefix.
  415.      * @param prefix    the prefix
  416.      * @return         true if the String starts with the specified prefix; false otherwise. 
  417.      */
  418.     public boolean startsWith(String prefix) {
  419.     return startsWith(prefix, 0);
  420.     }
  421.  
  422.     /**
  423.      * Determines whether the String ends with some suffix.
  424.      * @param suffix    the suffix
  425.      * @return         true if the String ends with the specified suffix; false otherwise.
  426.      */
  427.     public boolean endsWith(String suffix) {
  428.     return startsWith(suffix, count - suffix.count);
  429.     }
  430.  
  431.     /**
  432.      * Returns a hashcode for this String. This is a large
  433.      * number composed of the character values in the String.
  434.      */
  435.     public int hashCode() {
  436.     int h = 0;
  437.     int off = offset;
  438.     char val[] = value;
  439.     int len = count;
  440.  
  441.     if (len < 16) {
  442.         for (int i = len ; i > 0; i--) {
  443.         h = (h * 37) + val[off++];
  444.         }
  445.     } else {
  446.         // only sample some characters
  447.         int skip = len / 8;
  448.         for (int i = len ; i > 0; i -= skip, off += skip) {
  449.         h = (h * 39) + val[off];
  450.         }
  451.     }
  452.     return h;
  453.     }
  454.  
  455.     /**
  456.      * Returns the index within this String of the first occurrence of the specified 
  457.      * character.  This method returns -1 if the index is not found.
  458.      * @param ch    the character to search for
  459.      */
  460.     public int indexOf(int ch) {
  461.     return indexOf(ch, 0);
  462.     }
  463.  
  464.     /**
  465.      * Returns the index within this String of the first occurrence of the specified 
  466.      * character, starting the search at fromIndex.  This method 
  467.      * returns -1 if the index is not found.
  468.      * @param ch    the character to search for
  469.      * @param fromIndex    the index to start the search from
  470.      */
  471.     public int indexOf(int ch, int fromIndex) {
  472.     int max = offset + count;
  473.     char v[] = value;
  474.  
  475.     for (int i = offset + fromIndex ; i < max ; i++) {
  476.         if (v[i] == ch) {
  477.         return i - offset;
  478.         }
  479.     }
  480.     return -1;
  481.     }
  482.  
  483.     /**
  484.      * Returns the index within this String of the last occurrence of the specified character.
  485.      * The String is searched backwards starting at the last character.
  486.      * This method returns -1 if the index is not found.
  487.      * @param ch    the character to search for
  488.      */
  489.     public int lastIndexOf(int ch) {
  490.     return lastIndexOf(ch, count - 1);
  491.     }
  492.  
  493.     /**
  494.      * Returns the index within this String of the last occurrence of the specified character.
  495.      * The String is searched backwards starting at fromIndex.
  496.      * This method returns -1 if the index is not found.
  497.      * @param ch    the character to search for
  498.      * @param fromIndex    the index to start the search from
  499.      */
  500.     public int lastIndexOf(int ch, int fromIndex) {
  501.     int min = offset;
  502.     char v[] = value;
  503.     
  504.     for (int i = offset + ((fromIndex >= count) ? count - 1 : fromIndex) ; i >= min ; i--) {
  505.         if (v[i] == ch) {
  506.         return i - offset;
  507.         }
  508.     }
  509.     return -1;
  510.     }
  511.  
  512.     /**
  513.      * Returns the index within this String of the first occurrence of the specified substring.
  514.      * This method returns -1 if the index is not found.
  515.      * @param str     the substring to search for
  516.      */
  517.     public int indexOf(String str) {
  518.     return indexOf(str, 0);
  519.     }
  520.  
  521.     /**
  522.      * Returns the index within this String of the first occurrence of the specified substring.
  523.      * The search is started at fromIndex.
  524.      * This method returns -1 if the index is not found.
  525.      * @param str     the substring to search for
  526.      * @param fromIndex    the index to start the search from
  527.      */
  528.     public int indexOf(String str, int fromIndex) {
  529.     char v1[] = value;
  530.     char v2[] = str.value;
  531.     int max = offset + (count - str.count);
  532.       test:
  533.     for (int i = offset + ((fromIndex < 0) ? 0 : fromIndex); i <= max ; i++) {
  534.         int n = str.count;
  535.         int j = i;
  536.         int k = str.offset;
  537.         while (n-- != 0) {
  538.         if (v1[j++] != v2[k++]) {
  539.             continue test;
  540.         }
  541.         }
  542.         return i - offset;
  543.     }
  544.     return -1;
  545.     }
  546.  
  547.     /**
  548.      * Returns the index within this String of the rightmost occurrence
  549.      * of the specified substring.  The rightmost empty string "" is
  550.      * considered to occur at the index value this.length().
  551.      * This method returns -1 if the index is not found.
  552.      * @param str     the substring to search for
  553.      */
  554.     public int lastIndexOf(String str) {
  555.     return lastIndexOf(str, count);
  556.     }
  557.  
  558.     /**
  559.      * Returns the index within this String of the last occurrence of
  560.      * the specified substring.
  561.      * The returned index indicates the start of the substring, and it
  562.      * must be equal to or less than fromIndex.
  563.      * This method returns -1 if the index is not found.
  564.      * @param str     the substring to search for
  565.      * @param fromIndex    the index to start the search from
  566.      */
  567.     public int lastIndexOf(String str, int fromIndex) {
  568.  
  569.     /* Check arguments; return immediately where possible.
  570.      * Deliberately not checking for null str, to be consistent with
  571.      * with other String methods.
  572.      */
  573.     if (fromIndex < 0) {
  574.         return -1;
  575.     } else if (fromIndex > count - str.count) {
  576.         fromIndex = count - str.count;
  577.     }
  578.  
  579.     /* Empty string always matches. */
  580.     if (str.count == 0) {
  581.         return fromIndex;
  582.     }
  583.  
  584.     /* Find the rightmost substring match. */
  585.     char v1[] = value;
  586.     char v2[] = str.value;
  587.  
  588.     for (int i = offset + fromIndex; i >= offset; --i) {
  589.         int n = str.count;
  590.         int thisIndex = i;
  591.         int strIndex = str.offset;
  592.         while (v1[thisIndex++] == v2[strIndex++]) {
  593.         if (--n <= 0) {
  594.             return i - offset;
  595.         }
  596.         }
  597.     }
  598.     return -1;
  599.     }
  600.  
  601.     /**
  602.      * Returns the substring of this String. The substring is specified
  603.      * by a beginIndex (inclusive) and the end of the string.
  604.      * @param beginIndex the beginning index, inclusive
  605.      */
  606.     public String substring(int beginIndex) {
  607.     return substring(beginIndex, length());
  608.     }
  609.  
  610.     /**
  611.      * Returns the substring of a String. The substring is specified
  612.      * by a beginIndex (inclusive) and an endIndex (exclusive).
  613.      * @param beginIndex the beginning index, inclusive
  614.      * @param endIndex the ending index, exclusive
  615.      * @exception StringIndexOutOfBoundsException If the beginIndex or the endIndex is out 
  616.      * of range.
  617.      */
  618.     public String substring(int beginIndex, int endIndex) {
  619.     if (beginIndex > endIndex) {
  620.         int tmp = beginIndex;
  621.         beginIndex = endIndex;
  622.         endIndex = tmp;
  623.     }
  624.     if (beginIndex < 0) {
  625.         throw new StringIndexOutOfBoundsException(beginIndex);
  626.     } 
  627.     if (endIndex > count) {
  628.         throw new StringIndexOutOfBoundsException(endIndex);
  629.     }
  630.     return ((beginIndex == 0) && (endIndex == count)) ? this :
  631.         new String(offset + beginIndex, endIndex - beginIndex, value);
  632.     }
  633.  
  634.     /**
  635.      * Concatenates the specified string to the end of this String and
  636.      * returns a new String object representing the concatenation.
  637.      * @param str    the String which is concatenated to the end of this String
  638.      * @return the concatenated String
  639.      */
  640.     public String concat(String str) {
  641.     int otherLen = str.length();
  642.     if (otherLen == 0) {
  643.         return this;
  644.     }
  645.     char buf[] = new char[count + otherLen];
  646.     getChars(0, count, buf, 0);
  647.     str.getChars(0, otherLen, buf, count);
  648.     return new String(0, count + otherLen, buf);
  649.     }
  650.  
  651.     /**
  652.      * Converts this String by replacing all occurences of oldChar with newChar.
  653.      * @param oldChar    the old character
  654.      * @param newChar    the new character
  655.      */
  656.     public String replace(char oldChar, char newChar) {
  657.     if (oldChar != newChar) {
  658.         int len = count;
  659.         int i = -1;
  660.         while (++i < len) {
  661.         if (value[offset + i] == oldChar) {
  662.             break;
  663.         }
  664.         }
  665.         if (i < len) {
  666.         char buf[] = new char[len];
  667.         for (int j = 0 ; j < i ; j++) {
  668.             buf[j] = value[offset+j];
  669.         }
  670.         while (i < len) {
  671.             char c = value[offset + i];
  672.             buf[i] = (c == oldChar) ? newChar : c;
  673.             i++;
  674.         }
  675.         return new String(0, len, buf);
  676.         }
  677.     }
  678.     return this;
  679.     }
  680.  
  681.     /**
  682.      * Converts all of the characters in this String to lower case.
  683.      * @return the String, converted to lowercase.
  684.      * @see Character#toLowerCase
  685.      * @see String#toUpperCase
  686.      */
  687.     public String toLowerCase() {
  688.     int len = count;
  689.     int i, j;
  690.     char z;
  691.     scan: {
  692.         for (i = 0 ; i < len ; i++) {
  693.         char c = value[offset+i];
  694.         z = Character.toLowerCase(c);
  695.         if (c != z) break scan;
  696.         }
  697.         return this;
  698.     }
  699.     char buf[] = new char[len];
  700.     for (j = 0; j < i; ++j) {
  701.         buf[j] = value[offset+j];
  702.     }
  703.     buf[j++] = z;
  704.     for (; j < len ; ++j) {
  705.         buf[j] = Character.toLowerCase(value[offset+j]);
  706.     }
  707.     return new String(0, len, buf);
  708.     }
  709.  
  710.     /**
  711.      * Converts all of the characters in this String to upper case.
  712.      * @return the String, converted to uppercase.
  713.      * @see Character#toUpperCase
  714.      * @see String#toLowerCase
  715.      */
  716.     public String toUpperCase() {
  717.     int len = count;
  718.     int i, j;
  719.     char z;
  720.     scan: {
  721.         for (i = 0 ; i < len ; i++) {
  722.         char c = value[offset+i];
  723.         z = Character.toUpperCase(c);
  724.         if (c != z) break scan;
  725.         }
  726.         return this;
  727.     }
  728.     char buf[] = new char[len];
  729.     for (j = 0; j < i; ++j) {
  730.         buf[j] = value[offset+j];
  731.     }
  732.     buf[j++] = z;
  733.     for (; j < len ; ++j) {
  734.         buf[j] = Character.toUpperCase(value[offset+j]);
  735.     }
  736.     return new String(0, len, buf);
  737.     }
  738.  
  739.  
  740.  
  741.     /**
  742.      * Trims leading and trailing whitespace from this String.
  743.      * @return the String, with whitespace removed.
  744.      */
  745.     public String trim() {
  746.     int len = count;
  747.     int st = 0;
  748.     while ((st < len) && (value[offset + st] <= ' ')) {
  749.         st++;
  750.     }
  751.     while ((st < len) && (value[offset + len - 1] <= ' ')) {
  752.         len--;
  753.     }
  754.     return ((st > 0) || (len < count)) ? substring(st, len) : this;
  755.     }
  756.  
  757.     /**
  758.      * Converts the object (in this case already a String) to a
  759.      * String. Otherwise this class would inherit class Object's
  760.      * version of tostring().  
  761.      * @return the String itself.
  762.      */
  763.     public String toString() {
  764.     return this;
  765.     }
  766.  
  767.     /**
  768.      * Converts this String to a character array. This creates a new array.
  769.      * @return     an array of characters.
  770.      */
  771.     public char[] toCharArray() {
  772.     int i, max = length();
  773.     char result[] = new char[max];
  774.     getChars(0, max, result, 0);
  775.     return result;
  776.     }
  777.  
  778.     /**
  779.      * Returns a String that represents the String value of the object.
  780.      * The object may choose how to represent itself by implementing
  781.      * the toString() method.
  782.      * @param obj    the object to be converted
  783.      */
  784.     public static String valueOf(Object obj) {
  785.     return (obj == null) ? "null" : obj.toString();
  786.     }
  787.  
  788.     /**
  789.      * Returns a String that is equivalent to the specified character array.
  790.      * Uses the original array as the body of the String (ie. it does not
  791.      * copy it to a new array).
  792.      * @param data    the character array
  793.      */
  794.     public static String valueOf(char data[]) {
  795.     return new String(data);
  796.     }
  797.  
  798.     /**
  799.      * Returns a String that is equivalent to the specified character array.
  800.      * @param data    the character array
  801.      * @param offset    the offset into the value of the String
  802.      * @param count     the length of the value of the String
  803.      */
  804.     public static String valueOf(char data[], int offset, int count) {
  805.     return new String(data, offset, count);
  806.     }
  807.  
  808.     
  809.     /**
  810.      * Returns a String that is equivalent to the specified character array.
  811.      * It creates a new array and copies the characters into it.
  812.      * @param data    the character array
  813.      * @param offset    the offset into the value of the String
  814.      * @param count     the length of the value of the String
  815.      */
  816.     public static String copyValueOf(char data[], int offset, int count) {
  817.     // All public String constructors now copy the data.
  818.     return new String(data, offset, count);
  819.     }
  820.  
  821.     /**
  822.      * Returns a String that is equivalent to the specified character array.
  823.      * It creates a new array and copies the characters into it.
  824.      * @param data    the character array
  825.      */
  826.     public static String copyValueOf(char data[]) {
  827.     return copyValueOf(data, 0, data.length);
  828.     }
  829.  
  830.     /**
  831.      * Returns a String object that represents the state of the specified boolean.
  832.      * @param b    the boolean
  833.      */
  834.     public static String valueOf(boolean b) {
  835.     return b ? "true" : "false";
  836.     }
  837.  
  838.     /**
  839.      * Returns a String object that contains a single character
  840.      * @param c the character
  841.      * @return     the resulting String.
  842.      */
  843.     public static String valueOf(char c) {
  844.     char data[] = {c};
  845.     return new String(0, 1, data);
  846.     }
  847.  
  848.     /**
  849.      * Returns a String object that represents the value of the specified integer.
  850.      * @param i    the integer
  851.      */
  852.     public static String valueOf(int i) {
  853.         return Integer.toString(i, 10);
  854.     }
  855.  
  856.     /**
  857.      * Returns a String object that represents the value of the specified long.
  858.      * @param l    the long
  859.      */
  860.     public static String valueOf(long l) {
  861.         return Long.toString(l, 10);
  862.     }
  863.  
  864.     /**
  865.      * Returns a String object that represents the value of the specified float.
  866.      * @param f    the float
  867.      */
  868.     public static String valueOf(float f) {
  869.     return Float.toString(f);
  870.     }
  871.  
  872.     /**
  873.      * Returns a String object that represents the value of the specified double.
  874.      * @param d    the double
  875.      */
  876.     public static String valueOf(double d) {
  877.     return Double.toString(d);
  878.     }
  879.  
  880.  
  881.     /**
  882.      * The set of internalized Strings.
  883.      */
  884.     private static Hashtable InternSet;
  885.  
  886.     /**
  887.      * Returns a String that is equal to this String
  888.      * but which is guaranteed to be from the unique String pool.  For example:
  889.      * <pre>s1.intern() == s2.intern() <=> s1.equals(s2).</pre>
  890.      */
  891.     public String intern() {
  892.     if (InternSet == null) {
  893.         InternSet = new Hashtable();
  894.     }
  895.     String s = (String) InternSet.get(this);
  896.     if (s != null) {
  897.         return s;
  898.     }
  899.     InternSet.put(this, this);
  900.     return this;
  901.     }
  902.  
  903.     /**
  904.      * Computes the length of this string's UTF encoded form.
  905.      */
  906.     int utfLength() {
  907.     int limit = offset + count;
  908.     int utflen = 0;
  909.     for (int i = offset; i < limit; i++) {
  910.         int c = value[i];
  911.         if ((c >= 0x0001) && (c <= 0x007F)) {
  912.         utflen++;
  913.         } else if (c > 0x07FF) {
  914.         utflen += 3;
  915.         } else {
  916.         utflen += 2;
  917.         }
  918.     }
  919.     return utflen;
  920.     }
  921. }
  922.