/* * See ECMA 15.5.4.15 */ private static string js_substring(Context cx, string target, object [] args) { int length = target.Length; double start = ScriptConvert.ToInteger(args, 0); double end; if (start < 0) { start = 0; } else if (start > length) { start = length; } if (args.Length <= 1 || args [1] == Undefined.Value) { end = length; } else { end = ScriptConvert.ToInteger(args [1]); if (end < 0) { end = 0; } else if (end > length) { end = length; } // swap if end < start if (end < start) { if (cx.Version != Context.Versions.JS1_2) { double temp = start; start = end; end = temp; } else { // Emulate old JDK1.0 java.lang.String.substring() end = start; } } } return(target.Substring((int)start, ((int)end) - ((int)start))); }
private static string js_slice(string target, object [] args) { if (args.Length != 0) { double begin = ScriptConvert.ToInteger(args [0]); double end; int length = target.Length; if (begin < 0) { begin += length; if (begin < 0) { begin = 0; } } else if (begin > length) { begin = length; } if (args.Length == 1) { end = length; } else { end = ScriptConvert.ToInteger(args [1]); if (end < 0) { end += length; if (end < 0) { end = 0; } } else if (end > length) { end = length; } if (end < begin) { end = begin; } } return(target.Substring((int)begin, ((int)end) - ((int)begin))); } return(target); }
/* * * 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 = ScriptConvert.ToString(args, 0); double begin = ScriptConvert.ToInteger(args, 1); if (begin > target.Length) { return(-1); } else { if (begin < 0) { begin = 0; } return(target.IndexOf(search, (int)begin)); } }
public override object ExecIdCall(IdFunctionObject f, Context cx, IScriptable scope, IScriptable thisObj, object [] args) { if (!f.HasTag(STRING_TAG)) { return(base.ExecIdCall(f, cx, scope, thisObj, args)); } int id = f.MethodId; switch (id) { case ConstructorId_fromCharCode: { int N = args.Length; if (N < 1) { return(""); } System.Text.StringBuilder sb = new System.Text.StringBuilder(N); for (int i = 0; i != N; ++i) { sb.Append(ScriptConvert.ToUint16(args [i])); } return(sb.ToString()); } case Id_constructor: { string s = (args.Length >= 1) ? ScriptConvert.ToString(args [0]) : ""; if (thisObj == null) { // new String(val) creates a new String object. return(new BuiltinString(s)); } // String(val) converts val to a string value. return(s); } case Id_toString: case Id_valueOf: // ECMA 15.5.4.2: 'the toString function is not generic. return(RealThis(thisObj, f).m_Value); case Id_toSource: { string s = RealThis(thisObj, f).m_Value; return("(new String(\"" + ScriptRuntime.escapeString(s) + "\"))"); } case Id_charAt: case Id_charCodeAt: { // See ECMA 15.5.4.[4,5] string target = ScriptConvert.ToString(thisObj); double pos = ScriptConvert.ToInteger(args, 0); if (pos < 0 || pos >= target.Length) { if (id == Id_charAt) { return(""); } else { return(double.NaN); } } char c = target [(int)pos]; if (id == Id_charAt) { return(Convert.ToString(c)); } else { return((int)c); } } case Id_indexOf: return(js_indexOf(ScriptConvert.ToString(thisObj), args)); case Id_lastIndexOf: return(js_lastIndexOf(ScriptConvert.ToString(thisObj), args)); case Id_split: return(ImplSplit(cx, scope, ScriptConvert.ToString(thisObj), args)); case Id_substring: return(js_substring(cx, ScriptConvert.ToString(thisObj), args)); case Id_toLowerCase: // See ECMA 15.5.4.11 return(ScriptConvert.ToString(thisObj).ToLower()); case Id_toUpperCase: // See ECMA 15.5.4.12 return(ScriptConvert.ToString(thisObj).ToUpper()); case Id_substr: return(js_substr(ScriptConvert.ToString(thisObj), args)); case Id_concat: return(js_concat(ScriptConvert.ToString(thisObj), args)); case Id_slice: return(js_slice(ScriptConvert.ToString(thisObj), args)); case Id_bold: return(Tagify(thisObj, "b", null, null)); case Id_italics: return(Tagify(thisObj, "i", null, null)); case Id_fixed: return(Tagify(thisObj, "tt", null, null)); case Id_strike: return(Tagify(thisObj, "strike", null, null)); case Id_small: return(Tagify(thisObj, "small", null, null)); case Id_big: return(Tagify(thisObj, "big", null, null)); 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 = ScriptConvert.ToString(thisObj); string s2 = ScriptConvert.ToString(args, 0); return((id == Id_equals) ? s1.Equals(s2) : s1.ToUpper().Equals(s2.ToUpper())); } case Id_match: case Id_search: case Id_replace: { RegExpActions actionType; if (id == Id_match) { actionType = EcmaScript.NET.RegExpActions.Match; } else if (id == Id_search) { actionType = EcmaScript.NET.RegExpActions.Search; } else { actionType = EcmaScript.NET.RegExpActions.Replace; } return(cx.regExpProxy.Perform(cx, scope, thisObj, args, actionType)); } } throw new ArgumentException(Convert.ToString(id)); }