2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * A class for formatting numbers that follows printf conventions.
20 * Also implements C-like atoi and atof functions
21 * @version 1.03 25 Oct 1997
22 * @author Cay Horstmann
36 private int precision;
42 private boolean leading_zeroes;
44 private boolean show_plus;
46 private boolean alternate;
48 private boolean show_space;
50 private boolean left_align;
52 private char fmt; // one of cdeEfgGiosxXos
55 * Creates a new Format object.
60 public Format(String s)
66 leading_zeroes = false;
73 int length = s.length();
76 // 0 = prefix, 1 = flags, 2 = width, 3 = precision,
77 // 4 = format, 5 = end
80 while (parse_state == 0)
86 else if (s.charAt(i) == '%')
90 if (s.charAt(i + 1) == '%')
102 throw new java.lang.IllegalArgumentException();
107 pre = pre + s.charAt(i);
113 while (parse_state == 1)
119 else if (s.charAt(i) == ' ')
123 else if (s.charAt(i) == '-')
127 else if (s.charAt(i) == '+')
131 else if (s.charAt(i) == '0')
133 leading_zeroes = true;
135 else if (s.charAt(i) == '#')
148 while (parse_state == 2)
154 else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9'))
156 width = ((width * 10) + s.charAt(i)) - '0';
159 else if (s.charAt(i) == '.')
171 while (parse_state == 3)
177 else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9'))
179 precision = ((precision * 10) + s.charAt(i)) - '0';
188 if (parse_state == 4)
204 post = s.substring(i, length);
209 * Formats the number following printf conventions. Main limitation: Can only
210 * handle one format parameter at a time Use multiple Format objects to format
211 * more than one number
214 * the format string following printf conventions The string
215 * has a prefix, a format code and a suffix. The prefix and
216 * suffix become part of the formatted output. The format code
217 * directs the formatting of the (single) parameter to be
218 * formatted. The code has the following structure
220 * <li> a % (required)
221 * <li> a modifier (optional)
224 * <dd> forces display of + for positive numbers
226 * <dd> show leading zeroes
228 * <dd> align left in the field
230 * <dd> prepend a space in front of positive numbers
232 * <dd> use "alternate" format. Add 0 or 0x for octal or
233 * hexadecimal numbers. Don't suppress trailing zeroes in
234 * general floating point format.
236 * <li> an integer denoting field width (optional)
237 * <li> a period followed by an integer denoting precision
239 * <li> a format descriptor (required)
242 * <dd> floating point number in fixed format
244 * <dd> floating point number in exponential notation
245 * (scientific format). The E format results in an uppercase E
246 * for the exponent (1.14130E+003), the e format in a lowercase
249 * <dd> floating point number in general format (fixed format
250 * for small numbers, exponential format for large numbers).
251 * Trailing zeroes are suppressed. The G format results in an
252 * uppercase E for the exponent (if any), the g format in a
255 * <dd> integer in decimal
257 * <dd> integer in hexadecimal
259 * <dd> integer in octal
266 * @exception IllegalArgumentException
270 public static String getHexString(java.awt.Color color)
275 r = Integer.toHexString(color.getRed());
282 g = Integer.toHexString(color.getGreen());
289 b = Integer.toHexString(color.getBlue());
300 * prints a formatted number following printf conventions
307 * the double to print
309 public static void print(java.io.PrintStream s, String fmt, double x)
311 s.print(new Format(fmt).form(x));
315 * prints a formatted number following printf conventions
324 public static void print(java.io.PrintStream s, String fmt, long x)
326 s.print(new Format(fmt).form(x));
330 * prints a formatted number following printf conventions
339 public static void print(java.io.PrintStream s, String fmt, char x)
341 s.print(new Format(fmt).form(x));
345 * prints a formatted number following printf conventions
348 * a PrintStream, fmt the format string
350 * a string that represents the digits to print
352 public static void print(java.io.PrintStream s, String fmt, String x)
354 s.print(new Format(fmt).form(x));
358 * Converts a string of digits (decimal, octal or hex) to an integer
362 * @return the numeric value of the prefix of s representing a base 10 integer
364 public static int atoi(String s)
366 return (int) atol(s);
370 * Converts a string of digits (decimal, octal or hex) to a long integer
374 * @return the numeric value of the prefix of s representing a base 10 integer
376 public static long atol(String s)
380 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
385 if ((i < s.length()) && (s.charAt(i) == '0'))
387 if (((i + 1) < s.length())
388 && ((s.charAt(i + 1) == 'x') || (s.charAt(i + 1) == 'X')))
390 return parseLong(s.substring(i + 2), 16);
394 return parseLong(s, 8);
399 return parseLong(s, 10);
411 * @return DOCUMENT ME!
413 private static long parseLong(String s, int base)
419 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
424 if ((i < s.length()) && (s.charAt(i) == '-'))
429 else if ((i < s.length()) && (s.charAt(i) == '+'))
434 while (i < s.length())
436 char ch = s.charAt(i);
438 if (('0' <= ch) && (ch < ('0' + base)))
440 r = ((r * base) + ch) - '0';
442 else if (('A' <= ch) && (ch < (('A' + base) - 10)))
444 r = ((r * base) + ch) - 'A' + 10;
446 else if (('a' <= ch) && (ch < (('a' + base) - 10)))
448 r = ((r * base) + ch) - 'a' + 10;
462 * Converts a string of digits to an double
467 public static double atof(String s)
471 double r = 0; // integer part
472 // double f = 0; // fractional part
473 double p = 1; // exponent of fractional part
474 int state = 0; // 0 = int part, 1 = frac part
476 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
481 if ((i < s.length()) && (s.charAt(i) == '-'))
486 else if ((i < s.length()) && (s.charAt(i) == '+'))
491 while (i < s.length())
493 char ch = s.charAt(i);
495 if (('0' <= ch) && (ch <= '9'))
499 r = ((r * 10) + ch) - '0';
504 r = r + (p * (ch - '0'));
518 else if ((ch == 'e') || (ch == 'E'))
520 long e = (int) parseLong(s.substring(i + 1), 10);
522 return sign * r * Math.pow(10, e);
536 * Formats a double into a string (like sprintf in C)
539 * the number to format
540 * @return the formatted string
541 * @exception IllegalArgumentException
544 public String form(double x)
565 else if ((fmt == 'e') || (fmt == 'E') || (fmt == 'g') || (fmt == 'G'))
571 throw new java.lang.IllegalArgumentException();
574 return pad(sign(s, r));
578 * Formats a long integer into a string (like sprintf in C)
581 * the number to format
582 * @return the formatted string
584 public String form(long x)
589 if ((fmt == 'd') || (fmt == 'i'))
593 r = ("" + x).substring(1);
604 r = convert(x, 3, 7, "01234567");
608 r = convert(x, 4, 15, "0123456789abcdef");
612 r = convert(x, 4, 15, "0123456789ABCDEF");
616 throw new java.lang.IllegalArgumentException();
619 return pad(sign(s, r));
623 * Formats a character into a string (like sprintf in C)
626 * the value to format
627 * @return the formatted string
629 public String form(char c)
633 throw new java.lang.IllegalArgumentException();
642 * Formats a string into a larger string (like sprintf in C)
645 * the value to format
646 * @return the formatted string
648 public String form(String s)
652 throw new java.lang.IllegalArgumentException();
657 s = s.substring(0, precision);
671 * @return DOCUMENT ME!
673 private static String repeat(char c, int n)
680 StringBuffer s = new StringBuffer(n);
682 for (int i = 0; i < n; i++)
702 * @return DOCUMENT ME!
704 private static String convert(long x, int n, int m, String d)
715 r = d.charAt((int) (x & m)) + r;
728 * @return DOCUMENT ME!
730 private String pad(String r)
732 String p = repeat(' ', width - r.length());
736 return pre + r + p + post;
740 return pre + p + r + post;
752 * @return DOCUMENT ME!
754 private String sign(int s, String r)
775 if ((fmt == 'o') && alternate && (r.length() > 0)
776 && (r.charAt(0) != '0'))
780 else if ((fmt == 'x') && alternate)
784 else if ((fmt == 'X') && alternate)
796 else if (((fmt == 'd') || (fmt == 'i') || (fmt == 'x') || (fmt == 'X') || (fmt == 'o'))
802 return p + repeat('0', w - p.length() - r.length()) + r;
811 * @return DOCUMENT ME!
813 private String fixed_format(double d)
815 boolean removeTrailing = ((fmt == 'G') || (fmt == 'g')) && !alternate;
817 // remove trailing zeroes and decimal point
818 if (d > 0x7FFFFFFFFFFFFFFFL)
820 return exp_format(d);
825 return (long) (d + 0.5) + (removeTrailing ? "" : ".");
828 long whole = (long) d;
829 double fr = d - whole; // fractional part
831 if ((fr >= 1) || (fr < 0))
833 return exp_format(d);
837 String leading_zeroes = "";
839 for (int i = 1; (i <= precision) && (factor <= 0x7FFFFFFFFFFFFFFFL); i++)
842 leading_zeroes = leading_zeroes + "0";
845 long l = (long) ((factor * fr) + 0.5);
854 String z = leading_zeroes + l;
855 z = "." + z.substring(z.length() - precision, z.length());
859 int t = z.length() - 1;
861 while ((t >= 0) && (z.charAt(t) == '0'))
866 if ((t >= 0) && (z.charAt(t) == '.'))
871 z = z.substring(0, t + 1);
883 * @return DOCUMENT ME!
885 private String exp_format(double d)
909 if (((fmt == 'g') || (fmt == 'G')) && (e >= -4) && (e < precision))
911 return fixed_format(d);
915 f = f + fixed_format(d);
917 if ((fmt == 'e') || (fmt == 'g'))
939 return f + p.substring(p.length() - 3, p.length());