📄 textutils.java
字号:
/* -*- tab-width: 4 -*- * * Electric(tm) VLSI Design System * * File: TextUtils.java * * Copyright (c) 2003 Sun Microsystems and Static Free Software * * Electric(tm) is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * Electric(tm) is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Electric(tm); see the file COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, Mass 02111-1307, USA. */package com.sun.electric.database.text;import com.sun.electric.database.hierarchy.Cell;import com.sun.electric.database.hierarchy.View;import com.sun.electric.database.network.Network;import com.sun.electric.database.topology.Connection;import com.sun.electric.database.variable.EvalJavaBsh;import com.sun.electric.database.variable.TextDescriptor;import com.sun.electric.technology.Technology;import java.awt.Color;import java.awt.Toolkit;import java.awt.datatransfer.*;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.net.URI;import java.net.URISyntaxException;import java.net.URL;import java.net.URLConnection;import java.net.URLDecoder;import java.text.DecimalFormat;import java.text.NumberFormat;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Comparator;import java.util.Date;import java.util.Enumeration;import java.util.List;import java.util.Locale;import java.util.StringTokenizer;import java.util.TimeZone;import java.util.zip.ZipEntry;import java.util.zip.ZipFile;/** * This class is a collection of text utilities. */public class TextUtils{ /** * Determines if the specified character is a ISO-LATIN-1 digit * (<code>'0'</code> through <code>'9'</code>). * <p> * This can be method instead of Character, if we are not ready * to handle Arabi-Indic, Devanagaru and other digits. * * @param ch the character to be tested. * @return <code>true</code> if the character is a ISO-LATIN-1 digit; * <code>false</code> otherwise. * @see java.lang.Character#isDigit(char) */ public static boolean isDigit(char ch) { return '0' <= ch && ch <= '9'; } /** * Determines if the specified character is a letter or digit. * <p> * A character is considered to be a letter or digit if either * <code>Character.isLetter(char ch)</code> or * <code>TextUtils.isDigit(char ch)</code> returns * <code>true</code> for the character. * * @param ch the character to be tested. * @return <code>true</code> if the character is a letter or digit; * <code>false</code> otherwise. * @see TextUtils#isDigit(char) * @see java.lang.Character#isJavaLetterOrDigit(char) * @see java.lang.Character#isLetter(char) */ public static boolean isLetterOrDigit(char ch) { return isDigit(ch) || Character.isLetter(ch); } /** * Returns canonic char for ignore-case comparison . * This is the same as Character.toLowerCase(Character.toUpperCase(ch)). * @param ch given char. * @return canonic char fo rthe given char. */ public static char canonicChar(char ch) { if (ch <= 'Z') { if (ch >= 'A') ch += 'a' - 'A'; } else { if (ch >= '\u0080') ch = Character.toLowerCase(Character.toUpperCase(ch)); } return ch; } /** * 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)) * @param s given String * @return canonic String * Simple "toLowerCase" is not sufficent. * For example ("\u0131").equalsIgnoreCase("i") , but Character.toLowerCase('\u0131') == '\u0131' . */ public static String canonicString(String s) { int i = 0; for (; i < s.length(); i++) { char ch = s.charAt(i); if (canonicChar(ch) != ch) break; } if (i == s.length()) return s; char[] chars = s.toCharArray(); for (; i < s.length(); i++) chars[i] = canonicChar(chars[i]); return new String(chars); }// static {// for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; i++) {// char ch = (char)i;// char toLower = Character.toLowerCase(ch);// char toUpper = Character.toUpperCase(ch);// char canonic = canonicChar(toUpper);// if (canonic != toLower) {// System.out.println(ch + " " + Integer.toHexString(ch) +// " lower " + toLower + " " + Integer.toHexString(toLower) +// " upper " + toUpper + " " + Integer.toHexString(toUpper) +// " canonic " + canonic + " " + Integer.toHexString(canonic));// assert Character.toLowerCase(Character.toUpperCase(canonic)) == canonic;// }// }// } /** * Method to determine if one string is a subset of another, but case-insensitive. * @param main the main string. * @param with the substring. * @return true if the main string starts with the substring, ignoring case. */ public static boolean startsWithIgnoreCase(String main, String with) { int mainLen = main.length(); int withLen = with.length(); if (withLen > mainLen) return false; for(int i=0; i<withLen; i++) { char mainChr = canonicChar(main.charAt(i)); char withChr = canonicChar(with.charAt(i)); if (mainChr != withChr) return false; } return true; } /** * Method to parse the floating-point number in a string. * <P> * There is one reason to use this method instead of Double.parseDouble... * <UL> * <LI>This method does not throw an exception if the number is invalid (or blank). * </UL> * @param text the string with a number in it. * @return the numeric value. */ public static double atof(String text) { try { return Double.parseDouble(text); } catch (NumberFormatException e) { return atof(text, null); } } /** * 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. * @param text the string to convert to a double * @param defaultVal the value to return if the string cannot be converted to a double * @return the numeric value */ public static double atof(String text, Double defaultVal) { // remove commas that denote 1000's separators text = text.replaceAll(",", ""); double v = 0; try { Number n = parsePostFixNumber(text); v = n.doubleValue(); } catch (NumberFormatException ex) { int start = 0; while (start < text.length() && text.charAt(start) == ' ') start++; int end = start; // allow initial + or - if (end < text.length() && (text.charAt(end) == '-' || text.charAt(end) == '+')) end++; // allow digits while (end < text.length() && TextUtils.isDigit(text.charAt(end))) end++; // allow decimal point and digits beyond it if (end < text.length() && text.charAt(end) == '.') { end++; while (end < text.length() && TextUtils.isDigit(text.charAt(end))) end++; } // allow exponent if (end < text.length() && (text.charAt(end) == 'e' || text.charAt(end) == 'E')) { end++; if (end < text.length() && (text.charAt(end) == '-' || text.charAt(end) == '+')) end++; while (end < text.length() && TextUtils.isDigit(text.charAt(end))) end++; } if (end <= start) { if (defaultVal != null) return defaultVal.doubleValue(); return 0; } try { v = Double.parseDouble(text.substring(start, end-start)); } catch (NumberFormatException e) { v = 0; } } return v; } /** * Method to parse the number in a string. * <P> * There are many reasons to use this method instead of Integer.parseInt... * <UL> * <LI>This method can handle any radix. * If the number begins with "0", presume base 8. * If the number begins with "0b", presume base 2. * If the number begins with "0x", presume base 16. * Otherwise presume base 10. * <LI>This method can handle numbers that affect the sign bit. * If you give 0xFFFFFFFF to Integer.parseInt, you get a numberFormatPostFix exception. * This method properly returns -1. * <LI>This method does not require that the entire string be part of the number. * If there is extra text after the end, Integer.parseInt fails (for example "123xx"). * <LI>This method does not throw an exception if the number is invalid (or blank). * </UL> * @param s the string with a number in it. * @return the numeric value. */ public static int atoi(String s) { return atoi(s, 0, 0); } /** * Method to parse the number in a string. * See the comments for "atoi(String s)" for reasons why this method exists. * @param s the string with a number in it. * @param pos the starting position in the string to find the number. * @return the numeric value. */ public static int atoi(String s, int pos) { return atoi(s, pos, 0); } /** * Method to parse the number in a string. * See the comments for "atoi(String s)" for reasons why this method exists. * @param s the string with a number in it. * @param pos the starting position in the string to find the number. * @param base the forced base of the number (0 to determine it automatically). * @return the numeric value. */ public static int atoi(String s, int pos, int base) { int num = 0; int sign = 1; int len = s.length(); if (pos < len && s.charAt(pos) == '-') { pos++; sign = -1; } if (base == 0) { base = 10; if (pos < len && s.charAt(pos) == '0') { pos++; base = 8; if (pos < len && (s.charAt(pos) == 'x' || s.charAt(pos) == 'X')) { pos++; base = 16; } else if (pos < len && (s.charAt(pos) == 'b' || s.charAt(pos) == 'B')) { pos++; base = 2; } } } for(; pos < len; pos++) { char cat = s.charAt(pos); int digit = Character.digit(cat, base); if (digit < 0) break; num = num * base + digit;// if ((cat >= 'a' && cat <= 'f') || (cat >= 'A' && cat <= 'F'))// {// if (base != 16) break;// num = num * 16;// if (cat >= 'a' && cat <= 'f') num += cat - 'a' + 10; else// num += cat - 'A' + 10;// continue;// }// if (!TextUtils.isDigit(cat)) break;// if (cat >= '8' && base == 8) break;// num = num * base + cat - '0'; } return(num * sign); } /** * Method to get the numeric value of a string that may be an expression. * @param expression the string that may be an expression. * @return the numeric value of the expression. * This method uses the Bean Shell to evaluate non-numeric strings. */ public static double getValueOfExpression(String expression) { if (isANumber(expression)) { double res = atof(expression); return res; } Object o = EvalJavaBsh.evalJavaBsh.doEvalLine(expression); if (o == null) return 0; if (o instanceof Double) return ((Double)o).doubleValue(); if (o instanceof Integer) return ((Integer)o).intValue(); return 0; } /** * Method to convert a string with color values into an array of colors. * @param 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. * @return an array of Color values. */ public static Color [] getTransparentColors(String str) { String [] colorNames = str.split("/"); Color [] colors = new Color[colorNames.length]; for(int i=0; i<colorNames.length; i++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -