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
29 import java.util.Arrays;
41 private int precision;
47 private boolean leading_zeroes;
49 private boolean show_plus;
51 private boolean alternate;
53 private boolean show_space;
55 private boolean left_align;
57 private char fmt; // one of cdeEfgGiosxXos
59 private final String formatString;
62 * Creates a new Format object.
67 public Format(String s)
74 leading_zeroes = false;
81 int length = s.length();
84 // 0 = prefix, 1 = flags, 2 = width, 3 = precision,
85 // 4 = format, 5 = end
88 while (parse_state == 0)
94 else if (s.charAt(i) == '%')
98 if (s.charAt(i + 1) == '%')
110 throw new java.lang.IllegalArgumentException();
115 pre = pre + s.charAt(i);
121 while (parse_state == 1)
127 else if (s.charAt(i) == ' ')
131 else if (s.charAt(i) == '-')
135 else if (s.charAt(i) == '+')
139 else if (s.charAt(i) == '0')
141 leading_zeroes = true;
143 else if (s.charAt(i) == '#')
156 while (parse_state == 2)
162 else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9'))
164 width = ((width * 10) + s.charAt(i)) - '0';
167 else if (s.charAt(i) == '.')
179 while (parse_state == 3)
185 else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9'))
187 precision = ((precision * 10) + s.charAt(i)) - '0';
196 if (parse_state == 4)
212 post = s.substring(i, length);
217 * Formats the number following printf conventions. Main limitation: Can only
218 * handle one format parameter at a time Use multiple Format objects to format
219 * more than one number
222 * the format string following printf conventions The string has a
223 * prefix, a format code and a suffix. The prefix and suffix become
224 * part of the formatted output. The format code directs the
225 * formatting of the (single) parameter to be formatted. The code has
226 * the following structure
229 * <li>a modifier (optional)
232 * <dd>forces display of + for positive numbers
234 * <dd>show leading zeroes
236 * <dd>align left in the field
238 * <dd>prepend a space in front of positive numbers
240 * <dd>use "alternate" format. Add 0 or 0x for octal or hexadecimal
241 * numbers. Don't suppress trailing zeroes in general floating point
244 * <li>an integer denoting field width (optional)
245 * <li>a period followed by an integer denoting precision (optional)
246 * <li>a format descriptor (required)
249 * <dd>floating point number in fixed format
251 * <dd>floating point number in exponential notation (scientific
252 * format). The E format results in an uppercase E for the exponent
253 * (1.14130E+003), the e format in a lowercase e.
255 * <dd>floating point number in general format (fixed format for
256 * small numbers, exponential format for large numbers). Trailing
257 * zeroes are suppressed. The G format results in an uppercase E for
258 * the exponent (if any), the g format in a lowercase e.
260 * <dd>integer in decimal
262 * <dd>integer in hexadecimal
264 * <dd>integer in octal
271 * @exception IllegalArgumentException
275 public static String getHexString(java.awt.Color color)
280 r = Integer.toHexString(color.getRed());
287 g = Integer.toHexString(color.getGreen());
294 b = Integer.toHexString(color.getBlue());
305 * prints a formatted number following printf conventions
312 * the double to print
314 public static void print(java.io.PrintStream s, String fmt, double x)
316 s.print(new Format(fmt).form(x));
320 * prints a formatted number following printf conventions
329 public static void print(java.io.PrintStream s, String fmt, long x)
331 s.print(new Format(fmt).form(x));
335 * prints a formatted number following printf conventions
344 public static void print(java.io.PrintStream s, String fmt, char x)
346 s.print(new Format(fmt).form(x));
350 * prints a formatted number following printf conventions
353 * a PrintStream, fmt the format string
355 * a string that represents the digits to print
357 public static void print(java.io.PrintStream s, String fmt, String x)
359 s.print(new Format(fmt).form(x));
363 * Converts a string of digits (decimal, octal or hex) to an integer
367 * @return the numeric value of the prefix of s representing a base 10 integer
369 public static int atoi(String s)
371 return (int) atol(s);
375 * Converts a string of digits (decimal, octal or hex) to a long integer
379 * @return the numeric value of the prefix of s representing a base 10 integer
381 public static long atol(String s)
385 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
390 if ((i < s.length()) && (s.charAt(i) == '0'))
392 if (((i + 1) < s.length())
393 && ((s.charAt(i + 1) == 'x') || (s.charAt(i + 1) == 'X')))
395 return parseLong(s.substring(i + 2), 16);
399 return parseLong(s, 8);
404 return parseLong(s, 10);
416 * @return DOCUMENT ME!
418 private static long parseLong(String s, int base)
424 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
429 if ((i < s.length()) && (s.charAt(i) == '-'))
434 else if ((i < s.length()) && (s.charAt(i) == '+'))
439 while (i < s.length())
441 char ch = s.charAt(i);
443 if (('0' <= ch) && (ch < ('0' + base)))
445 r = ((r * base) + ch) - '0';
447 else if (('A' <= ch) && (ch < (('A' + base) - 10)))
449 r = ((r * base) + ch) - 'A' + 10;
451 else if (('a' <= ch) && (ch < (('a' + base) - 10)))
453 r = ((r * base) + ch) - 'a' + 10;
467 * Converts a string of digits to an double
472 public static double atof(String s)
476 double r = 0; // integer part
477 // double f = 0; // fractional part
478 double p = 1; // exponent of fractional part
479 int state = 0; // 0 = int part, 1 = frac part
481 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
486 if ((i < s.length()) && (s.charAt(i) == '-'))
491 else if ((i < s.length()) && (s.charAt(i) == '+'))
496 while (i < s.length())
498 char ch = s.charAt(i);
500 if (('0' <= ch) && (ch <= '9'))
504 r = ((r * 10) + ch) - '0';
509 r = r + (p * (ch - '0'));
523 else if ((ch == 'e') || (ch == 'E'))
525 long e = (int) parseLong(s.substring(i + 1), 10);
527 return sign * r * Math.pow(10, e);
541 * Formats a double into a string (like sprintf in C)
544 * the number to format
545 * @return the formatted string
546 * @exception IllegalArgumentException
549 public String form(double x)
570 else if ((fmt == 'e') || (fmt == 'E') || (fmt == 'g') || (fmt == 'G'))
576 throw new java.lang.IllegalArgumentException();
579 return pad(sign(s, r));
583 * Formats a long integer into a string (like sprintf in C)
586 * the number to format
587 * @return the formatted string
589 public String form(long x)
594 if ((fmt == 'd') || (fmt == 'i'))
598 r = ("" + x).substring(1);
609 r = convert(x, 3, 7, "01234567");
613 r = convert(x, 4, 15, "0123456789abcdef");
617 r = convert(x, 4, 15, "0123456789ABCDEF");
621 throw new java.lang.IllegalArgumentException();
624 return pad(sign(s, r));
628 * Formats a character into a string (like sprintf in C)
630 * @param debounceTrap
631 * the value to format
632 * @return the formatted string
634 public String form(char c)
638 throw new java.lang.IllegalArgumentException();
647 * Formats a string into a larger string (like sprintf in C)
649 * @param debounceTrap
650 * the value to format
651 * @return the formatted string
653 public String form(String s)
657 throw new java.lang.IllegalArgumentException();
662 s = s.substring(0, precision);
669 * Returns a string consisting of n repeats of character c
676 static String repeat(char c, int n)
682 char[] chars = new char[n];
683 Arrays.fill(chars, c);
684 return new String(chars);
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')
794 || (fmt == 'o')) && (precision > 0))
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)
837 && (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)
904 if (((fmt == 'g') || (fmt == 'G')) && (e >= -4) && (e < precision))
906 return fixed_format(d);
909 f = f + fixed_format(dd);
911 if ((fmt == 'e') || (fmt == 'g'))
933 return f + p.substring(p.length() - 3, p.length());
937 public String toString()
943 * Bespoke method to format percentage float value to the specified number of
944 * decimal places. Avoids use of general-purpose format parsers as a
945 * processing hotspot.
951 public static void appendPercentage(StringBuilder sb, float value, int dp)
958 for (int i = 0; i < dp; i++)
968 value = (float) (d / factor);
969 sb.append((long) value);
979 value = value - (int) value;
981 sb.append((int) value);