home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / lang / String.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  10.5 KB  |  578 lines

  1. package java.lang;
  2.  
  3. import java.util.Hashtable;
  4.  
  5. public final class String {
  6.    private char[] value;
  7.    private int offset;
  8.    private int count;
  9.    private static Hashtable InternSet;
  10.  
  11.    public String() {
  12.       this.value = new char[0];
  13.    }
  14.  
  15.    public String(String value) {
  16.       this.count = value.length();
  17.       this.value = new char[this.count];
  18.       value.getChars(0, this.count, this.value, 0);
  19.    }
  20.  
  21.    public String(char[] value) {
  22.       this.count = value.length;
  23.       this.value = new char[this.count];
  24.       System.arraycopy(value, 0, this.value, 0, this.count);
  25.    }
  26.  
  27.    public String(char[] value, int offset, int count) {
  28.       if (offset < 0) {
  29.          throw new StringIndexOutOfBoundsException(offset);
  30.       } else if (count < 0) {
  31.          throw new StringIndexOutOfBoundsException(count);
  32.       } else if (offset + count > value.length) {
  33.          throw new StringIndexOutOfBoundsException(offset + count);
  34.       } else {
  35.          this.value = new char[count];
  36.          this.count = count;
  37.          System.arraycopy(value, offset, this.value, 0, count);
  38.       }
  39.    }
  40.  
  41.    public String(byte[] ascii, int hibyte, int offset, int count) {
  42.       if (offset < 0) {
  43.          throw new StringIndexOutOfBoundsException(offset);
  44.       } else if (count < 0) {
  45.          throw new StringIndexOutOfBoundsException(count);
  46.       } else if (offset + count > ascii.length) {
  47.          throw new StringIndexOutOfBoundsException(offset + count);
  48.       } else {
  49.          char[] value = new char[count];
  50.          this.count = count;
  51.          this.value = value;
  52.          if (hibyte == 0) {
  53.             for(int i = count; i-- > 0; value[i] = (char)(ascii[i + offset] & 255)) {
  54.             }
  55.  
  56.          } else {
  57.             hibyte <<= 8;
  58.  
  59.             for(int i = count; i-- > 0; value[i] = (char)(hibyte | ascii[i + offset] & 255)) {
  60.             }
  61.  
  62.          }
  63.       }
  64.    }
  65.  
  66.    public String(byte[] ascii, int hibyte) {
  67.       this(ascii, hibyte, 0, ascii.length);
  68.    }
  69.  
  70.    public String(StringBuffer buffer) {
  71.       synchronized(buffer){}
  72.  
  73.       try {
  74.          buffer.setShared();
  75.          this.value = buffer.getValue();
  76.          this.offset = 0;
  77.          this.count = buffer.length();
  78.       } catch (Throwable var4) {
  79.          throw var4;
  80.       }
  81.  
  82.    }
  83.  
  84.    private String(int offset, int count, char[] value) {
  85.       this.value = value;
  86.       this.offset = offset;
  87.       this.count = count;
  88.    }
  89.  
  90.    public int length() {
  91.       return this.count;
  92.    }
  93.  
  94.    public char charAt(int index) {
  95.       if (index >= 0 && index < this.count) {
  96.          return this.value[index + this.offset];
  97.       } else {
  98.          throw new StringIndexOutOfBoundsException(index);
  99.       }
  100.    }
  101.  
  102.    public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) {
  103.       System.arraycopy(this.value, this.offset + srcBegin, dst, dstBegin, srcEnd - srcBegin);
  104.    }
  105.  
  106.    public void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin) {
  107.       int j = dstBegin;
  108.       int n = this.offset + srcEnd;
  109.  
  110.       for(int i = this.offset + srcBegin; i < n; dst[j++] = (byte)this.value[i++]) {
  111.       }
  112.  
  113.    }
  114.  
  115.    public boolean equals(Object anObject) {
  116.       if (anObject != null && anObject instanceof String) {
  117.          String anotherString = (String)anObject;
  118.          int n = this.count;
  119.          if (n == anotherString.count) {
  120.             char[] v1 = this.value;
  121.             char[] v2 = anotherString.value;
  122.             int i = this.offset;
  123.             int j = anotherString.offset;
  124.  
  125.             while(n-- != 0) {
  126.                if (v1[i++] != v2[j++]) {
  127.                   return false;
  128.                }
  129.             }
  130.  
  131.             return true;
  132.          }
  133.       }
  134.  
  135.       return false;
  136.    }
  137.  
  138.    public boolean equalsIgnoreCase(String anotherString) {
  139.       return anotherString != null && anotherString.count == this.count && this.regionMatches(true, 0, anotherString, 0, this.count);
  140.    }
  141.  
  142.    public int compareTo(String anotherString) {
  143.       int len1 = this.count;
  144.       int len2 = anotherString.count;
  145.       int n = Math.min(len1, len2);
  146.       char[] v1 = this.value;
  147.       char[] v2 = anotherString.value;
  148.       int i = this.offset;
  149.       int j = anotherString.offset;
  150.  
  151.       while(n-- != 0) {
  152.          char c1 = v1[i++];
  153.          char c2 = v2[j++];
  154.          if (c1 != c2) {
  155.             return c1 - c2;
  156.          }
  157.       }
  158.  
  159.       return len1 - len2;
  160.    }
  161.  
  162.    public boolean regionMatches(int toffset, String other, int ooffset, int len) {
  163.       char[] ta = this.value;
  164.       int to = this.offset + toffset;
  165.       int tlim = this.offset + this.count;
  166.       char[] pa = other.value;
  167.       int po = other.offset + ooffset;
  168.       int plim = po + other.count;
  169.       if (ooffset >= 0 && toffset >= 0 && to + len <= tlim && po + len <= plim) {
  170.          while(len-- > 0) {
  171.             if (ta[to++] != pa[po++]) {
  172.                return false;
  173.             }
  174.          }
  175.  
  176.          return true;
  177.       } else {
  178.          return false;
  179.       }
  180.    }
  181.  
  182.    public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) {
  183.       char[] ta = this.value;
  184.       int to = this.offset + toffset;
  185.       int tlim = this.offset + this.count;
  186.       char[] pa = other.value;
  187.       int po = other.offset + ooffset;
  188.       int plim = po + other.count;
  189.       if (ooffset >= 0 && toffset >= 0 && to + len <= tlim && po + len <= plim) {
  190.          while(true) {
  191.             if (len-- <= 0) {
  192.                return true;
  193.             }
  194.  
  195.             char c1 = ta[to++];
  196.             char c2 = pa[po++];
  197.             if (c1 != c2) {
  198.                if (!ignoreCase) {
  199.                   break;
  200.                }
  201.  
  202.                char u1 = Character.toUpperCase(c1);
  203.                char u2 = Character.toUpperCase(c2);
  204.                if (u1 != u2 && Character.toLowerCase(u1) != Character.toLowerCase(u2)) {
  205.                   break;
  206.                }
  207.             }
  208.          }
  209.  
  210.          return false;
  211.       } else {
  212.          return false;
  213.       }
  214.    }
  215.  
  216.    public boolean startsWith(String prefix, int toffset) {
  217.       char[] ta = this.value;
  218.       int to = this.offset + toffset;
  219.       int tlim = this.offset + this.count;
  220.       char[] pa = prefix.value;
  221.       int po = prefix.offset;
  222.       int pc = prefix.count;
  223.       if (toffset >= 0 && to + pc <= tlim) {
  224.          do {
  225.             --pc;
  226.             if (pc < 0) {
  227.                return true;
  228.             }
  229.          } while(ta[to++] == pa[po++]);
  230.  
  231.          return false;
  232.       } else {
  233.          return false;
  234.       }
  235.    }
  236.  
  237.    public boolean startsWith(String prefix) {
  238.       return this.startsWith(prefix, 0);
  239.    }
  240.  
  241.    public boolean endsWith(String suffix) {
  242.       return this.startsWith(suffix, this.count - suffix.count);
  243.    }
  244.  
  245.    public int hashCode() {
  246.       int h = 0;
  247.       int off = this.offset;
  248.       char[] val = this.value;
  249.       int len = this.count;
  250.       if (len < 16) {
  251.          for(int i = len; i > 0; --i) {
  252.             h = h * 37 + val[off++];
  253.          }
  254.       } else {
  255.          int skip = len / 8;
  256.  
  257.          for(int i = len; i > 0; off += skip) {
  258.             h = h * 39 + val[off];
  259.             i -= skip;
  260.          }
  261.       }
  262.  
  263.       return h;
  264.    }
  265.  
  266.    public int indexOf(int ch) {
  267.       return this.indexOf(ch, 0);
  268.    }
  269.  
  270.    public int indexOf(int ch, int fromIndex) {
  271.       int max = this.offset + this.count;
  272.       char[] v = this.value;
  273.  
  274.       for(int i = this.offset + fromIndex; i < max; ++i) {
  275.          if (v[i] == ch) {
  276.             return i - this.offset;
  277.          }
  278.       }
  279.  
  280.       return -1;
  281.    }
  282.  
  283.    public int lastIndexOf(int ch) {
  284.       return this.lastIndexOf(ch, this.count - 1);
  285.    }
  286.  
  287.    public int lastIndexOf(int ch, int fromIndex) {
  288.       int min = this.offset;
  289.       char[] v = this.value;
  290.  
  291.       for(int i = this.offset + (fromIndex >= this.count ? this.count - 1 : fromIndex); i >= min; --i) {
  292.          if (v[i] == ch) {
  293.             return i - this.offset;
  294.          }
  295.       }
  296.  
  297.       return -1;
  298.    }
  299.  
  300.    public int indexOf(String str) {
  301.       return this.indexOf(str, 0);
  302.    }
  303.  
  304.    public int indexOf(String str, int fromIndex) {
  305.       char[] v1 = this.value;
  306.       char[] v2 = str.value;
  307.       int max = this.offset + (this.count - str.count);
  308.       int i = this.offset + (fromIndex < 0 ? 0 : fromIndex);
  309.  
  310.       label27:
  311.       while(i <= max) {
  312.          int n = str.count;
  313.          int j = i;
  314.          int k = str.offset;
  315.  
  316.          while(n-- != 0) {
  317.             if (v1[j++] != v2[k++]) {
  318.                ++i;
  319.                continue label27;
  320.             }
  321.          }
  322.  
  323.          return i - this.offset;
  324.       }
  325.  
  326.       return -1;
  327.    }
  328.  
  329.    public int lastIndexOf(String str) {
  330.       return this.lastIndexOf(str, this.count);
  331.    }
  332.  
  333.    public int lastIndexOf(String str, int fromIndex) {
  334.       if (fromIndex < 0) {
  335.          return -1;
  336.       } else {
  337.          if (fromIndex > this.count - str.count) {
  338.             fromIndex = this.count - str.count;
  339.          }
  340.  
  341.          if (str.count == 0) {
  342.             return fromIndex;
  343.          } else {
  344.             char[] v1 = this.value;
  345.             char[] v2 = str.value;
  346.  
  347.             for(int i = this.offset + fromIndex; i >= this.offset; --i) {
  348.                int n = str.count;
  349.                int thisIndex = i;
  350.                int strIndex = str.offset;
  351.  
  352.                while(v1[thisIndex++] == v2[strIndex++]) {
  353.                   --n;
  354.                   if (n <= 0) {
  355.                      return i - this.offset;
  356.                   }
  357.                }
  358.             }
  359.  
  360.             return -1;
  361.          }
  362.       }
  363.    }
  364.  
  365.    public String substring(int beginIndex) {
  366.       return this.substring(beginIndex, this.length());
  367.    }
  368.  
  369.    public String substring(int beginIndex, int endIndex) {
  370.       if (beginIndex > endIndex) {
  371.          int tmp = beginIndex;
  372.          beginIndex = endIndex;
  373.          endIndex = tmp;
  374.       }
  375.  
  376.       if (beginIndex < 0) {
  377.          throw new StringIndexOutOfBoundsException(beginIndex);
  378.       } else if (endIndex > this.count) {
  379.          throw new StringIndexOutOfBoundsException(endIndex);
  380.       } else {
  381.          return beginIndex == 0 && endIndex == this.count ? this : new String(this.offset + beginIndex, endIndex - beginIndex, this.value);
  382.       }
  383.    }
  384.  
  385.    public String concat(String str) {
  386.       int otherLen = str.length();
  387.       if (otherLen == 0) {
  388.          return this;
  389.       } else {
  390.          char[] buf = new char[this.count + otherLen];
  391.          this.getChars(0, this.count, buf, 0);
  392.          str.getChars(0, otherLen, buf, this.count);
  393.          return new String(0, this.count + otherLen, buf);
  394.       }
  395.    }
  396.  
  397.    public String replace(char oldChar, char newChar) {
  398.       if (oldChar != newChar) {
  399.          int len = this.count;
  400.          int i = -1;
  401.  
  402.          do {
  403.             ++i;
  404.          } while(i < len && this.value[this.offset + i] != oldChar);
  405.  
  406.          if (i < len) {
  407.             char[] buf = new char[len];
  408.  
  409.             for(int j = 0; j < i; ++j) {
  410.                buf[j] = this.value[this.offset + j];
  411.             }
  412.  
  413.             while(i < len) {
  414.                char c = this.value[this.offset + i];
  415.                buf[i] = c == oldChar ? newChar : c;
  416.                ++i;
  417.             }
  418.  
  419.             return new String(0, len, buf);
  420.          }
  421.       }
  422.  
  423.       return this;
  424.    }
  425.  
  426.    public String toLowerCase() {
  427.       int len = this.count;
  428.  
  429.       for(int i = 0; i < len; ++i) {
  430.          char c = this.value[this.offset + i];
  431.          char z = Character.toLowerCase(c);
  432.          if (c != z) {
  433.             char[] buf = new char[len];
  434.  
  435.             int j;
  436.             for(j = 0; j < i; ++j) {
  437.                buf[j] = this.value[this.offset + j];
  438.             }
  439.  
  440.             for(buf[j++] = z; j < len; ++j) {
  441.                buf[j] = Character.toLowerCase(this.value[this.offset + j]);
  442.             }
  443.  
  444.             return new String(0, len, buf);
  445.          }
  446.       }
  447.  
  448.       return this;
  449.    }
  450.  
  451.    public String toUpperCase() {
  452.       int len = this.count;
  453.  
  454.       for(int i = 0; i < len; ++i) {
  455.          char c = this.value[this.offset + i];
  456.          char z = Character.toUpperCase(c);
  457.          if (c != z) {
  458.             char[] buf = new char[len];
  459.  
  460.             int j;
  461.             for(j = 0; j < i; ++j) {
  462.                buf[j] = this.value[this.offset + j];
  463.             }
  464.  
  465.             for(buf[j++] = z; j < len; ++j) {
  466.                buf[j] = Character.toUpperCase(this.value[this.offset + j]);
  467.             }
  468.  
  469.             return new String(0, len, buf);
  470.          }
  471.       }
  472.  
  473.       return this;
  474.    }
  475.  
  476.    public String trim() {
  477.       int len = this.count;
  478.  
  479.       int st;
  480.       for(st = 0; st < len && this.value[this.offset + st] <= ' '; ++st) {
  481.       }
  482.  
  483.       while(st < len && this.value[this.offset + len - 1] <= ' ') {
  484.          --len;
  485.       }
  486.  
  487.       return st <= 0 && len >= this.count ? this : this.substring(st, len);
  488.    }
  489.  
  490.    public String toString() {
  491.       return this;
  492.    }
  493.  
  494.    public char[] toCharArray() {
  495.       int max = this.length();
  496.       char[] result = new char[max];
  497.       this.getChars(0, max, result, 0);
  498.       return result;
  499.    }
  500.  
  501.    public static String valueOf(Object obj) {
  502.       return obj == null ? "null" : obj.toString();
  503.    }
  504.  
  505.    public static String valueOf(char[] data) {
  506.       return new String(data);
  507.    }
  508.  
  509.    public static String valueOf(char[] data, int offset, int count) {
  510.       return new String(data, offset, count);
  511.    }
  512.  
  513.    public static String copyValueOf(char[] data, int offset, int count) {
  514.       return new String(data, offset, count);
  515.    }
  516.  
  517.    public static String copyValueOf(char[] data) {
  518.       return copyValueOf(data, 0, data.length);
  519.    }
  520.  
  521.    public static String valueOf(boolean b) {
  522.       return b ? "true" : "false";
  523.    }
  524.  
  525.    public static String valueOf(char c) {
  526.       char[] data = new char[]{c};
  527.       return new String(0, 1, data);
  528.    }
  529.  
  530.    public static String valueOf(int i) {
  531.       return Integer.toString(i, 10);
  532.    }
  533.  
  534.    public static String valueOf(long l) {
  535.       return Long.toString(l, 10);
  536.    }
  537.  
  538.    public static String valueOf(float f) {
  539.       return Float.toString(f);
  540.    }
  541.  
  542.    public static String valueOf(double d) {
  543.       return Double.toString(d);
  544.    }
  545.  
  546.    public String intern() {
  547.       if (InternSet == null) {
  548.          InternSet = new Hashtable();
  549.       }
  550.  
  551.       String s = (String)InternSet.get(this);
  552.       if (s != null) {
  553.          return s;
  554.       } else {
  555.          InternSet.put(this, this);
  556.          return this;
  557.       }
  558.    }
  559.  
  560.    int utfLength() {
  561.       int limit = this.offset + this.count;
  562.       int utflen = 0;
  563.  
  564.       for(int i = this.offset; i < limit; ++i) {
  565.          int c = this.value[i];
  566.          if (c >= 1 && c <= 127) {
  567.             ++utflen;
  568.          } else if (c > 2047) {
  569.             utflen += 3;
  570.          } else {
  571.             utflen += 2;
  572.          }
  573.       }
  574.  
  575.       return utflen;
  576.    }
  577. }
  578.