2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
3 * Copyright (C) 2014 The Jalview Authors
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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
20 * A class for formatting numbers that follows printf conventions.
21 * Also implements C-like atoi and atof functions
22 * @version 1.03 25 Oct 1997
23 * @author Cay Horstmann
37 private int precision;
43 private boolean leading_zeroes;
45 private boolean show_plus;
47 private boolean alternate;
49 private boolean show_space;
51 private boolean left_align;
53 private char fmt; // one of cdeEfgGiosxXos
56 * Creates a new Format object.
61 public Format(String s)
67 leading_zeroes = false;
74 int length = s.length();
77 // 0 = prefix, 1 = flags, 2 = width, 3 = precision,
78 // 4 = format, 5 = end
81 while (parse_state == 0)
87 else if (s.charAt(i) == '%')
91 if (s.charAt(i + 1) == '%')
103 throw new java.lang.IllegalArgumentException();
108 pre = pre + s.charAt(i);
114 while (parse_state == 1)
120 else if (s.charAt(i) == ' ')
124 else if (s.charAt(i) == '-')
128 else if (s.charAt(i) == '+')
132 else if (s.charAt(i) == '0')
134 leading_zeroes = true;
136 else if (s.charAt(i) == '#')
149 while (parse_state == 2)
155 else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9'))
157 width = ((width * 10) + s.charAt(i)) - '0';
160 else if (s.charAt(i) == '.')
172 while (parse_state == 3)
178 else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9'))
180 precision = ((precision * 10) + s.charAt(i)) - '0';
189 if (parse_state == 4)
205 post = s.substring(i, length);
210 * Formats the number following printf conventions. Main limitation: Can only
211 * handle one format parameter at a time Use multiple Format objects to format
212 * more than one number
215 * the format string following printf conventions The string has a
216 * prefix, a format code and a suffix. The prefix and suffix become
217 * part of the formatted output. The format code directs the
218 * formatting of the (single) parameter to be formatted. The code has
219 * the following structure
222 * <li>a modifier (optional)
225 * <dd>forces display of + for positive numbers
227 * <dd>show leading zeroes
229 * <dd>align left in the field
231 * <dd>prepend a space in front of positive numbers
233 * <dd>use "alternate" format. Add 0 or 0x for octal or hexadecimal
234 * numbers. Don't suppress trailing zeroes in general floating point
237 * <li>an integer denoting field width (optional)
238 * <li>a period followed by an integer denoting precision (optional)
239 * <li>a format descriptor (required)
242 * <dd>floating point number in fixed format
244 * <dd>floating point number in exponential notation (scientific
245 * format). The E format results in an uppercase E for the exponent
246 * (1.14130E+003), the e format in a lowercase e.
248 * <dd>floating point number in general format (fixed format for
249 * small numbers, exponential format for large numbers). Trailing
250 * zeroes are suppressed. The G format results in an uppercase E for
251 * the exponent (if any), the g format in a lowercase e.
253 * <dd>integer in decimal
255 * <dd>integer in hexadecimal
257 * <dd>integer in octal
264 * @exception IllegalArgumentException
268 public static String getHexString(java.awt.Color color)
273 r = Integer.toHexString(color.getRed());
280 g = Integer.toHexString(color.getGreen());
287 b = Integer.toHexString(color.getBlue());
298 * prints a formatted number following printf conventions
305 * the double to print
307 public static void print(java.io.PrintStream s, String fmt, double x)
309 s.print(new Format(fmt).form(x));
313 * prints a formatted number following printf conventions
322 public static void print(java.io.PrintStream s, String fmt, long x)
324 s.print(new Format(fmt).form(x));
328 * prints a formatted number following printf conventions
337 public static void print(java.io.PrintStream s, String fmt, char x)
339 s.print(new Format(fmt).form(x));
343 * prints a formatted number following printf conventions
346 * a PrintStream, fmt the format string
348 * a string that represents the digits to print
350 public static void print(java.io.PrintStream s, String fmt, String x)
352 s.print(new Format(fmt).form(x));
356 * Converts a string of digits (decimal, octal or hex) to an integer
360 * @return the numeric value of the prefix of s representing a base 10 integer
362 public static int atoi(String s)
364 return (int) atol(s);
368 * Converts a string of digits (decimal, octal or hex) to a long integer
372 * @return the numeric value of the prefix of s representing a base 10 integer
374 public static long atol(String s)
378 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
383 if ((i < s.length()) && (s.charAt(i) == '0'))
385 if (((i + 1) < s.length())
386 && ((s.charAt(i + 1) == 'x') || (s.charAt(i + 1) == 'X')))
388 return parseLong(s.substring(i + 2), 16);
392 return parseLong(s, 8);
397 return parseLong(s, 10);
409 * @return DOCUMENT ME!
411 private static long parseLong(String s, int base)
417 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
422 if ((i < s.length()) && (s.charAt(i) == '-'))
427 else if ((i < s.length()) && (s.charAt(i) == '+'))
432 while (i < s.length())
434 char ch = s.charAt(i);
436 if (('0' <= ch) && (ch < ('0' + base)))
438 r = ((r * base) + ch) - '0';
440 else if (('A' <= ch) && (ch < (('A' + base) - 10)))
442 r = ((r * base) + ch) - 'A' + 10;
444 else if (('a' <= ch) && (ch < (('a' + base) - 10)))
446 r = ((r * base) + ch) - 'a' + 10;
460 * Converts a string of digits to an double
465 public static double atof(String s)
469 double r = 0; // integer part
470 // double f = 0; // fractional part
471 double p = 1; // exponent of fractional part
472 int state = 0; // 0 = int part, 1 = frac part
474 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
479 if ((i < s.length()) && (s.charAt(i) == '-'))
484 else if ((i < s.length()) && (s.charAt(i) == '+'))
489 while (i < s.length())
491 char ch = s.charAt(i);
493 if (('0' <= ch) && (ch <= '9'))
497 r = ((r * 10) + ch) - '0';
502 r = r + (p * (ch - '0'));
516 else if ((ch == 'e') || (ch == 'E'))
518 long e = (int) parseLong(s.substring(i + 1), 10);
520 return sign * r * Math.pow(10, e);
534 * Formats a double into a string (like sprintf in C)
537 * the number to format
538 * @return the formatted string
539 * @exception IllegalArgumentException
542 public String form(double x)
563 else if ((fmt == 'e') || (fmt == 'E') || (fmt == 'g') || (fmt == 'G'))
569 throw new java.lang.IllegalArgumentException();
572 return pad(sign(s, r));
576 * Formats a long integer into a string (like sprintf in C)
579 * the number to format
580 * @return the formatted string
582 public String form(long x)
587 if ((fmt == 'd') || (fmt == 'i'))
591 r = ("" + x).substring(1);
602 r = convert(x, 3, 7, "01234567");
606 r = convert(x, 4, 15, "0123456789abcdef");
610 r = convert(x, 4, 15, "0123456789ABCDEF");
614 throw new java.lang.IllegalArgumentException();
617 return pad(sign(s, r));
621 * Formats a character into a string (like sprintf in C)
624 * the value to format
625 * @return the formatted string
627 public String form(char c)
631 throw new java.lang.IllegalArgumentException();
640 * Formats a string into a larger string (like sprintf in C)
643 * the value to format
644 * @return the formatted string
646 public String form(String s)
650 throw new java.lang.IllegalArgumentException();
655 s = s.substring(0, precision);
669 * @return DOCUMENT ME!
671 private static String repeat(char c, int n)
678 StringBuffer s = new StringBuffer(n);
680 for (int i = 0; i < n; i++)
700 * @return DOCUMENT ME!
702 private static String convert(long x, int n, int m, String d)
713 r = d.charAt((int) (x & m)) + r;
726 * @return DOCUMENT ME!
728 private String pad(String r)
730 String p = repeat(' ', width - r.length());
734 return pre + r + p + post;
738 return pre + p + r + post;
750 * @return DOCUMENT ME!
752 private String sign(int s, String r)
773 if ((fmt == 'o') && alternate && (r.length() > 0)
774 && (r.charAt(0) != '0'))
778 else if ((fmt == 'x') && alternate)
782 else if ((fmt == 'X') && alternate)
794 else if (((fmt == 'd') || (fmt == 'i') || (fmt == 'x') || (fmt == 'X') || (fmt == 'o'))
800 return p + repeat('0', w - p.length() - r.length()) + r;
809 * @return DOCUMENT ME!
811 private String fixed_format(double d)
813 boolean removeTrailing = ((fmt == 'G') || (fmt == 'g')) && !alternate;
815 // remove trailing zeroes and decimal point
816 if (d > 0x7FFFFFFFFFFFFFFFL)
818 return exp_format(d);
823 return (long) (d + 0.5) + (removeTrailing ? "" : ".");
826 long whole = (long) d;
827 double fr = d - whole; // fractional part
829 if ((fr >= 1) || (fr < 0))
831 return exp_format(d);
835 String leading_zeroes = "";
837 for (int i = 1; (i <= precision) && (factor <= 0x7FFFFFFFFFFFFFFFL); i++)
840 leading_zeroes = leading_zeroes + "0";
843 long l = (long) ((factor * fr) + 0.5);
852 String z = leading_zeroes + l;
853 z = "." + z.substring(z.length() - precision, z.length());
857 int t = z.length() - 1;
859 while ((t >= 0) && (z.charAt(t) == '0'))
864 if ((t >= 0) && (z.charAt(t) == '.'))
869 z = z.substring(0, t + 1);
881 * @return DOCUMENT ME!
883 private String exp_format(double d)
907 if (((fmt == 'g') || (fmt == 'G')) && (e >= -4) && (e < precision))
909 return fixed_format(d);
913 f = f + fixed_format(d);
915 if ((fmt == 'e') || (fmt == 'g'))
937 return f + p.substring(p.length() - 3, p.length());