2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
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
216 * has a prefix, a format code and a suffix. The prefix and
217 * suffix become part of the formatted output. The format code
218 * directs the formatting of the (single) parameter to be
219 * formatted. The code has the following structure
221 * <li> a % (required)
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
234 * hexadecimal numbers. Don't suppress trailing zeroes in
235 * general floating point format.
237 * <li> an integer denoting field width (optional)
238 * <li> a period followed by an integer denoting precision
240 * <li> a format descriptor (required)
243 * <dd> floating point number in fixed format
245 * <dd> floating point number in exponential notation
246 * (scientific format). The E format results in an uppercase E
247 * for the exponent (1.14130E+003), the e format in a lowercase
250 * <dd> floating point number in general format (fixed format
251 * for small numbers, exponential format for large numbers).
252 * Trailing zeroes are suppressed. The G format results in an
253 * uppercase E for the exponent (if any), the g format in a
256 * <dd> integer in decimal
258 * <dd> integer in hexadecimal
260 * <dd> integer in octal
267 * @exception IllegalArgumentException
271 public static String getHexString(java.awt.Color color)
276 r = Integer.toHexString(color.getRed());
283 g = Integer.toHexString(color.getGreen());
290 b = Integer.toHexString(color.getBlue());
301 * prints a formatted number following printf conventions
308 * the double to print
310 public static void print(java.io.PrintStream s, String fmt, double x)
312 s.print(new Format(fmt).form(x));
316 * prints a formatted number following printf conventions
325 public static void print(java.io.PrintStream s, String fmt, long x)
327 s.print(new Format(fmt).form(x));
331 * prints a formatted number following printf conventions
340 public static void print(java.io.PrintStream s, String fmt, char x)
342 s.print(new Format(fmt).form(x));
346 * prints a formatted number following printf conventions
349 * a PrintStream, fmt the format string
351 * a string that represents the digits to print
353 public static void print(java.io.PrintStream s, String fmt, String x)
355 s.print(new Format(fmt).form(x));
359 * Converts a string of digits (decimal, octal or hex) to an integer
363 * @return the numeric value of the prefix of s representing a base 10 integer
365 public static int atoi(String s)
367 return (int) atol(s);
371 * Converts a string of digits (decimal, octal or hex) to a long integer
375 * @return the numeric value of the prefix of s representing a base 10 integer
377 public static long atol(String s)
381 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
386 if ((i < s.length()) && (s.charAt(i) == '0'))
388 if (((i + 1) < s.length())
389 && ((s.charAt(i + 1) == 'x') || (s.charAt(i + 1) == 'X')))
391 return parseLong(s.substring(i + 2), 16);
395 return parseLong(s, 8);
400 return parseLong(s, 10);
412 * @return DOCUMENT ME!
414 private static long parseLong(String s, int base)
420 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
425 if ((i < s.length()) && (s.charAt(i) == '-'))
430 else if ((i < s.length()) && (s.charAt(i) == '+'))
435 while (i < s.length())
437 char ch = s.charAt(i);
439 if (('0' <= ch) && (ch < ('0' + base)))
441 r = ((r * base) + ch) - '0';
443 else if (('A' <= ch) && (ch < (('A' + base) - 10)))
445 r = ((r * base) + ch) - 'A' + 10;
447 else if (('a' <= ch) && (ch < (('a' + base) - 10)))
449 r = ((r * base) + ch) - 'a' + 10;
463 * Converts a string of digits to an double
468 public static double atof(String s)
472 double r = 0; // integer part
473 // double f = 0; // fractional part
474 double p = 1; // exponent of fractional part
475 int state = 0; // 0 = int part, 1 = frac part
477 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
482 if ((i < s.length()) && (s.charAt(i) == '-'))
487 else if ((i < s.length()) && (s.charAt(i) == '+'))
492 while (i < s.length())
494 char ch = s.charAt(i);
496 if (('0' <= ch) && (ch <= '9'))
500 r = ((r * 10) + ch) - '0';
505 r = r + (p * (ch - '0'));
519 else if ((ch == 'e') || (ch == 'E'))
521 long e = (int) parseLong(s.substring(i + 1), 10);
523 return sign * r * Math.pow(10, e);
537 * Formats a double into a string (like sprintf in C)
540 * the number to format
541 * @return the formatted string
542 * @exception IllegalArgumentException
545 public String form(double x)
566 else if ((fmt == 'e') || (fmt == 'E') || (fmt == 'g') || (fmt == 'G'))
572 throw new java.lang.IllegalArgumentException();
575 return pad(sign(s, r));
579 * Formats a long integer into a string (like sprintf in C)
582 * the number to format
583 * @return the formatted string
585 public String form(long x)
590 if ((fmt == 'd') || (fmt == 'i'))
594 r = ("" + x).substring(1);
605 r = convert(x, 3, 7, "01234567");
609 r = convert(x, 4, 15, "0123456789abcdef");
613 r = convert(x, 4, 15, "0123456789ABCDEF");
617 throw new java.lang.IllegalArgumentException();
620 return pad(sign(s, r));
624 * Formats a character into a string (like sprintf in C)
627 * the value to format
628 * @return the formatted string
630 public String form(char c)
634 throw new java.lang.IllegalArgumentException();
643 * Formats a string into a larger string (like sprintf in C)
646 * the value to format
647 * @return the formatted string
649 public String form(String s)
653 throw new java.lang.IllegalArgumentException();
658 s = s.substring(0, precision);
672 * @return DOCUMENT ME!
674 private static String repeat(char c, int n)
681 StringBuffer s = new StringBuffer(n);
683 for (int i = 0; i < n; i++)
703 * @return DOCUMENT ME!
705 private static String convert(long x, int n, int m, String d)
716 r = d.charAt((int) (x & m)) + r;
729 * @return DOCUMENT ME!
731 private String pad(String r)
733 String p = repeat(' ', width - r.length());
737 return pre + r + p + post;
741 return pre + p + r + post;
753 * @return DOCUMENT ME!
755 private String sign(int s, String r)
776 if ((fmt == 'o') && alternate && (r.length() > 0)
777 && (r.charAt(0) != '0'))
781 else if ((fmt == 'x') && alternate)
785 else if ((fmt == 'X') && alternate)
797 else if (((fmt == 'd') || (fmt == 'i') || (fmt == 'x') || (fmt == 'X') || (fmt == 'o'))
803 return p + repeat('0', w - p.length() - r.length()) + r;
812 * @return DOCUMENT ME!
814 private String fixed_format(double d)
816 boolean removeTrailing = ((fmt == 'G') || (fmt == 'g')) && !alternate;
818 // remove trailing zeroes and decimal point
819 if (d > 0x7FFFFFFFFFFFFFFFL)
821 return exp_format(d);
826 return (long) (d + 0.5) + (removeTrailing ? "" : ".");
829 long whole = (long) d;
830 double fr = d - whole; // fractional part
832 if ((fr >= 1) || (fr < 0))
834 return exp_format(d);
838 String leading_zeroes = "";
840 for (int i = 1; (i <= precision) && (factor <= 0x7FFFFFFFFFFFFFFFL); i++)
843 leading_zeroes = leading_zeroes + "0";
846 long l = (long) ((factor * fr) + 0.5);
855 String z = leading_zeroes + l;
856 z = "." + z.substring(z.length() - precision, z.length());
860 int t = z.length() - 1;
862 while ((t >= 0) && (z.charAt(t) == '0'))
867 if ((t >= 0) && (z.charAt(t) == '.'))
872 z = z.substring(0, t + 1);
884 * @return DOCUMENT ME!
886 private String exp_format(double d)
910 if (((fmt == 'g') || (fmt == 'G')) && (e >= -4) && (e < precision))
912 return fixed_format(d);
916 f = f + fixed_format(d);
918 if ((fmt == 'e') || (fmt == 'g'))
940 return f + p.substring(p.length() - 3, p.length());