1 4 package gnu.lists; 5 6 11 12 public class CharBuffer extends StableVector 13 implements CharSeq, java.io.Serializable 14 { 15 private FString string; 17 18 public CharBuffer(FString str) 19 { 20 super(str); 21 string = str; 22 } 23 24 public CharBuffer(int initialSize) 25 { 26 this(new FString(initialSize)); 27 } 28 29 protected CharBuffer () 30 { 31 } 32 33 public int length() { return size(); } 34 35 public char charAt(int index) 36 { 37 if (index >= gapStart) 39 index += gapEnd - gapStart; 40 return string.charAt(index); 41 } 42 43 45 public void getChars (int srcBegin, int srcEnd, char[] dst, int dstBegin) 46 { 47 char[] array = string.data; 48 int count; 49 if (srcBegin < gapStart) 50 { 51 count = (srcEnd < gapStart ? srcEnd : gapStart) - srcBegin; 52 if (count > 0) 53 { 54 System.arraycopy(array, srcBegin, dst, dstBegin, count); 55 srcBegin += count; 56 dstBegin += count; 57 } 58 } 59 int gapSize = gapEnd - gapStart; 60 srcBegin += gapSize; 61 srcEnd += gapSize; 62 count = srcEnd - srcBegin; 63 if (count > 0) 64 System.arraycopy(array, srcBegin, dst, dstBegin, count); 65 } 66 67 public void setCharAt(int index, char value) 68 { 69 if (index >= gapStart) 71 index += gapEnd - gapStart; 72 string.setCharAt(index, value); 73 } 74 75 76 public CharSequence subSequence(int start, int end) 77 { 78 int sz = size(); 79 if (start < 0 || end < start || end > sz) 80 throw new IndexOutOfBoundsException (); 81 return new SubCharSeq(this, 82 base.createPos(start, false), 83 base.createPos(end, true)); 84 } 85 86 87 public void fill(int fromIndex, int toIndex, char value) 88 { 89 char[] array = string.data; 90 int i = fromIndex; 91 int limit = gapStart < toIndex ? gapStart : toIndex; 92 for (; i < limit; i++) 93 array[i] = value; 94 int gapSize = gapEnd - gapStart; 95 i = limit + gapSize; 96 limit += toIndex; 97 for (; i < limit; i++) 98 array[i] = value; 99 } 100 101 102 public final void fill (char value) 103 { 104 char[] array = string.data; 105 for (int i = array.length; --i >= gapEnd; ) 106 array[i] = value; 107 for (int i = gapStart; --i >= 0; ) 108 array[i] = value; 109 } 110 111 public char[] getArray() { return (char[]) base.getBuffer(); } 112 113 public void delete(int where, int count) 114 { 115 int ipos = createPos(where, false); 116 removePos(ipos, count); 117 releasePos(ipos); 118 } 119 120 public void insert(int where, String str, boolean beforeMarkers) 121 { 122 int len = str.length(); 123 gapReserve(where, len); 124 str.getChars(0, len, string.data, where); 125 gapStart += len; 126 } 127 128 public void consume(int start, int count, Consumer dest) 129 { 130 char[] array = string.data; 131 if (start < gapStart) 132 { 133 int count0 = gapStart - start; 134 if (count0 > count) 135 count0 = count; 136 dest.write(array, start, count0); 137 count -= count0; 138 start += count; 139 } 140 if (count > 0) 141 { 142 start += gapEnd - gapStart; 143 dest.write(array, start, count); 144 } 145 } 146 147 public String toString() 148 { 149 int len = size(); 150 int start = getSegment(0, len); 151 return new String (getArray(), start, len); 152 } 153 154 155 164 170 171 public void writeTo(int start, int count, java.io.Writer dest) 172 throws java.io.IOException 173 { 174 char[] array = string.data; 175 if (start < gapStart) 176 { 177 int count0 = gapStart - start; 178 if (count0 > count) 179 count0 = count; 180 dest.write(array, start, count0); 181 count -= count0; 182 start += count; 183 } 184 if (count > 0) 185 { 186 start += gapEnd - gapStart; 187 dest.write(array, start, count); 188 } 189 } 190 191 public void writeTo(java.io.Writer dest) throws java.io.IOException 192 { 193 char[] array = string.data; 194 dest.write(array, 0, gapStart); 195 dest.write(array, gapEnd, array.length - gapEnd); 196 } 197 198 public void dump() 199 { 200 System.err.println("Buffer Content dump. size:"+size()+" buffer:"+getArray().length); 201 System.err.print("before gap: \""); 202 System.err.print(new String (getArray(), 0, gapStart)); 203 System.err.println("\" (gapStart:"+gapStart+" gapEnd:"+gapEnd+')'); 204 System.err.print("after gap: \""); 205 System.err.print(new String (getArray(), gapEnd, getArray().length-gapEnd)); 206 System.err.println("\""); 207 int poslen = positions == null ? 0 : positions.length; 208 System.err.println("Positions (size: "+poslen+" free:"+free+"):"); 209 boolean[] isFree = null; 210 if (free != -2) 211 { 212 isFree = new boolean[positions.length]; 213 for (int i = free; i >= 0; i = positions[i]) 214 isFree[i] = true; 215 } 216 for (int i = 0; i < poslen; i++) 217 { 218 int pos = positions[i]; 219 if (free == -2 ? pos != FREE_POSITION : ! isFree[i]) 220 System.err.println("position#"+i+": "+(pos>>1)+" isAfter:"+(pos&1)); 221 } 222 } 223 } 224 | Popular Tags |