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

Unified Diff: src/objects.cc

Issue 2018963002: [builtins] Migrate String.prototype.trim/trimLeft/trimRight to C++. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 7 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
« src/builtins.cc ('K') | « src/objects.h ('k') | src/runtime/runtime.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/objects.cc
diff --git a/src/objects.cc b/src/objects.cc
index a47a45dcb4b01df9d29e384b9f1adff55a26e3b3..3eb5368e4f1d5b7ed61ee85a2e5e6f2bc0933022 100644
--- a/src/objects.cc
+++ b/src/objects.cc
@@ -9982,6 +9982,36 @@ bool DescriptorArray::IsEqualTo(DescriptorArray* other) {
}
#endif
+// static
+Handle<String> String::Trim(Handle<String> string, TrimMode mode) {
+ Isolate* const isolate = string->GetIsolate();
+
+ // Flatten the input {string}.
Franzi 2016/05/27 14:20:08 Do we need this comment?
Benedikt Meurer 2016/05/27 17:20:58 Done.
+ string = String::Flatten(string);
+ int const length = string->length();
+
+ // Perform left trimming if requested.
+ int left = 0;
+ UnicodeCache* unicode_cache = isolate->unicode_cache();
+ if (mode & kTrimLeft) {
Franzi 2016/05/27 14:20:08 For readability, I would prefer if (mode == kTrim
Benedikt Meurer 2016/05/27 17:20:58 Good suggestion, thanks. Did that.
+ while (left < length &&
+ unicode_cache->IsWhiteSpaceOrLineTerminator(string->Get(left))) {
+ left++;
+ }
+ }
+
+ // Perform right trimming if requested.
+ int right = length;
+ if (mode & kTrimRight) {
+ while (
+ right > left &&
+ unicode_cache->IsWhiteSpaceOrLineTerminator(string->Get(right - 1))) {
+ right--;
+ }
+ }
+
+ return isolate->factory()->NewSubString(string, left, right);
+}
bool String::LooksValid() {
if (!GetIsolate()->heap()->Contains(this)) return false;
« src/builtins.cc ('K') | « src/objects.h ('k') | src/runtime/runtime.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698