2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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 has a
215 * prefix, a format code and a suffix. The prefix and suffix become
216 * part of the formatted output. The format code directs the
217 * formatting of the (single) parameter to be formatted. The code has
218 * the following structure
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 hexadecimal
233 * numbers. Don't suppress trailing zeroes in general floating point
236 * <li>an integer denoting field width (optional)
237 * <li>a period followed by an integer denoting precision (optional)
238 * <li>a format descriptor (required)
241 * <dd>floating point number in fixed format
243 * <dd>floating point number in exponential notation (scientific
244 * format). The E format results in an uppercase E for the exponent
245 * (1.14130E+003), the e format in a lowercase e.
247 * <dd>floating point number in general format (fixed format for
248 * small numbers, exponential format for large numbers). Trailing
249 * zeroes are suppressed. The G format results in an uppercase E for
250 * the exponent (if any), the g format in a lowercase e.
252 * <dd>integer in decimal
254 * <dd>integer in hexadecimal
256 * <dd>integer in octal
263 * @exception IllegalArgumentException
267 public static String getHexString(java.awt.Color color)
272 r = Integer.toHexString(color.getRed());
279 g = Integer.toHexString(color.getGreen());
286 b = Integer.toHexString(color.getBlue());
297 * prints a formatted number following printf conventions
304 * the double to print
306 public static void print(java.io.PrintStream s, String fmt, double x)
308 s.print(new Format(fmt).form(x));
312 * prints a formatted number following printf conventions
321 public static void print(java.io.PrintStream s, String fmt, long x)
323 s.print(new Format(fmt).form(x));
327 * prints a formatted number following printf conventions
336 public static void print(java.io.PrintStream s, String fmt, char x)
338 s.print(new Format(fmt).form(x));
342 * prints a formatted number following printf conventions
345 * a PrintStream, fmt the format string
347 * a string that represents the digits to print
349 public static void print(java.io.PrintStream s, String fmt, String x)
351 s.print(new Format(fmt).form(x));
355 * Converts a string of digits (decimal, octal or hex) to an integer
359 * @return the numeric value of the prefix of s representing a base 10 integer
361 public static int atoi(String s)
363 return (int) atol(s);
367 * Converts a string of digits (decimal, octal or hex) to a long integer
371 * @return the numeric value of the prefix of s representing a base 10 integer
373 public static long atol(String s)
377 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
382 if ((i < s.length()) && (s.charAt(i) == '0'))
384 if (((i + 1) < s.length())
385 && ((s.charAt(i + 1) == 'x') || (s.charAt(i + 1) == 'X')))
387 return parseLong(s.substring(i + 2), 16);
391 return parseLong(s, 8);
396 return parseLong(s, 10);
408 * @return DOCUMENT ME!
410 private static long parseLong(String s, int base)
416 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
421 if ((i < s.length()) && (s.charAt(i) == '-'))
426 else if ((i < s.length()) && (s.charAt(i) == '+'))
431 while (i < s.length())
433 char ch = s.charAt(i);
435 if (('0' <= ch) && (ch < ('0' + base)))
437 r = ((r * base) + ch) - '0';
439 else if (('A' <= ch) && (ch < (('A' + base) - 10)))
441 r = ((r * base) + ch) - 'A' + 10;
443 else if (('a' <= ch) && (ch < (('a' + base) - 10)))
445 r = ((r * base) + ch) - 'a' + 10;
459 * Converts a string of digits to an double
464 public static double atof(String s)
468 double r = 0; // integer part
469 // double f = 0; // fractional part
470 double p = 1; // exponent of fractional part
471 int state = 0; // 0 = int part, 1 = frac part
473 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
478 if ((i < s.length()) && (s.charAt(i) == '-'))
483 else if ((i < s.length()) && (s.charAt(i) == '+'))
488 while (i < s.length())
490 char ch = s.charAt(i);
492 if (('0' <= ch) && (ch <= '9'))
496 r = ((r * 10) + ch) - '0';
501 r = r + (p * (ch - '0'));
515 else if ((ch == 'e') || (ch == 'E'))
517 long e = (int) parseLong(s.substring(i + 1), 10);
519 return sign * r * Math.pow(10, e);
533 * Formats a double into a string (like sprintf in C)
536 * the number to format
537 * @return the formatted string
538 * @exception IllegalArgumentException
541 public String form(double x)
562 else if ((fmt == 'e') || (fmt == 'E') || (fmt == 'g') || (fmt == 'G'))
568 throw new java.lang.IllegalArgumentException();
571 return pad(sign(s, r));
575 * Formats a long integer into a string (like sprintf in C)
578 * the number to format
579 * @return the formatted string
581 public String form(long x)
586 if ((fmt == 'd') || (fmt == 'i'))
590 r = ("" + x).substring(1);
601 r = convert(x, 3, 7, "01234567");
605 r = convert(x, 4, 15, "0123456789abcdef");
609 r = convert(x, 4, 15, "0123456789ABCDEF");
613 throw new java.lang.IllegalArgumentException();
616 return pad(sign(s, r));
620 * Formats a character into a string (like sprintf in C)
623 * the value to format
624 * @return the formatted string
626 public String form(char c)
630 throw new java.lang.IllegalArgumentException();
639 * Formats a string into a larger string (like sprintf in C)
642 * the value to format
643 * @return the formatted string
645 public String form(String s)
649 throw new java.lang.IllegalArgumentException();
654 s = s.substring(0, precision);
668 * @return DOCUMENT ME!
670 private static String repeat(char c, int n)
677 StringBuffer s = new StringBuffer(n);
679 for (int i = 0; i < n; i++)
699 * @return DOCUMENT ME!
701 private static String convert(long x, int n, int m, String d)
712 r = d.charAt((int) (x & m)) + r;
725 * @return DOCUMENT ME!
727 private String pad(String r)
729 String p = repeat(' ', width - r.length());
733 return pre + r + p + post;
737 return pre + p + r + post;
749 * @return DOCUMENT ME!
751 private String sign(int s, String r)
772 if ((fmt == 'o') && alternate && (r.length() > 0)
773 && (r.charAt(0) != '0'))
777 else if ((fmt == 'x') && alternate)
781 else if ((fmt == 'X') && alternate)
793 else if (((fmt == 'd') || (fmt == 'i') || (fmt == 'x') || (fmt == 'X') || (fmt == 'o'))
799 return p + repeat('0', w - p.length() - r.length()) + r;
808 * @return DOCUMENT ME!
810 private String fixed_format(double d)
812 boolean removeTrailing = ((fmt == 'G') || (fmt == 'g')) && !alternate;
814 // remove trailing zeroes and decimal point
815 if (d > 0x7FFFFFFFFFFFFFFFL)
817 return exp_format(d);
822 return (long) (d + 0.5) + (removeTrailing ? "" : ".");
825 long whole = (long) d;
826 double fr = d - whole; // fractional part
828 if ((fr >= 1) || (fr < 0))
830 return exp_format(d);
834 String leading_zeroes = "";
836 for (int i = 1; (i <= precision) && (factor <= 0x7FFFFFFFFFFFFFFFL); i++)
839 leading_zeroes = leading_zeroes + "0";
842 long l = (long) ((factor * fr) + 0.5);
851 String z = leading_zeroes + l;
852 z = "." + z.substring(z.length() - precision, z.length());
856 int t = z.length() - 1;
858 while ((t >= 0) && (z.charAt(t) == '0'))
863 if ((t >= 0) && (z.charAt(t) == '.'))
868 z = z.substring(0, t + 1);
880 * @return DOCUMENT ME!
882 private String exp_format(double d)
906 if (((fmt == 'g') || (fmt == 'G')) && (e >= -4) && (e < precision))
908 return fixed_format(d);
912 f = f + fixed_format(d);
914 if ((fmt == 'e') || (fmt == 'g'))
936 return f + p.substring(p.length() - 3, p.length());