com.sun.electric.database.text
Class TextUtils

java.lang.Object
  extended by com.sun.electric.database.text.TextUtils

public class TextUtils
extends java.lang.Object

This class is a collection of text utilities.


Nested Class Summary
static class TextUtils.CellsByDate
          Comparator class for sorting Cells by their date.
static class TextUtils.CellsByName
          Comparator class for sorting Cells by their name (NOT considering numbers in the names).
static class TextUtils.CellsByVersion
          Comparator class for sorting Cells by their version number.
static class TextUtils.CellsByView
          Comparator class for sorting Cells by their view order.
static class TextUtils.NetworksByName
          Comparator class for sorting Networks by their name.
static class TextUtils.ObjectsByToString
          Comparator class for sorting Objects by their string name.
static class TextUtils.PrefsByName
          Comparator class for sorting Preferences by their name.
static class TextUtils.UnitScale
          Unit is a typesafe enum class that describes a unit scale (metric factors of 10).
static class TextUtils.WhatToSearch
          Class to define the kind of text string to search
 
Field Summary
static java.util.Comparator<Connection> CONNECTIONS_ORDER
           
static java.util.Comparator<java.lang.String> STRING_NUMBER_ORDER
          A comparator object for sorting Strings that may have numbers in them.
 
Constructor Summary
TextUtils()
           
 
Method Summary
static double atof(java.lang.String text)
          Method to parse the floating-point number in a string.
static double atof(java.lang.String text, java.lang.Double defaultVal)
          This is the same as TextUtils.atof, except upon failure to convert the passed text to a number, it returns the value in 'defaultVal'.
static int atoi(java.lang.String s)
          Method to parse the number in a string.
static int atoi(java.lang.String s, int pos)
          Method to parse the number in a string.
static int atoi(java.lang.String s, int pos, int base)
          Method to parse the number in a string.
static char canonicChar(char ch)
          Returns canonic char for ignore-case comparison .
static java.lang.String canonicString(java.lang.String s)
          Returns canonic string for ignore-case comparision .
static double convertDistance(double value, Technology tech, TextUtils.UnitScale unitScale)
          Method to convert a database coordinate into real spacing.
static double convertFromDistance(double value, Technology tech, TextUtils.UnitScale unitScale)
          Method to convert real spacing into a database coordinate.
static java.lang.String convertToEngineeringNotation(double value)
          Method to converts a floating point number into engineering units such as pico, micro, milli, etc.
static java.lang.String convertToEngineeringNotation(double value, java.lang.String unit)
          Method to converts a floating point number into engineering units such as pico, micro, milli, etc.
static java.lang.String convertToEngineeringNotation(double time, java.lang.String unit, int precpower)
           
static java.lang.String correctName(java.lang.String n, boolean onlyBrackets, boolean correctBrackets)
          Method to replace all special characters in the instance name coming from external files such as"/"..
static int findStringInString(java.lang.String string, java.lang.String search, int startingPos, boolean caseSensitive, boolean reverse)
          Method to find a string inside of another string.
static java.lang.String formatDate(java.util.Date date)
          Method to convert a Date to a String using local TimeZone.
static java.lang.String formatDateGMT(java.util.Date date)
          Method to convert a Date to a String using GMT TimeZone.
static java.lang.String formatDouble(double v)
          Method to convert a double to a string.
static java.lang.String formatDouble(double v, int numFractions)
          Method to convert a double to a string.
static java.lang.String formatDoublePostFix(double v)
          Method to convert a double to a string.
static java.util.List<java.lang.String> getAllResources(java.lang.String resourceName)
          Method to examine a path and return all resources found there.
static java.lang.String getElapsedTime(long milliseconds)
          Method to describe a time value as a String.
static java.lang.String getExtension(java.net.URL url)
          Method to return the extension of the file in a URL.
static java.io.File getFile(java.net.URL url)
          Get the file for the URL.
static java.lang.String getFileNameWithoutExtension(java.lang.String fileName)
          Method to return the pure file name of a file path.
static java.lang.String getFileNameWithoutExtension(java.net.URL url)
          Method to return the pure file name of a URL.
static java.lang.String getFilePath(java.net.URL url)
          Method to return the directory path part of a URL (excluding the file name).
static java.lang.String getTextOnClipboard()
          Method for obtaining the string on the system clipboard.
static java.awt.Color[] getTransparentColors(java.lang.String str)
          Method to convert a string with color values into an array of colors.
static java.io.InputStream getURLStream(java.net.URL url)
          Method to open an input stream to a URL.
static java.io.InputStream getURLStream(java.net.URL url, java.lang.StringBuffer errorMsg)
          Method to open an input stream to a URL.
static double getValueOfExpression(java.lang.String expression)
          Method to get the numeric value of a string that may be an expression.
static boolean isANumber(java.lang.String pp)
          Method to determine whether or not a string is a number.
static boolean isANumberPostFix(java.lang.String pp)
          Method to determine whether or not a string is a postfix formatted number, such as 1.02f.
static boolean isDigit(char ch)
          Determines if the specified character is a ISO-LATIN-1 digit ('0' through '9').
static boolean isLetterOrDigit(char ch)
          Determines if the specified character is a letter or digit.
static java.lang.String makeUnits(double value, AbstractTextDescriptor.Unit units)
          Method to convert a floating point value to a string, given that it is a particular type of unit.
static java.net.URL makeURLToFile(java.lang.String fileName)
          Method to convert a file path to a URL.
static java.lang.Number parsePostFixNumber(java.lang.String s)
          Try to parse the user input String s into a Number.
static java.lang.String[] parseString(java.lang.String line, java.lang.String delim)
          Method to break a line into keywords, separated by white space or comma
static void printLongString(java.lang.String str)
          Method to print a very long string.
static void setTextOnClipboard(java.lang.String text)
          Method to set the string stored on the system clipboard.
static boolean startsWithIgnoreCase(java.lang.String main, java.lang.String with)
          Method to determine if one string is a subset of another, but case-insensitive.
static java.lang.String toBlankPaddedString(double value, int width)
          Method to convert a double to a string that is left-padded with spaces
static java.lang.String toBlankPaddedString(int value, int width)
          Method to convert an integer to a string that is left-padded with spaces
static boolean URLExists(java.net.URL url)
          Method to tell whether a given URL exists.
static boolean URLExists(java.net.URL url, java.lang.StringBuffer errorMsg)
          Method to tell whether a given URL exists.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STRING_NUMBER_ORDER

public static final java.util.Comparator<java.lang.String> STRING_NUMBER_ORDER
A comparator object for sorting Strings that may have numbers in them. Created once because it is used often.


CONNECTIONS_ORDER

public static final java.util.Comparator<Connection> CONNECTIONS_ORDER
Constructor Detail

TextUtils

public TextUtils()
Method Detail

isDigit

public static boolean isDigit(char ch)
Determines if the specified character is a ISO-LATIN-1 digit ('0' through '9').

This can be method instead of Character, if we are not ready to handle Arabi-Indic, Devanagaru and other digits.

Parameters:
ch - the character to be tested.
Returns:
true if the character is a ISO-LATIN-1 digit; false otherwise.
See Also:
Character.isDigit(char)

isLetterOrDigit

public static boolean isLetterOrDigit(char ch)
Determines if the specified character is a letter or digit.

A character is considered to be a letter or digit if either Character.isLetter(char ch) or TextUtils.isDigit(char ch) returns true for the character.

Parameters:
ch - the character to be tested.
Returns:
true if the character is a letter or digit; false otherwise.
See Also:
isDigit(char), Character.isJavaLetterOrDigit(char), Character.isLetter(char)

canonicChar

public static char canonicChar(char ch)
Returns canonic char for ignore-case comparison . This is the same as Character.toLowerCase(Character.toUpperCase(ch)).

Parameters:
ch - given char.
Returns:
canonic char fo rthe given char.

canonicString

public static java.lang.String canonicString(java.lang.String s)
Returns canonic string for ignore-case comparision . FORALL String s1, s2: s1.equalsIgnoreCase(s2) == canonicString(s1).equals(canonicString(s2) FORALL String s: canonicString(canonicString(s)).equals(canonicString(s))

Parameters:
s - given String
Returns:
canonic String Simple "toLowerCase" is not sufficent. For example ("?").equalsIgnoreCase("i") , but Character.toLowerCase('?') == '?' .

startsWithIgnoreCase

public static boolean startsWithIgnoreCase(java.lang.String main,
                                           java.lang.String with)
Method to determine if one string is a subset of another, but case-insensitive.

Parameters:
main - the main string.
with - the substring.
Returns:
true if the main string starts with the substring, ignoring case.

atof

public static double atof(java.lang.String text)
Method to parse the floating-point number in a string.

There is one reason to use this method instead of Double.parseDouble...

Parameters:
text - the string with a number in it.
Returns:
the numeric value.

atof

public static double atof(java.lang.String text,
                          java.lang.Double defaultVal)
This is the same as TextUtils.atof, except upon failure to convert the passed text to a number, it returns the value in 'defaultVal'. If 'defaultVal' is null and the text cannot be converted to a number, the method returns 0.

Parameters:
text - the string to convert to a double
defaultVal - the value to return if the string cannot be converted to a double
Returns:
the numeric value

atoi

public static int atoi(java.lang.String s)
Method to parse the number in a string.

There are many reasons to use this method instead of Integer.parseInt...

Parameters:
s - the string with a number in it.
Returns:
the numeric value.

atoi

public static int atoi(java.lang.String s,
                       int pos)
Method to parse the number in a string. See the comments for "atoi(String s)" for reasons why this method exists.

Parameters:
s - the string with a number in it.
pos - the starting position in the string to find the number.
Returns:
the numeric value.

atoi

public static int atoi(java.lang.String s,
                       int pos,
                       int base)
Method to parse the number in a string. See the comments for "atoi(String s)" for reasons why this method exists.

Parameters:
s - the string with a number in it.
pos - the starting position in the string to find the number.
base - the forced base of the number (0 to determine it automatically).
Returns:
the numeric value.

getValueOfExpression

public static double getValueOfExpression(java.lang.String expression)
Method to get the numeric value of a string that may be an expression.

Parameters:
expression - the string that may be an expression.
Returns:
the numeric value of the expression. This method uses the Bean Shell to evaluate non-numeric strings.

getTransparentColors

public static java.awt.Color[] getTransparentColors(java.lang.String str)
Method to convert a string with color values into an array of colors.

Parameters:
str - the string, with colors separated by "/" and the RGB values in a color separated by ",". For example, "255,0,0/0,0,255" describes two colors: red and blue.
Returns:
an array of Color values.

formatDoublePostFix

public static java.lang.String formatDoublePostFix(double v)
Method to convert a double to a string. Also scales number and appends appropriate postfix UnitScale string.

Parameters:
v - the double value to format.
Returns:
the string representation of the number.

formatDouble

public static java.lang.String formatDouble(double v)
Method to convert a double to a string. If the double has no precision past the decimal, none will be shown.

Parameters:
v - the double value to format.
Returns:
the string representation of the number.

formatDouble

public static java.lang.String formatDouble(double v,
                                            int numFractions)
Method to convert a double to a string. It will show up to 'numFractions' digits past the decimal point if numFractions is greater than zero. If numFractions is 0, it will show infinite (as far as doubles go) precision. If the double has no precision past the decimal, none will be shown. This method is now thread safe.

Parameters:
v - the double value to format.
numFractions - the number of digits to the right of the decimal point.
Returns:
the string representation of the number.

formatDate

public static java.lang.String formatDate(java.util.Date date)
Method to convert a Date to a String using local TimeZone.

Parameters:
date - the date to format.
Returns:
the string representation of the date.

formatDateGMT

public static java.lang.String formatDateGMT(java.util.Date date)
Method to convert a Date to a String using GMT TimeZone.

Parameters:
date - the date to format.
Returns:
the string representation of the date.

convertToEngineeringNotation

public static java.lang.String convertToEngineeringNotation(double value)
Method to converts a floating point number into engineering units such as pico, micro, milli, etc.

Parameters:
value - floating point value to be converted to engineering notation.

convertToEngineeringNotation

public static java.lang.String convertToEngineeringNotation(double value,
                                                            java.lang.String unit)
Method to converts a floating point number into engineering units such as pico, micro, milli, etc.

Parameters:
value - floating point value to be converted to engineering notation.
unit - a unit string to append to the result (null for none).

convertToEngineeringNotation

public static java.lang.String convertToEngineeringNotation(double time,
                                                            java.lang.String unit,
                                                            int precpower)

toBlankPaddedString

public static java.lang.String toBlankPaddedString(int value,
                                                   int width)
Method to convert an integer to a string that is left-padded with spaces

Parameters:
value - the integer value.
width - the minimum field width. If the result is less than this, extra spaces are added to the beginning.
Returns:
a string describing the integer.

toBlankPaddedString

public static java.lang.String toBlankPaddedString(double value,
                                                   int width)
Method to convert a double to a string that is left-padded with spaces

Parameters:
value - the double value.
width - the minimum field width. If the result is less than this, extra spaces are added to the beginning.
Returns:
a string describing the double.

isANumber

public static boolean isANumber(java.lang.String pp)
Method to determine whether or not a string is a number. This method allows hexadecimal numbers as well as those with exponents.

Parameters:
pp - the string to test.
Returns:
true if it is a number.

isANumberPostFix

public static boolean isANumberPostFix(java.lang.String pp)
Method to determine whether or not a string is a postfix formatted number, such as 1.02f.

Parameters:
pp - the string to test.
Returns:
true if it is a postfix number.

getElapsedTime

public static java.lang.String getElapsedTime(long milliseconds)
Method to describe a time value as a String.

Parameters:
milliseconds - the time span in milli-seconds.
Returns:
a String describing the time span with the format: days : hours : minutes : seconds

findStringInString

public static int findStringInString(java.lang.String string,
                                     java.lang.String search,
                                     int startingPos,
                                     boolean caseSensitive,
                                     boolean reverse)
Method to find a string inside of another string.

Parameters:
string - the main string being searched.
search - the string being located in the main string.
startingPos - the starting position in the main string to look (0 to search the whole string).
caseSensitive - true to do a case-sensitive search.
reverse - true to search from the back of the string.
Returns:
the position of the search string. Returns negative if the string is not found.

parseString

public static java.lang.String[] parseString(java.lang.String line,
                                             java.lang.String delim)
Method to break a line into keywords, separated by white space or comma

Parameters:
line - the string to tokenize.
delim - the delimiters.
Returns:
an array of Strings for each keyword on the line.

convertDistance

public static double convertDistance(double value,
                                     Technology tech,
                                     TextUtils.UnitScale unitScale)
Method to convert a database coordinate into real spacing.

Parameters:
value - the database coordinate to convert.
tech - the technology to use for conversion (provides a real scaling).
unitScale - the type of unit desired.
Returns:
the database coordinate in the desired units. For example, if the given technology has a scale of 200 nanometers per unit, and the value 7 is given, then that is 1.4 microns (1400 nanometers). If the desired units are UnitScale.MICRO, then the returned value will be 1.4.

convertFromDistance

public static double convertFromDistance(double value,
                                         Technology tech,
                                         TextUtils.UnitScale unitScale)
Method to convert real spacing into a database coordinate.

Parameters:
value - the real distance to convert.
tech - the technology to use for conversion (provides a real scaling).
unitScale - the type of unit desired.
Returns:
the real spacing in the database units. For example, if the given technology has a scale of 200 nanometers per unit, and the value 1.6 is given with the scale UnitScale.MICRO, then that is 1.6 microns (1600 nanometers). Since the technology has 200 nanometers per unit, this converts to 8 units.

makeUnits

public static java.lang.String makeUnits(double value,
                                         AbstractTextDescriptor.Unit units)
Method to convert a floating point value to a string, given that it is a particular type of unit. Each unit has a default scale. For example, if Capacitance value 0.0000012 is being converted, and Capacitance is currently using microFarads, then the result will be "1.2m". If, however, capacitance is currently using milliFarads, the result will be 0.0012u".

Parameters:
value - the floating point value.
units - the type of unit.
Returns:
a string describing that value in the current unit.

parsePostFixNumber

public static java.lang.Number parsePostFixNumber(java.lang.String s)
                                           throws java.lang.NumberFormatException
Try to parse the user input String s into a Number. Conversion into the following formats is tried in order. If a conversion is successful, that object is returned. If no conversions are successful, this throws a NumberFormatException. This method removes any UnitScale postfix, and scales the number accordingly. No characters in the string are ignored - the string in its entirety (sans removed postfix) must be able to be parsed into the Number by the usual Integer.parseInt(), Double.parseDouble() methods.

Formats: Integer, Long, Double

Parameters:
s - the string to parse
Returns:
a Number that represents the string in its entirety
Throws:
java.lang.NumberFormatException - if the String is not a parsable Number.

printLongString

public static void printLongString(java.lang.String str)
Method to print a very long string. The string is broken sensibly.


setTextOnClipboard

public static void setTextOnClipboard(java.lang.String text)
Method to set the string stored on the system clipboard.

Parameters:
text - the new text for the clipboard. If text is null, the contents is clean.

getTextOnClipboard

public static java.lang.String getTextOnClipboard()
Method for obtaining the string on the system clipboard.

Returns:
the string on the system clipboard (returns a null string if nothing is found).

makeURLToFile

public static java.net.URL makeURLToFile(java.lang.String fileName)
Method to convert a file path to a URL.

Parameters:
fileName - the path to the file.
Returns:
the URL to that file (null on error).

getFile

public static java.io.File getFile(java.net.URL url)
Get the file for the URL. The code new File(url.getPath()) returns an illegal leading slash on windows, and has forward slashes instead of back slashes. This method generates the correct File using new File(url.toURI())

use getPath() on the returned File to get the correct String file path.

This should only be needed when running an external process under windows with command line arguments containing file paths. Otherwise, the Java IO code does the correct conversion.

Parameters:
url - the URL to convert to a File.
Returns:
the File. Will return null if URL does not point to a file.

getFilePath

public static java.lang.String getFilePath(java.net.URL url)
Method to return the directory path part of a URL (excluding the file name). For example, the URL "file:/users/strubin/gates.elib" has the directory part "/users/strubin/".

Parameters:
url - the URL to the file.
Returns:
the directory path part (including the trailing "/", ":", or "\"). If there is no directory part, returns "".

getFileNameWithoutExtension

public static java.lang.String getFileNameWithoutExtension(java.net.URL url)
Method to return the pure file name of a URL. The pure file name excludes the directory path and the extension. It is used to find the library name from a URL. For example, the URL "file:/users/strubin/gates.elib" has the pure file name "gates".

Parameters:
url - the URL to the file.
Returns:
the pure file name.

getFileNameWithoutExtension

public static java.lang.String getFileNameWithoutExtension(java.lang.String fileName)
Method to return the pure file name of a file path. The pure file name excludes the directory path and the extension. It is used to find the library name from a file patj. For example, the file path "file:/users/strubin/gates.elib" has the pure file name "gates".

Parameters:
fileName - full name of file.
Returns:
the pure file name.

getExtension

public static java.lang.String getExtension(java.net.URL url)
Method to return the extension of the file in a URL. The extension is the part after the last dot. For example, the URL "file:/users/strubin/gates.elib" has the extension "elib".

Parameters:
url - the URL to the file.
Returns:
the extension of the file ("" if none).

getURLStream

public static java.io.InputStream getURLStream(java.net.URL url)
Method to open an input stream to a URL.

Parameters:
url - the URL to the file.
Returns:
the InputStream, or null if the file cannot be found.

getURLStream

public static java.io.InputStream getURLStream(java.net.URL url,
                                               java.lang.StringBuffer errorMsg)
Method to open an input stream to a URL.

Parameters:
url - the URL to the file.
errorMsg - a string buffer in which to print any error message. If null, any error message is printed to System.out
Returns:
the InputStream, or null if the file cannot be found.

URLExists

public static boolean URLExists(java.net.URL url)
Method to tell whether a given URL exists.

Parameters:
url - the URL in question.
Returns:
true if the file exists.

URLExists

public static boolean URLExists(java.net.URL url,
                                java.lang.StringBuffer errorMsg)
Method to tell whether a given URL exists.

Parameters:
url - the URL in question.
errorMsg - a string buffer in which to print any error message. If null, errors are not printed.
Returns:
true if the file exists.

getAllResources

public static java.util.List<java.lang.String> getAllResources(java.lang.String resourceName)
Method to examine a path and return all resources found there.

Parameters:
resourceName - the path to examine.
Returns:
a List of all resource names found there.

correctName

public static java.lang.String correctName(java.lang.String n,
                                           boolean onlyBrackets,
                                           boolean correctBrackets)
Method to replace all special characters in the instance name coming from external files such as"/"..

Parameters:
n -
onlyBrackets -
correctBrackets -
Returns:
String where characters "/", "[", "]" are replacedby "_". "\" is removed.