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