01: /*
02: * SegmentCharSequence.java
03: * :noTabs=false:
04: *
05: * Copyright (C) 2006 Marcelo Vanzin
06: *
07: * This library is free software; you can redistribute it and/or modify
08: * it under the terms of the GNU Library General Public License as published
09: * by the Free Software Foundation; either version 2 of the License, or
10: * (at your option) any later version.
11: *
12: * This library is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU Library General Public License for more details.
16: *
17: * You should have received a copy of the GNU Library General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20: */
21: package org.gjt.sp.util;
22:
23: import java.io.Serializable;
24: import javax.swing.text.Segment;
25:
26: /**
27: * Class that lets java.util.regex search within a javax.swing.text.Segment.
28: *
29: * @author Marcelo Vanzin
30: */
31: public class SegmentCharSequence implements CharSequence, Serializable {
32:
33: public SegmentCharSequence(Segment seg) {
34: this (seg, false);
35: }
36:
37: public SegmentCharSequence(Segment seg, boolean reverse) {
38: this (seg, 0, seg.count);
39: this .reverse = reverse;
40: }
41:
42: public SegmentCharSequence(Segment seg, int off, int len) {
43: this .offset = off;
44: this .length = len;
45: this .seg = seg;
46: }
47:
48: public char charAt(int index) {
49: if (reverse)
50: index = length - index - 1;
51: return seg.array[seg.offset + offset + index];
52: }
53:
54: public int length() {
55: return length;
56: }
57:
58: public CharSequence subSequence(int start, int end) {
59: if (reverse)
60: throw new IllegalStateException(
61: "reverse sub-sequences are not supported");
62: return new SegmentCharSequence(seg, offset + start, end - start);
63: }
64:
65: public String toString() {
66: return new String(seg.array, offset + seg.offset, length);
67: }
68:
69: private boolean reverse;
70: private int offset;
71: private int length;
72: private Segment seg;
73:
74: }
|