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

Unified Diff: src/harmony-string.js

Issue 406863003: Implement String.prototype.codePointAt and String.fromCodePoint. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 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 | src/messages.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/harmony-string.js
diff --git a/src/harmony-string.js b/src/harmony-string.js
index 4cd8e6687edaeacb51079801b74480a21cc352b0..a8ef1ddb1246842e8bbb3336551c8e26428e6f22 100644
--- a/src/harmony-string.js
+++ b/src/harmony-string.js
@@ -120,17 +120,73 @@ function StringContains(searchString /* position */) { // length == 1
}
+// ES6 Draft 05-22-2014, section 21.1.3.3
+function StringCodePointAt(pos) {
+ CHECK_OBJECT_COERCIBLE(this, "String.prototype.codePointAt");
+
+ var string = TO_STRING_INLINE(this);
+ var size = string.length;
+ pos = TO_INTEGER(pos);
+ if (pos < 0 || pos >= size) {
+ return UNDEFINED;
+ }
+ var first = %_StringCharCodeAt(string, pos);
+ if (first < 0xD800 || first > 0xDBFF || pos + 1 == size) {
+ return first;
+ }
+ var second = %_StringCharCodeAt(string, pos + 1);
+ if (second < 0xDC00 || second > 0xDFFF) {
+ return first;
+ }
+ return (first - 0xD800) * 0x400 + second + 0x2400;
+}
+
+
+// ES6 Draft 05-22-2014, section 21.1.2.2
+function StringFromCodePoint(_) { // length = 1
+ var code;
+ var length = %_ArgumentsLength();
+ var index;
+ var result = "";
+ for (index = 0; index < length; index++) {
+ code = %_Arguments(index);
+ if (!%_IsSmi(code)) {
+ code = ToNumber(code);
+ }
+ if (code < 0 || code > 0x10FFFF || code !== TO_INTEGER(code)) {
+ throw MakeRangeError("invalid_code_point", [code]);
+ }
+ if (code <= 0xFFFF) {
+ result += %_StringCharFromCode(code);
+ } else {
+ code -= 0x10000;
+ result += StringFromCharCode(
+ code >>> 10 & 0x3FF | 0xD800,
+ 0xDC00 | code & 0x3FF
+ );
+ }
+ }
+ return result;
+}
+
+
// -------------------------------------------------------------------
function ExtendStringPrototype() {
%CheckIsBootstrapping();
+ // Set up the non-enumerable functions on the String object.
+ InstallFunctions($String, DONT_ENUM, $Array(
+ "fromCodePoint", StringFromCodePoint
+ ));
+
// Set up the non-enumerable functions on the String prototype object.
InstallFunctions($String.prototype, DONT_ENUM, $Array(
- "repeat", StringRepeat,
- "startsWith", StringStartsWith,
+ "codePointAt", StringCodePointAt,
+ "contains", StringContains,
"endsWith", StringEndsWith,
- "contains", StringContains
+ "repeat", StringRepeat,
+ "startsWith", StringStartsWith
));
}
« no previous file with comments | « no previous file | src/messages.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698