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

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: Address Franziskas comments. 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
« no previous file with comments | « 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..02197f2d038a4134e96e9429a21640abcc05270d 100644
--- a/src/objects.cc
+++ b/src/objects.cc
@@ -9982,6 +9982,34 @@ bool DescriptorArray::IsEqualTo(DescriptorArray* other) {
}
#endif
+// static
+Handle<String> String::Trim(Handle<String> string, TrimMode mode) {
+ Isolate* const isolate = string->GetIsolate();
+ 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 == kTrim || mode == kTrimLeft) {
+ while (left < length &&
+ unicode_cache->IsWhiteSpaceOrLineTerminator(string->Get(left))) {
+ left++;
+ }
+ }
+
+ // Perform right trimming if requested.
+ int right = length;
+ if (mode == kTrim || 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;
« no previous file with comments | « src/objects.h ('k') | src/runtime/runtime.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698