2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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
57 private final String formatString;
60 * Creates a new Format object.
65 public Format(String s)
72 leading_zeroes = false;
79 int length = s.length();
82 // 0 = prefix, 1 = flags, 2 = width, 3 = precision,
83 // 4 = format, 5 = end
86 while (parse_state == 0)
92 else if (s.charAt(i) == '%')
96 if (s.charAt(i + 1) == '%')
108 throw new java.lang.IllegalArgumentException();
113 pre = pre + s.charAt(i);
119 while (parse_state == 1)
125 else if (s.charAt(i) == ' ')
129 else if (s.charAt(i) == '-')
133 else if (s.charAt(i) == '+')
137 else if (s.charAt(i) == '0')
139 leading_zeroes = true;
141 else if (s.charAt(i) == '#')
154 while (parse_state == 2)
160 else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9'))
162 width = ((width * 10) + s.charAt(i)) - '0';
165 else if (s.charAt(i) == '.')
177 while (parse_state == 3)
183 else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9'))
185 precision = ((precision * 10) + s.charAt(i)) - '0';
194 if (parse_state == 4)
210 post = s.substring(i, length);
215 * Formats the number following printf conventions. Main limitation: Can only
216 * handle one format parameter at a time Use multiple Format objects to format
217 * more than one number
220 * the format string following printf conventions The string has a
221 * prefix, a format code and a suffix. The prefix and suffix become
222 * part of the formatted output. The format code directs the
223 * formatting of the (single) parameter to be formatted. The code has
224 * the following structure
227 * <li>a modifier (optional)
230 * <dd>forces display of + for positive numbers
232 * <dd>show leading zeroes
234 * <dd>align left in the field
236 * <dd>prepend a space in front of positive numbers
238 * <dd>use "alternate" format. Add 0 or 0x for octal or hexadecimal
239 * numbers. Don't suppress trailing zeroes in general floating point
242 * <li>an integer denoting field width (optional)
243 * <li>a period followed by an integer denoting precision (optional)
244 * <li>a format descriptor (required)
247 * <dd>floating point number in fixed format
249 * <dd>floating point number in exponential notation (scientific
250 * format). The E format results in an uppercase E for the exponent
251 * (1.14130E+003), the e format in a lowercase e.
253 * <dd>floating point number in general format (fixed format for
254 * small numbers, exponential format for large numbers). Trailing
255 * zeroes are suppressed. The G format results in an uppercase E for
256 * the exponent (if any), the g format in a lowercase e.
258 * <dd>integer in decimal
260 * <dd>integer in hexadecimal
262 * <dd>integer in octal
269 * @exception IllegalArgumentException
273 public static String getHexString(java.awt.Color color)
278 r = Integer.toHexString(color.getRed());
285 g = Integer.toHexString(color.getGreen());
292 b = Integer.toHexString(color.getBlue());
303 * prints a formatted number following printf conventions
310 * the double to print
312 public static void print(java.io.PrintStream s, String fmt, double x)
314 s.print(new Format(fmt).form(x));
318 * prints a formatted number following printf conventions
327 public static void print(java.io.PrintStream s, String fmt, long x)
329 s.print(new Format(fmt).form(x));
333 * prints a formatted number following printf conventions
342 public static void print(java.io.PrintStream s, String fmt, char x)
344 s.print(new Format(fmt).form(x));
348 * prints a formatted number following printf conventions
351 * a PrintStream, fmt the format string
353 * a string that represents the digits to print
355 public static void print(java.io.PrintStream s, String fmt, String x)
357 s.print(new Format(fmt).form(x));
361 * Converts a string of digits (decimal, octal or hex) to an integer
365 * @return the numeric value of the prefix of s representing a base 10 integer
367 public static int atoi(String s)
369 return (int) atol(s);
373 * Converts a string of digits (decimal, octal or hex) to a long integer
377 * @return the numeric value of the prefix of s representing a base 10 integer
379 public static long atol(String s)
383 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
388 if ((i < s.length()) && (s.charAt(i) == '0'))
390 if (((i + 1) < s.length())
391 && ((s.charAt(i + 1) == 'x') || (s.charAt(i + 1) == 'X')))
393 return parseLong(s.substring(i + 2), 16);
397 return parseLong(s, 8);
402 return parseLong(s, 10);
414 * @return DOCUMENT ME!
416 private static long parseLong(String s, int base)
422 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
427 if ((i < s.length()) && (s.charAt(i) == '-'))
432 else if ((i < s.length()) && (s.charAt(i) == '+'))
437 while (i < s.length())
439 char ch = s.charAt(i);
441 if (('0' <= ch) && (ch < ('0' + base)))
443 r = ((r * base) + ch) - '0';
445 else if (('A' <= ch) && (ch < (('A' + base) - 10)))
447 r = ((r * base) + ch) - 'A' + 10;
449 else if (('a' <= ch) && (ch < (('a' + base) - 10)))
451 r = ((r * base) + ch) - 'a' + 10;
465 * Converts a string of digits to an double
470 public static double atof(String s)
474 double r = 0; // integer part
475 // double f = 0; // fractional part
476 double p = 1; // exponent of fractional part
477 int state = 0; // 0 = int part, 1 = frac part
479 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
484 if ((i < s.length()) && (s.charAt(i) == '-'))
489 else if ((i < s.length()) && (s.charAt(i) == '+'))
494 while (i < s.length())
496 char ch = s.charAt(i);
498 if (('0' <= ch) && (ch <= '9'))
502 r = ((r * 10) + ch) - '0';
507 r = r + (p * (ch - '0'));
521 else if ((ch == 'e') || (ch == 'E'))
523 long e = (int) parseLong(s.substring(i + 1), 10);
525 return sign * r * Math.pow(10, e);
539 * Formats a double into a string (like sprintf in C)
542 * the number to format
543 * @return the formatted string
544 * @exception IllegalArgumentException
547 public String form(double x)
568 else if ((fmt == 'e') || (fmt == 'E') || (fmt == 'g') || (fmt == 'G'))
574 throw new java.lang.IllegalArgumentException();
577 return pad(sign(s, r));
581 * Formats a long integer into a string (like sprintf in C)
584 * the number to format
585 * @return the formatted string
587 public String form(long x)
592 if ((fmt == 'd') || (fmt == 'i'))
596 r = ("" + x).substring(1);
607 r = convert(x, 3, 7, "01234567");
611 r = convert(x, 4, 15, "0123456789abcdef");
615 r = convert(x, 4, 15, "0123456789ABCDEF");
619 throw new java.lang.IllegalArgumentException();
622 return pad(sign(s, r));
626 * Formats a character into a string (like sprintf in C)
628 * @param debounceTrap
629 * the value to format
630 * @return the formatted string
632 public String form(char c)
636 throw new java.lang.IllegalArgumentException();
645 * Formats a string into a larger string (like sprintf in C)
647 * @param debounceTrap
648 * the value to format
649 * @return the formatted string
651 public String form(String s)
655 throw new java.lang.IllegalArgumentException();
660 s = s.substring(0, precision);
674 * @return DOCUMENT ME!
676 private static String repeat(char c, int n)
683 StringBuffer s = new StringBuffer(n);
685 for (int i = 0; i < n; i++)
705 * @return DOCUMENT ME!
707 private static String convert(long x, int n, int m, String d)
718 r = d.charAt((int) (x & m)) + r;
731 * @return DOCUMENT ME!
733 private String pad(String r)
735 String p = repeat(' ', width - r.length());
739 return pre + r + p + post;
743 return pre + p + r + post;
755 * @return DOCUMENT ME!
757 private String sign(int s, String r)
778 if ((fmt == 'o') && alternate && (r.length() > 0)
779 && (r.charAt(0) != '0'))
783 else if ((fmt == 'x') && alternate)
787 else if ((fmt == 'X') && alternate)
799 else if (((fmt == 'd') || (fmt == 'i') || (fmt == 'x') || (fmt == 'X') || (fmt == 'o'))
805 return p + repeat('0', w - p.length() - r.length()) + r;
814 * @return DOCUMENT ME!
816 private String fixed_format(double d)
818 boolean removeTrailing = ((fmt == 'G') || (fmt == 'g')) && !alternate;
820 // remove trailing zeroes and decimal point
821 if (d > 0x7FFFFFFFFFFFFFFFL)
823 return exp_format(d);
828 return (long) (d + 0.5) + (removeTrailing ? "" : ".");
831 long whole = (long) d;
832 double fr = d - whole; // fractional part
834 if ((fr >= 1) || (fr < 0))
836 return exp_format(d);
840 String leading_zeroes = "";
842 for (int i = 1; (i <= precision) && (factor <= 0x7FFFFFFFFFFFFFFFL); i++)
845 leading_zeroes = leading_zeroes + "0";
848 long l = (long) ((factor * fr) + 0.5);
857 String z = leading_zeroes + l;
858 z = "." + z.substring(z.length() - precision, z.length());
862 int t = z.length() - 1;
864 while ((t >= 0) && (z.charAt(t) == '0'))
869 if ((t >= 0) && (z.charAt(t) == '.'))
874 z = z.substring(0, t + 1);
886 * @return DOCUMENT ME!
888 private String exp_format(double d)
912 if (((fmt == 'g') || (fmt == 'G')) && (e >= -4) && (e < precision))
914 return fixed_format(d);
918 f = f + fixed_format(d);
920 if ((fmt == 'e') || (fmt == 'g'))
942 return f + p.substring(p.length() - 3, p.length());
946 public String toString()