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) {
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);
--- /dev/null
+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);
+ }
+}
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) {
}
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);
}
}
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;
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
--- /dev/null
+class StringBufferTest {
+ public static void main(String str[]) {
+ String a="hello world";
+ StringBuffer b=new StringBuffer(a);
+ b.append(a);
+ System.printString(b.toString());
+ }
+}
--- /dev/null
+hello worldhello world
\ No newline at end of file