4 import java.nio.charset.Charset;
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
17 private java.lang.StringBuilder sb;
18 String s; // used by JavaScript only; no Java references
20 //TODO: JS experiment with using array and .push() here
30 sb = new java.lang.StringBuilder();
34 public static SB newN(int n) {
37 * return new javajs.util.SB();
40 // not perfect, because it requires defining sb twice.
41 // We can do better...
43 sb.sb = new java.lang.StringBuilder(n);
48 public static SB newS(String s) {
52 * var sb = new javajs.util.SB();
59 sb.sb = new java.lang.StringBuilder(s);
64 public SB append(String s) {
77 public SB appendC(char c) {
90 public SB appendI(int i) {
103 public SB appendB(boolean b) {
117 * note that JavaScript could drop off the ".0" in "1.0"
121 public SB appendF(float f) {
126 * if (sf.indexOf(".") < 0 && sf.indexOf("e") < 0)
137 public SB appendD(double d) {
142 * if (sf.indexOf(".") < 0 && sf.indexOf("e") < 0)
153 public SB appendSB(SB buf) {
166 public SB appendO(Object data) {
171 * this.s += data.toString();
181 public void appendCB(char[] cb, int off, int len) {
185 * this.s += cb.slice(off,off+len).join("");
189 sb.append(cb, off, len);
194 public String toString() {
202 return sb.toString();
206 public int length() {
210 * return this.s.length;
218 public int indexOf(String s) {
222 * return this.s.indexOf(s);
226 return sb.indexOf(s);
230 public char charAt(int i) {
234 * return this.s.charAt(i);
242 public int charCodeAt(int i) {
246 * return this.s.charCodeAt(i);
250 return sb.codePointAt(i);
254 public void setLength(int n) {
258 * this.s = this.s.substring(0, n);
265 public int lastIndexOf(String s) {
269 * return this.s.lastIndexOf(s);
272 return sb.lastIndexOf(s);
276 public int indexOf2(String s, int i) {
280 * return this.s.indexOf(s, i);
283 return sb.indexOf(s, i);
287 public String substring(int i) {
291 * return this.s.substring(i);
294 return sb.substring(i);
298 public String substring2(int i, int j) {
302 * return this.s.substring(i, j);
305 return sb.substring(i, j);
310 * simple byte conversion properly implementing UTF-8. * Used for base64
311 * conversion and allows for offset
315 * or -1 for full length (then off must = 0)
318 public byte[] toBytes(int off, int len) {
324 * just a string in JavaScript
332 cs = Charset.forName("UTF-8");
334 return (len > 0 ? substring2(off, off + len)
335 : off == 0 ? toString()
336 : substring2(off, length() - off)).getBytes(cs);
339 public void replace(int start, int end, String str) {
343 * this.s = this.s.substring(0, start) + str + this.s.substring(end);
346 sb.replace(start, end, str);
350 public void insert(int offset, String str) {
351 replace(offset, offset, str);