Adding StringBuffer class and test case. This gives append functionality.
authorbdemsky <bdemsky>
Wed, 25 Oct 2006 00:14:33 +0000 (00:14 +0000)
committerbdemsky <bdemsky>
Wed, 25 Oct 2006 00:14:33 +0000 (00:14 +0000)
Robust/src/ClassLibrary/String.java
Robust/src/ClassLibrary/StringBuffer.java [new file with mode: 0644]
Robust/src/Main/Main.java
Robust/src/Runtime/runtime.c
Robust/src/Tests/DoTests
Robust/src/Tests/StringBufferTest.java [new file with mode: 0644]
Robust/src/Tests/output/StringBufferTest.output.goal [new file with mode: 0644]

index 3285f0fe06a6d378f7a14c767435bae31e3a3e78..b771e4da79a5b1c5028f0cff8b78b73432608162 100644 (file)
@@ -1,40 +1,60 @@
 public class String {
-    char string[];
+    char value[];
+    int count;
+    int offset;
 
     public String(char str[]) {
        char charstr[]=new char[str.length];
        for(int i=0;i<str.length;i++)
            charstr[i]=str[i];
-       this.string=charstr;
+       this.value=charstr;
+       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++)
            charstr[i]=(char)str[i];
-       this.string=charstr;
+       this.value=charstr;
+       this.count=str.length;
+       this.offset=0;
+    }
+
+    public String(String str) {
+       this.value=str.value;
+       this.count=str.count;
+       this.offset=str.offset;
+    }
+
+    public String(StringBuffer strbuf) {
+       value=new char[strbuf.length()];
+       count=strbuf.length();
+       offset=0;
+       for(int i=0;i<count;i++)
+           value[i]=strbuf.value[i];
     }
 
     char[] toCharArray() {
-       char str[]=new char[string.length];
-       for(int i=0;i<string.length;i++)
-           str[i]=string[i];
+       char str[]=new char[count];
+       for(int i=0;i<count;i++)
+           str[i]=value[i+offset];
        return str;
     }
 
     byte[] getBytes() {
-       byte str[]=new byte[string.length];
-       for(int i=0;i<string.length;i++)
-           str[i]=(byte)string[i];
+       byte str[]=new byte[count];
+       for(int i=0;i<value.length;i++)
+           str[i]=(byte)value[i+offset];
        return str;
     }
 
     public int length() {
-       return string.length;
+       return count;
     }
 
-    public char charAt(int x) {
-       return string[x];
+    public char charAt(int i) {
+       return value[i+offset];
     }
 
     public static String valueOf(Object o) {
@@ -58,16 +78,16 @@ public class String {
            chararray=new char[length+1];
        else
            chararray=new char[length];
-       int offset;
+       int voffset;
        if (x<0) {
            chararray[0]='-';
-           offset=1;
+           voffset=1;
            x=-x;
        } else
-           offset=0;
+           voffset=0;
                
        do {
-           chararray[--length+offset]=(char)(x%10+'0');
+           chararray[--length+voffset]=(char)(x%10+'0');
            x=x/10;
        } while (length!=0);
        return new String(chararray);
diff --git a/Robust/src/ClassLibrary/StringBuffer.java b/Robust/src/ClassLibrary/StringBuffer.java
new file mode 100644 (file)
index 0000000..4ac60a2
--- /dev/null
@@ -0,0 +1,49 @@
+public class StringBuffer {
+    char value[];
+    int count;
+    int offset;
+    //    private static final int DEFAULTSIZE=16;
+
+    public StringBuffer(String str) {
+       value=new char[str.value+16];//16 is DEFAULTSIZE
+       count=str.count;
+       offset=0;
+       for(int i=0;i<count;i++)
+           value[i]=str.value[i+str.offset];
+    }
+
+    public int length() {
+       return count;
+    }
+
+    public int capacity() {
+       return value.length;
+    }
+
+    public char charAt(int x) {
+       return value[x+offset];
+    }
+
+    public void append(String s) {
+       if ((s.count+count+offset)>value.length) {
+           // Need to allocate
+           char newvalue[]=new char[s.count+count+16]; //16 is DEFAULTSIZE
+           for(int i=0;i<count;i++)
+               newvalue[i]=value[i+offset];
+           for(int i=0;i<s.count;i++)
+               newvalue[i+count]=s.value[i+s.offset];
+           value=newvalue;
+           count+=s.count;
+           offset=0;
+       } else {
+           for(int i=0;i<s.count;i++) {
+               value[i+count+offset]=s.value[i+s.offset];
+           }
+           count+=s.count;
+       }
+    }
+
+    public String toString() {
+       return new String(this);
+    }
+}
index 7aa8cbef2fda3da16531365fb1d6c18e2bc73362..356c45c62c554213cf3a72ba1fe86ef317f2ef28 100644 (file)
@@ -51,6 +51,7 @@ public class Main {
       readSourceFile(state, ClassLibraryPrefix+"Object.java");
       readSourceFile(state, ClassLibraryPrefix+"System.java");
       readSourceFile(state, ClassLibraryPrefix+"String.java");
+      readSourceFile(state, ClassLibraryPrefix+"StringBuffer.java");
       readSourceFile(state, ClassLibraryPrefix+"FileInputStream.java");
       readSourceFile(state, ClassLibraryPrefix+"FileOutputStream.java");
       if (state.TASK) {
index ea1bc29106edfd3f86fee5afe7a426ab13194e8f..a3563ae7455d6347bd996cfe043ed19cb3324555 100644 (file)
@@ -427,10 +427,11 @@ int ___Object______hashcode____(struct ___Object___ * ___this___) {
 }
 
 void ___System______printString____L___String___(struct ___String___ * s) {
-    struct ArrayObject * chararray=s->___string___;
+    struct ArrayObject * chararray=s->___value___;
     int i;
-    for(i=0;i<chararray->___length___;i++) {
-       short s= ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i];
+    int offset=s->___offset___;
+    for(i=0;i<s->___count___;i++) {
+       short s= ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i+offset];
        putchar(s);
     }
 }
@@ -452,7 +453,10 @@ struct ___String___ * NewString(const char *str,int length) {
   struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
   struct ___String___ * strobj=allocate_new(STRINGTYPE);
   int i;
-  strobj->___string___=chararray;
+  strobj->___value___=chararray;
+  strobj->___count___=length;
+  strobj->___offset___=0;
+
   for(i=0;i<length;i++) {
     ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i]=(short)str[i];  }
   return strobj;
index e6de08baa7e7bd2a5f87703c92b9562f484531be..347206db0fc6a0c7699f9542bfa8eb1baec2462a 100755 (executable)
@@ -7,6 +7,7 @@ dotest BoundsFail2 BoundsFail2.java
 dotest BoundsFail3 BoundsFail3.java
 dotest BoundsFail4 BoundsFail4.java
 dotest StringTest StringTest.java
+dotest StringBufferTest StringBufferTest.java
 dotest Test Test.java
 dotest virtualcalltest virtualcalltest.java
 dotest IncTest IncTest.java
diff --git a/Robust/src/Tests/StringBufferTest.java b/Robust/src/Tests/StringBufferTest.java
new file mode 100644 (file)
index 0000000..f68ced0
--- /dev/null
@@ -0,0 +1,8 @@
+class StringBufferTest {
+    public static void main(String str[]) {
+       String a="hello world";
+       StringBuffer b=new StringBuffer(a);
+       b.append(a);
+       System.printString(b.toString());
+    }
+}
diff --git a/Robust/src/Tests/output/StringBufferTest.output.goal b/Robust/src/Tests/output/StringBufferTest.output.goal
new file mode 100644 (file)
index 0000000..2df7ebc
--- /dev/null
@@ -0,0 +1 @@
+hello worldhello world
\ No newline at end of file