JAL-1807 test
[jalviewjs.git] / bin / jalview / util / Format.js
1 Clazz.declarePackage ("jalview.util");
2 Clazz.load (null, "jalview.util.Format", ["java.lang.Character", "$.IllegalArgumentException", "$.StringBuffer"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.width = 0;
5 this.precision = 0;
6 this.pre = null;
7 this.post = null;
8 this.leading_zeroes = false;
9 this.show_plus = false;
10 this.alternate = false;
11 this.show_space = false;
12 this.left_align = false;
13 this.fmt = '\0';
14 this.formatString = null;
15 Clazz.instantialize (this, arguments);
16 }, jalview.util, "Format");
17 Clazz.makeConstructor (c$, 
18 function (s) {
19 this.formatString = s;
20 this.width = 0;
21 this.precision = -1;
22 this.pre = "";
23 this.post = "";
24 this.leading_zeroes = false;
25 this.show_plus = false;
26 this.alternate = false;
27 this.show_space = false;
28 this.left_align = false;
29 this.fmt = ' ';
30 var length = s.length;
31 var parse_state = 0;
32 var i = 0;
33 while (parse_state == 0) {
34 if (i >= length) {
35 parse_state = 5;
36 } else if (s.charAt (i) == '%') {
37 if (i < (length - 1)) {
38 if (s.charAt (i + 1) == '%') {
39 this.pre = this.pre + '%';
40 i++;
41 } else {
42 parse_state = 1;
43 }} else {
44 throw  new IllegalArgumentException ();
45 }} else {
46 this.pre = this.pre + s.charAt (i);
47 }i++;
48 }
49 while (parse_state == 1) {
50 if (i >= length) {
51 parse_state = 5;
52 } else if (s.charAt (i) == ' ') {
53 this.show_space = true;
54 } else if (s.charAt (i) == '-') {
55 this.left_align = true;
56 } else if (s.charAt (i) == '+') {
57 this.show_plus = true;
58 } else if (s.charAt (i) == '0') {
59 this.leading_zeroes = true;
60 } else if (s.charAt (i) == '#') {
61 this.alternate = true;
62 } else {
63 parse_state = 2;
64 i--;
65 }i++;
66 }
67 while (parse_state == 2) {
68 if (i >= length) {
69 parse_state = 5;
70 } else if (('0' <= s.charAt (i)) && (s.charAt (i) <= '9')) {
71 this.width = ((this.width * 10) + s.charCodeAt (i)) - 48;
72 i++;
73 } else if (s.charAt (i) == '.') {
74 parse_state = 3;
75 this.precision = 0;
76 i++;
77 } else {
78 parse_state = 4;
79 }}
80 while (parse_state == 3) {
81 if (i >= length) {
82 parse_state = 5;
83 } else if (('0' <= s.charAt (i)) && (s.charAt (i) <= '9')) {
84 this.precision = ((this.precision * 10) + s.charCodeAt (i)) - 48;
85 i++;
86 } else {
87 parse_state = 4;
88 }}
89 if (parse_state == 4) {
90 if (i >= length) {
91 parse_state = 5;
92 } else {
93 this.fmt = s.charAt (i);
94 }i++;
95 }if (i < length) {
96 this.post = s.substring (i, length);
97 }}, "~S");
98 c$.getHexString = Clazz.defineMethod (c$, "getHexString", 
99 function (color) {
100 var r;
101 var g;
102 var b;
103 r = Integer.toHexString (color.getRed ());
104 if (r.length < 2) {
105 r = "0" + r;
106 }g = Integer.toHexString (color.getGreen ());
107 if (g.length < 2) {
108 g = "0" + g;
109 }b = Integer.toHexString (color.getBlue ());
110 if (b.length < 2) {
111 b = "0" + b;
112 }return r + g + b;
113 }, "java.awt.Color");
114 c$.printDouble = Clazz.defineMethod (c$, "printDouble", 
115 function (s, fmt, x) {
116 s.print ( new jalview.util.Format (fmt).formDouble (x));
117 }, "java.io.PrintStream,~S,~N");
118 c$.printLong = Clazz.defineMethod (c$, "printLong", 
119 function (s, fmt, x) {
120 s.print ( new jalview.util.Format (fmt).formLong (x));
121 }, "java.io.PrintStream,~S,~N");
122 c$.printChar = Clazz.defineMethod (c$, "printChar", 
123 function (s, fmt, x) {
124 s.print ( new jalview.util.Format (fmt).formChar (x));
125 }, "java.io.PrintStream,~S,~S");
126 c$.print = Clazz.defineMethod (c$, "print", 
127 function (s, fmt, x) {
128 s.print ( new jalview.util.Format (fmt).form (x));
129 }, "java.io.PrintStream,~S,~S");
130 c$.atoi = Clazz.defineMethod (c$, "atoi", 
131 function (s) {
132 return jalview.util.Format.atol (s);
133 }, "~S");
134 c$.atol = Clazz.defineMethod (c$, "atol", 
135 function (s) {
136 var i = 0;
137 while ((i < s.length) && Character.isWhitespace (s.charAt (i))) {
138 i++;
139 }
140 if ((i < s.length) && (s.charAt (i) == '0')) {
141 if (((i + 1) < s.length) && ((s.charAt (i + 1) == 'x') || (s.charAt (i + 1) == 'X'))) {
142 return jalview.util.Format.parseLong (s.substring (i + 2), 16);
143 } else {
144 return jalview.util.Format.parseLong (s, 8);
145 }} else {
146 return jalview.util.Format.parseLong (s, 10);
147 }}, "~S");
148 c$.parseLong = Clazz.defineMethod (c$, "parseLong", 
149 ($fz = function (s, base) {
150 var i = 0;
151 var sign = 1;
152 var r = 0;
153 while ((i < s.length) && Character.isWhitespace (s.charAt (i))) {
154 i++;
155 }
156 if ((i < s.length) && (s.charAt (i) == '-')) {
157 sign = -1;
158 i++;
159 } else if ((i < s.length) && (s.charAt (i) == '+')) {
160 i++;
161 }while (i < s.length) {
162 var ch = s.charAt (i);
163 if (('0' <= ch) && (ch.charCodeAt (0) < (48 + base))) {
164 r = ((r * base) + ch.charCodeAt (0)) - 48;
165 } else if (('A' <= ch) && (ch.charCodeAt (0) < ((65 + base) - 10))) {
166 r = ((r * base) + ch.charCodeAt (0)) - 65 + 10;
167 } else if (('a' <= ch) && (ch.charCodeAt (0) < ((97 + base) - 10))) {
168 r = ((r * base) + ch.charCodeAt (0)) - 97 + 10;
169 } else {
170 return r * sign;
171 }i++;
172 }
173 return r * sign;
174 }, $fz.isPrivate = true, $fz), "~S,~N");
175 c$.atof = Clazz.defineMethod (c$, "atof", 
176 function (s) {
177 var i = 0;
178 var sign = 1;
179 var r = 0;
180 var p = 1;
181 var state = 0;
182 while ((i < s.length) && Character.isWhitespace (s.charAt (i))) {
183 i++;
184 }
185 if ((i < s.length) && (s.charAt (i) == '-')) {
186 sign = -1;
187 i++;
188 } else if ((i < s.length) && (s.charAt (i) == '+')) {
189 i++;
190 }while (i < s.length) {
191 var ch = s.charAt (i);
192 if (('0' <= ch) && (ch <= '9')) {
193 if (state == 0) {
194 r = ((r * 10) + ch.charCodeAt (0)) - 48;
195 } else if (state == 1) {
196 p = p / 10;
197 r = r + (p * (ch.charCodeAt (0) - 48));
198 }} else if (ch == '.') {
199 if (state == 0) {
200 state = 1;
201 } else {
202 return sign * r;
203 }} else if ((ch == 'e') || (ch == 'E')) {
204 var e = jalview.util.Format.parseLong (s.substring (i + 1), 10);
205 return sign * r * Math.pow (10, e);
206 } else {
207 return sign * r;
208 }i++;
209 }
210 return sign * r;
211 }, "~S");
212 Clazz.defineMethod (c$, "formDouble", 
213 function (x) {
214 var r;
215 if (this.precision < 0) {
216 this.precision = 6;
217 }var s = 1;
218 if (x < 0) {
219 x = -x;
220 s = -1;
221 }if (this.fmt == 'f') {
222 r = this.fixed_format (x);
223 } else if ((this.fmt == 'e') || (this.fmt == 'E') || (this.fmt == 'g') || (this.fmt == 'G')) {
224 r = this.exp_format (x);
225 } else {
226 throw  new IllegalArgumentException ();
227 }return this.pad (this.sign (s, r));
228 }, "~N");
229 Clazz.defineMethod (c$, "formLong", 
230 function (x) {
231 var r;
232 var s = 0;
233 if ((this.fmt == 'd') || (this.fmt == 'i')) {
234 if (x < 0) {
235 r = ("" + x).substring (1);
236 s = -1;
237 } else {
238 r = "" + x;
239 s = 1;
240 }} else if (this.fmt == 'o') {
241 r = jalview.util.Format.convert (x, 3, 7, "01234567");
242 } else if (this.fmt == 'x') {
243 r = jalview.util.Format.convert (x, 4, 15, "0123456789abcdef");
244 } else if (this.fmt == 'X') {
245 r = jalview.util.Format.convert (x, 4, 15, "0123456789ABCDEF");
246 } else {
247 throw  new IllegalArgumentException ();
248 }return this.pad (this.sign (s, r));
249 }, "~N");
250 Clazz.defineMethod (c$, "formChar", 
251 function (c) {
252 if (this.fmt != 'c') {
253 throw  new IllegalArgumentException ();
254 }var r = "" + c;
255 return this.pad (r);
256 }, "~S");
257 Clazz.defineMethod (c$, "form", 
258 function (s) {
259 if (this.fmt != 's') {
260 throw  new IllegalArgumentException ();
261 }if (this.precision >= 0) {
262 s = s.substring (0, this.precision);
263 }return this.pad (s);
264 }, "~S");
265 c$.repeat = Clazz.defineMethod (c$, "repeat", 
266 ($fz = function (c, n) {
267 if (n <= 0) {
268 return "";
269 }var s =  new StringBuffer (n);
270 for (var i = 0; i < n; i++) {
271 s.append (c);
272 }
273 return s.toString ();
274 }, $fz.isPrivate = true, $fz), "~S,~N");
275 c$.convert = Clazz.defineMethod (c$, "convert", 
276 ($fz = function (x, n, m, d) {
277 if (x == 0) {
278 return "0";
279 }var r = "";
280 while (x != 0) {
281 r = d.charAt ((x & m)) + r;
282 x = x >>> n;
283 }
284 return r;
285 }, $fz.isPrivate = true, $fz), "~N,~N,~N,~S");
286 Clazz.defineMethod (c$, "pad", 
287 ($fz = function (r) {
288 var p = jalview.util.Format.repeat (' ', this.width - r.length);
289 if (this.left_align) {
290 return this.pre + r + p + this.post;
291 } else {
292 return this.pre + p + r + this.post;
293 }}, $fz.isPrivate = true, $fz), "~S");
294 Clazz.defineMethod (c$, "sign", 
295 ($fz = function (s, r) {
296 var p = "";
297 if (s < 0) {
298 p = "-";
299 } else if (s > 0) {
300 if (this.show_plus) {
301 p = "+";
302 } else if (this.show_space) {
303 p = " ";
304 }} else {
305 if ((this.fmt == 'o') && this.alternate && (r.length > 0) && (r.charAt (0) != '0')) {
306 p = "0";
307 } else if ((this.fmt == 'x') && this.alternate) {
308 p = "0x";
309 } else if ((this.fmt == 'X') && this.alternate) {
310 p = "0X";
311 }}var w = 0;
312 if (this.leading_zeroes) {
313 w = this.width;
314 } else if (((this.fmt == 'd') || (this.fmt == 'i') || (this.fmt == 'x') || (this.fmt == 'X') || (this.fmt == 'o')) && (this.precision > 0)) {
315 w = this.precision;
316 }return p + jalview.util.Format.repeat ('0', w - p.length - r.length) + r;
317 }, $fz.isPrivate = true, $fz), "~N,~S");
318 Clazz.defineMethod (c$, "fixed_format", 
319 ($fz = function (d) {
320 var removeTrailing = ((this.fmt == 'G') || (this.fmt == 'g')) && !this.alternate;
321 if (d > 0x7FFFFFFFFFFFFFFF) {
322 return this.exp_format (d);
323 }if (this.precision == 0) {
324 return Clazz.doubleToLong (d + 0.5) + (removeTrailing ? "" : ".");
325 }var whole = Clazz.doubleToLong (d);
326 var fr = d - whole;
327 if ((fr >= 1) || (fr < 0)) {
328 return this.exp_format (d);
329 }var factor = 1;
330 var leading_zeroes = "";
331 for (var i = 1; (i <= this.precision) && (factor <= 0x7FFFFFFFFFFFFFFF); i++) {
332 factor *= 10;
333 leading_zeroes = leading_zeroes + "0";
334 }
335 var l = Clazz.doubleToLong ((factor * fr) + 0.5);
336 if (l >= factor) {
337 l = 0;
338 whole++;
339 }var z = leading_zeroes + l;
340 z = "." + z.substring (z.length - this.precision, z.length);
341 if (removeTrailing) {
342 var t = z.length - 1;
343 while ((t >= 0) && (z.charAt (t) == '0')) {
344 t--;
345 }
346 if ((t >= 0) && (z.charAt (t) == '.')) {
347 t--;
348 }z = z.substring (0, t + 1);
349 }return whole + z;
350 }, $fz.isPrivate = true, $fz), "~N");
351 Clazz.defineMethod (c$, "exp_format", 
352 ($fz = function (d) {
353 var f = "";
354 var e = 0;
355 var dd = d;
356 var factor = 1;
357 if (d != 0) {
358 while (dd > 10) {
359 e++;
360 factor /= 10;
361 dd = dd / 10;
362 }
363 while (dd < 1) {
364 e--;
365 factor *= 10;
366 dd = dd * 10;
367 }
368 }if (((this.fmt == 'g') || (this.fmt == 'G')) && (e >= -4) && (e < this.precision)) {
369 return this.fixed_format (d);
370 }d = d * factor;
371 f = f + this.fixed_format (d);
372 if ((this.fmt == 'e') || (this.fmt == 'g')) {
373 f = f + "e";
374 } else {
375 f = f + "E";
376 }var p = "000";
377 if (e >= 0) {
378 f = f + "+";
379 p = p + e;
380 } else {
381 f = f + "-";
382 p = p + (-e);
383 }return f + p.substring (p.length - 3, p.length);
384 }, $fz.isPrivate = true, $fz), "~N");
385 Clazz.overrideMethod (c$, "toString", 
386 function () {
387 return this.formatString;
388 });
389 });