2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
22 * A class for formatting numbers that follows printf conventions.
23 * Also implements C-like atoi and atof functions
24 * @version 1.03 25 Oct 1997
25 * @author Cay Horstmann
39 private int precision;
45 private boolean leading_zeroes;
47 private boolean show_plus;
49 private boolean alternate;
51 private boolean show_space;
53 private boolean left_align;
55 private char fmt; // one of cdeEfgGiosxXos
58 * Creates a new Format object.
63 public Format(String s)
69 leading_zeroes = false;
76 int length = s.length();
79 // 0 = prefix, 1 = flags, 2 = width, 3 = precision,
80 // 4 = format, 5 = end
83 while (parse_state == 0)
89 else if (s.charAt(i) == '%')
93 if (s.charAt(i + 1) == '%')
105 throw new java.lang.IllegalArgumentException();
110 pre = pre + s.charAt(i);
116 while (parse_state == 1)
122 else if (s.charAt(i) == ' ')
126 else if (s.charAt(i) == '-')
130 else if (s.charAt(i) == '+')
134 else if (s.charAt(i) == '0')
136 leading_zeroes = true;
138 else if (s.charAt(i) == '#')
151 while (parse_state == 2)
157 else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9'))
159 width = ((width * 10) + s.charAt(i)) - '0';
162 else if (s.charAt(i) == '.')
174 while (parse_state == 3)
180 else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9'))
182 precision = ((precision * 10) + s.charAt(i)) - '0';
191 if (parse_state == 4)
207 post = s.substring(i, length);
212 * Formats the number following printf conventions. Main limitation: Can only
213 * handle one format parameter at a time Use multiple Format objects to format
214 * more than one number
217 * the format string following printf conventions The string has a
218 * prefix, a format code and a suffix. The prefix and suffix become
219 * part of the formatted output. The format code directs the
220 * formatting of the (single) parameter to be formatted. The code has
221 * the following structure
224 * <li>a modifier (optional)
227 * <dd>forces display of + for positive numbers
229 * <dd>show leading zeroes
231 * <dd>align left in the field
233 * <dd>prepend a space in front of positive numbers
235 * <dd>use "alternate" format. Add 0 or 0x for octal or hexadecimal
236 * numbers. Don't suppress trailing zeroes in general floating point
239 * <li>an integer denoting field width (optional)
240 * <li>a period followed by an integer denoting precision (optional)
241 * <li>a format descriptor (required)
244 * <dd>floating point number in fixed format
246 * <dd>floating point number in exponential notation (scientific
247 * format). The E format results in an uppercase E for the exponent
248 * (1.14130E+003), the e format in a lowercase e.
250 * <dd>floating point number in general format (fixed format for
251 * small numbers, exponential format for large numbers). Trailing
252 * zeroes are suppressed. The G format results in an uppercase E for
253 * the exponent (if any), the g format in a lowercase e.
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());