4 import javajs.J2SIgnoreImport;
7 * Interesting thing here is that JavaScript is 3x faster than Java in handling strings.
9 * Java StringBuilder is final, unfortunately. I guess they weren't thinking about Java2Script!
11 * The reason we have to do this that several overloaded append methods is WAY too expensive
15 @J2SIgnoreImport({java.lang.StringBuilder.class})
18 private java.lang.StringBuilder sb;
19 String s; // used by JavaScript only; no Java references
21 //TODO: JS experiment with using array and .push() here
31 sb = new java.lang.StringBuilder();
35 public static SB newN(int n) {
38 * return new javajs.util.SB();
41 // not perfect, because it requires defining sb twice.
42 // We can do better...
44 sb.sb = new java.lang.StringBuilder(n);
49 public static SB newS(String s) {
53 * var sb = new javajs.util.SB();
60 sb.sb = new java.lang.StringBuilder(s);
65 public SB append(String s) {
78 public SB appendC(char c) {
91 public SB appendI(int i) {
104 public SB appendB(boolean b) {
118 * note that JavaScript could drop off the ".0" in "1.0"
122 public SB appendF(float f) {
127 * if (sf.indexOf(".") < 0 && sf.indexOf("e") < 0)
138 public SB appendD(double d) {
143 * if (sf.indexOf(".") < 0 && sf.indexOf("e") < 0)
154 public SB appendSB(SB buf) {
167 public SB appendO(Object data) {
172 * this.s += data.toString();
182 public void appendCB(char[] cb, int off, int len) {
186 * for (var i = len,j=off; --i >= 0;)
191 sb.append(cb, off, len);
196 public String toString() {
204 return sb.toString();
208 public int length() {
212 * return this.s.length;
220 public int indexOf(String s) {
224 * return this.s.indexOf(s);
228 return sb.indexOf(s);
232 public char charAt(int i) {
236 * return this.s.charAt(i);
244 public int charCodeAt(int i) {
248 * return this.s.charCodeAt(i);
252 return sb.codePointAt(i);
256 public void setLength(int n) {
260 * this.s = this.s.substring(0, n);
267 public int lastIndexOf(String s) {
271 * return this.s.lastIndexOf(s);
274 return sb.lastIndexOf(s);
278 public int indexOf2(String s, int i) {
282 * return this.s.indexOf(s, i);
285 return sb.indexOf(s, i);
289 public String substring(int i) {
293 * return this.s.substring(i);
296 return sb.substring(i);
300 public String substring2(int i, int j) {
304 * return this.s.substring(i, j);
307 return sb.substring(i, j);
312 * simple byte conversion not allowing for unicode.
313 * Used for base64 conversion and allows for offset
315 * @param len or -1 for full length (then off must = 0)
318 public byte[] toBytes(int off, int len) {
320 len = length() - off;
321 byte[] b = new byte[len];
322 for (int i = off + len, j = i - off; --i >= off;)
323 b[--j] = (byte) charAt(i);
327 public void replace(int start, int end, String str) {
331 * this.s = this.s.substring(0, start) + str + this.s.substring(end);
334 sb.replace(start, end, str);
338 public void insert(int offset, String str) {
339 replace(offset, offset, str);