📄 miscutilities.java
字号:
if(clsName[i] == '/') clsName[i] = '.'; return new String(clsName,0,clsName.length - 6); } //}}} //{{{ classToFile() method /** * Converts a class name to a file name. All periods are replaced * with slashes and the '.class' extension is added. * @param name The class name */ public static String classToFile(String name) { return name.replace('.','/').concat(".class"); } //}}} //}}} //{{{ Text methods //{{{ getLeadingWhiteSpace() method /** * Returns the number of leading white space characters in the * specified string. * @param str The string */ public static int getLeadingWhiteSpace(String str) { int whitespace = 0;loop: for(;whitespace < str.length();) { switch(str.charAt(whitespace)) { case ' ': case '\t': whitespace++; break; default: break loop; } } return whitespace; } //}}} //{{{ getTrailingWhiteSpace() method /** * Returns the number of trailing whitespace characters in the * specified string. * @param str The string * @since jEdit 2.5pre5 */ public static int getTrailingWhiteSpace(String str) { int whitespace = 0;loop: for(int i = str.length() - 1; i >= 0; i--) { switch(str.charAt(i)) { case ' ': case '\t': whitespace++; break; default: break loop; } } return whitespace; } //}}} //{{{ getLeadingWhiteSpaceWidth() method /** * Returns the width of the leading white space in the specified * string. * @param str The string * @param tabSize The tab size */ public static int getLeadingWhiteSpaceWidth(String str, int tabSize) { int whitespace = 0;loop: for(int i = 0; i < str.length(); i++) { switch(str.charAt(i)) { case ' ': whitespace++; break; case '\t': whitespace += (tabSize - whitespace % tabSize); break; default: break loop; } } return whitespace; } //}}} //{{{ getVirtualWidth() method /** * Returns the virtual column number (taking tabs into account) of the * specified offset in the segment. * * @param seg The segment * @param tabSize The tab size * @since jEdit 4.1pre1 */ public static int getVirtualWidth(Segment seg, int tabSize) { int virtualPosition = 0; for (int i = 0; i < seg.count; i++) { char ch = seg.array[seg.offset + i]; if (ch == '\t') { virtualPosition += tabSize - (virtualPosition % tabSize); } else { ++virtualPosition; } } return virtualPosition; } //}}} //{{{ getOffsetOfVirtualColumn() method /** * Returns the array offset of a virtual column number (taking tabs * into account) in the segment. * * @param seg The segment * @param tabSize The tab size * @param column The virtual column number * @param totalVirtualWidth If this array is non-null, the total * virtual width will be stored in its first location if this method * returns -1. * * @return -1 if the column is out of bounds * * @since jEdit 4.1pre1 */ public static int getOffsetOfVirtualColumn(Segment seg, int tabSize, int column, int[] totalVirtualWidth) { int virtualPosition = 0; for (int i = 0; i < seg.count; i++) { char ch = seg.array[seg.offset + i]; if (ch == '\t') { int tabWidth = tabSize - (virtualPosition % tabSize); if(virtualPosition >= column) return i; else virtualPosition += tabWidth; } else { if(virtualPosition >= column) return i; else ++virtualPosition; } } if(totalVirtualWidth != null) totalVirtualWidth[0] = virtualPosition; return -1; } //}}} //{{{ createWhiteSpace() method /** * Creates a string of white space with the specified length.<p> * * To get a whitespace string tuned to the current buffer's * settings, call this method as follows: * * <pre>myWhitespace = MiscUtilities.createWhiteSpace(myLength, * (buffer.getBooleanProperty("noTabs") ? 0 * : buffer.getTabSize()));</pre> * * @param len The length * @param tabSize The tab size, or 0 if tabs are not to be used */ public static String createWhiteSpace(int len, int tabSize) { return createWhiteSpace(len,tabSize,0); } //}}} //{{{ createWhiteSpace() method /** * Creates a string of white space with the specified length.<p> * * To get a whitespace string tuned to the current buffer's * settings, call this method as follows: * * <pre>myWhitespace = MiscUtilities.createWhiteSpace(myLength, * (buffer.getBooleanProperty("noTabs") ? 0 * : buffer.getTabSize()));</pre> * * @param len The length * @param tabSize The tab size, or 0 if tabs are not to be used * @param start The start offset, for tab alignment * @since jEdit 4.2pre1 */ public static String createWhiteSpace(int len, int tabSize, int start) { StringBuffer buf = new StringBuffer(); if(tabSize == 0) { while(len-- > 0) buf.append(' '); } else if(len == 1) buf.append(' '); else { int count = (len + start % tabSize) / tabSize; if(count != 0) len += start; while(count-- > 0) buf.append('\t'); count = len % tabSize; while(count-- > 0) buf.append(' '); } return buf.toString(); } //}}} //{{{ globToRE() method /** * Converts a Unix-style glob to a regular expression.<p> * * ? becomes ., * becomes .*, {aa,bb} becomes (aa|bb). * @param glob The glob pattern */ public static String globToRE(String glob) { final Object NEG = new Object(); final Object GROUP = new Object(); Stack state = new Stack(); StringBuffer buf = new StringBuffer(); boolean backslash = false; for(int i = 0; i < glob.length(); i++) { char c = glob.charAt(i); if(backslash) { buf.append('\\'); buf.append(c); backslash = false; continue; } switch(c) { case '\\': backslash = true; break; case '?': buf.append('.'); break; case '.': case '+': case '(': case ')': buf.append('\\'); buf.append(c); break; case '*': buf.append(".*"); break; case '|': if(backslash) buf.append("\\|"); else buf.append('|'); break; case '{': buf.append('('); if(i + 1 != glob.length() && glob.charAt(i + 1) == '!') { buf.append('?'); state.push(NEG); } else state.push(GROUP); break; case ',': if(!state.isEmpty() && state.peek() == GROUP) buf.append('|'); else buf.append(','); break; case '}': if(!state.isEmpty()) { buf.append(")"); if(state.pop() == NEG) buf.append(".*"); } else buf.append('}'); break; default: buf.append(c); } } return buf.toString(); } //}}} //{{{ escapesToChars() method /** * Converts "\n" and "\t" escapes in the specified string to * newlines and tabs. * @param str The string * @since jEdit 2.3pre1 */ public static String escapesToChars(String str) { StringBuffer buf = new StringBuffer(); for(int i = 0; i < str.length(); i++) { char c = str.charAt(i); switch(c) { case '\\': if(i == str.length() - 1) { buf.append('\\'); break; } c = str.charAt(++i); switch(c) { case 'n': buf.append('\n'); break; case 't': buf.append('\t'); break; default: buf.append(c); break; } break; default: buf.append(c); } } return buf.toString(); } //}}} //{{{ charsToEscapes() method /** * Escapes newlines, tabs, backslashes, and quotes in the specified * string. * @param str The string * @since jEdit 2.3pre1 */ public static String charsToEscapes(String str) { return charsToEscapes(str,"\n\t\\\"'"); } //}}} //{{{ charsToEscapes() method /** * Escapes the specified characters in the specified string. * @param str The string * @param toEscape Any characters that require escaping * @since jEdit 4.1pre3 */ public static String charsToEscapes(String str, String toEscape) { StringBuffer buf = new StringBuffer(); for(int i = 0; i < str.length(); i++) { char c = str.charAt(i); if(toEscape.indexOf(c) != -1) { if(c == '\n') buf.append("\\n"); else if(c == '\t') buf.append("\\t"); else { buf.append('\\'); buf.append(c); } } else buf.append(c); } return buf.toString(); } //}}} //{{{ compareVersions() method /** * @deprecated Call <code>compareStrings()</code> instead */ public static int compareVersions(String v1, String v2) { return compareStrings(v1,v2,false); } //}}} //{{{ compareStrings() method /** * Compares two strings.<p> * * Unlike <function>String.compareTo()</function>, * this method correctly recognizes and handles embedded numbers. * For example, it places "My file 2" before "My file 10".<p> * * @param str1 The first string * @param str2 The second string * @param ignoreCase If true, case will be ignored * @return negative If str1 < str2, 0 if both are the same, * positive if str1 > str2 * @since jEdit 4.0pre1 */ public static int compareStrings(String str1, String str2, boolean ignoreCase) { char[] char1 = str1.toCharArray(); char[] char2 = str2.toCharArray(); int len = Math.min(char1.length,char2.length); for(int i = 0, j = 0; i < len && j < len; i++, j++) { char ch1 = char1[i]; char ch2 = char2[j]; if(Character.isDigit(ch1) && Character.isDigit(ch2) && ch1 != '0' && ch2 != '0') { int _i = i + 1; int _j = j + 1; for(; _i < char1.length; _i++) { if(!Character.isDigit(char1[_i])) { //_i--; break; } } for(; _j < char2.length; _j++) { if(!Character.isDigit(char2[_j])) { //_j--; break; } } int len1 = _i - i; int len2 = _j - j; if(len1 > len2) return 1; else if(len1 < len2) return -1; else { for(int k = 0; k < len1; k++) { ch1 = char1[i + k]; ch2 = char2[j + k]; if(ch1 != ch2) return ch1 - ch2; } } i = _i - 1; j = _j - 1; } else { if(ignoreCase) { ch1 = Character.toLowerCase(ch1); ch2 = Character.toLowerCase(ch2); } if(ch1 != ch2) return ch1 - ch2; } } return char1.length - char2.length; } //}}} //{{{ stringsEqual() method /** * @deprecated Call <code>objectsEqual()</code> instead. */ public static boolean stringsEqual(String s1, String s2) { return objectsEqual(s1,s2); } //}}} //{{{ objectsEqual() method /** * Returns if two strings are equal. This correctly handles null pointers, * as opposed to calling <code>o1.equals(o2)</code>. * @since jEdit 4.2pre1 */ public static boolean objectsEqual(Object o1, Object o2) { if(o1 == null) { if(o2 == null) return true; else return false; } else if(o2 == null) return false; else return o1.equals(o2); } //}}} //{{{ charsToEntities() method /** * Converts <, >, & in the string to their HTML entity * equivalents. * @param str The string * @since jEdit 4.2pre1 */ public static String charsToEntities(String str) { StringBuffer buf = new StringBuffer(str.length()); for(int i = 0; i < str.length(); i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -