📄 stringutils.java
字号:
/* #ifdef JAVA5 */ // StringBuilder result = new StringBuilder(str1); /* #else */ StringBuffer result = new StringBuffer(str1); /* #endif */ result.append(str2); int count = args.length; for (int i = 0; i < count; i++) { Object arg = SequenceUtils.coerceToZeroOrOne(args[i], "concat", i+2); result.append(TextUtils.stringValue(arg)); } return result.toString(); } /** This implements the XQuery <code>fn:compare</code> function. */ public static Object compare (Object val1, Object val2, NamedCollator coll) { if (val1 == Values.empty || val1 == null || val2 == Values.empty || val2 == null) return Values.empty; if (coll == null) coll = NamedCollator.codepointCollation; int ret = coll.compare(val1.toString(), val2.toString()); return ret < 0 ? IntNum.minusOne() : ret > 0 ? IntNum.one() : IntNum.zero(); } public static void stringToCodepoints$X (Object arg, CallContext ctx) { String str = coerceToString(arg, "string-to-codepoints", 1, ""); int len = str.length(); Consumer out = ctx.consumer; for (int i = 0; i < len; ) { int ch = str.charAt(i++); if (ch >= 0xD800 && ch < 0xDC00 && i < len) ch = (ch - 0xD800) * 0x400 + (str.charAt(i++) - 0xDC00) + 0x10000; out.writeInt(ch); } } private static void appendCodepoint (Object code, StringBuffer sbuf) { IntNum I = (IntNum) gnu.kawa.xml.XIntegerType.integerType.cast(code); int i = I.intValue(); if (i <= 0 || (i > 0xD7FF && (i < 0xE000 || (i > 0xFFFD && i < 0x10000) || i > 0x10FFFF))) throw new IllegalArgumentException("codepoints-to-string: "+i+" is not a valid XML character [FOCH0001]"); if (i >= 0x10000) { sbuf.append((char) (((i - 0x10000) >> 10) + 0xD800)); i = (i & 0x3FF) + 0xDC00; } sbuf.append((char) i); } public static String codepointsToString (Object arg) { if (arg == null) return ""; StringBuffer sbuf = new StringBuffer(); if (arg instanceof Values) { Values vals = (Values) arg; int ipos = vals.startPos(); while ((ipos = vals.nextPos(ipos)) != 0) appendCodepoint(vals.getPosPrevious(ipos), sbuf); } else appendCodepoint(arg, sbuf); return sbuf.toString(); } public static String encodeForUri (Object arg) { return encodeForUri(arg, 'U'); } public static String iriToUri (Object arg) { return encodeForUri(arg, 'I'); } public static String escapeHtmlUri (Object arg) { return encodeForUri(arg, 'H'); } static String encodeForUri (Object arg, char mode) { String str; if (arg instanceof String || arg instanceof UntypedAtomic) str = arg.toString(); else if (arg == null || arg == Values.empty) str = ""; else throw new ClassCastException(); return URIPath.encodeForUri(str, mode); } public static String normalizeSpace (Object arg) { String str = coerceToString(arg, "normalize-space", 1, ""); int len = str.length(); StringBuffer sbuf = null; int skipped = 0; for (int i = 0; i < len; i++) { char ch = str.charAt(i); if (Character.isWhitespace(ch)) { if (sbuf == null && skipped == 0 && i > 0) sbuf = new StringBuffer(str.substring(0, i)); skipped++; } else { if (skipped > 0) { if (sbuf != null) sbuf.append(' '); else if (skipped > 1 || i == 1 || str.charAt(i-1) != ' ') sbuf = new StringBuffer(); skipped = 0; } if (sbuf != null) sbuf.append(ch); } } return sbuf != null ? sbuf.toString() : skipped > 0 ? "" : str; } /* #ifdef use:java.util.regex */ public static Pattern makePattern (String pattern, String flags) { int fl = 0; for (int i = flags.length(); --i >= 0; ) { char ch = flags.charAt(i); switch (ch) { case 'i': fl |= Pattern.CASE_INSENSITIVE|Pattern.UNICODE_CASE; break; case 's': fl |= Pattern.DOTALL; break; case 'x': StringBuffer sbuf = new StringBuffer(); int plen = pattern.length(); for (int j = 0; j < plen; j++) { char pch = pattern.charAt(j); if (! Character.isWhitespace(pch)) sbuf.append(pch); } pattern = sbuf.toString(); break; case 'm': fl |= Pattern.MULTILINE; break; default: throw new IllegalArgumentException("unknown 'replace' flag"); } } return Pattern.compile(pattern, fl); } /* #endif */ public static boolean matches (Object input, String pattern) { return matches(input, pattern, ""); } public static boolean matches (Object arg, String pattern, String flags) { /* #ifdef use:java.util.regex */ String str; if (arg instanceof String || arg instanceof UntypedAtomic) str = arg.toString(); else if (arg == null || arg == Values.empty) str = ""; else throw new ClassCastException(); return makePattern(pattern, flags).matcher(str).find(); /* #else */ // throw new Error("fn:matches requires java.util.regex (JDK 1.4 or equivalent)"); /* #endif */ } public static String replace (Object input, String pattern, String replacement) { return replace(input, pattern, replacement, ""); } public static String replace (Object arg, String pattern, String replacement, String flags) { /* #ifdef use:java.util.regex */ String str; if (arg instanceof String || arg instanceof UntypedAtomic) str = arg.toString(); else if (arg == null || arg == Values.empty) str = ""; else throw new ClassCastException(); return makePattern(pattern, flags).matcher(str).replaceAll(replacement); /* #else */ // throw new Error("fn:replace requires java.util.regex (JDK 1.4 or equivalent)"); /* #endif */ } public static void tokenize$X (Object arg, String pattern, CallContext ctx) { tokenize$X(arg, pattern, "", ctx); } public static void tokenize$X (Object arg, String pattern, String flags, CallContext ctx) { /* #ifdef use:java.util.regex */ String str; if (arg instanceof String || arg instanceof UntypedAtomic) str = arg.toString(); else if (arg == null || arg == Values.empty) str = ""; else throw new ClassCastException(); Consumer out = ctx.consumer; Matcher matcher = makePattern(pattern, flags).matcher(str); int len = str.length(); if (len == 0) return; int start = 0; for (;;) { boolean matched = matcher.find(); if (! matched) { out.writeObject(str.substring(start)); break; } int end = matcher.start(); out.writeObject(str.substring(start, end)); start = matcher.end(); if (start == end) throw new IllegalArgumentException("pattern matches empty string"); } /* #else */ // throw new Error("fn:tokenize requires java.util.regex (JDK 1.4 or equivalent)"); /* #endif */ } public static Object codepointEqual (Object arg1, Object arg2) { String str1 = coerceToString(arg1, "codepoint-equal", 1, null); String str2 = coerceToString(arg2, "codepoint-equal", 2, null); if (str1 == null || str2 == null) return Values.empty; return str1.equals(str2) ? Boolean.TRUE : Boolean.FALSE; } public static Object normalizeUnicode (Object arg) { return normalizeUnicode(arg, "NFC"); } public static Object normalizeUnicode (Object arg, String form) { String str = coerceToString(arg, "normalize-unicode", 1, ""); form = form.trim().toUpperCase(); if ("".equals(form)) return str; /* #ifdef use:java.text.Normalizer */ // Normalizer.Form nform; // if ("NFC".equals(form)) // nform = Normalizer.Form.NFC; // else if ("NFD".equals(form)) // nform = Normalizer.Form.NFD; // else if ("NFKC".equals(form)) // nform = Normalizer.Form.NFKC; // else if ("NFKD".equals(form)) // nform = Normalizer.Form.NFKD; // else // throw new RuntimeException("normalize-unicode: unknown normalization form '"+form+'\''); // return Normalizer.normalize(str, nform); /* #else */ throw AbstractSequence.unsupportedException("normalize-unicode form "+form); /* #endif */ }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -