File: java.lang.String

package info (click to toggle)
bock 0.20.2.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,228 kB
  • ctags: 1,370
  • sloc: ansic: 7,367; java: 5,553; yacc: 963; lex: 392; makefile: 243; sh: 90; perl: 42
file content (451 lines) | stat: -rw-r--r-- 9,981 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// java.lang.String -- an implementation of the Java Language Spec sec 20.12
// Written by Charles Briscoe-Smith; refer to the file LEGAL for details.

package java.lang;

import java.util.Hashtable;

public final class String {

	// CONSTRUCTORS
	public String() { len=0; }
	public String(String value) throws NullPointerException {
		data=value.data;
		start=value.start;
		len=value.len;
	}
	public String(StringBuffer buffer) throws NullPointerException {
		data=buffer.getPrivateData();
		start=buffer.getPrivateStart();
		len=buffer.length();
	}
	public String(char[] value) throws NullPointerException {
		this(value, 0, value.length);
	}
	public String(char[] value, int offset, int count)
			throws NullPointerException,
			       IndexOutOfBoundsException {
		if (count<0) throw new IndexOutOfBoundsException();
		len=count;
		if (count>0) {
			data=new char[count];
			start=0;
			for (int i=0; i<count; i++) {
				data[i]=value[i+offset];
			}
		}
	}
	public String(byte[] ascii, int hibyte)
			throws NullPointerException {
		this(ascii, hibyte, 0, ascii.length);
	}
	public String(byte[] ascii, int hibyte, int offset, int count)
			throws NullPointerException,
			       IndexOutOfBoundsException {
		if (count<0) throw new IndexOutOfBoundsException();
		len=count;
		if (count>0) {
			data=new char[count];
			start=0;
			for (int i=0; i<count; i++) {
				data[i]=((hibyte & 0xff) << 8)
				        | (ascii[i] & 0xff);
			}
		}
	}

	// PRIVATE
	private char[] data;
	private int start;
	private int len;

	// PUBLIC INTERFACE
	public String toString() {
		return this;
	}
	public boolean equals(Object obj) {
		try {
			String s=(String) obj;
			if (s.len!=len) return false;
			for (int i=0; i<len; i++) {
				if (s.data[s.start+i]!=data[start+i])
					return false;
			}
			return true;
		} catch (NullPointerException e) {
			return false;
		} catch (ClassCastException e) {
			return false;
		}
	}
	public int hashCode()
	{
		int accum=0;
		int mult=1;
		for (int i=len-1; i>=0; i--) {
			accum+=data[start+i]*mult;
			mult*=31;
		}
		return accum;
	}

	public int length() {
		return len;
	}
	public char charAt(int index) {
		if (index<0 || index>=len)
			throw new IndexOutOfBoundsException();
		return data[start+index];
	}

	public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
			throws NullPointerException,
			       IndexOutOfBoundsException
	{
		if (srcBegin<0 || srcBegin>srcEnd || srcEnd>len
		    || dstBegin<0 || dstBegin+(srcEnd-srcBegin)>dst.length)
		{
			throw new IndexOutOfBoundsException();
		}
		while (srcBegin<srcEnd) {
			dst[dstBegin++]=data[start+(srcBegin++)];
		}
	}
	public void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
			throws NullPointerException,
			       IndexOutOfBoundsException
	{
		if (srcBegin<0 || srcBegin>srcEnd || srcEnd>len
		    || dstBegin<0 || dstBegin+(srcEnd-srcBegin)>dst.length)
		{
			throw new IndexOutOfBoundsException();
		}
		while (srcBegin<srcEnd) {
			dst[dstBegin++]=(byte) data[start+(srcBegin++)];
		}
	}

	public char[] toCharArray()
	{
		char[] chars=new char[len];
		getChars(0, len, chars, 0);
		return chars;
	}
	public boolean equalsIgnoreCase(String s) {
		try {
			if (s.len!=len) return false;
			for (int i=0; i<len; i++) {
				char my=data[start+i];
				char other=s.data[s.start+i];
				if (my != other
				    && Character.toUpperCase(my)
				            != Character.toUpperCase(other)
				    && Character.toLowerCase(my)
				            != Character.toLowerCase(other))
				{
					return false;
				}
			}
			return true;
		} catch (NullPointerException e) {
			return false;
		}
	}
	public int compareTo(String other) throws NullPointerException
	{
		for (int i=0; i<len && i<other.len; i++) {
			char my=data[start+i];
			char it=other.data[other.start+i];
			if (my != it) return my-it;
		}
		return len-other.len;
	}
	public boolean regionMatches(int toffset,
	                             String other, int oofset, int len)
			throws NullPointerException
	{
		return regionMatches(false, toffset, other, oofset, len);
	}
	public boolean regionMatches(boolean ignoreCase, int toffset,
	                             String other, int oofset, int len)
			throws NullPointerException
	{
		if (toffset<0 || oofset<0
		    || toffset+len>this.len || oofset+len>other.len)
		{
			return false;
		}
		for (int i=0; i<len; i++) {
			char my=data[start+i];
			char ot=other.data[other.start+i];
			if (my != ot
			    && (ignoreCase==false
			        || (Character.toUpperCase(my)
			                    != Character.toUpperCase(ot)
			            && Character.toLowerCase(my)
			                    != Character.toLowerCase(ot))))
			{
				return false;
			}
		}
		return true;
	}

	public boolean startsWith(String prefix) throws NullPointerException
	{
		if (prefix.len>len)
			return false;
		return substring(0, prefix.len).equals(prefix);
/*		for (int i=0; i<prefix.length; i++) {
			if (prefix.data[prefix.start+i]!=data[start+i])
				return false;
		}
		return true;
*/	}
	public boolean startsWith(String prefix, int toffset)
			throws NullPointerException
	{
		if (toffset<0 || toffset>len || prefix.len+toffset>len)
			return false;
		return substring(toffset, prefix.len+toffset).equals(prefix);
	}
	public boolean endsWith(String suffix) throws NullPointerException
	{
		if (suffix.len>len)
			return false;
		return substring(len-suffix.len).equals(suffix);
	}

	public int indexOf(int ch)
	{
		return indexOf(ch, 0);
	}
	public int indexOf(int ch, int from)
	{
		for (int i=from<0 ? 0 : from; i<len; i++) {
			if (data[start+i]==ch)
				return i;
		}
		return -1;
	}
	public int indexOf(String s) throws NullPointerException
	{
		return indexOf(s, 0);
	}
	public int indexOf(String s, int from) throws NullPointerException
	{
		// FIXME: this is inefficient
		for (int i=from<0 ? 0 : from; i<len-s.len; i++) {
			if (substring(i, i+s.len).equals(s))
				return i;
		}
		return -1;
	}

	public int lastIndexOf(int ch)
	{
		return lastIndexOf(ch, len);
	}
	public int lastIndexOf(int ch, int from)
	{
		for (int i=from>=len ? len-1 : from; i>0; i--) {
			if (data[start+i]==ch)
				return i;
		}
		return -1;
	}
	public int lastIndexOf(String s) throws NullPointerException
	{
		return lastIndexOf(s, len);
	}
	public int lastIndexOf(String s, int from) throws NullPointerException
	{
		// FIXME: this is inefficient
		int i=len-s.len;
		if (from<i) i=from;
		while (i>=0) {
			if (substring(i, i+s.len).equals(s))
				return i;
			i--;
		}
		return -1;
	}

	public String substring(int begin) throws IndexOutOfBoundsException
	{
		if (begin<0 || begin>len)
			throw new IndexOutOfBoundsException();
		String s=new String();
		s.data=data;
		s.start=start+begin;
		s.len=len-begin;
		return s;
	}
	public String substring(int begin, int end)
			throws IndexOutOfBoundsException
	{
		if (begin<0 || begin>end || end>len)
			throw new IndexOutOfBoundsException();
		String s=new String();
		s.data=data;
		s.start=start+begin;
		s.len=end-begin;
		return s;
	}

	public String concat(String str) throws NullPointerException {
		if (str.len==0)
			return this;
		// FIXME: use a StringBuffer
		char[] newstr=new char[this.len+str.len];
		int p=0;
		for (int i=0; i<len; i++) {
			newstr[p++]=data[start+i];
		}
		for (int i=0; i<str.len; i++) {
			newstr[p++]=str.data[str.start+i];
		}
		return new String(newstr);
	}

	public String replace(char find, char repl)
	{
		char[] newstr=null;
		for (int i=0; i<len; i++) {
			if (data[start+i]==find) {
				if (newstr==null) {
					newstr=new char[len];
					for (int j=0; j<i; j++) {
						newstr[j]=data[start+j];
					}
				}
				newstr[i]=repl;
			} else if (newstr!=null) {
				newstr[i]=data[start+i];
			}
		}
		if (newstr==null)
			return this;
		else
			return new String(newstr);
	}

	public String toLowerCase()
	{
		char[] newstr=null;
		for (int i=0; i<len; i++) {
			char c=data[start+i];
			if (newstr==null && Character.toLowerCase(c)!=c) {
				newstr=new char[len];
				for (int j=0; j<i; j++) {
					newstr[j]=data[start+j];
				}
			}
			if (newstr!=null) {
				newstr[i]=Character.toLowerCase(c);
			}
		}
		if (newstr==null)
			return this;
		else
			return new String(newstr);
	}

	public String toUpperCase()
	{
		char[] newstr=null;
		for (int i=0; i<len; i++) {
			char c=data[start+i];
			if (newstr==null && Character.toUpperCase(c)!=c) {
				newstr=new char[len];
				for (int j=0; j<i; j++) {
					newstr[j]=data[start+j];
				}
			}
			if (newstr!=null) {
				newstr[i]=Character.toUpperCase(c);
			}
		}
		if (newstr==null)
			return this;
		else
			return new String(newstr);
	}

	public String trim()
	{
		if (len==0 || (data[start]>0x20 && data[start+len-1]>0x20))
			return this;
		int j=-1;
		int k=-1;
		for (int i=0; i<len; i++) {
			if (data[start+i]>0x20) {
				if (j==-1) j=i;
				k=i;
			}
		}
		if (j==-1) return new String();
		return substring(j, k+1);
	}

	public static String valueOf(Object o)
	{
		if (o==null) return "null";
		return o.toString();
	}

	public static String valueOf(char[] d) throws NullPointerException
	{
		return new String(d);
	}

	public static String valueOf(char[] d, int o, int c)
			throws NullPointerException, IndexOutOfBoundsException
	{
		return new String(d, o, c);
	}

	public static String valueOf(boolean b)
	{
		return b ? "true" : "false";
	}

	public static String valueOf(char c)
	{
		String s=new String();
		s.data=new char[1];
		s.data[0]=c;
		s.len=1;
		s.start=0;
		return s;
	}

	public static String valueOf(int i)
	{
		return Integer.toString(i);
	}

	public static String valueOf(long l)
	{
		return Long.toString(l);
	}

//	public static String valueOf(float f)
//	{
//		return Float.toString(f);
//	}

//	public static String valueOf(double d)
//	{
//		return Double.toString(d);
//	}

	private static Hashtable stringtable=new Hashtable();

	public String intern() {
		if (stringtable.containsKey(this))
			return stringtable.get(this);
		stringtable.put(this, this);
		return this;
	}
}