Index: src/runtime/runtime-i18n.cc |
diff --git a/src/runtime/runtime-i18n.cc b/src/runtime/runtime-i18n.cc |
index 27f970bdb4b17392cee21a375e7a04a49c4985aa..84d2b128e1a8e43f41ea497c009a0f8f60e66d44 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,253 @@ RUNTIME_FUNCTION(Runtime_BreakIteratorBreakType) { |
return *isolate->factory()->NewStringFromStaticChars("unknown"); |
} |
} |
+ |
+namespace { |
+inline void LocaleConvertCaseHelper(icu::UnicodeString* s, bool is_to_upper, |
+ const icu::Locale& locale) { |
+ // TODO(jshin): Greek and Lithuanian need to use 'transliterator'. |
+ if (is_to_upper) |
+ s->toUpper(locale); |
+ else |
+ s->toLower(locale); |
+} |
+void ConvertCaseWithTransliterator(icu::UnicodeString* input, bool is_to_upper, |
+ const char* language) { |
+#if 0 |
+ char transliteratorId[8]; |
+ |
+ strncpy(transliteratorId, language, 2); |
+ transliteratorId[2] = '-'; |
+ strncpy(transliteratorId + 3, is_to_upper ? "Lower" : "Upper", 5); |
+#endif |
+ |
+ icu::UnicodeString transliteratorId; |
+ if (strcmp("el", language) == 0) { |
+ transliteratorId = UNICODE_STRING_SIMPLE("el-Upper"); |
+ } else if (strcmp("lt", language) == 0) { |
+ transliteratorId = is_to_upper ? UNICODE_STRING_SIMPLE("lt-Upper") |
+ : UNICODE_STRING_SIMPLE("lt-Lower"); |
+ } |
+ |
+ UErrorCode status = U_ZERO_ERROR; |
+ base::SmartPointer<icu::Transliterator> translit( |
+ icu::Transliterator::createInstance(transliteratorId, UTRANS_FORWARD, |
+ status)); |
+ // RUNTIME_ASSERT(U_SUCCESS(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* case_conversion_languages[] = { |
+ "az", "el", "lt", "tr", |
+ }; |
+ RUNTIME_ASSERT(locale_id >= -1 && |
+ locale_id < |
+ static_cast<int>(arraysize(case_conversion_languages))); |
+ int32_t length = s->length(); |
+ icu::UnicodeString converted; |
+ { |
+ DisallowHeapAllocation no_gc; |
+ DCHECK(s->IsFlat()); |
+ String::FlatContent flat = s->GetFlatContent(); |
+ |
+ const UChar* src; |
+ if (flat.IsOneByte()) { |
+ base::SmartArrayPointer<uc16> sap = s->ToWideCString(); |
+ src = reinterpret_cast<const UChar*>(sap.get()); |
+ converted = icu::UnicodeString(src, length); |
+ } else { |
+ src = reinterpret_cast<const UChar*>(flat.ToUC16Vector().start()); |
+ converted = icu::UnicodeString(src, length); |
+ } |
+ } |
+#if 0 |
+ v8::String::Value string_value(v8::Utils::ToLocal(s)); |
+ icu::UnicodeString converted( |
+ false, reinterpret_cast<const UChar*>(*string_value), length); |
jungshik at Google
2016/04/11 17:47:29
a question: Using this public API is more concise
|
+#endif |
+ |
+ if (locale_id == -1) { |
+ LocaleConvertCaseHelper(&converted, is_to_upper, icu::Locale::getRoot()); |
+ } else { |
+ const char* case_conversion_language = case_conversion_languages[locale_id]; |
+ // el-Upper, lt-Upper, lt-Lower need to be done with transliterator. |
+ if ((locale_id == 1 && is_to_upper) || locale_id == 2) { |
+ ConvertCaseWithTransliterator(&converted, is_to_upper, |
+ case_conversion_language); |
+ } else { |
+ LocaleConvertCaseHelper(&converted, is_to_upper, |
+ icu::Locale(case_conversion_language)); |
+ } |
+ } |
+ |
+ 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; |
+} |
+ |
+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 |