1 19 package org.netbeans.modules.java.source.pretty; 20 21 import java.io.Writer ; 22 import java.io.IOException ; 23 24 public final class CharBuffer { 25 char[] chars; 26 int used; 27 int col; 28 int maxcol; 29 static final int UNLIMITED = 999999; 30 int rightMargin = 72; 31 int leftMargin = 0; 32 int hardRightMargin = UNLIMITED; 33 public final int length() { return used; } 34 public void setLength(int l) { if (l < used) used = l < 0 ? 0 : l; } 35 public CharBuffer() { chars = new char[10]; } 36 public CharBuffer(int rm) { 37 this(); 38 rightMargin = rm; 39 } 40 public final boolean hasMargin() { 41 return hardRightMargin != UNLIMITED; 42 } 43 public int harden() { 44 int ret = hardRightMargin; 45 hardRightMargin = rightMargin; 46 return ret; 47 } 48 public void restore(int n) { 49 hardRightMargin = n; 50 } 51 private final void needRoom(int n) { 52 if (chars.length <= used+n) { 53 char[] nc = new char[(used+n)*2]; 54 System.arraycopy(chars, 0, nc, 0, used); 55 chars = nc; 56 } 57 } 58 private static RuntimeException err = new IndexOutOfBoundsException (); 59 private void columnOverflowCheck() { 60 if ((col > hardRightMargin || maxcol > hardRightMargin) && 61 hardRightMargin != UNLIMITED) throw err; 62 } 63 void toCol(int n) { 64 while (col < n) append(' '); 66 } 67 void toLeftMargin() { 68 toCol(leftMargin); 69 } 70 void toColExactly(int n) { 71 if (n < 0) n = 0; 72 if (n < col) nlTerm(); 73 toCol(n); 74 } 75 void notRightOf(int n) { 76 if (n > col) needSpace(); 77 else toColExactly(n); 78 } 79 void ctlChar() { 80 switch (chars[used-1]) { 81 case '\n': 82 if (hardRightMargin != UNLIMITED) throw err; 83 if (col > maxcol) maxcol = col; 84 col = 0; 85 break; 86 case '\t': 87 col = col+8 & ~(7); 88 break; 89 case '\b': 90 if (col > maxcol) maxcol = col; 91 col--; 92 break; 93 } 94 columnOverflowCheck(); 95 } 96 private void append0(char c) { 97 chars[used++] = c; 98 if (c < ' ') ctlChar(); 99 else if (++col > hardRightMargin) columnOverflowCheck(); 100 } 101 public final void append(char c) { needRoom(1); append0(c); } 102 public final void append(char[] b) { 103 if (b != null) append(b, 0, b.length); 104 } 105 public final void append(char[] b, int off, int len) { 106 if (b != null) { 107 needRoom(len); 108 while (--len >= 0) append0(b[off++]); 109 } 110 } 111 public void append(String s) { 112 int len = s.length(); 113 needRoom(len); 114 for (int i = 0; i < len; i++) append0(s.charAt(i)); 115 } 116 public void append(CharBuffer cb) { append(cb.chars, 0, cb.used); } 117 public void appendUtf8(byte[] src, int i, int len) { 118 int limit = i + len; 119 while (i < limit) { 120 int b = src[i++] & 0xFF; 121 if (b >= 0xE0) { 122 b = (b & 0x0F) << 12; 123 b = b | (src[i++] & 0x3F) << 6; 124 b = b | (src[i++] & 0x3F); 125 } else if (b >= 0xC0) { 126 b = (b & 0x1F) << 6; 127 b = b | (src[i++] & 0x3F); 128 } 129 append((char) b); 130 } 131 } 132 public char[] toCharArray() { 133 char[] nm = new char[used]; 134 System.arraycopy(chars, 0, nm, 0, used); 135 return nm; 136 } 137 public void copyClear(CharBuffer cb) { 138 char[] t = chars; 139 chars = cb.chars; 140 used = cb.used; 141 cb.chars = t; 142 cb.used = 0; 143 } 144 public void appendClear(CharBuffer cb) { 145 if (used == 0) copyClear(cb); 146 else { append(cb); cb.used = 0; } 147 } 148 public void clear() { used = 0; col = 0; maxcol = 0; } 149 public int width() { return col > maxcol ? col : maxcol; } 150 public String toString() { return new String (chars, 0, used); } 151 public void writeTo(Writer w) throws IOException { 152 w.write(chars, 0, used); 153 } 154 public String substring(int off, int end) { 155 return new String (chars, off, end-off); 156 } 157 public void to(Writer w) 158 throws IOException { to(w, 0, used); } 159 public void to(Writer w, int st, int len) 160 throws IOException { w.write(chars, st, len); } 161 public boolean equals(Object o) { 162 if (o instanceof String ) { 163 String s = (String )o; 164 if (s.length() != used) return false; 165 for (int i = used; --i >= 0; ) 166 if (chars[i] != s.charAt(i)) return false; 167 return true; 168 } 169 return o == this; 170 } 171 public boolean equals(char[] o) { 172 if (o.length != used) return false; 173 for (int i = used; --i >= 0; ) 174 if (chars[i] != o[i]) return false; 175 return true; 176 } 177 public void trim() { 178 int st = 0; 179 int end = used; 180 while (st < end && chars[st] <= ' ') st++; 181 while (st < end && chars[end-1] <= ' ') end--; 182 if (st >= end) used = 0; 183 else { 184 used = end-st; 185 if (st > 0) System.arraycopy(chars, st, chars, 0, used); 186 } 187 } 188 public boolean endsWith(String s) { 189 int len = s.length(); 190 if (len > used) return false; 191 for (int i = used; --len >= 0; --i) 192 if (chars[i] != s.charAt(len)) return false; 193 return true; 194 } 195 public boolean startsWith(String s) { 196 int len = s.length(); 197 if (len > used) return false; 198 while (--len >= 0) 199 if (chars[len] != s.charAt(len)) return false; 200 return true; 201 } 202 public void needSpace() { 203 int t = used; 204 if(t>0 && chars[t-1]>' ') 205 append(' '); 206 } 207 public void nlTerm() { 208 if(hasMargin()) 209 needSpace(); 210 else { 211 int t = used; 212 if (t <= 0) return; 213 while (t > 0 && chars[t-1] <= ' ') t--; 214 used = t; 215 append('\n'); 216 } 217 } 218 public void blankline() { 219 if(hasMargin()) 220 needSpace(); 221 else { 222 nlTerm(); 223 append('\n'); 224 } 225 } 226 } 227 | Popular Tags |