added Global string and other changes for compilation
[IRC.git] / Robust / src / Benchmarks / Distributed / SpamFilter / GString.java
1 public class GString {
2   char value[];
3   int count;
4   int offset;
5
6   public GString() {
7   }
8
9   public GString(char c) {
10     char[] str = global new char[1];
11     str[0] = c;
12     GString(str);
13   }
14
15   public GString(String str) {
16     value = global new char[str.count];
17     for(int i =0; i< str.count;i++) {
18       value[i] = str.value[i+str.offset];
19     }
20     count = str.count;
21     offset = 0;
22   }
23
24   public GString(GString gstr) {
25     this.value = gstr.value;
26     this.count = gstr.count;
27     this.offset = gstr.offset;
28   }
29
30   public GString(StringBuffer gsb) {
31     value = global new char[gsb.length()];
32     count = gsb.length();
33     offset = 0;
34     for (int i = 0; i < count; i++) 
35       value[i] = gsb.value[i];
36   }
37
38   public GString(char str[]) {
39     char charstr[]=new char[str.length];
40     for(int i=0; i<str.length; i++)
41       charstr[i]=str[i];
42     this.value=charstr;
43     this.count=str.length;
44     this.offset=0;
45   }
46
47   public static char[] toLocalCharArray(GString str) {
48     char[] c;
49     int length;
50
51     length = str.length();
52
53     c = new char[length];
54
55     for (int i = 0; i < length; i++) {
56       c[i] = str.value[i+str.offset];
57     }
58     return c;
59   }
60
61   public String toLocalString() {
62     return new String(toLocalCharArray(this));
63   }
64
65   public int length() {
66     return count;
67   }
68
69   public int indexOf(int ch, int fromIndex) {
70     for (int i = fromIndex; i < count; i++)
71       if (this.charAt(i) == ch) 
72         return i;
73     return -1;
74   }
75
76   public int lastindexOf(int ch) {
77     return this.lastindexOf(ch, count - 1);
78   }
79
80   public int lastindexOf(int ch, int fromIndex) {
81     for (int i = fromIndex; i > 0; i--) 
82       if (this.charAt(i) == ch) 
83         return i;
84     return -1;
85   }
86
87   public char charAt(int i) {
88     return value[i+offset];
89   }
90
91   public int indexOf(String str) {
92     return this.indexOf(str, 0);
93   }
94
95   public int indexOf(String str, int fromIndex) {
96     if (fromIndex < 0) 
97       fromIndex = 0;
98     for (int i = fromIndex; i <= (count-str.count); i++)
99       if (regionMatches(i, str, 0, str.count)) 
100         return i;
101     return -1;
102   }     
103
104   public boolean regionMatches(int toffset, String other, int ooffset, int len) {
105     if (toffset < 0 || ooffset < 0 || (toffset+len) > count || (ooffset+len) > other.count)
106       return false;
107
108     for (int i = 0; i < len; i++) {
109       if (other.value[i+other.offset+ooffset] != this.value[i+this.offset+toffset])
110         return false;
111     }
112     return true;
113   }
114
115   public String subString(int beginIndex, int endIndex) {
116     return substring(beginIndex, endIndex);
117   }
118
119   public String substring(int beginIndex, int endIndex) {
120     String str;
121     str = global new String();
122     str.value = this.value;
123     str.count = endIndex-beginIndex;
124     str.offset = this.offset + beginIndex;
125     return str; 
126   }
127
128   public static String valueOf(Object o) {
129     if (o==null)
130       return "null";
131     else
132       return o.toString();
133   }
134 }