stringmodule.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,731 行 · 第 1/5 页
JAVA
2,731 行
try { CRC32 crc = new CRC32(); int ch; while ((ch = is.read()) >= 0) { crc.update((byte) ch); } return crc.getValue() & 0xffffffff; } catch (IOException e) { throw new QuercusModuleException(e); } } public static String crypt(String string, @Optional String salt) { if (string == null) string = ""; if (salt == null || salt.equals("")) { salt = ("" + Crypt.resultToChar(RandomUtil.nextInt(0x40)) + Crypt.resultToChar(RandomUtil.nextInt(0x40))); } return Crypt.crypt(string, salt); } // XXX: echo /** * Explodes a string into an array * * @param separator the separator string * @param string the string to be exploded * @param limit the max number of elements * @return an array of exploded values */ public static Value explode(StringValue separator, StringValue string, @Optional("0x7fffffff") long limit) { if (separator.length() == 0) return BooleanValue.FALSE; ArrayValue array = new ArrayValueImpl(); int head = 0; int tail; int i = 0; int separatorLength = separator.length(); while ((tail = string.indexOf(separator, head)) >= 0) { if (limit <= i + 1) break; LongValue key = LongValue.create(i++); StringValue chunk = string.substring(head, tail); array.put(key, chunk); head = tail + separatorLength; } LongValue key = LongValue.create(i); StringValue chunk = string.substring(head); array.put(key, chunk); return array; } /** * Use printf style formatting to write a string to a file. * @param fd the file to write to * @param format the format string * @param args the valujes to apply to the format string */ public static Value fprintf(Env env, @NotNull BinaryOutput os, StringValue format, Value []args) { Value value = sprintf(env, format, args); return FileModule.fwrite(env, os, value.toInputStream(), Integer.MAX_VALUE); } // XXX: get_html_translation_table // XXX: hebrev // XXX: hebrevc // XXX: html_entity_decode // XXX: htmlentities // XXX: htmlspecialchars_decode // XXX: htmlspecialchars /** * implodes an array into a string * * @param glueV the separator string * @param piecesV the array to be imploded * * @return a string of imploded values */ public static Value implode(Env env, Value glueV, @Optional Value piecesV) { StringValue glue; ArrayValue pieces; if (piecesV.isArray()) { pieces = piecesV.toArrayValue(env); glue = glueV.toStringValue(); } else if (glueV.isArray()) { pieces = glueV.toArrayValue(env); glue = piecesV.toStringValue(); } else { env.warning(L.l("neither argument to implode is an array: {0}, {1}", glueV.getClass().getName(), piecesV.getClass().getName())); return NullValue.NULL; } StringValue sb = glue.createStringBuilder(); boolean isFirst = true; Iterator<Value> iter = pieces.getValueIterator(env); while (iter.hasNext()) { if (! isFirst) sb = sb.append(glue); isFirst = false; sb = sb.append(iter.next()); } return sb; } /** * implodes an array into a string * * @param glueV the separator string * @param piecesV the array to be imploded * * @return a string of imploded values */ public static Value join(Env env, Value glueV, Value piecesV) { return implode(env, glueV, piecesV); } // XXX: lcfirst // XXX: levenshtein /** * Gets locale-specific symbols. * XXX: locale charset */ public static ArrayValue localeconv(Env env) { ArrayValueImpl array = new ArrayValueImpl(); QuercusLocale money = env.getLocaleInfo().getMonetary(); Locale locale = money.getLocale(); DecimalFormatSymbols decimal = new DecimalFormatSymbols(locale); Currency currency = NumberFormat.getInstance(locale).getCurrency(); array.put(env.createString("decimal_point"), env.createString(decimal.getDecimalSeparator())); array.put(env.createString("thousands_sep"), env.createString(decimal.getGroupingSeparator())); //array.put("grouping", ""); array.put(env.createString("int_curr_symbol"), env.createString(decimal.getInternationalCurrencySymbol())); array.put(env.createString("currency_symbol"), env.createString(decimal.getCurrencySymbol())); array.put(env.createString("mon_decimal_point"), env.createString(decimal.getMonetaryDecimalSeparator())); array.put(env.createString("mon_thousands_sep"), env.createString(decimal.getGroupingSeparator())); //array.put("mon_grouping", ""); array.put(env.createString("positive_sign"), env.getEmptyString()); array.put(env.createString("negative_sign"), env.createString(decimal.getMinusSign())); array.put(env.createString("int_frac_digits"), LongValue.create(currency.getDefaultFractionDigits())); array.put(env.createString("frac_digits"), LongValue.create(currency.getDefaultFractionDigits())); //array.put("p_cs_precedes", ""); //array.put("p_sep_by_space", ""); //array.put("n_cs_precedes", ""); //array.put("n_sep_by_space", ""); //array.put("p_sign_posn", ""); //array.put("n_sign_posn", ""); return array; } /** * Removes leading whitespace. * * @param string the string to be trimmed * @param characters optional set of characters to trim * @return the trimmed string */ public static StringValue ltrim(Env env, StringValue string, @Optional String characters) { if (characters == null) characters = ""; boolean []trim; if (characters.equals("")) trim = TRIM_WHITESPACE; else trim = parseCharsetBitmap(characters); for (int i = 0; i < string.length(); i++) { char ch = string.charAt(i); if (ch >= 256 || ! trim[ch]) { if (i == 0) return string; else return string.substring(i); } } return env.getEmptyString(); } /** * returns the md5 hash * * @param source the string * @param rawOutput if true, return the raw binary * * @return a string of imploded values */ public static Value md5(Env env, InputStream is, @Optional boolean rawOutput) { try { MessageDigest md = MessageDigest.getInstance("MD5"); // XXX: iso-8859-1 int ch; while ((ch = is.read()) >= 0) { md.update((byte) ch); } byte []digest = md.digest(); return hashToValue(env, digest, rawOutput); } catch (Exception e) { throw new QuercusModuleException(e); } } /** * returns the md5 hash * * @param source the string * @param rawOutput if true, return the raw binary * * @return a string of imploded values */ public static Value md5_file(Env env, Path source, @Optional boolean rawOutput) { try { MessageDigest md = MessageDigest.getInstance("MD5"); InputStream is = null; try { is = source.openRead(); int d; while ((d = is.read()) >= 0) { md.update((byte) d); } byte []digest = md.digest(); return hashToValue(env, digest, rawOutput); } catch (IOException e) { log.log(Level.FINE, e.toString(), e); return BooleanValue.FALSE; } finally { try { if (is != null) is.close(); } catch (IOException e) { } } } catch (Exception e) { throw new QuercusModuleException(e); } } private static StringValue digestToString(Env env, byte []digest) { StringValue sb = env.createUnicodeBuilder(); for (int i = 0; i < digest.length; i++) { int d1 = (digest[i] >> 4) & 0xf; int d2 = (digest[i] & 0xf); sb.append(toHexChar(d1)); sb.append(toHexChar(d2)); } return sb; } /** * Returns the metaphone of a string. * This implentation produces identical results to the php version, which does contain some bugs. */ public static String metaphone(String string) { if (string == null) string = ""; int length = string.length(); int index = 0; char ch = 0; // ignore everything up until first letter for (; index < length; index++) { ch = toUpperCase(string.charAt(index)); if ('A' <= ch && ch <= 'Z') break; } if (index == length) return ""; int lastIndex = length - 1; StringBuilder result = new StringBuilder(length); // special case first letter char nextCh = index < lastIndex ? toUpperCase(string.charAt(index + 1)) : 0; switch (ch) { case 'A': if (nextCh == 'E') { result.append('E'); index += 2; } else { result.append('A'); index += 1; } break; case 'E': case 'I': case 'O': case 'U': result.append(ch); index += 1; break; case 'G': case 'K': case 'P': if (nextCh == 'N') { result.append('N'); index += 2; } break; case 'W': if (nextCh == 'H' || nextCh == 'R') { result.append(nextCh); index += 2; } else { switch (nextCh) { case 'A': case 'E': case 'I': case 'O': case 'U': result.append('W'); index += 2; break; default: break; } } break; case 'X': result.append('S'); index += 1; break; default: break; } // the rest of the letters char prevCh; for (; index < length; index++) { if (index > 0) prevCh = toUpperCase(string.charAt(index - 1)); else prevCh = 0; ch = toUpperCase(string.charAt(index)); if (ch < 'A' || ch > 'Z') continue; if (ch == prevCh && ch != 'C') continue; if (index + 1 < length) nextCh = toUpperCase(string.charAt(index + 1)); else nextCh = 0; char nextnextCh; if (index + 2 < length) nextnextCh = toUpperCase(string.charAt(index + 2)); else nextnextCh = 0; switch (ch) { case 'B': if (prevCh != 'M') result.append('B'); break; case 'C': switch (nextCh) { case 'E': case 'I': case 'Y': // makesoft if (nextCh == 'I' && nextnextCh == 'A') { result.append('X'); } else if (prevCh == 'S') { } else { result.append('S'); } break; default: if (nextCh == 'H') { result.append('X'); index++; } else { result.append('K'); } break; } break; case 'D': if (nextCh == 'G') { switch (nextnextCh) { case 'E': case 'I': case 'Y': // makesoft result.append('J'); index++; break; default: result.append('T'); break; } } else result.append('T'); break; case 'G': if (nextCh == 'H') { boolean isSilent = false; if (index - 3 >= 0) { char prev3Ch = toUpperCase(string.charAt(index - 3)); switch (prev3Ch) { // noghtof case 'B': case 'D': case 'H': isSilent = true; break; default: break; } } if (!isSilent) { if (index - 4 >= 0) { char prev4Ch = toUpperCase(string.charAt(index - 4)); isSilent = (prev4Ch == 'H'); } } if (!isSilent) { result.append('F'); index++; } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?