1 21 package org.gjt.sp.util; 22 23 import java.io.Serializable ; 24 import javax.swing.text.Segment ; 25 26 31 public class SegmentCharSequence implements CharSequence , Serializable 32 { 33 34 public SegmentCharSequence(Segment seg) 35 { 36 this(seg, false); 37 } 38 39 public SegmentCharSequence(Segment seg, boolean reverse) 40 { 41 this(seg, 0, seg.count); 42 this.reverse = reverse; 43 } 44 45 public SegmentCharSequence(Segment seg, int off, int len) 46 { 47 this.offset = off; 48 this.length = len; 49 this.seg = seg; 50 } 51 52 public char charAt(int index) 53 { 54 if (reverse) 55 index = length - index - 1; 56 return seg.array[seg.offset + offset + index]; 57 } 58 59 public int length() 60 { 61 return length; 62 } 63 64 public CharSequence subSequence(int start, int end) 65 { 66 if (reverse) 67 throw new IllegalStateException ("reverse sub-sequences are not supported"); 68 return new SegmentCharSequence(seg, offset + start, end - start); 69 } 70 71 public String toString() 72 { 73 return new String (seg.array, offset+seg.offset, length); 74 } 75 76 private boolean reverse; 77 private int offset; 78 private int length; 79 private Segment seg; 80 81 } 82 83 | Popular Tags |