📄 nativestring.java
字号:
case Id_blink: return tagify(thisObj, "blink", null, null); case Id_sup: return tagify(thisObj, "sup", null, null); case Id_sub: return tagify(thisObj, "sub", null, null); case Id_fontsize: return tagify(thisObj, "font", "size", args); case Id_fontcolor: return tagify(thisObj, "font", "color", args); case Id_link: return tagify(thisObj, "a", "href", args); case Id_anchor: return tagify(thisObj, "a", "name", args); case Id_equals: case Id_equalsIgnoreCase: { String s1 = ScriptRuntime.toString(thisObj); String s2 = ScriptRuntime.toString(args, 0); return ScriptRuntime.wrapBoolean( (id == Id_equals) ? s1.equals(s2) : s1.equalsIgnoreCase(s2)); } case Id_match: case Id_search: case Id_replace: { int actionType; if (id == Id_match) { actionType = RegExpProxy.RA_MATCH; } else if (id == Id_search) { actionType = RegExpProxy.RA_SEARCH; } else { actionType = RegExpProxy.RA_REPLACE; } return ScriptRuntime.checkRegExpProxy(cx). action(cx, scope, thisObj, args, actionType); } // ECMA-262 1 5.5.4.9 case Id_localeCompare: { // For now, create and configure a collator instance. I can't // actually imagine that this'd be slower than caching them // a la ClassCache, so we aren't trying to outsmart ourselves // with a caching mechanism for now. Collator collator = Collator.getInstance(cx.getLocale()); collator.setStrength(Collator.IDENTICAL); collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION); return ScriptRuntime.wrapNumber(collator.compare( ScriptRuntime.toString(thisObj), ScriptRuntime.toString(args, 0))); } case Id_toLocaleLowerCase: { return ScriptRuntime.toString(thisObj) .toLowerCase(cx.getLocale()); } case Id_toLocaleUpperCase: { return ScriptRuntime.toString(thisObj) .toUpperCase(cx.getLocale()); } } throw new IllegalArgumentException(String.valueOf(id)); } } private static NativeString realThis(Scriptable thisObj, IdFunctionObject f) { if (!(thisObj instanceof NativeString)) throw incompatibleCallError(f); return (NativeString)thisObj; } /* * HTML composition aids. */ private static String tagify(Object thisObj, String tag, String attribute, Object[] args) { String str = ScriptRuntime.toString(thisObj); StringBuffer result = new StringBuffer(); result.append('<'); result.append(tag); if (attribute != null) { result.append(' '); result.append(attribute); result.append("=\""); result.append(ScriptRuntime.toString(args, 0)); result.append('"'); } result.append('>'); result.append(str); result.append("</"); result.append(tag); result.append('>'); return result.toString(); } public String toString() { return string; } /* Make array-style property lookup work for strings. * XXX is this ECMA? A version check is probably needed. In js too. */ public Object get(int index, Scriptable start) { if (0 <= index && index < string.length()) { return string.substring(index, index + 1); } return super.get(index, start); } public void put(int index, Scriptable start, Object value) { if (0 <= index && index < string.length()) { return; } super.put(index, start, value); } /* * * See ECMA 15.5.4.6. Uses Java String.indexOf() * OPT to add - BMH searching from jsstr.c. */ private static int js_indexOf(String target, Object[] args) { String search = ScriptRuntime.toString(args, 0); double begin = ScriptRuntime.toInteger(args, 1); if (begin > target.length()) { return -1; } else { if (begin < 0) begin = 0; return target.indexOf(search, (int)begin); } } /* * * See ECMA 15.5.4.7 * */ private static int js_lastIndexOf(String target, Object[] args) { String search = ScriptRuntime.toString(args, 0); double end = ScriptRuntime.toNumber(args, 1); if (end != end || end > target.length()) end = target.length(); else if (end < 0) end = 0; return target.lastIndexOf(search, (int)end); } /* * Used by js_split to find the next split point in target, * starting at offset ip and looking either for the given * separator substring, or for the next re match. ip and * matchlen must be reference variables (assumed to be arrays of * length 1) so they can be updated in the leading whitespace or * re case. * * Return -1 on end of string, >= 0 for a valid index of the next * separator occurrence if found, or the string length if no * separator is found. */ private static int find_split(Context cx, Scriptable scope, String target, String separator, int version, RegExpProxy reProxy, Scriptable re, int[] ip, int[] matchlen, boolean[] matched, String[][] parensp) { int i = ip[0]; int length = target.length(); /* * Perl4 special case for str.split(' '), only if the user has selected * JavaScript1.2 explicitly. Split on whitespace, and skip leading w/s. * Strange but true, apparently modeled after awk. */ if (version == Context.VERSION_1_2 && re == null && separator.length() == 1 && separator.charAt(0) == ' ') { /* Skip leading whitespace if at front of str. */ if (i == 0) { while (i < length && Character.isWhitespace(target.charAt(i))) i++; ip[0] = i; } /* Don't delimit whitespace at end of string. */ if (i == length) return -1; /* Skip over the non-whitespace chars. */ while (i < length && !Character.isWhitespace(target.charAt(i))) i++; /* Now skip the next run of whitespace. */ int j = i; while (j < length && Character.isWhitespace(target.charAt(j))) j++; /* Update matchlen to count delimiter chars. */ matchlen[0] = j - i; return i; } /* * Stop if past end of string. If at end of string, we will * return target length, so that * * "ab,".split(',') => new Array("ab", "") * * and the resulting array converts back to the string "ab," * for symmetry. NB: This differs from perl, which drops the * trailing empty substring if the LIMIT argument is omitted. */ if (i > length) return -1; /* * Match a regular expression against the separator at or * above index i. Return -1 at end of string instead of * trying for a match, so we don't get stuck in a loop. */ if (re != null) { return reProxy.find_split(cx, scope, target, separator, re, ip, matchlen, matched, parensp); } /* * Deviate from ECMA by never splitting an empty string by any separator * string into a non-empty array (an array of length 1 that contains the * empty string). */ if (version != Context.VERSION_DEFAULT && version < Context.VERSION_1_3 && length == 0) return -1; /* * Special case: if sep is the empty string, split str into * one character substrings. Let our caller worry about * whether to split once at end of string into an empty * substring. * * For 1.2 compatibility, at the end of the string, we return the length as * the result, and set the separator length to 1 -- this allows the caller * to include an additional null string at the end of the substring list. */ if (separator.length() == 0) { if (version == Context.VERSION_1_2) { if (i == length) { matchlen[0] = 1; return i; } return i + 1; } return (i == length) ? -1 : i + 1; } /* Punt to j.l.s.indexOf; return target length if separator is * not found. */ if (ip[0] >= length) return length; i = target.indexOf(separator, ip[0]); return (i != -1) ? i : length; } /* * See ECMA 15.5.4.8. Modified to match JS 1.2 - optionally takes * a limit argument and accepts a regular expression as the split * argument. */ private static Object js_split(Context cx, Scriptable scope, String target, Object[] args) { // create an empty Array to return; Scriptable top = getTopLevelScope(scope); Scriptable result = ScriptRuntime.newObject(cx, top, "Array", null); // return an array consisting of the target if no separator given // don't check against undefined, because we want // 'fooundefinedbar'.split(void 0) to split to ['foo', 'bar'] if (args.length < 1) { result.put(0, result, target); return result; } // Use the second argument as the split limit, if given. boolean limited = (args.length > 1) && (args[1] != Undefined.instance); long limit = 0; // Initialize to avoid warning. if (limited) { /* Clamp limit between 0 and 1 + string length. */ limit = ScriptRuntime.toUint32(args[1]); if (limit > target.length()) limit = 1 + target.length(); } String separator = null; int[] matchlen = new int[1]; Scriptable re = null; RegExpProxy reProxy = null; if (args[0] instanceof Scriptable) { reProxy = ScriptRuntime.getRegExpProxy(cx); if (reProxy != null) { Scriptable test = (Scriptable)args[0]; if (reProxy.isRegExp(test)) { re = test; } } } if (re == null) { separator = ScriptRuntime.toString(args[0]); matchlen[0] = separator.length();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -