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

Side by Side Diff: src/objects.cc

Issue 2018983002: [builtins] Also migrate String.prototype.toLowerCase/toUpperCase to C++. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@StringTrim
Patch Set: REBASE Created 4 years, 6 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 unified diff | Download patch
« no previous file with comments | « src/objects.h ('k') | src/runtime/runtime.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 the V8 project authors. All rights reserved. 1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/objects.h" 5 #include "src/objects.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 #include <iomanip> 8 #include <iomanip>
9 #include <sstream> 9 #include <sstream>
10 10
(...skipping 9963 matching lines...) Expand 10 before | Expand all | Expand 10 after
9974 if (IsEmpty()) return other->IsEmpty(); 9974 if (IsEmpty()) return other->IsEmpty();
9975 if (other->IsEmpty()) return false; 9975 if (other->IsEmpty()) return false;
9976 if (length() != other->length()) return false; 9976 if (length() != other->length()) return false;
9977 for (int i = 0; i < length(); ++i) { 9977 for (int i = 0; i < length(); ++i) {
9978 if (get(i) != other->get(i)) return false; 9978 if (get(i) != other->get(i)) return false;
9979 } 9979 }
9980 return true; 9980 return true;
9981 } 9981 }
9982 #endif 9982 #endif
9983 9983
9984 namespace {
9985
9986 bool ToUpperOverflows(uc32 character) {
9987 // y with umlauts and the micro sign are the only characters that stop
9988 // fitting into one-byte when converting to uppercase.
9989 static const uc32 yuml_code = 0xff;
9990 static const uc32 micro_code = 0xb5;
9991 return (character == yuml_code || character == micro_code);
9992 }
9993
9994 template <class Converter>
9995 MaybeHandle<Object> ConvertCaseHelper(
9996 Isolate* isolate, Handle<String> string, Handle<SeqString> result,
9997 int result_length, unibrow::Mapping<Converter, 128>* mapping) {
9998 DisallowHeapAllocation no_gc;
9999 // We try this twice, once with the assumption that the result is no longer
10000 // than the input and, if that assumption breaks, again with the exact
10001 // length. This may not be pretty, but it is nicer than what was here before
10002 // and I hereby claim my vaffel-is.
10003 //
10004 // NOTE: This assumes that the upper/lower case of an ASCII
10005 // character is also ASCII. This is currently the case, but it
10006 // might break in the future if we implement more context and locale
10007 // dependent upper/lower conversions.
10008 bool has_changed_character = false;
10009
10010 // Convert all characters to upper case, assuming that they will fit
10011 // in the buffer
10012 StringCharacterStream stream(*string);
10013 unibrow::uchar chars[Converter::kMaxWidth];
10014 // We can assume that the string is not empty
10015 uc32 current = stream.GetNext();
10016 bool ignore_overflow = Converter::kIsToLower || result->IsSeqTwoByteString();
10017 for (int i = 0; i < result_length;) {
10018 bool has_next = stream.HasMore();
10019 uc32 next = has_next ? stream.GetNext() : 0;
10020 int char_length = mapping->get(current, next, chars);
10021 if (char_length == 0) {
10022 // The case conversion of this character is the character itself.
10023 result->Set(i, current);
10024 i++;
10025 } else if (char_length == 1 &&
10026 (ignore_overflow || !ToUpperOverflows(current))) {
10027 // Common case: converting the letter resulted in one character.
10028 DCHECK(static_cast<uc32>(chars[0]) != current);
10029 result->Set(i, chars[0]);
10030 has_changed_character = true;
10031 i++;
10032 } else if (result_length == string->length()) {
10033 bool overflows = ToUpperOverflows(current);
10034 // We've assumed that the result would be as long as the
10035 // input but here is a character that converts to several
10036 // characters. No matter, we calculate the exact length
10037 // of the result and try the whole thing again.
10038 //
10039 // Note that this leaves room for optimization. We could just
10040 // memcpy what we already have to the result string. Also,
10041 // the result string is the last object allocated we could
10042 // "realloc" it and probably, in the vast majority of cases,
10043 // extend the existing string to be able to hold the full
10044 // result.
10045 int next_length = 0;
10046 if (has_next) {
10047 next_length = mapping->get(next, 0, chars);
10048 if (next_length == 0) next_length = 1;
10049 }
10050 int current_length = i + char_length + next_length;
10051 while (stream.HasMore()) {
10052 current = stream.GetNext();
10053 overflows |= ToUpperOverflows(current);
10054 // NOTE: we use 0 as the next character here because, while
10055 // the next character may affect what a character converts to,
10056 // it does not in any case affect the length of what it convert
10057 // to.
10058 int char_length = mapping->get(current, 0, chars);
10059 if (char_length == 0) char_length = 1;
10060 current_length += char_length;
10061 if (current_length > String::kMaxLength) {
10062 AllowHeapAllocation allocate_error_and_return;
10063 THROW_NEW_ERROR(isolate, NewInvalidStringLengthError(), Object);
10064 }
10065 }
10066 // Try again with the real length. Return signed if we need
10067 // to allocate a two-byte string for to uppercase.
10068 if (overflows && !ignore_overflow) {
10069 return handle(Smi::FromInt(-current_length), isolate);
10070 }
10071 return handle(Smi::FromInt(current_length), isolate);
10072 } else {
10073 for (int j = 0; j < char_length; j++) {
10074 result->Set(i, chars[j]);
10075 i++;
10076 }
10077 has_changed_character = true;
10078 }
10079 current = next;
10080 }
10081 if (has_changed_character) {
10082 return result;
10083 } else {
10084 // If we didn't actually change anything in doing the conversion
10085 // we simple return the result and let the converted string
10086 // become garbage; there is no reason to keep two identical strings
10087 // alive.
10088 return string;
10089 }
10090 }
10091
10092 const uintptr_t kOneInEveryByte = kUintptrAllBitsSet / 0xFF;
10093 const uintptr_t kAsciiMask = kOneInEveryByte << 7;
10094
10095 // Given a word and two range boundaries returns a word with high bit
10096 // set in every byte iff the corresponding input byte was strictly in
10097 // the range (m, n). All the other bits in the result are cleared.
10098 // This function is only useful when it can be inlined and the
10099 // boundaries are statically known.
10100 // Requires: all bytes in the input word and the boundaries must be
10101 // ASCII (less than 0x7F).
10102 uintptr_t AsciiRangeMask(uintptr_t w, char m, char n) {
10103 // Use strict inequalities since in edge cases the function could be
10104 // further simplified.
10105 DCHECK(0 < m && m < n);
10106 // Has high bit set in every w byte less than n.
10107 uintptr_t tmp1 = kOneInEveryByte * (0x7F + n) - w;
10108 // Has high bit set in every w byte greater than m.
10109 uintptr_t tmp2 = w + kOneInEveryByte * (0x7F - m);
10110 return (tmp1 & tmp2 & (kOneInEveryByte * 0x80));
10111 }
10112
10113 #ifdef DEBUG
10114 bool CheckFastAsciiConvert(char* dst, const char* src, int length, bool changed,
10115 bool is_to_lower) {
10116 bool expected_changed = false;
10117 for (int i = 0; i < length; i++) {
10118 if (dst[i] == src[i]) continue;
10119 expected_changed = true;
10120 if (is_to_lower) {
10121 DCHECK('A' <= src[i] && src[i] <= 'Z');
10122 DCHECK(dst[i] == src[i] + ('a' - 'A'));
10123 } else {
10124 DCHECK('a' <= src[i] && src[i] <= 'z');
10125 DCHECK(dst[i] == src[i] - ('a' - 'A'));
10126 }
10127 }
10128 return (expected_changed == changed);
10129 }
10130 #endif
10131
10132 template <class Converter>
10133 bool FastAsciiConvert(char* dst, const char* src, int length,
10134 bool* changed_out) {
10135 #ifdef DEBUG
10136 char* saved_dst = dst;
10137 const char* saved_src = src;
10138 #endif
10139 DisallowHeapAllocation no_gc;
10140 // We rely on the distance between upper and lower case letters
10141 // being a known power of 2.
10142 DCHECK('a' - 'A' == (1 << 5));
10143 // Boundaries for the range of input characters than require conversion.
10144 static const char lo = Converter::kIsToLower ? 'A' - 1 : 'a' - 1;
10145 static const char hi = Converter::kIsToLower ? 'Z' + 1 : 'z' + 1;
10146 bool changed = false;
10147 uintptr_t or_acc = 0;
10148 const char* const limit = src + length;
10149
10150 // dst is newly allocated and always aligned.
10151 DCHECK(IsAligned(reinterpret_cast<intptr_t>(dst), sizeof(uintptr_t)));
10152 // Only attempt processing one word at a time if src is also aligned.
10153 if (IsAligned(reinterpret_cast<intptr_t>(src), sizeof(uintptr_t))) {
10154 // Process the prefix of the input that requires no conversion one aligned
10155 // (machine) word at a time.
10156 while (src <= limit - sizeof(uintptr_t)) {
10157 const uintptr_t w = *reinterpret_cast<const uintptr_t*>(src);
10158 or_acc |= w;
10159 if (AsciiRangeMask(w, lo, hi) != 0) {
10160 changed = true;
10161 break;
10162 }
10163 *reinterpret_cast<uintptr_t*>(dst) = w;
10164 src += sizeof(uintptr_t);
10165 dst += sizeof(uintptr_t);
10166 }
10167 // Process the remainder of the input performing conversion when
10168 // required one word at a time.
10169 while (src <= limit - sizeof(uintptr_t)) {
10170 const uintptr_t w = *reinterpret_cast<const uintptr_t*>(src);
10171 or_acc |= w;
10172 uintptr_t m = AsciiRangeMask(w, lo, hi);
10173 // The mask has high (7th) bit set in every byte that needs
10174 // conversion and we know that the distance between cases is
10175 // 1 << 5.
10176 *reinterpret_cast<uintptr_t*>(dst) = w ^ (m >> 2);
10177 src += sizeof(uintptr_t);
10178 dst += sizeof(uintptr_t);
10179 }
10180 }
10181 // Process the last few bytes of the input (or the whole input if
10182 // unaligned access is not supported).
10183 while (src < limit) {
10184 char c = *src;
10185 or_acc |= c;
10186 if (lo < c && c < hi) {
10187 c ^= (1 << 5);
10188 changed = true;
10189 }
10190 *dst = c;
10191 ++src;
10192 ++dst;
10193 }
10194
10195 if ((or_acc & kAsciiMask) != 0) return false;
10196
10197 DCHECK(CheckFastAsciiConvert(saved_dst, saved_src, length, changed,
10198 Converter::kIsToLower));
10199
10200 *changed_out = changed;
10201 return true;
10202 }
10203
10204 template <class Converter>
10205 MaybeHandle<String> ConvertCase(Handle<String> s, Isolate* isolate,
10206 unibrow::Mapping<Converter, 128>* mapping) {
10207 s = String::Flatten(s);
10208 int length = s->length();
10209 // Assume that the string is not empty; we need this assumption later
10210 if (length == 0) return s;
10211
10212 // Simpler handling of ASCII strings.
10213 //
10214 // NOTE: This assumes that the upper/lower case of an ASCII
10215 // character is also ASCII. This is currently the case, but it
10216 // might break in the future if we implement more context and locale
10217 // dependent upper/lower conversions.
10218 if (s->IsOneByteRepresentationUnderneath()) {
10219 // Same length as input.
10220 Handle<SeqOneByteString> result =
10221 isolate->factory()->NewRawOneByteString(length).ToHandleChecked();
10222 DisallowHeapAllocation no_gc;
10223 String::FlatContent flat_content = s->GetFlatContent();
10224 DCHECK(flat_content.IsFlat());
10225 bool has_changed_character = false;
10226 bool is_ascii = FastAsciiConvert<Converter>(
10227 reinterpret_cast<char*>(result->GetChars()),
10228 reinterpret_cast<const char*>(flat_content.ToOneByteVector().start()),
10229 length, &has_changed_character);
10230 // If not ASCII, we discard the result and take the 2 byte path.
10231 if (is_ascii) {
10232 if (has_changed_character) return result;
10233 return s;
10234 }
10235 }
10236
10237 Handle<SeqString> result; // Same length as input.
10238 if (s->IsOneByteRepresentation()) {
10239 result = isolate->factory()->NewRawOneByteString(length).ToHandleChecked();
10240 } else {
10241 result = isolate->factory()->NewRawTwoByteString(length).ToHandleChecked();
10242 }
10243
10244 Handle<Object> answer;
10245 ASSIGN_RETURN_ON_EXCEPTION(
10246 isolate, answer, ConvertCaseHelper(isolate, s, result, length, mapping),
10247 String);
10248 if (!answer->IsString()) {
10249 DCHECK(answer->IsSmi());
10250 length = Handle<Smi>::cast(answer)->value();
10251 if (s->IsOneByteRepresentation() && length > 0) {
10252 ASSIGN_RETURN_ON_EXCEPTION(
10253 isolate, result, isolate->factory()->NewRawOneByteString(length),
10254 String);
10255 } else {
10256 if (length < 0) length = -length;
10257 ASSIGN_RETURN_ON_EXCEPTION(
10258 isolate, result, isolate->factory()->NewRawTwoByteString(length),
10259 String);
10260 }
10261 ASSIGN_RETURN_ON_EXCEPTION(
10262 isolate, answer, ConvertCaseHelper(isolate, s, result, length, mapping),
10263 String);
10264 }
10265 return Handle<String>::cast(answer);
10266 }
10267
10268 } // namespace
10269
10270 // static
10271 MaybeHandle<String> String::ToLowerCase(Handle<String> string) {
10272 Isolate* const isolate = string->GetIsolate();
10273 return ConvertCase(string, isolate,
10274 isolate->runtime_state()->to_lower_mapping());
10275 }
10276
10277 // static
10278 MaybeHandle<String> String::ToUpperCase(Handle<String> string) {
10279 Isolate* const isolate = string->GetIsolate();
10280 return ConvertCase(string, isolate,
10281 isolate->runtime_state()->to_upper_mapping());
10282 }
10283
9984 // static 10284 // static
9985 Handle<String> String::Trim(Handle<String> string, TrimMode mode) { 10285 Handle<String> String::Trim(Handle<String> string, TrimMode mode) {
9986 Isolate* const isolate = string->GetIsolate(); 10286 Isolate* const isolate = string->GetIsolate();
9987 string = String::Flatten(string); 10287 string = String::Flatten(string);
9988 int const length = string->length(); 10288 int const length = string->length();
9989 10289
9990 // Perform left trimming if requested. 10290 // Perform left trimming if requested.
9991 int left = 0; 10291 int left = 0;
9992 UnicodeCache* unicode_cache = isolate->unicode_cache(); 10292 UnicodeCache* unicode_cache = isolate->unicode_cache();
9993 if (mode == kTrim || mode == kTrimLeft) { 10293 if (mode == kTrim || mode == kTrimLeft) {
(...skipping 8558 matching lines...) Expand 10 before | Expand all | Expand 10 after
18552 if (cell->value() != *new_value) { 18852 if (cell->value() != *new_value) {
18553 cell->set_value(*new_value); 18853 cell->set_value(*new_value);
18554 Isolate* isolate = cell->GetIsolate(); 18854 Isolate* isolate = cell->GetIsolate();
18555 cell->dependent_code()->DeoptimizeDependentCodeGroup( 18855 cell->dependent_code()->DeoptimizeDependentCodeGroup(
18556 isolate, DependentCode::kPropertyCellChangedGroup); 18856 isolate, DependentCode::kPropertyCellChangedGroup);
18557 } 18857 }
18558 } 18858 }
18559 18859
18560 } // namespace internal 18860 } // namespace internal
18561 } // namespace v8 18861 } // namespace v8
OLDNEW
« 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