~ubuntu-branches/ubuntu/maverick/electric/maverick

« back to all changes in this revision

Viewing changes to com/sun/electric/tool/simulation/test/TextUtils.java

  • Committer: Bazaar Package Importer
  • Author(s): Onkar Shinde
  • Date: 2010-01-09 16:26:04 UTC
  • mfrom: (1.1.4 upstream) (3.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100109162604-1ypvmy8ijmlc6oq7
Tags: 8.10-1
* New upstream version.
* debian/control
  - Add libjava3d-java and quilt build dependencies.
  - Update standards version to 3.8.3.
  - Add libjava3d-java as recommends to binary package.
* debian/rules
  - Use quilt patch system instead of simple patchsys.
  - Add java3d related jar files to DEB_JARS.
* debian/patches/*
  - Update as per current upstream source. Convert to quilt.
* debian/ant.properties
  - Do not disable 3D plugin anymore.
  - Use new property to disable compilation of OS X related classes.
* debian/wrappers/electric
  - Add java3d related jar files to runtime classpath.
* debian/README.source
  - Change text to the appropriate one for quilt.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * File: TextUtils.java
 
3
 *
 
4
 * Copyright (c) 2004,2005 by Sun Microsystems, Inc.
 
5
 *
 
6
 */
 
7
package com.sun.electric.tool.simulation.test;
 
8
 
 
9
import java.text.NumberFormat;
 
10
import java.text.DecimalFormat;
 
11
import java.util.Locale;
 
12
 
 
13
/**
 
14
 * This class is a collection of text utilities.
 
15
 */
 
16
public class TextUtils
 
17
{
 
18
    /**
 
19
     * Determines if the specified character is a ISO-LATIN-1 digit
 
20
         * (<code>'0'</code> through <code>'9'</code>).
 
21
     * <p>
 
22
         * This can be method instead of Character, if we are not ready
 
23
         * to handle Arabi-Indic, Devanagaru and other digits.
 
24
         *
 
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)
 
29
     */
 
30
    public static boolean isDigit(char ch) {
 
31
        return '0' <= ch && ch <= '9';
 
32
    }
 
33
 
 
34
    /**
 
35
     * Determines if the specified character is a letter or digit.
 
36
     * <p>
 
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.
 
41
     *
 
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)
 
48
     */
 
49
    public static boolean isLetterOrDigit(char ch) {
 
50
        return isDigit(ch) || Character.isLetter(ch);
 
51
    }
 
52
 
 
53
        /**
 
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.
 
58
         */
 
59
        public static boolean startsWithIgnoreCase(String main, String with)
 
60
        {
 
61
                int mainLen = main.length();
 
62
                int withLen = with.length();
 
63
                if (withLen > mainLen) return false;
 
64
                for(int i=0; i<withLen; i++)
 
65
                {
 
66
                        char mainChr = Character.toLowerCase(main.charAt(i));
 
67
                        char withChr = Character.toLowerCase(with.charAt(i));
 
68
                        if (mainChr != withChr) return false;
 
69
                }
 
70
                return true;
 
71
        }
 
72
 
 
73
        /**
 
74
         * Method to parse the number in a string.
 
75
         * <P>
 
76
         * There are many reasons to use this method instead of Integer.parseInt...
 
77
         * <UL>
 
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).
 
89
         * </UL>
 
90
         * @param s the string with a number in it.
 
91
         * @return the numeric value.
 
92
         */
 
93
        public static int atoi(String s)
 
94
        {
 
95
                return atoi(s, 0, 0);
 
96
        }
 
97
 
 
98
        /**
 
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.
 
104
         */
 
105
        public static int atoi(String s, int pos)
 
106
        {
 
107
                return atoi(s, pos, 0);
 
108
        }
 
109
 
 
110
        /**
 
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.
 
117
         */
 
118
        public static int atoi(String s, int pos, int base)
 
119
        {
 
120
                int num = 0;
 
121
                int sign = 1;
 
122
                int len = s.length();
 
123
                if (pos < len && s.charAt(pos) == '-')
 
124
                {
 
125
                        pos++;
 
126
                        sign = -1;
 
127
                }
 
128
                if (base == 0)
 
129
                {
 
130
                        base = 10;
 
131
                        if (pos < len && s.charAt(pos) == '0')
 
132
                        {
 
133
                                pos++;
 
134
                                base = 8;
 
135
                                if (pos < len && (s.charAt(pos) == 'x' || s.charAt(pos) == 'X'))
 
136
                                {
 
137
                                        pos++;
 
138
                                        base = 16;
 
139
                                } else if (pos < len && (s.charAt(pos) == 'b' || s.charAt(pos) == 'B'))
 
140
                                {
 
141
                                        pos++;
 
142
                                        base = 2;
 
143
                                }
 
144
                        }
 
145
                }
 
146
                for(; pos < len; pos++)
 
147
                {
 
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'))
 
153
//                      {
 
154
//                              if (base != 16) break;
 
155
//                              num = num * 16;
 
156
//                              if (cat >= 'a' && cat <= 'f') num += cat - 'a' + 10; else
 
157
//                                      num += cat - 'A' + 10;
 
158
//                              continue;
 
159
//                      }
 
160
//                      if (!TextUtils.isDigit(cat)) break;
 
161
//                      if (cat >= '8' && base == 8) break;
 
162
//                      num = num * base + cat - '0';
 
163
                }
 
164
                return(num * sign);
 
165
        }
 
166
 
 
167
    private static NumberFormat numberFormatSpecific = null;
 
168
 
 
169
    /**
 
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.
 
174
     */
 
175
    public static String formatDouble(double v)
 
176
    {
 
177
        return formatDouble(v, 3);
 
178
    }
 
179
 
 
180
        /**
 
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.
 
189
         */
 
190
        public static synchronized String formatDouble(double v, int numFractions)
 
191
        {
 
192
                if (numberFormatSpecific == null) {
 
193
            numberFormatSpecific = NumberFormat.getInstance(Locale.US);
 
194
            if (numberFormatSpecific != null) numberFormatSpecific.setGroupingUsed(false);
 
195
            try {
 
196
                DecimalFormat d = (DecimalFormat)numberFormatSpecific;
 
197
                d.setDecimalSeparatorAlwaysShown(false);
 
198
            } catch (Exception e) {}
 
199
 
 
200
        }
 
201
        if (numFractions == 0) {
 
202
            numberFormatSpecific.setMaximumFractionDigits(340);
 
203
        } else {
 
204
            numberFormatSpecific.setMaximumFractionDigits(numFractions);
 
205
        }
 
206
                return numberFormatSpecific.format(v);
 
207
        }
 
208
 
 
209
}