KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > util > CharSegment


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.util;
30
31 /**
32  * CharSegment is a section of a character buffer
33  */

34 public class CharSegment implements CharSequence JavaDoc {
35   protected char []_buffer;
36   protected int _offset;
37   protected int _length;
38
39   public CharSegment()
40   {
41   }
42
43   /**
44    * Constructs a char segment based on a char array.
45    */

46   public CharSegment(char []buffer, int offset, int length)
47   {
48     _buffer = buffer;
49     _offset = offset;
50     _length = length;
51   }
52
53   /**
54    * Sets the char segment to a new buffer triple.
55    */

56   public void init(char []buffer, int offset, int length)
57   {
58     _buffer = buffer;
59     _offset = offset;
60     _length = length;
61   }
62
63   /**
64    * Returns the character count of the buffer's contents.
65    */

66   public int length()
67   {
68     return _length;
69   }
70
71   /**
72    * Returns the buffer length
73    */

74   public int getLength()
75   {
76     return _length;
77   }
78
79   public int getOffset()
80   {
81     return _offset;
82   }
83
84   /**
85    * Returns the char at the specified offset.
86    */

87   public char charAt(int i)
88   {
89     if (i < 0 || _length <= i)
90       throw new IndexOutOfBoundsException JavaDoc();
91
92     return _buffer[i + _offset];
93   }
94
95   /**
96    * Returns the last character of the buffer
97    *
98    * @throws IndexOutOfBoundsException for an empty buffer
99    */

100   public char getLastChar()
101   {
102     if (_length == 0)
103       throw new IndexOutOfBoundsException JavaDoc();
104
105     return _buffer[_offset + _length - 1];
106   }
107
108   /**
109    * Returns the buffer's char array.
110    */

111   public char []getBuffer()
112   {
113     return _buffer;
114   }
115
116   /**
117    * Copies characters to the destination buffer.
118    */

119   public void getChars(int srcBegin, int srcEnd, char []dst, int dstBegin)
120   {
121     srcBegin += _offset;
122     srcEnd += _offset;
123
124     char []buffer = _buffer;
125     while (srcBegin < srcEnd)
126       dst[dstBegin++] = buffer[srcBegin++];
127   }
128
129   /**
130    * Returns a substring
131    */

132   public String JavaDoc substring(int start)
133   {
134     if (_length < start || start < 0)
135       throw new StringIndexOutOfBoundsException JavaDoc();
136
137     return new String JavaDoc(_buffer, _offset + start, _length - start);
138   }
139
140   /**
141    * Returns a substring
142    */

143   public String JavaDoc substring(int start, int end)
144   {
145     if (_length < start || start < 0 || end < start)
146       throw new StringIndexOutOfBoundsException JavaDoc();
147
148     return new String JavaDoc(_buffer, _offset + start, end - start);
149   }
150
151   /**
152    * Returns a subsequence
153    */

154   public CharSequence JavaDoc subSequence(int start, int end)
155   {
156     if (_length < start || start < 0 || end < start)
157       throw new StringIndexOutOfBoundsException JavaDoc();
158
159     return new String JavaDoc(_buffer, _offset + start, end - start);
160   }
161
162   /**
163    * Returns the index of a character in the CharSegment.
164    */

165   public int indexOf(char ch)
166   {
167     return indexOf(ch, 0);
168   }
169
170   /**
171    * Returns the index of a character in the CharSegment starting
172    * from an offset.
173    */

174   public final int indexOf(char ch, int start)
175   {
176     if (start < 0)
177       start = 0;
178
179     int end = _offset + _length;
180     start += _offset;
181
182     char []buffer = _buffer;
183     for (; start < end; start++) {
184       if (buffer[start] == ch)
185         return start - _offset;
186     }
187
188     return -1;
189   }
190
191   /**
192    * Returns the last index of a character in the CharSegment.
193    */

194   public final int lastIndexOf(char ch)
195   {
196     return lastIndexOf(ch, _length - 1);
197   }
198   /**
199    * Returns the last index of a character in the CharSegment starting
200    * from an offset.
201    */

202
203   public final int lastIndexOf(char ch, int start)
204   {
205     if (_length <= start)
206       start = _length - 1;
207
208     char []buffer = _buffer;
209     int offset = _offset;
210     for (; start >= 0; start--) {
211       if (buffer[start + offset] == ch)
212         return start;
213     }
214
215     return -1;
216   }
217
218   public int indexOf(String JavaDoc s)
219   {
220     return indexOf(s, 0);
221   }
222
223   public int indexOf(String JavaDoc s, int start)
224   {
225     int slen = s.length();
226
227     if (start < 0)
228       start = 0;
229
230     int end = _offset + _length - slen + 1;
231     start += _offset;
232
233     char []buffer = _buffer;
234     for (; start < end; start++) {
235       int i = 0;
236       for (; i < slen; i++) {
237         if (buffer[start+i] != s.charAt(i))
238           break;
239       }
240       if (i == slen)
241         return start - _offset;
242     }
243
244     return -1;
245   }
246
247   /**
248    * Returns the buffer's hash code
249    */

250   public int hashCode()
251   {
252     int hash = 0;
253     char []buffer = _buffer;
254     int begin = _offset;
255     int end = begin + _length;
256
257     for (; begin < end; begin++)
258       hash = 65521 * hash + buffer[begin] * 251 + 1021;
259
260     return hash;
261   }
262
263   /*
264    * Predicate testing if two char segments are equal
265    */

266   public final boolean equals(Object JavaDoc a)
267   {
268     if (this == a)
269       return true;
270
271     else if (a instanceof CharSegment) {
272       CharSegment cb = (CharSegment) a;
273
274       int length = _length;
275       if (length != cb._length)
276         return false;
277
278       char []buffer = _buffer;
279       char []aBuffer = cb._buffer;
280
281       int offset = _offset;
282       int aOffset = cb._offset;
283
284       for (int i = length - 1; i >= 0; i--)
285         if (buffer[offset + i] != aBuffer[aOffset + i])
286           return false;
287
288       return true;
289     }
290     else if (a instanceof CharSequence JavaDoc) {
291       CharSequence JavaDoc seq = (CharSequence JavaDoc) a;
292
293       int length = seq.length();
294
295       if (_length != length)
296         return false;
297
298       for (int i = length - 1; i >= 0; i--) {
299         if (_buffer[i] != seq.charAt(i))
300           return false;
301       }
302
303       return true;
304     }
305     else
306       return false;
307   }
308
309   /**
310    * Returns true if the two char segments are equal.
311    */

312   public boolean equals(CharSegment cb)
313   {
314     int length = _length;
315     if (length != cb._length)
316       return false;
317
318     char []buffer = _buffer;
319     char []aBuffer = cb._buffer;
320
321     int offset = _offset;
322     int aOffset = cb._offset;
323
324     for (int i = length - 1; i >= 0; i--)
325       if (buffer[offset + i] != aBuffer[aOffset + i])
326         return false;
327
328     return true;
329   }
330
331   /**
332    * Returns true if the CharSegment equals the char array.
333    */

334   public final boolean equals(char []cb, int length)
335   {
336     if (length != _length)
337       return false;
338
339     int offset = _offset;
340     char []buffer = _buffer;
341
342     for (int i = _length - 1; i >= 0; i--)
343       if (buffer[offset + i] != cb[i])
344         return false;
345
346     return true;
347   }
348
349   /**
350    * Returns true if the CharSegment equals the string.
351    */

352   public final boolean equalsIgnoreCase(String JavaDoc a)
353   {
354     int len = a.length();
355     if (_length != len)
356       return false;
357
358     int offset = _offset;
359     char []buffer = _buffer;
360
361     for (int i = 0; i < len; i++) {
362       char ca = buffer[offset + i];
363       char cb = a.charAt(i);
364
365       if (ca == cb) {
366       }
367
368       else if (Character.toLowerCase(ca) != Character.toLowerCase(cb))
369         return false;
370     }
371
372     return true;
373   }
374
375   /**
376    * Returns true if the two CharSegments are equivalent ignoring the case.
377    */

378   public final boolean equalsIgnoreCase(CharSegment b)
379   {
380     int length = _length;
381     if (length != b._length)
382       return false;
383
384     char []buffer = _buffer;
385     char []bBuffer = b._buffer;
386
387     int offset = _offset;
388     int bOffset = b._offset;
389
390     for (int i = length - 1; i >= 0; i--) {
391       char ca = buffer[offset + i];
392       char cb = bBuffer[bOffset + i];
393
394       if (ca != cb &&
395           Character.toLowerCase(ca) != Character.toLowerCase(cb))
396         return false;
397     }
398     return true;
399   }
400
401   /*
402    * XXX: unsure is this is legit
403    */

404   public final boolean matches(Object JavaDoc a)
405   {
406     if (a instanceof CharSegment) {
407       CharSegment cb = (CharSegment) a;
408       if (_length != cb._length)
409         return false;
410
411       int offset = _offset;
412       int bOffset = cb._offset;
413
414       char []buffer = _buffer;
415       char []cbBuffer = cb._buffer;
416
417       for (int i = _length - 1; i >= 0; i--)
418         if (buffer[offset + i] != cbBuffer[bOffset + i])
419           return false;
420
421       return true;
422     } else if (a instanceof String JavaDoc) {
423       String JavaDoc sa = (String JavaDoc) a;
424
425       if (_length != sa.length())
426         return false;
427
428       int offset = _offset;
429       char []buffer = _buffer;
430
431       for (int i = _length - 1; i >= 0; i--)
432         if (buffer[i + offset] != sa.charAt(i))
433           return false;
434
435       return true;
436     } else
437       return false;
438   }
439
440   /**
441    * Returns true if the charSegment matches the string.
442    */

443   public boolean matches(String JavaDoc sa)
444   {
445     if (_length != sa.length())
446       return false;
447
448     char []buffer = _buffer;
449     int offset = _offset;
450
451     for (int i = _length - 1; i >= 0; i--)
452       if (_buffer[_offset + i] != sa.charAt(i))
453         return false;
454
455     return true;
456   }
457
458   /**
459    * Returns true if the CharSegment matches the string, ignoring the case.
460    */

461   public boolean matchesIgnoreCase(String JavaDoc sa)
462   {
463     if (_length != sa.length())
464       return false;
465
466     char []buffer = _buffer;
467     int offset = _offset;
468     for (int i = _length - 1; i >= 0; i--) {
469       char ca = buffer[offset + i];
470       char cb = sa.charAt(i);
471
472       if (ca != cb && Character.toLowerCase(ca) != Character.toLowerCase(cb))
473         return false;
474     }
475
476     return true;
477   }
478
479   public boolean regionMatches(int off1, CharSegment buf, int off2, int len)
480   {
481     if (_length < off1 + len || buf._length < off2 + len)
482       return false;
483
484     char []buffer = _buffer;
485     char []bufBuffer = buf._buffer;
486     for (int i = len - 1; i >= 0; i--) {
487       if (buffer[off1 + i] != bufBuffer[off2 + i])
488         return false;
489     }
490
491     return true;
492   }
493
494   public boolean regionMatches(int off1, String JavaDoc buf, int off2, int len)
495   {
496     if (_length < off1 + len || buf.length() < off2 + len)
497       return false;
498
499     char []buffer = _buffer;
500     for (int i = 0; i < len; i++) {
501       if (buffer[off1 + i] != buf.charAt(off2 + i))
502         return false;
503     }
504
505     return true;
506   }
507
508   public boolean regionMatchesIgnoreCase(int off1, CharSegment buf,
509                                          int off2, int len)
510   {
511     if (_length < off1 + len || buf._length < off2 + len)
512       return false;
513
514     char []buffer = _buffer;
515     char []bufBuffer = buf._buffer;
516     for (int i = len -1; i >= 0; i--) {
517       if (Character.toLowerCase(buffer[off1 + i]) !=
518           Character.toLowerCase(bufBuffer[off2 + i]))
519         return false;
520     }
521
522     return true;
523   }
524
525   /**
526    * Returns true if the CharSegment starts with the string.
527    */

528   public boolean startsWith(String JavaDoc string)
529   {
530     if (string == null)
531       return false;
532
533     int strlen = string.length();
534     if (_length < strlen)
535       return false;
536
537     char []buffer = _buffer;
538     int offset = _offset;
539
540     while (--strlen >= 0) {
541       if (buffer[offset + strlen] != string.charAt(strlen))
542         return false;
543     }
544
545     return true;
546   }
547
548   /**
549    * Returns true if the CharSegment ends with the string.
550    */

551   public boolean endsWith(String JavaDoc string)
552   {
553     if (string == null)
554       return false;
555
556     int strlen = string.length();
557     if (_length < strlen)
558       return false;
559
560     char []buffer = _buffer;
561     int offset = _offset + _length - strlen;
562
563     while (--strlen >= 0) {
564       if (buffer[offset + strlen] != string.charAt(strlen))
565         return false;
566     }
567
568     return true;
569   }
570
571   /**
572    * Returns true if the CharSegment ends with the char segment.
573    */

574   public boolean endsWith(CharSegment cb)
575   {
576     if (cb == null)
577       return false;
578
579     int strlen = cb._length;
580     if (_length < strlen)
581       return false;
582
583     char []buffer = _buffer;
584     int offset = _offset + _length - strlen;
585
586     char []cbBuffer = cb._buffer;
587     int cbOffset = cb._offset;
588
589     while (--strlen >= 0) {
590       if (buffer[offset + strlen] != cbBuffer[cbOffset + strlen])
591         return false;
592     }
593
594     return true;
595   }
596
597   /**
598    * Converts the contents of the segment to lower case.
599    */

600   public CharSegment toLowerCase()
601   {
602     char []buffer = _buffer;
603     int len = _length;
604     int offset = _offset;
605
606     while (--len >= 0)
607       buffer[offset + len] = Character.toLowerCase(buffer[offset + len]);
608
609     return this;
610   }
611
612   /**
613    * String representation of the buffer.
614    */

615   public String JavaDoc toString()
616   {
617     return new String JavaDoc(_buffer, _offset, _length);
618   }
619 }
620
Popular Tags