changes: now Inference engine works fine with the EyeTracking benchmark.
[IRC.git] / Robust / src / ClassLibrary / String.java
index 07632bec02607f87dd5b739db3b9c37bcc6b9aef..53b9cec6bdae2d3060db4504c5fb1b961a5bd59c 100644 (file)
@@ -42,6 +42,29 @@ public class String {
     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;
     this.count=str.count;
@@ -89,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);
@@ -114,7 +141,7 @@ public class String {
   public int lastindexOf(int ch, int fromIndex) {
     for(int i=fromIndex; i>0; i--)
       if (this.charAt(i)==ch)
-       return i;
+        return i;
     return -1;
   }
 
@@ -123,7 +150,7 @@ public class String {
     for(int i=0; i<count; i++) {
       char x=charAt(i);
       if (x==oldch)
-       x=newch;
+        x=newch;
       buffer[i]=x;
     }
     return new String(buffer);
@@ -134,7 +161,7 @@ public class String {
     for(int i=0; i<count; i++) {
       char x=charAt(i);
       if (x>='a'&&x<='z') {
-       x=(char) ((x-'a')+'A');
+        x=(char) ((x-'a')+'A');
       }
       buffer[i]=x;
     }
@@ -146,7 +173,7 @@ public class String {
     for(int i=0; i<count; i++) {
       char x=charAt(i);
       if (x>='A'&&x<='Z') {
-       x=(char) ((x-'A')+'a');
+        x=(char) ((x-'A')+'a');
       }
       buffer[i]=x;
     }
@@ -160,7 +187,7 @@ public class String {
   public int indexOf(int ch, int fromIndex) {
     for(int i=fromIndex; i<count; i++)
       if (this.charAt(i)==ch)
-       return i;
+        return i;
     return -1;
   }
 
@@ -173,17 +200,22 @@ public class String {
       fromIndex=0;
     for(int i=fromIndex; i<=(count-str.count); i++)
       if (regionMatches(i, str, 0, str.count))
-       return i;
+        return i;
     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)
       k=fromIndex;
     for(; k>=0; k--) {
       if (regionMatches(k, str, 0, str.count))
-       return k;
+        return k;
     }
     return -1;
   }
@@ -206,7 +238,7 @@ public class String {
     for(int i=0; i<len; i++)
       if (other.value[i+other.offset+ooffset]!=
           this.value[i+this.offset+toffset])
-       return false;
+        return false;
     return true;
   }
 
@@ -224,6 +256,23 @@ public class String {
     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;
   }
@@ -296,7 +345,7 @@ public class String {
     s.value=chararray;
     return s;
   }
-  
+
   public static native int convertdoubletochar(double val, char [] chararray);
 
   public static String valueOf(long x) {
@@ -337,7 +386,7 @@ public class String {
     for( int i = 0; i < smallerlength; i++ ) {
       int valDiff = this.charAt(i) - s.charAt(i);
       if( valDiff != 0 ) {
-       return valDiff;
+        return valDiff;
       }
     }
     return count-s.count;
@@ -361,7 +410,7 @@ public class String {
       return false;
     for(int i=0; i<count; i++) {
       if (s.value[i+s.offset]!=value[i+offset])
-       return false;
+        return false;
     }
     return true;
   }
@@ -373,11 +422,11 @@ public class String {
       char l=s.value[i+s.offset];
       char r=value[i+offset];
       if (l>='a'&&l<='z')
-       l=(char)((l-'a')+'A');
+        l=(char)((l-'a')+'A');
       if (r>='a'&&r<='z')
-       r=(char)((r-'a')+'A');
+        r=(char)((r-'a')+'A');
       if (l!=r)
-       return false;
+        return false;
     }
     return true;
   }
@@ -387,19 +436,81 @@ public class String {
     int i;
     int cnt =0;
 
-    String tmpStr = new String();
-
+    // skip first spaces
     for(i = 0; i< count; i++) {
-      if(value[i] == '\n' || value[i] == '\t' || value[i] == ' ') {
-        splitted.addElement(tmpStr);
-        tmpStr = new String();
-      }else {
-        tmpStr += value[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++;
       }
     }
-    splitted.addElement(tmpStr);
+
+    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);
   }
 }