1 21 22 package net.percederberg.grammatica.parser.re; 23 24 35 public class CharBuffer { 36 37 40 private int length = 0; 41 42 45 private char[] contents = null; 46 47 50 public CharBuffer() { 51 this(16); 52 } 53 54 59 public CharBuffer(int initialSize) { 60 contents = new char[initialSize]; 61 } 62 63 68 public CharBuffer(String str) { 69 length = str.length(); 70 contents = str.toCharArray(); 71 } 72 73 79 public CharBuffer(StringBuffer str) { 80 length = str.length(); 81 contents = new char[length]; 82 str.getChars(0, length, contents, 0); 83 } 84 85 93 public CharBuffer append(boolean b) { 94 return append(String.valueOf(b)); 95 } 96 97 104 public CharBuffer append(char c) { 105 ensureCapacity(length + 1); 106 contents[length++] = c; 107 return this; 108 } 109 110 117 public CharBuffer append(char[] str) { 118 return append(str, 0, str.length); 119 } 120 121 130 public CharBuffer append(char[] str, int offset, int length) { 131 ensureCapacity(this.length + length); 132 System.arraycopy(str, offset, contents, this.length, length); 133 this.length += length; 134 return this; 135 } 136 137 145 public CharBuffer append (double d) { 146 return append(String.valueOf(d)); 147 } 148 149 157 public CharBuffer append(float f) { 158 return append(String.valueOf(f)); 159 } 160 161 169 public CharBuffer append(int i) { 170 return append(String.valueOf(i)); 171 } 172 173 181 public CharBuffer append(long l) { 182 return append(String.valueOf(l)); 183 } 184 185 193 public CharBuffer append(Object obj) { 194 return append(obj.toString()); 195 } 196 197 204 public CharBuffer append(String str) { 205 ensureCapacity(length + str.length()); 206 str.getChars(0, str.length(), contents, length); 207 length += str.length(); 208 return this; 209 } 210 211 218 public CharBuffer append(StringBuffer str) { 219 ensureCapacity(length + str.length()); 220 str.getChars(0, str.length(), contents, length); 221 length += str.length(); 222 return this; 223 } 224 225 236 public char charAt(int index) throws StringIndexOutOfBoundsException { 237 if (index < 0 || index >= length) { 238 throw new StringIndexOutOfBoundsException (index); 239 } 240 return contents[index]; 241 } 242 243 254 public CharBuffer delete(int start, int end) 255 throws StringIndexOutOfBoundsException { 256 257 if (start < 0) { 258 throw new StringIndexOutOfBoundsException (start); 259 } 260 if (end > length) { 261 end = length; 262 } 263 if (start > end) { 264 throw new StringIndexOutOfBoundsException (); 265 } 266 if (end - start > 0) { 267 System.arraycopy(contents, end, contents, start, length - end); 268 length -= (end - start); 269 } 270 return this; 271 } 272 273 278 public void ensureCapacity(int size) { 279 char[] newContents; 280 281 if (contents.length >= size) { 282 return; 283 } 284 if (size < 2 * contents.length + 2) { 285 size = 2 * contents.length + 2; 286 } 287 newContents = new char[size]; 288 System.arraycopy(contents, 0, newContents, 0, length); 289 contents = newContents; 290 } 291 292 297 public int length() { 298 return length; 299 } 300 301 312 public String substring(int start) 313 throws StringIndexOutOfBoundsException { 314 315 return substring(start, length); 316 } 317 318 330 public String substring(int start, int end) 331 throws StringIndexOutOfBoundsException { 332 333 if (start < 0) { 334 throw new StringIndexOutOfBoundsException (start); 335 } 336 if (end > length) { 337 throw new StringIndexOutOfBoundsException (end); 338 } 339 if (start > end) { 340 throw new StringIndexOutOfBoundsException (); 341 } 342 return new String (contents, start, end - start); 343 } 344 345 350 public String toString() { 351 return new String (contents, 0, length); 352 } 353 } 354 | Popular Tags |