1 Clazz.declarePackage ("jalview.util");
2 Clazz.load (null, "jalview.util.Format", ["java.lang.Character", "$.IllegalArgumentException", "$.StringBuffer"], function () {
3 c$ = Clazz.decorateAsClass (function () {
8 this.leading_zeroes = false;
9 this.show_plus = false;
10 this.alternate = false;
11 this.show_space = false;
12 this.left_align = false;
14 this.formatString = null;
15 Clazz.instantialize (this, arguments);
16 }, jalview.util, "Format");
17 Clazz.makeConstructor (c$,
19 this.formatString = s;
24 this.leading_zeroes = false;
25 this.show_plus = false;
26 this.alternate = false;
27 this.show_space = false;
28 this.left_align = false;
30 var length = s.length;
33 while (parse_state == 0) {
36 } else if (s.charAt (i) == '%') {
37 if (i < (length - 1)) {
38 if (s.charAt (i + 1) == '%') {
39 this.pre = this.pre + '%';
44 throw new IllegalArgumentException ();
46 this.pre = this.pre + s.charAt (i);
49 while (parse_state == 1) {
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;
67 while (parse_state == 2) {
70 } else if (('0' <= s.charAt (i)) && (s.charAt (i) <= '9')) {
71 this.width = ((this.width * 10) + s.charCodeAt (i)) - 48;
73 } else if (s.charAt (i) == '.') {
80 while (parse_state == 3) {
83 } else if (('0' <= s.charAt (i)) && (s.charAt (i) <= '9')) {
84 this.precision = ((this.precision * 10) + s.charCodeAt (i)) - 48;
89 if (parse_state == 4) {
93 this.fmt = s.charAt (i);
96 this.post = s.substring (i, length);
98 c$.getHexString = Clazz.defineMethod (c$, "getHexString",
103 r = Integer.toHexString (color.getRed ());
106 }g = Integer.toHexString (color.getGreen ());
109 }b = Integer.toHexString (color.getBlue ());
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",
132 return jalview.util.Format.atol (s);
134 c$.atol = Clazz.defineMethod (c$, "atol",
137 while ((i < s.length) && Character.isWhitespace (s.charAt (i))) {
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);
144 return jalview.util.Format.parseLong (s, 8);
146 return jalview.util.Format.parseLong (s, 10);
148 c$.parseLong = Clazz.defineMethod (c$, "parseLong",
149 ($fz = function (s, base) {
153 while ((i < s.length) && Character.isWhitespace (s.charAt (i))) {
156 if ((i < s.length) && (s.charAt (i) == '-')) {
159 } else if ((i < s.length) && (s.charAt (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;
174 }, $fz.isPrivate = true, $fz), "~S,~N");
175 c$.atof = Clazz.defineMethod (c$, "atof",
182 while ((i < s.length) && Character.isWhitespace (s.charAt (i))) {
185 if ((i < s.length) && (s.charAt (i) == '-')) {
188 } else if ((i < s.length) && (s.charAt (i) == '+')) {
190 }while (i < s.length) {
191 var ch = s.charAt (i);
192 if (('0' <= ch) && (ch <= '9')) {
194 r = ((r * 10) + ch.charCodeAt (0)) - 48;
195 } else if (state == 1) {
197 r = r + (p * (ch.charCodeAt (0) - 48));
198 }} else if (ch == '.') {
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);
212 Clazz.defineMethod (c$, "formDouble",
215 if (this.precision < 0) {
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);
226 throw new IllegalArgumentException ();
227 }return this.pad (this.sign (s, r));
229 Clazz.defineMethod (c$, "formLong",
233 if ((this.fmt == 'd') || (this.fmt == 'i')) {
235 r = ("" + x).substring (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");
247 throw new IllegalArgumentException ();
248 }return this.pad (this.sign (s, r));
250 Clazz.defineMethod (c$, "formChar",
252 if (this.fmt != 'c') {
253 throw new IllegalArgumentException ();
257 Clazz.defineMethod (c$, "form",
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);
265 c$.repeat = Clazz.defineMethod (c$, "repeat",
266 ($fz = function (c, n) {
269 }var s = new StringBuffer (n);
270 for (var i = 0; i < n; i++) {
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) {
281 r = d.charAt ((x & m)) + 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;
292 return this.pre + p + r + this.post;
293 }}, $fz.isPrivate = true, $fz), "~S");
294 Clazz.defineMethod (c$, "sign",
295 ($fz = function (s, r) {
300 if (this.show_plus) {
302 } else if (this.show_space) {
305 if ((this.fmt == 'o') && this.alternate && (r.length > 0) && (r.charAt (0) != '0')) {
307 } else if ((this.fmt == 'x') && this.alternate) {
309 } else if ((this.fmt == 'X') && this.alternate) {
312 if (this.leading_zeroes) {
314 } else if (((this.fmt == 'd') || (this.fmt == 'i') || (this.fmt == 'x') || (this.fmt == 'X') || (this.fmt == 'o')) && (this.precision > 0)) {
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);
327 if ((fr >= 1) || (fr < 0)) {
328 return this.exp_format (d);
330 var leading_zeroes = "";
331 for (var i = 1; (i <= this.precision) && (factor <= 0x7FFFFFFFFFFFFFFF); i++) {
333 leading_zeroes = leading_zeroes + "0";
335 var l = Clazz.doubleToLong ((factor * fr) + 0.5);
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')) {
346 if ((t >= 0) && (z.charAt (t) == '.')) {
348 }z = z.substring (0, t + 1);
350 }, $fz.isPrivate = true, $fz), "~N");
351 Clazz.defineMethod (c$, "exp_format",
352 ($fz = function (d) {
368 }if (((this.fmt == 'g') || (this.fmt == 'G')) && (e >= -4) && (e < this.precision)) {
369 return this.fixed_format (d);
371 f = f + this.fixed_format (d);
372 if ((this.fmt == 'e') || (this.fmt == 'g')) {
383 }return f + p.substring (p.length - 3, p.length);
384 }, $fz.isPrivate = true, $fz), "~N");
385 Clazz.overrideMethod (c$, "toString",
387 return this.formatString;