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