JAL-3026 adding javajs package
[jalview.git] / src / javajs / util / SB.java
1
2 package javajs.util;
3
4 import java.nio.charset.Charset;
5
6 import javajs.J2SIgnoreImport;
7
8 /**
9  * Interesting thing here is that JavaScript is 3x faster than Java in handling strings.
10  * 
11  * Java StringBuilder is final, unfortunately. I guess they weren't thinking about Java2Script!
12  * 
13  * The reason we have to do this that several overloaded append methods is WAY too expensive
14  * 
15  */
16
17 @J2SIgnoreImport({java.lang.StringBuilder.class, java.nio.charset.Charset.class})
18 public class SB {
19   
20   private java.lang.StringBuilder sb;
21   String s; // used by JavaScript only; no Java references
22   
23   //TODO: JS experiment with using array and .push() here
24
25   public SB() {
26     /**
27      * @j2sNative
28      * 
29      *            this.s = "";
30      * 
31      */
32     {
33       sb = new java.lang.StringBuilder();
34     }
35   }
36
37   public static SB newN(int n) {
38     /**
39      * @j2sNative
40      *            return new javajs.util.SB(); 
41      */
42     {
43       // not perfect, because it requires defining sb twice. 
44       // We can do better...
45       SB sb = new SB();
46       sb.sb = new java.lang.StringBuilder(n);
47       return sb;
48     }
49   }
50
51   public static SB newS(String s) {
52     /**
53      * @j2sNative 
54      * 
55      * var sb = new javajs.util.SB();
56      * sb.s = s;
57      * return sb; 
58      * 
59      */
60     {
61     SB sb = new SB();
62     sb.sb = new java.lang.StringBuilder(s);
63     return sb;
64     }
65   }
66
67   public SB append(String s) {
68     /**
69      * @j2sNative
70      * 
71      *            this.s += s
72      * 
73      */
74     {
75       sb.append(s);
76     }
77     return this;
78   }
79   
80   public SB appendC(char c) {
81     /**
82      * @j2sNative
83      * 
84      *            this.s += c;
85      */
86     {
87       sb.append(c);
88     }
89     return this;
90     
91   }
92
93   public SB appendI(int i) {
94     /**
95      * @j2sNative
96      * 
97      *            this.s += i
98      * 
99      */
100     {
101       sb.append(i);
102     }
103     return this;
104   }
105
106   public SB appendB(boolean b) {
107     /**
108      * @j2sNative
109      * 
110      *            this.s += b
111      * 
112      */
113     {
114       sb.append(b);
115     }
116     return this;
117   }
118
119   /**
120    * note that JavaScript could drop off the ".0" in "1.0"
121    * @param f
122    * @return this
123    */
124   public SB appendF(float f) {
125     /**
126      * @j2sNative
127      * 
128      * var sf = "" + f;
129      * if (sf.indexOf(".") < 0 && sf.indexOf("e") < 0)
130      *   sf += ".0" ;
131      *            this.s += sf;
132      * 
133      */
134     {
135       sb.append(f);
136     }
137     return this;
138   }
139
140   public SB appendD(double d) {
141     /**
142      * @j2sNative
143      * 
144      * var sf = "" + d;
145      * if (sf.indexOf(".") < 0 && sf.indexOf("e") < 0)
146      *   sf += ".0" ;
147      *            this.s += sf;
148      * 
149      */
150     {
151       sb.append(d);
152     }
153     return this;
154   }
155
156   public SB appendSB(SB buf) {
157     /**
158      * @j2sNative
159      * 
160      *            this.s += buf.s;
161      * 
162      */
163     {
164       sb.append(buf.sb);
165     }
166     return this;
167   }
168
169   public SB appendO(Object data) {
170     if (data != null) {
171       /**
172        * @j2sNative
173        * 
174        *            this.s += data.toString();
175        * 
176        */
177       {
178         sb.append(data);
179       }
180     }
181     return this;
182   }
183
184   public void appendCB(char[] cb, int off, int len) {
185     /**
186      * @j2sNative
187      * 
188      * this.s += cb.slice(off,off+len).join("");
189      * 
190      */
191     {
192        sb.append(cb, off, len);
193     }
194   }
195
196   @Override
197   public String toString() {
198     /**
199      * @j2sNative
200      * 
201      *            return this.s;
202      * 
203      */
204     {
205       return sb.toString();
206     }
207   }
208
209   public int length() {
210     /**
211      * @j2sNative
212      * 
213      *            return this.s.length;
214      * 
215      */
216     {
217       return sb.length();
218     }
219   }
220
221   public int indexOf(String s) {
222     /**
223      * @j2sNative
224      * 
225      *            return this.s.indexOf(s);
226      * 
227      */
228     {
229       return sb.indexOf(s);
230     }
231   }
232
233   public char charAt(int i) {
234     /**
235      * @j2sNative
236      * 
237      *            return this.s.charAt(i);
238      * 
239      */
240     {
241       return sb.charAt(i);
242     }
243   }
244
245   public int charCodeAt(int i) {
246     /**
247      * @j2sNative
248      * 
249      *            return this.s.charCodeAt(i);
250      * 
251      */
252     {
253       return sb.codePointAt(i);
254     }
255   }
256
257   public void setLength(int n) {
258     /**
259      * @j2sNative
260      * 
261      *            this.s = this.s.substring(0, n);
262      */
263     {
264       sb.setLength(n);
265     }
266   }
267
268   public int lastIndexOf(String s) {
269     /**
270      * @j2sNative
271      * 
272      *            return this.s.lastIndexOf(s);
273      */
274     {
275       return sb.lastIndexOf(s);
276     }
277   }
278
279   public int indexOf2(String s, int i) {
280     /**
281      * @j2sNative
282      * 
283      *            return this.s.indexOf(s, i);
284      */
285     {
286       return sb.indexOf(s, i);
287     }
288   }
289
290   public String substring(int i) {
291     /**
292      * @j2sNative
293      * 
294      *            return this.s.substring(i);
295      */
296     {
297       return sb.substring(i);
298     }
299   }
300
301   public String substring2(int i, int j) {
302     /**
303      * @j2sNative
304      * 
305      *            return this.s.substring(i, j);
306      */
307     {
308       return sb.substring(i, j);
309     }
310   }
311
312   /**
313    * simple byte conversion properly implementing UTF-8. 
314    * Used for base64 conversion and allows for offset
315    * 
316    * @param off
317    * @param len
318    *        or -1 for full length (then off must = 0)
319    * @return byte[]
320    */
321   public byte[] toBytes(int off, int len) {
322     if (len == 0)
323       return new byte[0];
324     Charset cs;
325     /**
326      * 
327      * just a string in JavaScript
328      * 
329      * @j2sNative
330      * 
331      *            cs = "UTF-8";
332      * 
333      */
334     {
335       cs = Charset.forName("UTF-8");
336     }
337     return (len > 0 ? substring2(off, off + len) 
338         : off == 0 ? toString()
339         : substring2(off, length() - off)).getBytes(cs);
340   }
341
342         public void replace(int start, int end, String str) {
343                 /**
344                  * @j2sNative
345                  * 
346                  * this.s = this.s.substring(0, start) + str + this.s.substring(end);
347                  */
348                 {
349                         sb.replace(start, end, str);
350                 }
351         }
352
353         public void insert(int offset, String str) {
354                 replace(offset, offset, str);
355         }
356
357 }