| Index: src/runtime/runtime-i18n.cc
|
| diff --git a/src/runtime/runtime-i18n.cc b/src/runtime/runtime-i18n.cc
|
| index 27f970bdb4b17392cee21a375e7a04a49c4985aa..b0eba79eeaccb329896a8a0b5be521495c201bc3 100644
|
| --- a/src/runtime/runtime-i18n.cc
|
| +++ b/src/runtime/runtime-i18n.cc
|
| @@ -29,10 +29,12 @@
|
| #include "unicode/rbbi.h"
|
| #include "unicode/smpdtfmt.h"
|
| #include "unicode/timezone.h"
|
| +#include "unicode/translit.h"
|
| #include "unicode/uchar.h"
|
| #include "unicode/ucol.h"
|
| #include "unicode/ucurr.h"
|
| #include "unicode/uloc.h"
|
| +#include "unicode/unistr.h"
|
| #include "unicode/unum.h"
|
| #include "unicode/uversion.h"
|
|
|
| @@ -749,6 +751,259 @@ RUNTIME_FUNCTION(Runtime_BreakIteratorBreakType) {
|
| return *isolate->factory()->NewStringFromStaticChars("unknown");
|
| }
|
| }
|
| +
|
| +namespace {
|
| +void ConvertCaseWithTransliterator(icu::UnicodeString* input,
|
| + const char* transliterator_id) {
|
| + UErrorCode status = U_ZERO_ERROR;
|
| + base::SmartPointer<icu::Transliterator> translit(
|
| + icu::Transliterator::createInstance(
|
| + icu::UnicodeString(transliterator_id, -1, US_INV), UTRANS_FORWARD,
|
| + status));
|
| + if (U_FAILURE(status)) return;
|
| + translit->transliterate(*input);
|
| +}
|
| +
|
| +MUST_USE_RESULT Object* LocaleConvertCase(Handle<String> s, Isolate* isolate,
|
| + bool is_to_upper, int locale_id) {
|
| + static const char* conversion_locales[] = {
|
| + "az", "el", "lt", "tr",
|
| + };
|
| + RUNTIME_ASSERT(locale_id >= -1 &&
|
| + locale_id < static_cast<int>(arraysize(conversion_locales)));
|
| + int32_t src_length = s->length();
|
| + const UChar* src = nullptr;
|
| +
|
| + base::SmartArrayPointer<uc16> sap;
|
| + if (s->IsOneByteRepresentationUnderneath()) {
|
| + sap = s->ToWideCString();
|
| + src = reinterpret_cast<const UChar*>(sap.get());
|
| + }
|
| +
|
| + // Greek (id == 1) uppercasing has to be done via transliteration.
|
| + // TODO(jshin): Drop this special-casing once ICU's regular case conversion
|
| + // API supports Greek uppercasing. See
|
| + // http://bugs.icu-project.org/trac/ticket/10582 .
|
| + // ICU's C API for transliteration is nasty and we just use C++ API.
|
| + if (V8_UNLIKELY(locale_id == 1 && is_to_upper)) {
|
| + icu::UnicodeString converted;
|
| + {
|
| + DisallowHeapAllocation no_gc;
|
| + String::FlatContent flat = s->GetFlatContent();
|
| + if (src == nullptr) {
|
| + DCHECK(flat.IsTwoByte());
|
| + src = reinterpret_cast<const UChar*>(flat.ToUC16Vector().start());
|
| + }
|
| + // Starts with the source string and will be replaced by the converted
|
| + // result.
|
| + converted.fastCopyFrom(icu::UnicodeString(false, src, src_length));
|
| + ConvertCaseWithTransliterator(&converted, "el-Upper");
|
| + }
|
| + Handle<String> result;
|
| + ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
|
| + isolate, result,
|
| + isolate->factory()->NewStringFromTwoByte(Vector<const uint16_t>(
|
| + reinterpret_cast<const uint16_t*>(converted.getBuffer()),
|
| + converted.length())));
|
| + return *result;
|
| + }
|
| +
|
| + typedef int32_t (*case_conversion_fn)(
|
| + UChar * dest, int32_t destCapacity, const UChar* src, int32_t srcLength,
|
| + const char* locale, UErrorCode* pErrorCode);
|
| + case_conversion_fn fn = is_to_upper ? u_strToUpper : u_strToLower;
|
| + const char* locale = locale_id == -1 ? "" : conversion_locales[locale_id];
|
| +
|
| + int32_t dest_length = src_length;
|
| + UErrorCode error;
|
| + Handle<SeqTwoByteString> result;
|
| +
|
| + // This is not a real loop. It'll be executed only once (no overflow) or
|
| + // twice (overflow).
|
| + do {
|
| + result =
|
| + isolate->factory()->NewRawTwoByteString(dest_length).ToHandleChecked();
|
| + base::SmartArrayPointer<uc16> sap;
|
| + DisallowHeapAllocation no_gc;
|
| + String::FlatContent flat = s->GetFlatContent();
|
| + // For OneByteString, |src| is already obtained with |sap| outside the loop.
|
| + if (flat.IsTwoByte())
|
| + src = reinterpret_cast<const UChar*>(flat.ToUC16Vector().start());
|
| + error = U_ZERO_ERROR;
|
| + dest_length = fn(reinterpret_cast<UChar*>(result->GetChars()), dest_length,
|
| + src, src_length, locale, &error);
|
| + } while (error == U_BUFFER_OVERFLOW_ERROR);
|
| +
|
| + // In most cases, the output will fill the destination buffer completely
|
| + // leading to an unterminated string (U_STRING_NOT_TERMINATED_WARNING).
|
| + // Only in rare cases, it'll be shorter than the destination buffer and
|
| + // |result| has to be truncated.
|
| + DCHECK(U_SUCCESS(error));
|
| + if (V8_LIKELY(error == U_STRING_NOT_TERMINATED_WARNING))
|
| + return *result;
|
| + if (U_SUCCESS(error)) {
|
| + return *Handle<SeqTwoByteString>::cast(
|
| + SeqString::Truncate(result, dest_length));
|
| + }
|
| + return *s;
|
| +}
|
| +
|
| +inline bool IsASCIIUpper(uint16_t ch) { return ch >= 'A' && ch <= 'Z'; }
|
| +
|
| +inline uint16_t ToASCIILower(uint16_t ch) {
|
| + return ch | ((ch >= 'A' && ch <= 'Z') << 5);
|
| +}
|
| +
|
| +inline uint16_t ToASCIIUpper(uint16_t ch) {
|
| + return ch & ~((ch >= 'a' && ch <= 'z') << 5);
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +RUNTIME_FUNCTION(Runtime_StringToLowerCaseI18N) {
|
| + HandleScope scope(isolate);
|
| + DCHECK_EQ(args.length(), 1);
|
| + CONVERT_ARG_HANDLE_CHECKED(String, s, 0);
|
| +
|
| + int length = s->length();
|
| + s = String::Flatten(s);
|
| + // First scan the string for uppercase and non-ASCII characters:
|
| + if (s->HasOnlyOneByteChars()) {
|
| + unsigned first_index_to_lower = length;
|
| + for (int index = 0; index < length; ++index) {
|
| + // Blink specializes this path for one-byte strings, so it
|
| + // does not need to do a generic get, but can do the equivalent
|
| + // of SeqOneByteStringGet.
|
| + uint16_t ch = s->Get(index);
|
| + if (V8_UNLIKELY(IsASCIIUpper(ch) || ch & ~0x7F)) {
|
| + first_index_to_lower = index;
|
| + break;
|
| + }
|
| + }
|
| +
|
| + // Nothing to do if the string is all ASCII with no uppercase.
|
| + if (first_index_to_lower == length) return *s;
|
| +
|
| + // We depend here on the invariant that the length of a Latin1
|
| + // string is invariant under ToLowerCase, and the result always
|
| + // fits in the Latin1 range (untrue for ToUpperCase, and might
|
| + // be untrue in some locales, but this is the root locale)
|
| + Handle<SeqOneByteString> result;
|
| + ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
|
| + isolate, result, isolate->factory()->NewRawOneByteString(length));
|
| + if (s->IsSeqOneByteString()) {
|
| + SeqOneByteString* source = SeqOneByteString::cast(*s);
|
| + CopyChars(result->GetChars(), source->GetChars(), first_index_to_lower);
|
| + } else {
|
| + // Do we have to worry about External{One,Two}ByteString?
|
| + DCHECK(s->IsSeqTwoByteString());
|
| + SeqTwoByteString* source = SeqTwoByteString::cast(*s);
|
| + CopyChars(result->GetChars(), source->GetChars(), first_index_to_lower);
|
| + }
|
| +
|
| + for (int index = first_index_to_lower; index < length; ++index) {
|
| + uint16_t ch = s->Get(index);
|
| + result->SeqOneByteStringSet(
|
| + index, V8_UNLIKELY(ch & ~0x7F) ? static_cast<uint16_t>(u_tolower(ch))
|
| + : ToASCIILower(ch));
|
| + }
|
| +
|
| + return *result;
|
| + }
|
| +
|
| + // Blink had an additional case here for ASCII 2-byte strings, but
|
| + // that is subsumed by the above code (assuming there isn't a false
|
| + // negative for HasOnlyOneByteChars).
|
| +
|
| + // Do a slower implementation for cases that include non-ASCII characters.
|
| + return LocaleConvertCase(s, isolate, false, -1);
|
| +}
|
| +
|
| +RUNTIME_FUNCTION(Runtime_StringToUpperCaseI18N) {
|
| + HandleScope scope(isolate);
|
| + DCHECK_EQ(args.length(), 1);
|
| + CONVERT_ARG_HANDLE_CHECKED(String, s, 0);
|
| +
|
| + // This function could be optimized for no-op cases the way lowercase
|
| + // counterpart is, but in empirical testing, few actual calls to upper()
|
| + // are no-ops. So, it wouldn't be worth the extra time for pre-scanning.
|
| +
|
| + int32_t length = s->length();
|
| + s = String::Flatten(s);
|
| + static const uint16_t sharp_s = 0x00DFu;
|
| +
|
| + if (s->HasOnlyOneByteChars()) {
|
| + Handle<SeqOneByteString> result;
|
| + ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
|
| + isolate, result, isolate->factory()->NewRawOneByteString(length));
|
| +
|
| + // Do a faster loop for the case where all the characters are ASCII.
|
| + uint16_t ored = 0;
|
| + for (int index = 0; index < length; ++index) {
|
| + uint16_t ch = s->Get(index);
|
| + ored |= ch;
|
| + result->SeqOneByteStringSet(index, ToASCIIUpper(ch));
|
| + }
|
| + if (!(ored & ~0x7F)) return *result;
|
| +
|
| + // Do a slower implementation for cases that include non-ASCII Latin-1
|
| + // characters.
|
| + int sharp_s_count = 0;
|
| +
|
| + // There are two special cases.
|
| + // 1. latin-1 characters when converted to upper case are 16 bit
|
| + // characters.
|
| + // 2. Lower case sharp-S converts to "SS" (two characters)
|
| + for (int32_t index = 0; index < length; ++index) {
|
| + uint16_t ch = s->Get(index);
|
| + if (V8_UNLIKELY(ch == sharp_s)) {
|
| + ++sharp_s_count;
|
| + continue;
|
| + }
|
| + uint16_t upper = static_cast<uint16_t>(u_toupper(static_cast<UChar>(ch)));
|
| + if (V8_UNLIKELY(upper > 0xff)) {
|
| + // Since this upper-cased character does not fit in an 8-bit string, we
|
| + // need to take the 16-bit path.
|
| + return LocaleConvertCase(s, isolate, true, -1);
|
| + }
|
| + result->SeqOneByteStringSet(index, upper);
|
| + }
|
| +
|
| + if (sharp_s_count == 0) return *result;
|
| +
|
| + // We have sharp_s_count sharp-s characters, but none of the other special
|
| + // characters.
|
| + ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
|
| + isolate, result,
|
| + isolate->factory()->NewRawOneByteString(length + sharp_s_count));
|
| + for (int32_t index = 0, dest_index = 0; index < length; ++index) {
|
| + uint16_t ch = s->Get(index);
|
| + if (ch == sharp_s) {
|
| + result->SeqOneByteStringSet(dest_index++, 'S');
|
| + result->SeqOneByteStringSet(dest_index++, 'S');
|
| + } else {
|
| + uint16_t upper =
|
| + static_cast<uint16_t>(u_toupper(static_cast<UChar>(ch)));
|
| + result->SeqOneByteStringSet(dest_index++, upper);
|
| + }
|
| + }
|
| +
|
| + return *result;
|
| + }
|
| +
|
| + return LocaleConvertCase(s, isolate, true, -1);
|
| +}
|
| +
|
| +RUNTIME_FUNCTION(Runtime_StringLocaleConvertCase) {
|
| + HandleScope scope(isolate);
|
| + DCHECK_EQ(args.length(), 3);
|
| + CONVERT_ARG_HANDLE_CHECKED(String, s, 0);
|
| + CONVERT_BOOLEAN_ARG_CHECKED(is_upper, 1);
|
| + CONVERT_NUMBER_CHECKED(int, lang_id, Int32, args[2]);
|
| +
|
| + return LocaleConvertCase(s, isolate, is_upper, lang_id);
|
| +}
|
| +
|
| } // namespace internal
|
| } // namespace v8
|
|
|
|
|