more IO stuff
[IRC.git] / Robust / src / ClassLibrary / String.java
index 9bb42aa66bede4c51054833774d280cf8a098c02..4048cc024f7a5242423a6de18ba04c0934dafc7c 100644 (file)
@@ -2,6 +2,7 @@ public class String {
     char value[];
     int count;
     int offset;
+    private int cachedHashcode;
 
     private String() {
     }
@@ -14,7 +15,7 @@ public class String {
        this.count=str.length;
        this.offset=0;
     }
-
+    
     public String(byte str[]) {
        char charstr[]=new char[str.length];
        for(int i=0;i<str.length;i++)
@@ -38,7 +39,20 @@ public class String {
            value[i]=strbuf.value[i];
     }
 
+    public boolean endsWith(String suffix) {
+       return regionMatches(count - suffix.count, suffix, 0, suffix.count);
+    }
+
+
+    public String substring(int beginIndex) {
+       return substring(beginIndex, this.count);
+    }
+
     public String subString(int beginIndex, int endIndex) {
+       return substring(beginIndex, endIndex);
+    }
+
+    public String substring(int beginIndex, int endIndex) {
        String str=new String();
        if (beginIndex>this.count||endIndex>this.count||beginIndex>endIndex) {
            // FIXME
@@ -53,6 +67,62 @@ public class String {
        return this.subString(beginIndex, this.count);
     }
 
+    public int lastindexOf(int ch) {
+       return this.lastindexOf(ch, count - 1);
+    }
+
+    public static String concat2(String s1, String s2) {
+       if (s1==null)
+           return "null".concat(s2);
+       else
+           return s1.concat(s2);
+    }
+
+    public String concat(String str) {
+       String newstr=new String();
+       newstr.count=this.count+str.count;
+       char charstr[]=new char[newstr.count];
+       newstr.value=charstr;
+       newstr.offset=0;
+       for(int i=0;i<count;i++) {
+           charstr[i]=value[i+offset];
+       }
+       for(int i=0;i<str.count;i++) {
+           charstr[i+count]=str.value[i+str.offset];
+       }
+       return newstr;
+    }
+
+    public int lastindexOf(int ch, int fromIndex) {
+       for(int i=fromIndex;i>0;i--)
+           if (this.charAt(i)==ch)
+               return i;
+       return -1;
+    }
+
+    public String replace(char oldch, char newch) {
+       char[] buffer=new char[count];
+       for(int i=0;i<count;i++) {
+           char x=charAt(i);
+           if (x==oldch)
+               x=newch;
+           buffer[i]=x;
+       }
+       return new String(buffer);
+    }
+
+    public String toUpperCase() {
+       char[] buffer=new char[count];
+       for(int i=0;i<count;i++) {
+           char x=charAt(i);
+           if (x>='a'&&x<='z') {
+               x=(char) ((x-'a')+'A');
+           }
+           buffer[i]=x;
+       }
+       return new String(buffer);
+    }
+
     public int indexOf(int ch) {
        return this.indexOf(ch, 0);
     }
@@ -77,6 +147,29 @@ public class String {
        return -1;
     }
 
+    public int lastIndexOf(String str, int fromIndex) {
+       int k=count-str.count;
+       if (k>fromIndex)
+           k=fromIndex;
+       for(;k>=0;k--) {
+           if (regionMatches(fromIndex, str, 0, str.count))
+               return k;
+       }
+       return -1;
+    }
+
+    public int lastIndexOf(String str) {
+       return lastIndexOf(str, count-str.count);
+    }
+    
+    public boolean startsWith(String str) {
+       return regionMatches(0, str, 0, str.count);
+    }
+
+    public boolean startsWith(String str, int toffset) {
+       return regionMatches(toffset, str, 0, str.count);
+    }
+
     public boolean regionMatches(int toffset, String other, int ooffset, int len) {
        if (toffset<0 || ooffset <0 || (toffset+len)>count || (ooffset+len)>other.count)
            return false;
@@ -96,7 +189,7 @@ public class String {
 
     public byte[] getBytes() {
        byte str[]=new byte[count];
-       for(int i=0;i<value.length;i++)
+       for(int i=0;i<count;i++)
            str[i]=(byte)value[i+offset];
        return str;
     }
@@ -114,7 +207,16 @@ public class String {
     }
 
     public static String valueOf(Object o) {
-       return o.toString();
+       if (o==null)
+           return "null";
+       else
+           return o.toString();
+    }
+
+    public static String valueOf(char c) {
+       char ar[]=new char[1];
+       ar[0]=c;
+       return new String(ar);
     }
 
     public static String valueOf(int x) {
@@ -148,4 +250,43 @@ public class String {
        } while (length!=0);
        return new String(chararray);
     }
+
+    public int hashCode() {
+       if (cachedHashcode!=0)
+           return cachedHashcode;
+       int hashcode=0;
+       for(int i=0;i<count;i++)
+           hashcode=hashcode*31+value[i+offset];
+       cachedHashcode=hashcode;
+       return hashcode;
+    }
+
+    public boolean equals(Object o) {
+       if (o.getType()!=getType())
+           return false;
+       String s=(String)o;
+       if (s.count!=count)
+           return false;
+       for(int i=0;i<count;i++) {
+           if (s.value[i+s.offset]!=value[i+offset])
+               return false;
+       }
+       return true;
+    }
+
+    public boolean equalsIgnoreCase(String s) {
+       if (s.count!=count)
+           return false;
+       for(int i=0;i<count;i++) {
+           char l=s.value[i+s.offset];
+           char r=value[i+offset];
+           if (l>='a'&&l<='z')
+               l=(char)((l-'a')+'A');
+           if (r>='a'&&r<='z')
+               r=(char)((r-'a')+'A');
+           if (l!=r)
+               return false;
+       }
+       return true;
+    }
 }