📄 string.js.svn-base
字号:
var matches = DoRegExpExecGlobal(regexp, subject); // If the regexp did not match, return null. if (matches.length == 0) return null; // Build the result array. var result = new $Array(match_string); for (var i = 0; i < matches.length; ++i) { var match = matches[i]; var match_string = subject.slice(match[0], match[1]); result[i] = match_string; } return result;}, DONT_ENUM);// ECMA-262 section 15.5.4.12%AddProperty($String.prototype, "search", function(re) { var regexp = new ORIGINAL_REGEXP(re); var s = ToString(this); var last_idx = regexp.lastIndex; // keep old lastIndex regexp.lastIndex = 0; // ignore re.global property var result = regexp.exec(s); regexp.lastIndex = last_idx; // restore lastIndex if (result == null) return -1; else return result.index;}, DONT_ENUM);// ECMA-262 section 15.5.4.13%AddProperty($String.prototype, "slice", function(start, end) { var s = ToString(this); var s_len = s.length; var start_i = TO_INTEGER(start); var end_i = s_len; if (end !== void 0) end_i = TO_INTEGER(end); if (start_i < 0) { start_i += s_len; if (start_i < 0) start_i = 0; } else { if (start_i > s_len) start_i = s_len; } if (end_i < 0) { end_i += s_len; if (end_i < 0) end_i = 0; } else { if (end_i > s_len) end_i = s_len; } var num_c = end_i - start_i; if (num_c < 0) num_c = 0; return SubString(s, start_i, start_i + num_c);}, DONT_ENUM);// ECMA-262 section 15.5.4.14%AddProperty($String.prototype, "split", function(separator, limit) { var subject = ToString(this); var result = []; var lim = (limit === void 0) ? 0xffffffff : ToUint32(limit); if (lim === 0) return result; // ECMA-262 says that if separator is undefined, the result should // be an array of size 1 containing the entire string. SpiderMonkey // and KJS have this behaviour only when no separator is given. If // undefined is explicitly given, they convert it to a string and // use that. We do as SpiderMonkey and KJS. if (%_ArgumentsLength() === 0) { result[result.length] = subject; return result; } var length = subject.length; var currentIndex = 0; var startIndex = 0; var sep = IS_REGEXP(separator) ? separator : ToString(separator); if (length === 0) { if (splitMatch(sep, subject, 0, 0) != null) return result; result[result.length] = subject; return result; } while (true) { if (startIndex === length) { result[result.length] = subject.slice(currentIndex, length); return result; } var match = splitMatch(sep, subject, currentIndex, startIndex); if (IS_NULL(match)) { result[result.length] = subject.slice(currentIndex, length); return result; } var endIndex = match[0]; // We ignore a zero-length match at the currentIndex. if (startIndex === endIndex && endIndex === currentIndex) { startIndex++; continue; } result[result.length] = match[1]; if (result.length === lim) return result; for (var i = 2; i < match.length; i++) { result[result.length] = match[i]; if (result.length === lim) return result; } startIndex = currentIndex = endIndex; }}, DONT_ENUM);// ECMA-262 section 15.5.4.14// Helper function used by split.function splitMatch(separator, subject, current_index, start_index) { if (IS_REGEXP(separator)) { var ovector = DoRegExpExec(separator, subject, start_index); if (ovector == null) return null; var nof_results = ovector.length >> 1; var result = new $Array(nof_results + 1); result[0] = ovector[1]; result[1] = subject.slice(current_index, ovector[0]); for (var i = 1; i < nof_results; i++) { var matching_start = ovector[2*i]; var matching_end = ovector[2*i + 1]; if (matching_start != -1 && matching_end != -1) { result[i + 1] = subject.slice(matching_start, matching_end); } } return result; } var separatorIndex = subject.indexOf(separator, start_index); if (separatorIndex === -1) return null; return [ separatorIndex + separator.length, subject.slice(current_index, separatorIndex) ];};// ECMA-262 section 15.5.4.15%AddProperty($String.prototype, "substring", function(start, end) { var s = ToString(this); var s_len = s.length; var start_i = TO_INTEGER(start); var end_i = s_len; if (!IS_UNDEFINED(end)) end_i = TO_INTEGER(end); if (start_i < 0) start_i = 0; if (start_i > s_len) start_i = s_len; if (end_i < 0) end_i = 0; if (end_i > s_len) end_i = s_len; if (start_i > end_i) { var tmp = end_i; end_i = start_i; start_i = tmp; } return SubString(s, start_i, end_i);}, DONT_ENUM);// This is not a part of ECMA-262.%AddProperty($String.prototype, "substr", function(start, n) { var s = ToString(this); var len; // Correct n: If not given, set to string length; if explicitly // set to undefined, zero, or negative, returns empty string. if (n === void 0) { len = s.length; } else { len = TO_INTEGER(n); if (len <= 0) return ''; } // Correct start: If not given (or undefined), set to zero; otherwise // convert to integer and handle negative case. if (start === void 0) { start = 0; } else { start = TO_INTEGER(start); // If positive, and greater than or equal to the string length, // return empty string. if (start >= s.length) return ''; // If negative and absolute value is larger than the string length, // use zero. if (start < 0) { start += s.length; if (start < 0) start = 0; } } var end = start + len; if (end > s.length) end = s.length; return SubString(s, start, end);}, DONT_ENUM);// ECMA-262, 15.5.4.16%AddProperty($String.prototype, "toLowerCase", function() { return %StringToLowerCase(ToString(this));}, DONT_ENUM);// ECMA-262, 15.5.4.17%AddProperty($String.prototype, "toLocaleLowerCase", $String.prototype.toLowerCase, DONT_ENUM);// ECMA-262, 15.5.4.18%AddProperty($String.prototype, "toUpperCase", function() { return %StringToUpperCase(ToString(this));}, DONT_ENUM);// ECMA-262, 15.5.4.19%AddProperty($String.prototype, "toLocaleUpperCase", $String.prototype.toUpperCase, DONT_ENUM);// ECMA-262, section 15.5.3.2%AddProperty($String, "fromCharCode", function(code) { var n = %_ArgumentsLength(); if (n == 1) return %CharFromCode(ToNumber(code) & 0xffff) // NOTE: This is not super-efficient, but it is necessary because we // want to avoid converting to numbers from within the virtual // machine. Maybe we can find another way of doing this? var codes = new $Array(n); for (var i = 0; i < n; i++) codes[i] = ToNumber(%_Arguments(i)); return %StringFromCharCodeArray(codes);}, DONT_ENUM);// ECMA-262, section 15.5.4.4function CharAt(pos) { var subject = ToString(this); var index = TO_INTEGER(pos); if (index >= subject.length || index < 0) return ""; return %CharFromCode(%StringCharCodeAt(subject, index));};%AddProperty($String.prototype, "charAt", CharAt, DONT_ENUM);// Helper function for very basic XSS protection.function HtmlEscape(str) { return ToString(str).replace(/</g, "<") .replace(/>/g, ">") .replace(/"/g, """) .replace(/'/g, "'");};// Compatibility support for KJS.// Tested by mozilla/js/tests/js1_5/Regress/regress-276103.js.%AddProperty($String.prototype, "link", function(link) { return "<a href=\"" + HtmlEscape(link) + "\">" + this + "</a>";}, DONT_ENUM);%AddProperty($String.prototype, "anchor", function(name) { return "<a name=\"" + HtmlEscape(name) + "\">" + this + "</a>";}, DONT_ENUM);%AddProperty($String.prototype, "fontcolor", function(color) { return "<font color=\"" + HtmlEscape(color) + "\">" + this + "</font>";}, DONT_ENUM);%AddProperty($String.prototype, "fontsize", function(size) { return "<font size=\"" + HtmlEscape(size) + "\">" + this + "</font>";}, DONT_ENUM);%AddProperty($String.prototype, "big", function() { return "<big>" + this + "</big>";}, DONT_ENUM);%AddProperty($String.prototype, "blink", function() { return "<blink>" + this + "</blink>";}, DONT_ENUM);%AddProperty($String.prototype, "bold", function() { return "<b>" + this + "</b>";}, DONT_ENUM);%AddProperty($String.prototype, "fixed", function() { return "<tt>" + this + "</tt>";}, DONT_ENUM);%AddProperty($String.prototype, "italics", function() { return "<i>" + this + "</i>";}, DONT_ENUM);%AddProperty($String.prototype, "small", function() { return "<small>" + this + "</small>";}, DONT_ENUM);%AddProperty($String.prototype, "strike", function() { return "<strike>" + this + "</strike>";}, DONT_ENUM);%AddProperty($String.prototype, "sub", function() { return "<sub>" + this + "</sub>";}, DONT_ENUM);%AddProperty($String.prototype, "sup", function() { return "<sup>" + this + "</sup>";}, DONT_ENUM);// StringBuilder support.function StringBuilder() { this.elements = new $Array();}function ReplaceResultBuilder(str) { this.elements = new $Array(); this.special_string = str;}ReplaceResultBuilder.prototype.add =StringBuilder.prototype.add = function(str) { if (!IS_STRING(str)) str = ToString(str); if (str.length > 0) { var elements = this.elements; elements[elements.length] = str; }}ReplaceResultBuilder.prototype.addSpecialSlice = function(start, end) { var len = end - start; if (len == 0) return; var elements = this.elements; if (start >= 0 && len >= 0 && start < 0x80000 && len < 0x800) { elements[elements.length] = (start << 11) + len; } else { elements[elements.length] = SubString(this.special_string, start, end); }}StringBuilder.prototype.generate = function() { return %StringBuilderConcat(this.elements, "");}ReplaceResultBuilder.prototype.generate = function() { return %StringBuilderConcat(this.elements, this.special_string);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -