Fix bugs for interfaces and a bug in the Class library
[IRC.git] / Robust / src / ClassLibrary / String.java
index dab2feeb26c6c36ff3cbf1b55bc73b6f0704182e..e68450351d47d33137a27aa0a05444719c308e9d 100644 (file)
@@ -7,6 +7,12 @@ public class String {
   private String() {
   }
 
+  public String(char c) {
+    char[] str = new char[1];
+    str[0] = c;
+    String(str);
+  }
+
   public String(char str[]) {
     char charstr[]=new char[str.length];
     for(int i=0; i<str.length; i++)
@@ -35,6 +41,29 @@ public class String {
     this.count=length;
     this.offset=0;
   }
+  
+  public String(byte str[], String encoding) {
+    int length = this.count;
+    if (length>(str.length))
+      length=str.length;
+    char charstr[]=new char[length];
+    for(int i=0; i<length; i++)
+      charstr[i]=(char)str[i];
+    this.value=charstr;
+    this.count=length;
+    this.offset=0;
+  }
+  
+  public String(char str[], int offset, int length) {
+    if (length>(str.length-offset))
+      length=str.length-offset;
+    char charstr[]=new char[length];
+    for(int i=0; i<length; i++)
+      charstr[i]=str[i+offset];
+    this.value=charstr;
+    this.count=length;
+    this.offset=0;
+  }
 
   public String(String str) {
     this.value=str.value;
@@ -83,6 +112,10 @@ public class String {
     return this.lastindexOf(ch, count - 1);
   }
 
+  public int lastIndexOf(char ch) {
+    return this.lastindexOf((int)ch, count - 1);
+  }
+  
   public static String concat2(String s1, String s2) {
     if (s1==null)
       return "null".concat(s2);
@@ -171,6 +204,11 @@ public class String {
     return -1;
   }
 
+       public int indexOfIgnoreCase(String str, int fromIndex) {
+               if (fromIndex < 0) 
+                       fromIndex = 0;
+       }
+
   public int lastIndexOf(String str, int fromIndex) {
     int k=count-str.count;
     if (k>fromIndex)
@@ -217,6 +255,23 @@ public class String {
       str[i]=(byte)value[i+offset];
     return str;
   }
+  
+  public void getChars(char dst[], int dstBegin) {
+    getChars(0, count, dst, dstBegin);
+  }
+  
+  public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
+    if((srcBegin < 0) || (srcEnd > count) || (srcBegin > srcEnd)) {
+      // FIXME
+      System.printString("Index error: "+srcBegin+" "+srcEnd+" "+count+"\n"+this);
+      System.exit(-1);
+    }
+    int len = srcEnd - srcBegin;
+    int j = dstBegin;
+    for(int i=srcBegin; i<srcEnd; i++)
+      dst[j++]=value[i+offset];
+    return;
+  }
 
   public int length() {
     return count;
@@ -282,9 +337,16 @@ public class String {
     return new String(chararray);
   }
 
-  public static String valueOf(double x) {
-    return valueOf((long)x);
+  public static String valueOf(double val) {
+    char[] chararray=new char[20];
+    String s=new String();
+    s.offset=0;
+    s.count=convertdoubletochar(val, chararray);
+    s.value=chararray;
+    return s;
   }
+  
+  public static native int convertdoubletochar(double val, char [] chararray);
 
   public static String valueOf(long x) {
     int length=0;
@@ -318,18 +380,16 @@ public class String {
     return new String(chararray);
   }
 
-  public int compareTo( String s ) {
-    int lenDiff = this.length() - s.length();
-    if( lenDiff != 0 ) {
-      return lenDiff;
-    }    
-    for( int i = 0; i < this.length(); ++i ) {
+  public int compareTo(String s) {
+    int smallerlength=count<s.count?count:s.count;
+
+    for( int i = 0; i < smallerlength; i++ ) {
       int valDiff = this.charAt(i) - s.charAt(i);
       if( valDiff != 0 ) {
        return valDiff;
       }
     }
-    return 0;
+    return count-s.count;
   }
 
   public int hashCode() {
@@ -370,4 +430,88 @@ public class String {
     }
     return true;
   }
+
+  public Vector split() {
+    Vector splitted = new Vector();
+    int i;
+    int cnt =0;
+
+    // skip first spaces
+    for(i = 0; i< count;i++) {
+      if(value[i+offset] != '\n' && value[i+offset] != '\t' && value[i+offset] != ' ') 
+         break;
+    }
+
+    int oldi=i;
+
+    while(i<count) {
+      if(value[i+offset] == '\n' || value[i+offset] == '\t' || value[i+offset] == ' ') {
+         String t=new String();
+         t.value=value;
+         t.offset=oldi;
+         t.count=i-oldi;
+         splitted.addElement(t);
+
+         // skip extra spaces
+         while( i < count && ( value[i+offset] == '\n' || value[i+offset] == '\t' || value[i+offset] == ' ')) {
+             i++;
+         }
+         oldi=i;
+      } else {
+         i++;
+      }
+    }
+
+    if(i!=oldi) {
+       String t=new String();
+       t.value=value;
+       t.offset=oldi;
+       t.count=i-oldi;
+       splitted.addElement(t);
+    }
+    
+    return splitted;
+  }
+
+  public boolean contains(String str)
+  {
+    int i,j;
+    char[] strChar = str.toCharArray();
+    int cnt;
+
+    for(i = 0; i < count; i++) {
+      if(value[i] == strChar[0]) {
+        cnt=0;
+        for(j=0; j < str.length() && i+j < count;j++) {
+          if(value[i+j] == strChar[j])
+            cnt++;
+        }
+        if(cnt == str.length())
+          return true;
+      }
+    }
+
+    return false;
+
+  }
+  
+  public String trim() {
+    int len = count;
+    int st = 0;
+    int off = offset;      /* avoid getfield opcode */
+    char[] val = value;    /* avoid getfield opcode */
+
+    while ((st < len) && (val[off + st] <= ' ')) {
+      st++;
+    }
+    while ((st < len) && (val[off + len - 1] <= ' ')) {
+      len--;
+    }
+    return ((st > 0) || (len < count)) ? substring(st, len) : this;
+  }
+  
+  public boolean matches(String regex) {
+    System.println("String.matches() is not fully supported");
+    return this.equals(regex);
+  }
 }