📄 stringutils.java
字号:
} if (((cmp = searchForWildcard.charAt(searchForPos)) == wildcardEscape) && ((searchForPos + 1) != searchForEnd)) { cmp = searchForWildcard.charAt(++searchForPos); } searchForPos++; do { while ((searchInPos != searchInEnd) && (Character.toUpperCase(searchIn.charAt(searchInPos)) != Character.toUpperCase( cmp))) searchInPos++; if (searchInPos++ == searchInEnd) { return WILD_COMPARE_NO_MATCH; } { int tmp = wildCompare(searchIn, searchForWildcard); if (tmp <= 0) { return (tmp); } } } while ((searchInPos != searchInEnd) && (searchForWildcard.charAt(0) != wildcardMany)); return WILD_COMPARE_NO_MATCH; } } return ((searchInPos != searchInEnd) ? WILD_COMPARE_MATCH_WITH_WILD : WILD_COMPARE_MATCH_NO_WILD); } public static long getLong(byte[] buf) throws NumberFormatException { int base = 10; int s = 0; /* Skip white space. */ while (Character.isWhitespace((char) buf[s]) && (s < buf.length)) { ++s; } if (s == buf.length) { throw new NumberFormatException(new String(buf)); } /* Check for a sign. */ boolean negative = false; if ((char) buf[s] == '-') { negative = true; ++s; } else if ((char) buf[s] == '+') { ++s; } /* Save the pointer so we can check later if anything happened. */ int save = s; long cutoff = Long.MAX_VALUE / base; long cutlim = (int) (Long.MAX_VALUE % base); if (negative) { cutlim++; } boolean overflow = false; long i = 0; for (; s < buf.length; s++) { char c = (char) buf[s]; if (Character.isDigit(c)) { c -= '0'; } else if (Character.isLetter(c)) { c = (char) (Character.toUpperCase(c) - 'A' + 10); } else { break; } if (c >= base) { break; } /* Check for overflow. */ if ((i > cutoff) || ((i == cutoff) && (c > cutlim))) { overflow = true; } else { i *= base; i += c; } } if (s == save) { throw new NumberFormatException(new String(buf)); } if (overflow) { throw new NumberFormatException(new String(buf)); } /* Return the result of the appropriate sign. */ return (negative ? (-i) : i); } public static int getInt(byte[] buf) throws NumberFormatException { int base = 10; int s = 0; /* Skip white space. */ while (Character.isWhitespace((char) buf[s]) && (s < buf.length)) { ++s; } if (s == buf.length) { throw new NumberFormatException(new String(buf)); } /* Check for a sign. */ boolean negative = false; if ((char) buf[s] == '-') { negative = true; ++s; } else if ((char) buf[s] == '+') { ++s; } /* Save the pointer so we can check later if anything happened. */ int save = s; int cutoff = Integer.MAX_VALUE / base; int cutlim = (Integer.MAX_VALUE % base); if (negative) { cutlim++; } boolean overflow = false; int i = 0; for (; s < buf.length; s++) { char c = (char) buf[s]; if (Character.isDigit(c)) { c -= '0'; } else if (Character.isLetter(c)) { c = (char) (Character.toUpperCase(c) - 'A' + 10); } else { break; } if (c >= base) { break; } /* Check for overflow. */ if ((i > cutoff) || ((i == cutoff) && (c > cutlim))) { overflow = true; } else { i *= base; i += c; } } if (s == save) { throw new NumberFormatException(new String(buf)); } if (overflow) { throw new NumberFormatException(new String(buf)); } /* Return the result of the appropriate sign. */ return (negative ? (-i) : i); } public static short getShort(byte[] buf) throws NumberFormatException { short base = 10; int s = 0; /* Skip white space. */ while (Character.isWhitespace((char) buf[s]) && (s < buf.length)) { ++s; } if (s == buf.length) { throw new NumberFormatException(new String(buf)); } /* Check for a sign. */ boolean negative = false; if ((char) buf[s] == '-') { negative = true; ++s; } else if ((char) buf[s] == '+') { ++s; } /* Save the pointer so we can check later if anything happened. */ int save = s; short cutoff = (short) (Short.MAX_VALUE / base); short cutlim = (short) (Short.MAX_VALUE % base); if (negative) { cutlim++; } boolean overflow = false; short i = 0; for (; s < buf.length; s++) { char c = (char) buf[s]; if (Character.isDigit(c)) { c -= '0'; } else if (Character.isLetter(c)) { c = (char) (Character.toUpperCase(c) - 'A' + 10); } else { break; } if (c >= base) { break; } /* Check for overflow. */ if ((i > cutoff) || ((i == cutoff) && (c > cutlim))) { overflow = true; } else { i *= base; i += c; } } if (s == save) { throw new NumberFormatException(new String(buf)); } if (overflow) { throw new NumberFormatException(new String(buf)); } /* Return the result of the appropriate sign. */ return (negative ? (short) -i : (short) i); } /** * Adds '+' to decimal numbers that are positive (MySQL doesn't understand * them otherwise * * @param dString The value as a string * * @return String the string with a '+' added (if needed) */ public static final String fixDecimalExponent(String dString) { int ePos = dString.indexOf("E"); //$NON-NLS-1$ if (ePos == -1) { ePos = dString.indexOf("e"); //$NON-NLS-1$ } if (ePos != -1) { if (dString.length() > (ePos + 1)) { char maybeMinusChar = dString.charAt(ePos + 1); if (maybeMinusChar != '-' && maybeMinusChar != '+') { StringBuffer buf = new StringBuffer(dString.length() + 1); buf.append(dString.substring(0, ePos + 1)); buf.append('+'); buf.append(dString.substring(ePos + 1, dString.length())); dString = buf.toString(); } } } return dString; } /** * @param bytesToStrip * @param prefix * @param suffix * @return */ public static byte[] stripEnclosure(byte[] source, String prefix, String suffix) { if (source.length >= prefix.length() + suffix.length() && startsWith(source, prefix) && endsWith(source, suffix)) { int totalToStrip = prefix.length() + suffix.length(); int enclosedLength = source.length - totalToStrip; byte[] enclosed = new byte[enclosedLength]; int startPos = prefix.length(); int numToCopy = enclosed.length; System.arraycopy(source, startPos, enclosed, 0, numToCopy); return enclosed; } return source; } private static boolean startsWith(byte[] dataFrom, String chars) { for (int i = 0; i < chars.length(); i++) { if (dataFrom[i] != chars.charAt(i)){ return false; } } return true; } private static boolean endsWith(byte[] dataFrom, String suffix){ for (int i = 1; i <= suffix.length(); i++) { int dfOffset = dataFrom.length - i; int suffixOffset = suffix.length() - i; if (dataFrom[dfOffset] != suffix.charAt(suffixOffset)) { return false; } } return true; } /** * Takes care of the fact that Sun changed the output of * BigDecimal.toString() between JDK-1.4 and JDK 5 * * @param decimal the big decimal to stringify * * @return a string representation of 'decimal' */ public static String consistentToString(BigDecimal decimal) { if (decimal == null) { return null; } if (toPlainStringMethod != null) { try { return (String)toPlainStringMethod.invoke(decimal, null); } catch (InvocationTargetException invokeEx) { // that's okay, we fall-through to decimal.toString() } catch (IllegalAccessException accessEx) { // that's okay, we fall-through to decimal.toString() } } return decimal.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -