Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(213)

Unified Diff: src/string.js

Issue 20673003: Implemented String.prototype.startsWith as per ES6 draft 07-15-13, section 15.5.3.22 (Closed) Base URL: git://github.com/v8/v8.git@master
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | test/mjsunit/string-startswith.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/string.js
diff --git a/src/string.js b/src/string.js
index cb82c166346fc7ba5491a1e9e5d28e0e9b5a5108..c73ef0697c3334a8bb0a11e741e682b6fb4d9c33 100644
--- a/src/string.js
+++ b/src/string.js
@@ -882,6 +882,37 @@ function StringFromCharCode(code) {
}
+// ES6 draft 07-15-13, section 15.5.3.22
+function StringStartsWith(searchString /* position */) { // length == 1
+ if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
+ throw MakeTypeError("called_on_null_or_undefined",
+ ["String.prototype.startsWith"]);
+ }
+
+ var s = TO_STRING_INLINE(this);
+ var s_len = s.length;
+ var ss = TO_STRING_INLINE(searchString);
+ var ss_len = ss.length;
+
+ if (ss_len > 0) {
+ var pos = 0;
+ if (%_ArgumentsLength() > 1) {
+ pos = %_Arguments(1); // position
+ pos = TO_INTEGER(pos);
+ }
+
+ var start = $Math.min($Math.max(pos, 0), s_len);
+ if (ss_len + start > s_len) {
+ return false;
+ }
+
+ return %StringIndexOf(s, ss, start) === start;
+ }
+
+ return true;
+}
+
+
// Helper function for very basic XSS protection.
function HtmlEscape(str) {
return TO_STRING_INLINE(str).replace(/</g, "&lt;")
@@ -1010,7 +1041,8 @@ function SetUpString() {
"small", StringSmall,
"strike", StringStrike,
"sub", StringSub,
- "sup", StringSup
+ "sup", StringSup,
+ "startsWith", StringStartsWith
));
}
« no previous file with comments | « no previous file | test/mjsunit/string-startswith.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698