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