2 * This file is part of the Vamsas Client version 0.2.
\r
3 * Copyright 2010 by Jim Procter, Iain Milne, Pierre Marguerite,
\r
4 * Andrew Waterhouse and Dominik Lindner.
\r
6 * Earlier versions have also been incorporated into Jalview version 2.4
\r
7 * since 2008, and TOPALi version 2 since 2007.
\r
9 * The Vamsas Client is free software: you can redistribute it and/or modify
\r
10 * it under the terms of the GNU Lesser General Public License as published by
\r
11 * the Free Software Foundation, either version 3 of the License, or
\r
12 * (at your option) any later version.
\r
14 * The Vamsas Client is distributed in the hope that it will be useful,
\r
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
17 * GNU Lesser General Public License for more details.
\r
19 * You should have received a copy of the GNU Lesser General Public License
\r
20 * along with the Vamsas Client. If not, see <http://www.gnu.org/licenses/>.
\r
23 * A class for formatting numbers that follows printf conventions.
\r
24 * Also implements C-like atoi and atof functions
\r
25 * @version 1.03 25 Oct 1997
\r
26 * @author Cay Horstmann
\r
29 package uk.ac.vamsas.objects.utils;
\r
33 public class Format {
\r
35 * Formats the number following printf conventions.
\r
36 * Main limitation: Can only handle one format parameter at a time
\r
37 * Use multiple Format objects to format more than one number
\r
38 * @param s the format string following printf conventions
\r
39 * The string has a prefix, a format code and a suffix. The prefix and suffix
\r
40 * become part of the formatted output. The format code directs the
\r
41 * formatting of the (single) parameter to be formatted. The code has the
\r
42 * following structure
\r
44 * <li> a % (required)
\r
45 * <li> a modifier (optional)
\r
47 * <dt> + <dd> forces display of + for positive numbers
\r
48 * <dt> 0 <dd> show leading zeroes
\r
49 * <dt> - <dd> align left in the field
\r
50 * <dt> space <dd> prepend a space in front of positive numbers
\r
51 * <dt> # <dd> use "alternate" format. Add 0 or 0x for octal or hexadecimal numbers. Don't suppress trailing zeroes in general floating point format.
\r
53 * <li> an integer denoting field width (optional)
\r
54 * <li> a period followed by an integer denoting precision (optional)
\r
55 * <li> a format descriptor (required)
\r
57 * <dt>f <dd> floating point number in fixed format
\r
58 * <dt>e, E <dd> floating point number in exponential notation (scientific format). The E format results in an uppercase E for the exponent (1.14130E+003), the e format in a lowercase e.
\r
59 * <dt>g, G <dd> floating point number in general format (fixed format for small numbers, exponential format for large numbers). Trailing zeroes are suppressed. The G format results in an uppercase E for the exponent (if any), the g format in a lowercase e.
\r
60 * <dt>d, i <dd> integer in decimal
\r
61 * <dt>x <dd> integer in hexadecimal
\r
62 * <dt>o <dd> integer in octal
\r
64 * <dt>c <dd> character
\r
67 * @exception IllegalArgumentException if bad format
\r
70 public Format(String s) {
\r
75 leading_zeroes = false;
\r
83 int length = s.length();
\r
84 int parse_state = 0;
\r
85 // 0 = prefix, 1 = flags, 2 = width, 3 = precision,
\r
86 // 4 = format, 5 = end
\r
89 while (parse_state == 0) {
\r
92 else if (s.charAt(i) == '%') {
\r
93 if (i < length - 1) {
\r
94 if (s.charAt(i + 1) == '%') {
\r
100 throw new java.lang.IllegalArgumentException();
\r
102 pre = pre + s.charAt(i);
\r
105 while (parse_state == 1) {
\r
108 else if (s.charAt(i) == ' ')
\r
110 else if (s.charAt(i) == '-')
\r
112 else if (s.charAt(i) == '+')
\r
114 else if (s.charAt(i) == '0')
\r
115 leading_zeroes = true;
\r
116 else if (s.charAt(i) == '#')
\r
124 while (parse_state == 2) {
\r
127 else if ('0' <= s.charAt(i) && s.charAt(i) <= '9') {
\r
128 width = width * 10 + s.charAt(i) - '0';
\r
130 } else if (s.charAt(i) == '.') {
\r
137 while (parse_state == 3) {
\r
140 else if ('0' <= s.charAt(i) && s.charAt(i) <= '9') {
\r
141 precision = precision * 10 + s.charAt(i) - '0';
\r
146 if (parse_state == 4) {
\r
154 post = s.substring(i, length);
\r
158 * prints a formatted number following printf conventions
\r
163 * the format string
\r
165 * the double to print
\r
168 public static void print(java.io.PrintStream s, String fmt, double x) {
\r
169 s.print(new Format(fmt).form(x));
\r
173 * prints a formatted number following printf conventions
\r
178 * the format string
\r
180 * the long to print
\r
182 public static void print(java.io.PrintStream s, String fmt, long x) {
\r
183 s.print(new Format(fmt).form(x));
\r
187 * prints a formatted number following printf conventions
\r
192 * the format string
\r
197 public static void print(java.io.PrintStream s, String fmt, char x) {
\r
198 s.print(new Format(fmt).form(x));
\r
202 * prints a formatted number following printf conventions
\r
205 * a PrintStream, fmt the format string
\r
207 * a string that represents the digits to print
\r
210 public static void print(java.io.PrintStream s, String fmt, String x) {
\r
211 s.print(new Format(fmt).form(x));
\r
215 * Converts a string of digits (decimal, octal or hex) to an integer
\r
219 * @return the numeric value of the prefix of s representing a base 10 integer
\r
222 public static int atoi(String s) {
\r
223 return (int) atol(s);
\r
227 * Converts a string of digits (decimal, octal or hex) to a long integer
\r
231 * @return the numeric value of the prefix of s representing a base 10 integer
\r
234 public static long atol(String s) {
\r
237 while (i < s.length() && Character.isWhitespace(s.charAt(i)))
\r
239 if (i < s.length() && s.charAt(i) == '0') {
\r
240 if (i + 1 < s.length()
\r
241 && (s.charAt(i + 1) == 'x' || s.charAt(i + 1) == 'X'))
\r
242 return parseLong(s.substring(i + 2), 16);
\r
244 return parseLong(s, 8);
\r
246 return parseLong(s, 10);
\r
249 private static long parseLong(String s, int base) {
\r
254 while (i < s.length() && Character.isWhitespace(s.charAt(i)))
\r
256 if (i < s.length() && s.charAt(i) == '-') {
\r
259 } else if (i < s.length() && s.charAt(i) == '+') {
\r
262 while (i < s.length()) {
\r
263 char ch = s.charAt(i);
\r
264 if ('0' <= ch && ch < '0' + base)
\r
265 r = r * base + ch - '0';
\r
266 else if ('A' <= ch && ch < 'A' + base - 10)
\r
267 r = r * base + ch - 'A' + 10;
\r
268 else if ('a' <= ch && ch < 'a' + base - 10)
\r
269 r = r * base + ch - 'a' + 10;
\r
278 * Converts a string of digits to an double
\r
284 public static double atof(String s) {
\r
287 double r = 0; // integer part
\r
288 double f = 0; // fractional part
\r
289 double p = 1; // exponent of fractional part
\r
290 int state = 0; // 0 = int part, 1 = frac part
\r
292 while (i < s.length() && Character.isWhitespace(s.charAt(i)))
\r
294 if (i < s.length() && s.charAt(i) == '-') {
\r
297 } else if (i < s.length() && s.charAt(i) == '+') {
\r
300 while (i < s.length()) {
\r
301 char ch = s.charAt(i);
\r
302 if ('0' <= ch && ch <= '9') {
\r
304 r = r * 10 + ch - '0';
\r
305 else if (state == 1) {
\r
307 r = r + p * (ch - '0');
\r
309 } else if (ch == '.') {
\r
314 } else if (ch == 'e' || ch == 'E') {
\r
315 long e = (int) parseLong(s.substring(i + 1), 10);
\r
316 return sign * r * Math.pow(10, e);
\r
325 * Formats a double into a string (like sprintf in C)
\r
328 * the number to format
\r
329 * @return the formatted string
\r
330 * @exception IllegalArgumentException
\r
334 public String form(double x) {
\r
344 r = fixed_format(x);
\r
345 else if (fmt == 'e' || fmt == 'E' || fmt == 'g' || fmt == 'G')
\r
348 throw new java.lang.IllegalArgumentException();
\r
350 return pad(sign(s, r));
\r
354 * Formats a long integer into a string (like sprintf in C)
\r
357 * the number to format
\r
358 * @return the formatted string
\r
361 public String form(long x) {
\r
364 if (fmt == 'd' || fmt == 'i') {
\r
366 r = ("" + x).substring(1);
\r
372 } else if (fmt == 'o')
\r
373 r = convert(x, 3, 7, "01234567");
\r
374 else if (fmt == 'x')
\r
375 r = convert(x, 4, 15, "0123456789abcdef");
\r
376 else if (fmt == 'X')
\r
377 r = convert(x, 4, 15, "0123456789ABCDEF");
\r
379 throw new java.lang.IllegalArgumentException();
\r
381 return pad(sign(s, r));
\r
385 * Formats a character into a string (like sprintf in C)
\r
388 * the value to format
\r
389 * @return the formatted string
\r
392 public String form(char c) {
\r
394 throw new java.lang.IllegalArgumentException();
\r
401 * Formats a string into a larger string (like sprintf in C)
\r
404 * the value to format
\r
405 * @return the formatted string
\r
408 public String form(String s) {
\r
410 throw new java.lang.IllegalArgumentException();
\r
411 if (precision >= 0)
\r
412 s = s.substring(0, precision);
\r
417 * a test stub for the format class
\r
420 public static void main(String[] a) {
\r
421 double x = 1.23456789012;
\r
423 double z = 1.2345e30;
\r
425 double u = 1.234e-5;
\r
427 Format.print(System.out, "x = |%f|\n", x);
\r
428 Format.print(System.out, "u = |%20f|\n", u);
\r
429 Format.print(System.out, "x = |% .5f|\n", x);
\r
430 Format.print(System.out, "w = |%20.5f|\n", w);
\r
431 Format.print(System.out, "x = |%020.5f|\n", x);
\r
432 Format.print(System.out, "x = |%+20.5f|\n", x);
\r
433 Format.print(System.out, "x = |%+020.5f|\n", x);
\r
434 Format.print(System.out, "x = |% 020.5f|\n", x);
\r
435 Format.print(System.out, "y = |%#+20.5f|\n", y);
\r
436 Format.print(System.out, "y = |%-+20.5f|\n", y);
\r
437 Format.print(System.out, "z = |%20.5f|\n", z);
\r
439 Format.print(System.out, "x = |%e|\n", x);
\r
440 Format.print(System.out, "u = |%20e|\n", u);
\r
441 Format.print(System.out, "x = |% .5e|\n", x);
\r
442 Format.print(System.out, "w = |%20.5e|\n", w);
\r
443 Format.print(System.out, "x = |%020.5e|\n", x);
\r
444 Format.print(System.out, "x = |%+20.5e|\n", x);
\r
445 Format.print(System.out, "x = |%+020.5e|\n", x);
\r
446 Format.print(System.out, "x = |% 020.5e|\n", x);
\r
447 Format.print(System.out, "y = |%#+20.5e|\n", y);
\r
448 Format.print(System.out, "y = |%-+20.5e|\n", y);
\r
450 Format.print(System.out, "x = |%g|\n", x);
\r
451 Format.print(System.out, "z = |%g|\n", z);
\r
452 Format.print(System.out, "w = |%g|\n", w);
\r
453 Format.print(System.out, "u = |%g|\n", u);
\r
454 Format.print(System.out, "y = |%.2g|\n", y);
\r
455 Format.print(System.out, "y = |%#.2g|\n", y);
\r
457 Format.print(System.out, "d = |%d|\n", d);
\r
458 Format.print(System.out, "d = |%20d|\n", d);
\r
459 Format.print(System.out, "d = |%020d|\n", d);
\r
460 Format.print(System.out, "d = |%+20d|\n", d);
\r
461 Format.print(System.out, "d = |% 020d|\n", d);
\r
462 Format.print(System.out, "d = |%-20d|\n", d);
\r
463 Format.print(System.out, "d = |%20.8d|\n", d);
\r
464 Format.print(System.out, "d = |%x|\n", d);
\r
465 Format.print(System.out, "d = |%20X|\n", d);
\r
466 Format.print(System.out, "d = |%#20x|\n", d);
\r
467 Format.print(System.out, "d = |%020X|\n", d);
\r
468 Format.print(System.out, "d = |%20.8x|\n", d);
\r
469 Format.print(System.out, "d = |%o|\n", d);
\r
470 Format.print(System.out, "d = |%020o|\n", d);
\r
471 Format.print(System.out, "d = |%#20o|\n", d);
\r
472 Format.print(System.out, "d = |%#020o|\n", d);
\r
473 Format.print(System.out, "d = |%20.12o|\n", d);
\r
475 Format.print(System.out, "s = |%-20s|\n", "Hello");
\r
476 Format.print(System.out, "s = |%-20c|\n", '!');
\r
478 // regression test to confirm fix of reported bugs
\r
480 Format.print(System.out, "|%i|\n", Long.MIN_VALUE);
\r
482 Format.print(System.out, "|%6.2e|\n", 0.0);
\r
483 Format.print(System.out, "|%6.2g|\n", 0.0);
\r
485 Format.print(System.out, "|%6.2f|\n", 9.99);
\r
486 Format.print(System.out, "|%6.2f|\n", 9.999);
\r
488 Format.print(System.out, "|%6.0f|\n", 9.999);
\r
491 private static String repeat(char c, int n) {
\r
494 StringBuffer s = new StringBuffer(n);
\r
495 for (int i = 0; i < n; i++)
\r
497 return s.toString();
\r
500 private static String convert(long x, int n, int m, String d) {
\r
505 r = d.charAt((int) (x & m)) + r;
\r
511 private String pad(String r) {
\r
512 String p = repeat(' ', width - r.length());
\r
514 return pre + r + p + post;
\r
516 return pre + p + r + post;
\r
519 private String sign(int s, String r) {
\r
526 else if (show_space)
\r
529 if (fmt == 'o' && alternate && r.length() > 0 && r.charAt(0) != '0')
\r
531 else if (fmt == 'x' && alternate)
\r
533 else if (fmt == 'X' && alternate)
\r
537 if (leading_zeroes)
\r
539 else if ((fmt == 'd' || fmt == 'i' || fmt == 'x' || fmt == 'X' || fmt == 'o')
\r
543 return p + repeat('0', w - p.length() - r.length()) + r;
\r
546 private String fixed_format(double d) {
\r
547 boolean removeTrailing = (fmt == 'G' || fmt == 'g') && !alternate;
\r
548 // remove trailing zeroes and decimal point
\r
550 if (d > 0x7FFFFFFFFFFFFFFFL)
\r
551 return exp_format(d);
\r
552 if (precision == 0)
\r
553 return (long) (d + 0.5) + (removeTrailing ? "" : ".");
\r
555 long whole = (long) d;
\r
556 double fr = d - whole; // fractional part
\r
557 if (fr >= 1 || fr < 0)
\r
558 return exp_format(d);
\r
561 String leading_zeroes = "";
\r
562 for (int i = 1; i <= precision && factor <= 0x7FFFFFFFFFFFFFFFL; i++) {
\r
564 leading_zeroes = leading_zeroes + "0";
\r
566 long l = (long) (factor * fr + 0.5);
\r
572 String z = leading_zeroes + l;
\r
573 z = "." + z.substring(z.length() - precision, z.length());
\r
575 if (removeTrailing) {
\r
576 int t = z.length() - 1;
\r
577 while (t >= 0 && z.charAt(t) == '0')
\r
579 if (t >= 0 && z.charAt(t) == '.')
\r
581 z = z.substring(0, t + 1);
\r
587 private String exp_format(double d) {
\r
604 if ((fmt == 'g' || fmt == 'G') && e >= -4 && e < precision)
\r
605 return fixed_format(d);
\r
608 f = f + fixed_format(d);
\r
610 if (fmt == 'e' || fmt == 'g')
\r
624 return f + p.substring(p.length() - 3, p.length());
\r
629 private int precision;
\r
631 private String pre;
\r
633 private String post;
\r
635 private boolean leading_zeroes;
\r
637 private boolean show_plus;
\r
639 private boolean alternate;
\r
641 private boolean show_space;
\r
643 private boolean left_align;
\r
645 private char fmt; // one of cdeEfgGiosxXos
\r