1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.joda.time.format;
17
18 import java.io.IOException;
19 import java.io.Writer;
20
21
22
23
24
25
26
27
28
29 public class FormatUtils {
30
31 private static final double LOG_10 = Math.log(10);
32
33
34
35
36 private FormatUtils() {
37 }
38
39
40
41
42
43
44
45
46
47
48
49 public static void appendPaddedInteger(StringBuffer buf, int value, int size) {
50 if (value < 0) {
51 buf.append('-');
52 if (value != Integer.MIN_VALUE) {
53 value = -value;
54 } else {
55 for (; size > 10; size--) {
56 buf.append('0');
57 }
58 buf.append("" + -(long)Integer.MIN_VALUE);
59 return;
60 }
61 }
62 if (value < 10) {
63 for (; size > 1; size--) {
64 buf.append('0');
65 }
66 buf.append((char)(value + '0'));
67 } else if (value < 100) {
68 for (; size > 2; size--) {
69 buf.append('0');
70 }
71
72
73
74 int d = ((value + 1) * 13421772) >> 27;
75 buf.append((char) (d + '0'));
76
77 buf.append((char) (value - (d << 3) - (d << 1) + '0'));
78 } else {
79 int digits;
80 if (value < 1000) {
81 digits = 3;
82 } else if (value < 10000) {
83 digits = 4;
84 } else {
85 digits = (int)(Math.log(value) / LOG_10) + 1;
86 }
87 for (; size > digits; size--) {
88 buf.append('0');
89 }
90 buf.append(Integer.toString(value));
91 }
92 }
93
94
95
96
97
98
99
100
101
102
103
104 public static void appendPaddedInteger(StringBuffer buf, long value, int size) {
105 int intValue = (int)value;
106 if (intValue == value) {
107 appendPaddedInteger(buf, intValue, size);
108 } else if (size <= 19) {
109 buf.append(Long.toString(value));
110 } else {
111 if (value < 0) {
112 buf.append('-');
113 if (value != Long.MIN_VALUE) {
114 value = -value;
115 } else {
116 for (; size > 19; size--) {
117 buf.append('0');
118 }
119 buf.append("9223372036854775808");
120 return;
121 }
122 }
123 int digits = (int)(Math.log(value) / LOG_10) + 1;
124 for (; size > digits; size--) {
125 buf.append('0');
126 }
127 buf.append(Long.toString(value));
128 }
129 }
130
131
132
133
134
135
136
137
138
139
140
141 public static void writePaddedInteger(Writer out, int value, int size)
142 throws IOException
143 {
144 if (value < 0) {
145 out.write('-');
146 if (value != Integer.MIN_VALUE) {
147 value = -value;
148 } else {
149 for (; size > 10; size--) {
150 out.write('0');
151 }
152 out.write("" + -(long)Integer.MIN_VALUE);
153 return;
154 }
155 }
156 if (value < 10) {
157 for (; size > 1; size--) {
158 out.write('0');
159 }
160 out.write(value + '0');
161 } else if (value < 100) {
162 for (; size > 2; size--) {
163 out.write('0');
164 }
165
166
167
168 int d = ((value + 1) * 13421772) >> 27;
169 out.write(d + '0');
170
171 out.write(value - (d << 3) - (d << 1) + '0');
172 } else {
173 int digits;
174 if (value < 1000) {
175 digits = 3;
176 } else if (value < 10000) {
177 digits = 4;
178 } else {
179 digits = (int)(Math.log(value) / LOG_10) + 1;
180 }
181 for (; size > digits; size--) {
182 out.write('0');
183 }
184 out.write(Integer.toString(value));
185 }
186 }
187
188
189
190
191
192
193
194
195
196
197
198 public static void writePaddedInteger(Writer out, long value, int size)
199 throws IOException
200 {
201 int intValue = (int)value;
202 if (intValue == value) {
203 writePaddedInteger(out, intValue, size);
204 } else if (size <= 19) {
205 out.write(Long.toString(value));
206 } else {
207 if (value < 0) {
208 out.write('-');
209 if (value != Long.MIN_VALUE) {
210 value = -value;
211 } else {
212 for (; size > 19; size--) {
213 out.write('0');
214 }
215 out.write("9223372036854775808");
216 return;
217 }
218 }
219 int digits = (int)(Math.log(value) / LOG_10) + 1;
220 for (; size > digits; size--) {
221 out.write('0');
222 }
223 out.write(Long.toString(value));
224 }
225 }
226
227
228
229
230
231
232
233
234
235 public static void appendUnpaddedInteger(StringBuffer buf, int value) {
236 if (value < 0) {
237 buf.append('-');
238 if (value != Integer.MIN_VALUE) {
239 value = -value;
240 } else {
241 buf.append("" + -(long)Integer.MIN_VALUE);
242 return;
243 }
244 }
245 if (value < 10) {
246 buf.append((char)(value + '0'));
247 } else if (value < 100) {
248
249
250
251 int d = ((value + 1) * 13421772) >> 27;
252 buf.append((char) (d + '0'));
253
254 buf.append((char) (value - (d << 3) - (d << 1) + '0'));
255 } else {
256 buf.append(Integer.toString(value));
257 }
258 }
259
260
261
262
263
264
265
266
267
268 public static void appendUnpaddedInteger(StringBuffer buf, long value) {
269 int intValue = (int)value;
270 if (intValue == value) {
271 appendUnpaddedInteger(buf, intValue);
272 } else {
273 buf.append(Long.toString(value));
274 }
275 }
276
277
278
279
280
281
282
283
284
285 public static void writeUnpaddedInteger(Writer out, int value)
286 throws IOException
287 {
288 if (value < 0) {
289 out.write('-');
290 if (value != Integer.MIN_VALUE) {
291 value = -value;
292 } else {
293 out.write("" + -(long)Integer.MIN_VALUE);
294 return;
295 }
296 }
297 if (value < 10) {
298 out.write(value + '0');
299 } else if (value < 100) {
300
301
302
303 int d = ((value + 1) * 13421772) >> 27;
304 out.write(d + '0');
305
306 out.write(value - (d << 3) - (d << 1) + '0');
307 } else {
308 out.write(Integer.toString(value));
309 }
310 }
311
312
313
314
315
316
317
318
319
320 public static void writeUnpaddedInteger(Writer out, long value)
321 throws IOException
322 {
323 int intValue = (int)value;
324 if (intValue == value) {
325 writeUnpaddedInteger(out, intValue);
326 } else {
327 out.write(Long.toString(value));
328 }
329 }
330
331
332
333
334
335 public static int calculateDigitCount(long value) {
336 if (value < 0) {
337 if (value != Long.MIN_VALUE) {
338 return calculateDigitCount(-value) + 1;
339 } else {
340 return 20;
341 }
342 }
343 return
344 (value < 10 ? 1 :
345 (value < 100 ? 2 :
346 (value < 1000 ? 3 :
347 (value < 10000 ? 4 :
348 ((int)(Math.log(value) / LOG_10) + 1)))));
349 }
350
351 static int parseTwoDigits(String text, int position) {
352 int value = text.charAt(position) - '0';
353 return ((value << 3) + (value << 1)) + text.charAt(position + 1) - '0';
354 }
355
356 static String createErrorMessage(final String text, final int errorPos) {
357 int sampleLen = errorPos + 32;
358 String sampleText;
359 if (text.length() <= sampleLen + 3) {
360 sampleText = text;
361 } else {
362 sampleText = text.substring(0, sampleLen).concat("...");
363 }
364
365 if (errorPos <= 0) {
366 return "Invalid format: \"" + sampleText + '"';
367 }
368
369 if (errorPos >= text.length()) {
370 return "Invalid format: \"" + sampleText + "\" is too short";
371 }
372
373 return "Invalid format: \"" + sampleText + "\" is malformed at \"" +
374 sampleText.substring(errorPos) + '"';
375 }
376
377 }