lose embedded source jars from upstream branch
[debian/openrocket] / core / src / net / sf / openrocket / util / TextUtil.java
1 package net.sf.openrocket.util;
2
3
4 public class TextUtil {
5         private static final char[] HEX = {
6                         '0', '1', '2', '3', '4', '5', '6', '7',
7                         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
8         };
9         
10         
11         /**
12          * Return the bytes formatted as a hexadecimal string.  The length of the
13          * string will be twice the number of bytes, with no spacing between the bytes
14          * and lowercase letters utilized.
15          * 
16          * @param bytes         the bytes to convert.
17          * @return                      the bytes in hexadecimal notation.
18          */
19         public static final String hexString(byte[] bytes) {
20                 StringBuilder sb = new StringBuilder(bytes.length * 2);
21                 for (byte b : bytes) {
22                         sb.append(HEX[(b >>> 4) & 0xF]);
23                         sb.append(HEX[b & 0xF]);
24                 }
25                 return sb.toString();
26         }
27         
28         /**
29          * Return a string of the double value with suitable precision (5 digits).
30          * The string is the shortest representation of the value including the
31          * required precision.
32          * 
33          * @param d             the value to present.
34          * @return              a representation with suitable precision.
35          */
36         public static final String doubleToString(double d) {
37                 
38                 // Check for special cases
39                 if (MathUtil.equals(d, 0))
40                         return "0";
41                 
42                 if (Double.isNaN(d))
43                         return "NaN";
44                 
45                 if (Double.isInfinite(d)) {
46                         if (d < 0)
47                                 return "-Inf";
48                         else
49                                 return "Inf";
50                 }
51                 
52
53                 final String sign = (d < 0) ? "-" : "";
54                 double abs = Math.abs(d);
55                 
56                 // Small and large values always in exponential notation
57                 if (abs < 0.001 || abs >= 100000000) {
58                         return sign + exponentialFormat(abs);
59                 }
60                 
61                 // Check whether decimal or exponential notation is shorter
62                 
63                 String exp = exponentialFormat(abs);
64                 String dec = decimalFormat(abs);
65                 
66                 if (dec.length() <= exp.length())
67                         return sign + dec;
68                 else
69                         return sign + exp;
70         }
71         
72         
73         /*
74          * value must be positive and not zero!
75          */
76         private static String exponentialFormat(double value) {
77                 int exp;
78                 
79                 exp = 0;
80                 while (value < 1.0) {
81                         value *= 10;
82                         exp--;
83                 }
84                 while (value >= 10.0) {
85                         value /= 10;
86                         exp++;
87                 }
88                 
89                 return shortDecimal(value, 4) + "e" + exp;
90         }
91         
92         
93         /*
94          * value must be positive and not zero!
95          */
96         private static String decimalFormat(double value) {
97                 if (value >= 10000)
98                         return "" + (int) (value + 0.5);
99                 
100                 int decimals = 1;
101                 double v = value;
102                 while (v < 1000) {
103                         v *= 10;
104                         decimals++;
105                 }
106                 
107                 return shortDecimal(value, decimals);
108         }
109         
110         
111
112
113         /*
114          * value must be positive!
115          */
116         private static String shortDecimal(double value, int decimals) {
117                 
118                 // Calculate rounding and limit values (rounding slightly smaller)
119                 int rounding = 1;
120                 double limit = 0.5;
121                 for (int i = 0; i < decimals; i++) {
122                         rounding *= 10;
123                         limit /= 10;
124                 }
125                 
126                 // Round value
127                 value = (Math.rint(value * rounding) + 0.1) / rounding;
128                 
129
130                 int whole = (int) value;
131                 value -= whole;
132                 
133
134                 if (value < limit)
135                         return "" + whole;
136                 limit *= 10;
137                 
138                 StringBuilder sb = new StringBuilder();
139                 sb.append("" + whole);
140                 sb.append('.');
141                 
142
143                 for (int i = 0; i < decimals; i++) {
144                         
145                         value *= 10;
146                         whole = (int) value;
147                         value -= whole;
148                         sb.append((char) ('0' + whole));
149                         
150                         if (value < limit)
151                                 return sb.toString();
152                         limit *= 10;
153                         
154                 }
155                 
156                 return sb.toString();
157         }
158         
159         
160         public static String htmlEncode(String s) {
161                 s = s.replace("&", "&amp;");
162                 s = s.replace("\"", "&quot;");
163                 s = s.replace("<", "&lt;");
164                 s = s.replace(">", "&gt;");
165                 return s;
166         }
167 }