Index: src/runtime/runtime-i18n.cc |
diff --git a/src/runtime/runtime-i18n.cc b/src/runtime/runtime-i18n.cc |
index 8f05809c50c7c87007fb27d2b18d93b6299edd60..cb863f65534bdcaff66e40b518bb1179261a4aaf 100644 |
--- a/src/runtime/runtime-i18n.cc |
+++ b/src/runtime/runtime-i18n.cc |
@@ -753,6 +753,7 @@ RUNTIME_FUNCTION(Runtime_BreakIteratorBreakType) { |
} |
namespace { |
+#if 0 |
inline void LocaleConvertCaseHelper(icu::UnicodeString* s, bool is_to_upper, |
const icu::Locale& locale) { |
if (is_to_upper) |
@@ -760,6 +761,7 @@ inline void LocaleConvertCaseHelper(icu::UnicodeString* s, bool is_to_upper, |
else |
s->toLower(locale); |
} |
+#endif |
void ConvertCaseWithTransliterator(icu::UnicodeString* input, |
const char* transliterator_id) { |
UErrorCode status = U_ZERO_ERROR; |
@@ -779,42 +781,52 @@ MUST_USE_RESULT Object* LocaleConvertCase(Handle<String> s, Isolate* isolate, |
RUNTIME_ASSERT(locale_id >= -1 && |
locale_id < static_cast<int>(arraysize(conversion_locales))); |
int32_t length = s->length(); |
- icu::UnicodeString converted; |
+ base::SmartArrayPointer<uc16> sap; |
+ const UChar* src; |
+ Handle<SeqTwoByteString> result = |
+ isolate->factory()->NewRawTwoByteString(length).ToHandleChecked(); |
{ |
DisallowHeapAllocation no_gc; |
DCHECK(s->IsFlat()); |
String::FlatContent flat = s->GetFlatContent(); |
- |
- const UChar* src; |
if (flat.IsOneByte()) { |
- base::SmartArrayPointer<uc16> sap = s->ToWideCString(); |
+ sap = s->ToWideCString(); |
src = reinterpret_cast<const UChar*>(sap.get()); |
- converted = icu::UnicodeString(src, length); |
} else { |
+ // Will the memory pointed to by |src| be available reliably |
+ // outside the block? Thai, will it survive 'gc' that may |
+ // be invoked when |result| is "relloced" below? |
src = reinterpret_cast<const UChar*>(flat.ToUC16Vector().start()); |
jungshik at Google
2016/04/14 00:32:34
adamk@ and littledan@:
Can you answer my question
adamk
2016/04/14 01:56:35
What you're doing here is basically defeating the
|
- converted = icu::UnicodeString(src, length); |
} |
} |
- if (locale_id == -1) { |
- LocaleConvertCaseHelper(&converted, is_to_upper, icu::Locale::getRoot()); |
- } else if (V8_UNLIKELY(locale_id == 1 && is_to_upper)) { |
- // TODO(jshin): Once http://bugs.icu-project.org/trac/ticket/10582 is |
- // fixed, remove this special-casing for uppercasing in Greek(el) locale. |
- // This is ~500 times slower than using the case conversion API. |
- ConvertCaseWithTransliterator(&converted, "el-Upper"); |
- } else { |
- LocaleConvertCaseHelper(&converted, is_to_upper, |
- icu::Locale(conversion_locales[locale_id])); |
+ if (V8_LIKELY(locale_id != 1 || !is_to_upper)) { |
+ 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; |
+ |
+ int32_t target_length = length; |
+ const char* locale = locale_id == -1 ? "" : conversion_locales[locale_id]; |
+ UErrorCode error; |
+ do { |
+ error = U_ZERO_ERROR; |
+ target_length = fn(reinterpret_cast<UChar*>(result->GetChars()), |
+ target_length, src, length, locale, &error); |
+ result->set_length(target_length); |
+ } while (error == U_BUFFER_OVERFLOW_ERROR); |
+ return U_SUCCESS(error) ? *result : *s; |
} |
- Handle<String> result; |
+ icu::UnicodeString converted(false, src, length); |
+ ConvertCaseWithTransliterator(&converted, "el-Upper"); |
+ Handle<String> greek_result; |
ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
- isolate, result, |
+ isolate, greek_result, |
isolate->factory()->NewStringFromTwoByte(Vector<const uint16_t>( |
reinterpret_cast<const uint16_t*>(converted.getBuffer()), |
converted.length()))); |
- return *result; |
+ return *greek_result; |
} |
inline bool IsASCIIUpper(uint16_t ch) { return ch >= 'A' && ch <= 'Z'; } |