4
* Copyright (c) 2004,2005 by Sun Microsystems, Inc.
7
package com.sun.electric.tool.simulation.test;
9
import java.text.NumberFormat;
10
import java.text.DecimalFormat;
11
import java.util.Locale;
14
* This class is a collection of text utilities.
16
public class TextUtils
19
* Determines if the specified character is a ISO-LATIN-1 digit
20
* (<code>'0'</code> through <code>'9'</code>).
22
* This can be method instead of Character, if we are not ready
23
* to handle Arabi-Indic, Devanagaru and other digits.
25
* @param ch the character to be tested.
26
* @return <code>true</code> if the character is a ISO-LATIN-1 digit;
27
* <code>false</code> otherwise.
28
* @see java.lang.Character#isDigit(char)
30
public static boolean isDigit(char ch) {
31
return '0' <= ch && ch <= '9';
35
* Determines if the specified character is a letter or digit.
37
* A character is considered to be a letter or digit if either
38
* <code>Character.isLetter(char ch)</code> or
39
* <code>TextUtils.isDigit(char ch)</code> returns
40
* <code>true</code> for the character.
42
* @param ch the character to be tested.
43
* @return <code>true</code> if the character is a letter or digit;
44
* <code>false</code> otherwise.
45
* @see TextUtils#isDigit(char)
46
* @see java.lang.Character#isJavaLetterOrDigit(char)
47
* @see java.lang.Character#isLetter(char)
49
public static boolean isLetterOrDigit(char ch) {
50
return isDigit(ch) || Character.isLetter(ch);
54
* Method to determine if one string is a subset of another, but case-insensitive.
55
* @param main the main string.
56
* @param with the substring.
57
* @return true if the main string starts with the substring, ignoring case.
59
public static boolean startsWithIgnoreCase(String main, String with)
61
int mainLen = main.length();
62
int withLen = with.length();
63
if (withLen > mainLen) return false;
64
for(int i=0; i<withLen; i++)
66
char mainChr = Character.toLowerCase(main.charAt(i));
67
char withChr = Character.toLowerCase(with.charAt(i));
68
if (mainChr != withChr) return false;
74
* Method to parse the number in a string.
76
* There are many reasons to use this method instead of Integer.parseInt...
78
* <LI>This method can handle any radix.
79
* If the number begins with "0", presume base 8.
80
* If the number begins with "0b", presume base 2.
81
* If the number begins with "0x", presume base 16.
82
* Otherwise presume base 10.
83
* <LI>This method can handle numbers that affect the sign bit.
84
* If you give 0xFFFFFFFF to Integer.parseInt, you get a numberFormatPostFix exception.
85
* This method properly returns -1.
86
* <LI>This method does not require that the entire string be part of the number.
87
* If there is extra text after the end, Integer.parseInt fails (for example "123xx").
88
* <LI>This method does not throw an exception if the number is invalid (or blank).
90
* @param s the string with a number in it.
91
* @return the numeric value.
93
public static int atoi(String s)
99
* Method to parse the number in a string.
100
* See the comments for "atoi(String s)" for reasons why this method exists.
101
* @param s the string with a number in it.
102
* @param pos the starting position in the string to find the number.
103
* @return the numeric value.
105
public static int atoi(String s, int pos)
107
return atoi(s, pos, 0);
111
* Method to parse the number in a string.
112
* See the comments for "atoi(String s)" for reasons why this method exists.
113
* @param s the string with a number in it.
114
* @param pos the starting position in the string to find the number.
115
* @param base the forced base of the number (0 to determine it automatically).
116
* @return the numeric value.
118
public static int atoi(String s, int pos, int base)
122
int len = s.length();
123
if (pos < len && s.charAt(pos) == '-')
131
if (pos < len && s.charAt(pos) == '0')
135
if (pos < len && (s.charAt(pos) == 'x' || s.charAt(pos) == 'X'))
139
} else if (pos < len && (s.charAt(pos) == 'b' || s.charAt(pos) == 'B'))
146
for(; pos < len; pos++)
148
char cat = s.charAt(pos);
149
int digit = Character.digit(cat, base);
150
if (digit < 0) break;
151
num = num * base + digit;
152
// if ((cat >= 'a' && cat <= 'f') || (cat >= 'A' && cat <= 'F'))
154
// if (base != 16) break;
156
// if (cat >= 'a' && cat <= 'f') num += cat - 'a' + 10; else
157
// num += cat - 'A' + 10;
160
// if (!TextUtils.isDigit(cat)) break;
161
// if (cat >= '8' && base == 8) break;
162
// num = num * base + cat - '0';
167
private static NumberFormat numberFormatSpecific = null;
170
* Method to convert a double to a string.
171
* If the double has no precision past the decimal, none will be shown.
172
* @param v the double value to format.
173
* @return the string representation of the number.
175
public static String formatDouble(double v)
177
return formatDouble(v, 3);
181
* Method to convert a double to a string.
182
* It will show up to 'numFractions' digits past the decimal point if numFractions is greater
183
* than zero. If numFractions is 0, it will show infinite (as far as doubles go) precision.
184
* If the double has no precision past the decimal, none will be shown.
185
* This method is now thread safe.
186
* @param v the double value to format.
187
* @param numFractions the number of digits to the right of the decimal point.
188
* @return the string representation of the number.
190
public static synchronized String formatDouble(double v, int numFractions)
192
if (numberFormatSpecific == null) {
193
numberFormatSpecific = NumberFormat.getInstance(Locale.US);
194
if (numberFormatSpecific != null) numberFormatSpecific.setGroupingUsed(false);
196
DecimalFormat d = (DecimalFormat)numberFormatSpecific;
197
d.setDecimalSeparatorAlwaysShown(false);
198
} catch (Exception e) {}
201
if (numFractions == 0) {
202
numberFormatSpecific.setMaximumFractionDigits(340);
204
numberFormatSpecific.setMaximumFractionDigits(numFractions);
206
return numberFormatSpecific.format(v);