update author list in license for (JAL-826)
[jalview.git] / src / jalview / util / Format.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
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.
10  * 
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.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 /**
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
23  */
24 package jalview.util;
25
26 /**
27  * DOCUMENT ME!
28  * 
29  * @author $author$
30  * @version $Revision$
31  */
32 public class Format
33 {
34   private int width;
35
36   private int precision;
37
38   private String pre;
39
40   private String post;
41
42   private boolean leading_zeroes;
43
44   private boolean show_plus;
45
46   private boolean alternate;
47
48   private boolean show_space;
49
50   private boolean left_align;
51
52   private char fmt; // one of cdeEfgGiosxXos
53
54   /**
55    * Creates a new Format object.
56    * 
57    * @param s
58    *          DOCUMENT ME!
59    */
60   public Format(String s)
61   {
62     width = 0;
63     precision = -1;
64     pre = "";
65     post = "";
66     leading_zeroes = false;
67     show_plus = false;
68     alternate = false;
69     show_space = false;
70     left_align = false;
71     fmt = ' ';
72
73     int length = s.length();
74     int parse_state = 0;
75
76     // 0 = prefix, 1 = flags, 2 = width, 3 = precision,
77     // 4 = format, 5 = end
78     int i = 0;
79
80     while (parse_state == 0)
81     {
82       if (i >= length)
83       {
84         parse_state = 5;
85       }
86       else if (s.charAt(i) == '%')
87       {
88         if (i < (length - 1))
89         {
90           if (s.charAt(i + 1) == '%')
91           {
92             pre = pre + '%';
93             i++;
94           }
95           else
96           {
97             parse_state = 1;
98           }
99         }
100         else
101         {
102           throw new java.lang.IllegalArgumentException();
103         }
104       }
105       else
106       {
107         pre = pre + s.charAt(i);
108       }
109
110       i++;
111     }
112
113     while (parse_state == 1)
114     {
115       if (i >= length)
116       {
117         parse_state = 5;
118       }
119       else if (s.charAt(i) == ' ')
120       {
121         show_space = true;
122       }
123       else if (s.charAt(i) == '-')
124       {
125         left_align = true;
126       }
127       else if (s.charAt(i) == '+')
128       {
129         show_plus = true;
130       }
131       else if (s.charAt(i) == '0')
132       {
133         leading_zeroes = true;
134       }
135       else if (s.charAt(i) == '#')
136       {
137         alternate = true;
138       }
139       else
140       {
141         parse_state = 2;
142         i--;
143       }
144
145       i++;
146     }
147
148     while (parse_state == 2)
149     {
150       if (i >= length)
151       {
152         parse_state = 5;
153       }
154       else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9'))
155       {
156         width = ((width * 10) + s.charAt(i)) - '0';
157         i++;
158       }
159       else if (s.charAt(i) == '.')
160       {
161         parse_state = 3;
162         precision = 0;
163         i++;
164       }
165       else
166       {
167         parse_state = 4;
168       }
169     }
170
171     while (parse_state == 3)
172     {
173       if (i >= length)
174       {
175         parse_state = 5;
176       }
177       else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9'))
178       {
179         precision = ((precision * 10) + s.charAt(i)) - '0';
180         i++;
181       }
182       else
183       {
184         parse_state = 4;
185       }
186     }
187
188     if (parse_state == 4)
189     {
190       if (i >= length)
191       {
192         parse_state = 5;
193       }
194       else
195       {
196         fmt = s.charAt(i);
197       }
198
199       i++;
200     }
201
202     if (i < length)
203     {
204       post = s.substring(i, length);
205     }
206   }
207
208   /**
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
212    * 
213    * @param s
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
219    *          <ul>
220    *          <li>a % (required)
221    *          <li>a modifier (optional)
222    *          <dl>
223    *          <dt>+
224    *          <dd>forces display of + for positive numbers
225    *          <dt>0
226    *          <dd>show leading zeroes
227    *          <dt>-
228    *          <dd>align left in the field
229    *          <dt>space
230    *          <dd>prepend a space in front of positive numbers
231    *          <dt>#
232    *          <dd>use "alternate" format. Add 0 or 0x for octal or hexadecimal
233    *          numbers. Don't suppress trailing zeroes in general floating point
234    *          format.
235    *          </dl>
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)
239    *          <dl>
240    *          <dt>f
241    *          <dd>floating point number in fixed format
242    *          <dt>e, E
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.
246    *          <dt>g, G
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.
251    *          <dt>d, i
252    *          <dd>integer in decimal
253    *          <dt>x
254    *          <dd>integer in hexadecimal
255    *          <dt>o
256    *          <dd>integer in octal
257    *          <dt>s
258    *          <dd>string
259    *          <dt>c
260    *          <dd>character
261    *          </dl>
262    *          </ul>
263    * @exception IllegalArgumentException
264    *              if bad format
265    * 
266    */
267   public static String getHexString(java.awt.Color color)
268   {
269     String r;
270     String g;
271     String b;
272     r = Integer.toHexString(color.getRed());
273
274     if (r.length() < 2)
275     {
276       r = "0" + r;
277     }
278
279     g = Integer.toHexString(color.getGreen());
280
281     if (g.length() < 2)
282     {
283       g = "0" + g;
284     }
285
286     b = Integer.toHexString(color.getBlue());
287
288     if (b.length() < 2)
289     {
290       b = "0" + b;
291     }
292
293     return r + g + b;
294   }
295
296   /**
297    * prints a formatted number following printf conventions
298    * 
299    * @param s
300    *          a PrintStream
301    * @param fmt
302    *          the format string
303    * @param x
304    *          the double to print
305    */
306   public static void print(java.io.PrintStream s, String fmt, double x)
307   {
308     s.print(new Format(fmt).form(x));
309   }
310
311   /**
312    * prints a formatted number following printf conventions
313    * 
314    * @param s
315    *          a PrintStream
316    * @param fmt
317    *          the format string
318    * @param x
319    *          the long to print
320    */
321   public static void print(java.io.PrintStream s, String fmt, long x)
322   {
323     s.print(new Format(fmt).form(x));
324   }
325
326   /**
327    * prints a formatted number following printf conventions
328    * 
329    * @param s
330    *          a PrintStream
331    * @param fmt
332    *          the format string
333    * @param x
334    *          the character to
335    */
336   public static void print(java.io.PrintStream s, String fmt, char x)
337   {
338     s.print(new Format(fmt).form(x));
339   }
340
341   /**
342    * prints a formatted number following printf conventions
343    * 
344    * @param s
345    *          a PrintStream, fmt the format string
346    * @param x
347    *          a string that represents the digits to print
348    */
349   public static void print(java.io.PrintStream s, String fmt, String x)
350   {
351     s.print(new Format(fmt).form(x));
352   }
353
354   /**
355    * Converts a string of digits (decimal, octal or hex) to an integer
356    * 
357    * @param s
358    *          a string
359    * @return the numeric value of the prefix of s representing a base 10 integer
360    */
361   public static int atoi(String s)
362   {
363     return (int) atol(s);
364   }
365
366   /**
367    * Converts a string of digits (decimal, octal or hex) to a long integer
368    * 
369    * @param s
370    *          a string
371    * @return the numeric value of the prefix of s representing a base 10 integer
372    */
373   public static long atol(String s)
374   {
375     int i = 0;
376
377     while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
378     {
379       i++;
380     }
381
382     if ((i < s.length()) && (s.charAt(i) == '0'))
383     {
384       if (((i + 1) < s.length())
385               && ((s.charAt(i + 1) == 'x') || (s.charAt(i + 1) == 'X')))
386       {
387         return parseLong(s.substring(i + 2), 16);
388       }
389       else
390       {
391         return parseLong(s, 8);
392       }
393     }
394     else
395     {
396       return parseLong(s, 10);
397     }
398   }
399
400   /**
401    * DOCUMENT ME!
402    * 
403    * @param s
404    *          DOCUMENT ME!
405    * @param base
406    *          DOCUMENT ME!
407    * 
408    * @return DOCUMENT ME!
409    */
410   private static long parseLong(String s, int base)
411   {
412     int i = 0;
413     int sign = 1;
414     long r = 0;
415
416     while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
417     {
418       i++;
419     }
420
421     if ((i < s.length()) && (s.charAt(i) == '-'))
422     {
423       sign = -1;
424       i++;
425     }
426     else if ((i < s.length()) && (s.charAt(i) == '+'))
427     {
428       i++;
429     }
430
431     while (i < s.length())
432     {
433       char ch = s.charAt(i);
434
435       if (('0' <= ch) && (ch < ('0' + base)))
436       {
437         r = ((r * base) + ch) - '0';
438       }
439       else if (('A' <= ch) && (ch < (('A' + base) - 10)))
440       {
441         r = ((r * base) + ch) - 'A' + 10;
442       }
443       else if (('a' <= ch) && (ch < (('a' + base) - 10)))
444       {
445         r = ((r * base) + ch) - 'a' + 10;
446       }
447       else
448       {
449         return r * sign;
450       }
451
452       i++;
453     }
454
455     return r * sign;
456   }
457
458   /**
459    * Converts a string of digits to an double
460    * 
461    * @param s
462    *          a string
463    */
464   public static double atof(String s)
465   {
466     int i = 0;
467     int sign = 1;
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
472
473     while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
474     {
475       i++;
476     }
477
478     if ((i < s.length()) && (s.charAt(i) == '-'))
479     {
480       sign = -1;
481       i++;
482     }
483     else if ((i < s.length()) && (s.charAt(i) == '+'))
484     {
485       i++;
486     }
487
488     while (i < s.length())
489     {
490       char ch = s.charAt(i);
491
492       if (('0' <= ch) && (ch <= '9'))
493       {
494         if (state == 0)
495         {
496           r = ((r * 10) + ch) - '0';
497         }
498         else if (state == 1)
499         {
500           p = p / 10;
501           r = r + (p * (ch - '0'));
502         }
503       }
504       else if (ch == '.')
505       {
506         if (state == 0)
507         {
508           state = 1;
509         }
510         else
511         {
512           return sign * r;
513         }
514       }
515       else if ((ch == 'e') || (ch == 'E'))
516       {
517         long e = (int) parseLong(s.substring(i + 1), 10);
518
519         return sign * r * Math.pow(10, e);
520       }
521       else
522       {
523         return sign * r;
524       }
525
526       i++;
527     }
528
529     return sign * r;
530   }
531
532   /**
533    * Formats a double into a string (like sprintf in C)
534    * 
535    * @param x
536    *          the number to format
537    * @return the formatted string
538    * @exception IllegalArgumentException
539    *              if bad argument
540    */
541   public String form(double x)
542   {
543     String r;
544
545     if (precision < 0)
546     {
547       precision = 6;
548     }
549
550     int s = 1;
551
552     if (x < 0)
553     {
554       x = -x;
555       s = -1;
556     }
557
558     if (fmt == 'f')
559     {
560       r = fixed_format(x);
561     }
562     else if ((fmt == 'e') || (fmt == 'E') || (fmt == 'g') || (fmt == 'G'))
563     {
564       r = exp_format(x);
565     }
566     else
567     {
568       throw new java.lang.IllegalArgumentException();
569     }
570
571     return pad(sign(s, r));
572   }
573
574   /**
575    * Formats a long integer into a string (like sprintf in C)
576    * 
577    * @param x
578    *          the number to format
579    * @return the formatted string
580    */
581   public String form(long x)
582   {
583     String r;
584     int s = 0;
585
586     if ((fmt == 'd') || (fmt == 'i'))
587     {
588       if (x < 0)
589       {
590         r = ("" + x).substring(1);
591         s = -1;
592       }
593       else
594       {
595         r = "" + x;
596         s = 1;
597       }
598     }
599     else if (fmt == 'o')
600     {
601       r = convert(x, 3, 7, "01234567");
602     }
603     else if (fmt == 'x')
604     {
605       r = convert(x, 4, 15, "0123456789abcdef");
606     }
607     else if (fmt == 'X')
608     {
609       r = convert(x, 4, 15, "0123456789ABCDEF");
610     }
611     else
612     {
613       throw new java.lang.IllegalArgumentException();
614     }
615
616     return pad(sign(s, r));
617   }
618
619   /**
620    * Formats a character into a string (like sprintf in C)
621    * 
622    * @param x
623    *          the value to format
624    * @return the formatted string
625    */
626   public String form(char c)
627   {
628     if (fmt != 'c')
629     {
630       throw new java.lang.IllegalArgumentException();
631     }
632
633     String r = "" + c;
634
635     return pad(r);
636   }
637
638   /**
639    * Formats a string into a larger string (like sprintf in C)
640    * 
641    * @param x
642    *          the value to format
643    * @return the formatted string
644    */
645   public String form(String s)
646   {
647     if (fmt != 's')
648     {
649       throw new java.lang.IllegalArgumentException();
650     }
651
652     if (precision >= 0)
653     {
654       s = s.substring(0, precision);
655     }
656
657     return pad(s);
658   }
659
660   /**
661    * DOCUMENT ME!
662    * 
663    * @param c
664    *          DOCUMENT ME!
665    * @param n
666    *          DOCUMENT ME!
667    * 
668    * @return DOCUMENT ME!
669    */
670   private static String repeat(char c, int n)
671   {
672     if (n <= 0)
673     {
674       return "";
675     }
676
677     StringBuffer s = new StringBuffer(n);
678
679     for (int i = 0; i < n; i++)
680     {
681       s.append(c);
682     }
683
684     return s.toString();
685   }
686
687   /**
688    * DOCUMENT ME!
689    * 
690    * @param x
691    *          DOCUMENT ME!
692    * @param n
693    *          DOCUMENT ME!
694    * @param m
695    *          DOCUMENT ME!
696    * @param d
697    *          DOCUMENT ME!
698    * 
699    * @return DOCUMENT ME!
700    */
701   private static String convert(long x, int n, int m, String d)
702   {
703     if (x == 0)
704     {
705       return "0";
706     }
707
708     String r = "";
709
710     while (x != 0)
711     {
712       r = d.charAt((int) (x & m)) + r;
713       x = x >>> n;
714     }
715
716     return r;
717   }
718
719   /**
720    * DOCUMENT ME!
721    * 
722    * @param r
723    *          DOCUMENT ME!
724    * 
725    * @return DOCUMENT ME!
726    */
727   private String pad(String r)
728   {
729     String p = repeat(' ', width - r.length());
730
731     if (left_align)
732     {
733       return pre + r + p + post;
734     }
735     else
736     {
737       return pre + p + r + post;
738     }
739   }
740
741   /**
742    * DOCUMENT ME!
743    * 
744    * @param s
745    *          DOCUMENT ME!
746    * @param r
747    *          DOCUMENT ME!
748    * 
749    * @return DOCUMENT ME!
750    */
751   private String sign(int s, String r)
752   {
753     String p = "";
754
755     if (s < 0)
756     {
757       p = "-";
758     }
759     else if (s > 0)
760     {
761       if (show_plus)
762       {
763         p = "+";
764       }
765       else if (show_space)
766       {
767         p = " ";
768       }
769     }
770     else
771     {
772       if ((fmt == 'o') && alternate && (r.length() > 0)
773               && (r.charAt(0) != '0'))
774       {
775         p = "0";
776       }
777       else if ((fmt == 'x') && alternate)
778       {
779         p = "0x";
780       }
781       else if ((fmt == 'X') && alternate)
782       {
783         p = "0X";
784       }
785     }
786
787     int w = 0;
788
789     if (leading_zeroes)
790     {
791       w = width;
792     }
793     else if (((fmt == 'd') || (fmt == 'i') || (fmt == 'x') || (fmt == 'X') || (fmt == 'o'))
794             && (precision > 0))
795     {
796       w = precision;
797     }
798
799     return p + repeat('0', w - p.length() - r.length()) + r;
800   }
801
802   /**
803    * DOCUMENT ME!
804    * 
805    * @param d
806    *          DOCUMENT ME!
807    * 
808    * @return DOCUMENT ME!
809    */
810   private String fixed_format(double d)
811   {
812     boolean removeTrailing = ((fmt == 'G') || (fmt == 'g')) && !alternate;
813
814     // remove trailing zeroes and decimal point
815     if (d > 0x7FFFFFFFFFFFFFFFL)
816     {
817       return exp_format(d);
818     }
819
820     if (precision == 0)
821     {
822       return (long) (d + 0.5) + (removeTrailing ? "" : ".");
823     }
824
825     long whole = (long) d;
826     double fr = d - whole; // fractional part
827
828     if ((fr >= 1) || (fr < 0))
829     {
830       return exp_format(d);
831     }
832
833     double factor = 1;
834     String leading_zeroes = "";
835
836     for (int i = 1; (i <= precision) && (factor <= 0x7FFFFFFFFFFFFFFFL); i++)
837     {
838       factor *= 10;
839       leading_zeroes = leading_zeroes + "0";
840     }
841
842     long l = (long) ((factor * fr) + 0.5);
843
844     if (l >= factor)
845     {
846       l = 0;
847       whole++;
848     }
849
850     // CSH 10-25-97
851     String z = leading_zeroes + l;
852     z = "." + z.substring(z.length() - precision, z.length());
853
854     if (removeTrailing)
855     {
856       int t = z.length() - 1;
857
858       while ((t >= 0) && (z.charAt(t) == '0'))
859       {
860         t--;
861       }
862
863       if ((t >= 0) && (z.charAt(t) == '.'))
864       {
865         t--;
866       }
867
868       z = z.substring(0, t + 1);
869     }
870
871     return whole + z;
872   }
873
874   /**
875    * DOCUMENT ME!
876    * 
877    * @param d
878    *          DOCUMENT ME!
879    * 
880    * @return DOCUMENT ME!
881    */
882   private String exp_format(double d)
883   {
884     String f = "";
885     int e = 0;
886     double dd = d;
887     double factor = 1;
888
889     if (d != 0)
890     {
891       while (dd > 10)
892       {
893         e++;
894         factor /= 10;
895         dd = dd / 10;
896       }
897
898       while (dd < 1)
899       {
900         e--;
901         factor *= 10;
902         dd = dd * 10;
903       }
904     }
905
906     if (((fmt == 'g') || (fmt == 'G')) && (e >= -4) && (e < precision))
907     {
908       return fixed_format(d);
909     }
910
911     d = d * factor;
912     f = f + fixed_format(d);
913
914     if ((fmt == 'e') || (fmt == 'g'))
915     {
916       f = f + "e";
917     }
918     else
919     {
920       f = f + "E";
921     }
922
923     String p = "000";
924
925     if (e >= 0)
926     {
927       f = f + "+";
928       p = p + e;
929     }
930     else
931     {
932       f = f + "-";
933       p = p + (-e);
934     }
935
936     return f + p.substring(p.length() - 3, p.length());
937   }
938 }