4 import java.nio.charset.Charset;
6 import javajs.J2SIgnoreImport;
9 * Interesting thing here is that JavaScript is 3x faster than Java in handling strings.
11 * Java StringBuilder is final, unfortunately. I guess they weren't thinking about Java2Script!
13 * The reason we have to do this that several overloaded append methods is WAY too expensive
17 @J2SIgnoreImport({java.lang.StringBuilder.class, java.nio.charset.Charset.class})
20 private java.lang.StringBuilder sb;
21 String s; // used by JavaScript only; no Java references
23 //TODO: JS experiment with using array and .push() here
33 sb = new java.lang.StringBuilder();
37 public static SB newN(int n) {
40 * return new javajs.util.SB();
43 // not perfect, because it requires defining sb twice.
44 // We can do better...
46 sb.sb = new java.lang.StringBuilder(n);
51 public static SB newS(String s) {
55 * var sb = new javajs.util.SB();
62 sb.sb = new java.lang.StringBuilder(s);
67 public SB append(String s) {
80 public SB appendC(char c) {
93 public SB appendI(int i) {
106 public SB appendB(boolean b) {
120 * note that JavaScript could drop off the ".0" in "1.0"
124 public SB appendF(float f) {
129 * if (sf.indexOf(".") < 0 && sf.indexOf("e") < 0)
140 public SB appendD(double d) {
145 * if (sf.indexOf(".") < 0 && sf.indexOf("e") < 0)
156 public SB appendSB(SB buf) {
169 public SB appendO(Object data) {
174 * this.s += data.toString();
184 public void appendCB(char[] cb, int off, int len) {
188 * this.s += cb.slice(off,off+len).join("");
192 sb.append(cb, off, len);
197 public String toString() {
205 return sb.toString();
209 public int length() {
213 * return this.s.length;
221 public int indexOf(String s) {
225 * return this.s.indexOf(s);
229 return sb.indexOf(s);
233 public char charAt(int i) {
237 * return this.s.charAt(i);
245 public int charCodeAt(int i) {
249 * return this.s.charCodeAt(i);
253 return sb.codePointAt(i);
257 public void setLength(int n) {
261 * this.s = this.s.substring(0, n);
268 public int lastIndexOf(String s) {
272 * return this.s.lastIndexOf(s);
275 return sb.lastIndexOf(s);
279 public int indexOf2(String s, int i) {
283 * return this.s.indexOf(s, i);
286 return sb.indexOf(s, i);
290 public String substring(int i) {
294 * return this.s.substring(i);
297 return sb.substring(i);
301 public String substring2(int i, int j) {
305 * return this.s.substring(i, j);
308 return sb.substring(i, j);
313 * simple byte conversion properly implementing UTF-8.
314 * Used for base64 conversion and allows for offset
318 * or -1 for full length (then off must = 0)
321 public byte[] toBytes(int off, int len) {
327 * just a string in JavaScript
335 cs = Charset.forName("UTF-8");
337 return (len > 0 ? substring2(off, off + len)
338 : off == 0 ? toString()
339 : substring2(off, length() - off)).getBytes(cs);
342 public void replace(int start, int end, String str) {
346 * this.s = this.s.substring(0, start) + str + this.s.substring(end);
349 sb.replace(start, end, str);
353 public void insert(int offset, String str) {
354 replace(offset, offset, str);