1 package com.opensymphony.module.sitemesh.html.util; 2 3 11 public class CharArray 12 { 13 int size = 0; 14 char[] buffer; 15 16 int subStrStart = 0; 21 int subStrLen = 0; 22 23 28 public CharArray(int size) 29 { 30 buffer = new char[size]; 31 } 32 33 36 public String toString() 37 { 38 return new String (buffer, 0, size); 39 } 40 41 47 public char charAt(int pos) 48 { 49 return buffer[pos]; 50 } 51 52 64 public void setLength(int newSize) 65 { 66 if (newSize < 0) 67 { 68 newSize = 0; 69 } 70 71 if (newSize <= size) 72 { 73 size = newSize; 74 } 75 else 76 { 77 if (newSize >= buffer.length) 78 grow(newSize); 79 for (; size < newSize; size++) 81 buffer[size] = '\0'; 82 } 83 } 84 85 88 public int length() 89 { 90 return size; 91 } 92 93 98 public CharArray append(CharArray chars) 99 { 100 return append(chars.buffer, 0, chars.size); 101 } 102 103 106 public CharArray append(char[] chars) 107 { 108 return append(chars, 0, chars.length); 109 } 110 111 public CharArray append(char[] chars, int position, int length) 112 { 113 int requiredSize = length + size; 114 if (requiredSize >= buffer.length) 115 grow(requiredSize); 116 System.arraycopy(chars, position, buffer, size, length); 117 size = requiredSize; 118 return this; 119 } 120 121 124 public CharArray append(char c) 125 { 126 if (buffer.length == size) 127 grow(0); 128 buffer[size++] = c; 129 return this; 130 } 131 132 137 public CharArray append(String str) 138 { 139 int requiredSize = str.length() + size; 140 if (requiredSize >= buffer.length) 141 grow(requiredSize); 142 143 for (int i = 0; i < str.length(); i++) 144 buffer[size + i] = str.charAt(i); 145 146 size = requiredSize; 147 return this; 148 } 149 150 155 public String substring(int begin, int end) 156 { 157 return new String (buffer, begin, end - begin); 158 } 159 160 168 public void setSubstr(int begin, int end) 169 { 170 subStrStart = begin; 171 subStrLen = end - begin; 172 } 173 174 177 public String getLowerSubstr() 178 { 179 for (int i = subStrStart; i < subStrStart + subStrLen; i++) 180 buffer[i] |= 32; 181 return new String (buffer, subStrStart, subStrLen); 182 } 183 184 190 public boolean compareLowerSubstr(String lowerStr) 191 { 192 if (lowerStr.length() != subStrLen || subStrLen <= 0) 194 return false; 195 196 for (int i = 0; i < lowerStr.length(); i++) { 197 if ((buffer[subStrStart + i] | 32) != lowerStr.charAt(i)) 199 return false; 200 } 201 return true; 202 } 203 204 211 public int substrHashCode() 212 { 213 int hash = 0; 214 int offset = subStrStart; 215 for (int i = 0; i < subStrLen; i++) { 216 hash = 31 * hash + (buffer[offset++] | 32); 217 } 218 return hash; 219 } 220 221 235 public boolean compareLower(String lowerStr, int offset) 236 { 237 if (offset < 0 || offset + lowerStr.length() > size) 239 return false; 240 241 for (int i = 0; i < lowerStr.length(); i++) 242 { 243 if ((buffer[offset + i] | 32) != lowerStr.charAt(i)) 245 return false; 246 } 247 return true; 248 } 249 250 254 private final void grow(int minSize) 255 { 256 int newCapacity = (buffer.length + 1) * 2; 257 if (newCapacity < 0) 258 { 259 newCapacity = Integer.MAX_VALUE; 260 } 261 else if (minSize > newCapacity) 262 { 263 newCapacity = minSize; 264 } 265 char newBuffer[] = new char[newCapacity]; 266 System.arraycopy(buffer, 0, newBuffer, 0, size); 267 buffer = newBuffer; 268 } 269 270 273 public final void clear() 274 { 275 size = 0; 276 } 277 278 } 279 | Popular Tags |