Chromium Code Reviews| 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; |