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

Side by Side Diff: src/runtime/runtime-i18n.cc

Issue 1812673005: Use ICU case conversion/transliterator for case conversion behind a flag (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: get DisallowHeapAllocation out of "anon" block and use copy-on-write ctor for UnicodeString for bet… Created 4 years, 8 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/runtime/runtime.h ('k') | src/runtime/runtime-strings.cc » ('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 2014 the V8 project authors. All rights reserved. 1 // Copyright 2014 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 5
6 #ifdef V8_I18N_SUPPORT 6 #ifdef V8_I18N_SUPPORT
7 #include "src/runtime/runtime-utils.h" 7 #include "src/runtime/runtime-utils.h"
8 8
9 #include "src/api.h" 9 #include "src/api.h"
10 #include "src/api-natives.h" 10 #include "src/api-natives.h"
(...skipping 11 matching lines...) Expand all
22 #include "unicode/dcfmtsym.h" 22 #include "unicode/dcfmtsym.h"
23 #include "unicode/decimfmt.h" 23 #include "unicode/decimfmt.h"
24 #include "unicode/dtfmtsym.h" 24 #include "unicode/dtfmtsym.h"
25 #include "unicode/dtptngen.h" 25 #include "unicode/dtptngen.h"
26 #include "unicode/locid.h" 26 #include "unicode/locid.h"
27 #include "unicode/numfmt.h" 27 #include "unicode/numfmt.h"
28 #include "unicode/numsys.h" 28 #include "unicode/numsys.h"
29 #include "unicode/rbbi.h" 29 #include "unicode/rbbi.h"
30 #include "unicode/smpdtfmt.h" 30 #include "unicode/smpdtfmt.h"
31 #include "unicode/timezone.h" 31 #include "unicode/timezone.h"
32 #include "unicode/translit.h"
32 #include "unicode/uchar.h" 33 #include "unicode/uchar.h"
33 #include "unicode/ucol.h" 34 #include "unicode/ucol.h"
34 #include "unicode/ucurr.h" 35 #include "unicode/ucurr.h"
35 #include "unicode/uloc.h" 36 #include "unicode/uloc.h"
37 #include "unicode/unistr.h"
36 #include "unicode/unum.h" 38 #include "unicode/unum.h"
37 #include "unicode/uversion.h" 39 #include "unicode/uversion.h"
38 40
39 41
40 namespace v8 { 42 namespace v8 {
41 namespace internal { 43 namespace internal {
42 44
43 RUNTIME_FUNCTION(Runtime_CanonicalizeLanguageTag) { 45 RUNTIME_FUNCTION(Runtime_CanonicalizeLanguageTag) {
44 HandleScope scope(isolate); 46 HandleScope scope(isolate);
45 Factory* factory = isolate->factory(); 47 Factory* factory = isolate->factory();
(...skipping 696 matching lines...) Expand 10 before | Expand all | Expand 10 after
742 } else if (status >= UBRK_WORD_LETTER && status < UBRK_WORD_LETTER_LIMIT) { 744 } else if (status >= UBRK_WORD_LETTER && status < UBRK_WORD_LETTER_LIMIT) {
743 return *isolate->factory()->NewStringFromStaticChars("letter"); 745 return *isolate->factory()->NewStringFromStaticChars("letter");
744 } else if (status >= UBRK_WORD_KANA && status < UBRK_WORD_KANA_LIMIT) { 746 } else if (status >= UBRK_WORD_KANA && status < UBRK_WORD_KANA_LIMIT) {
745 return *isolate->factory()->NewStringFromStaticChars("kana"); 747 return *isolate->factory()->NewStringFromStaticChars("kana");
746 } else if (status >= UBRK_WORD_IDEO && status < UBRK_WORD_IDEO_LIMIT) { 748 } else if (status >= UBRK_WORD_IDEO && status < UBRK_WORD_IDEO_LIMIT) {
747 return *isolate->factory()->NewStringFromStaticChars("ideo"); 749 return *isolate->factory()->NewStringFromStaticChars("ideo");
748 } else { 750 } else {
749 return *isolate->factory()->NewStringFromStaticChars("unknown"); 751 return *isolate->factory()->NewStringFromStaticChars("unknown");
750 } 752 }
751 } 753 }
754
755 namespace {
756 inline void LocaleConvertCaseHelper(icu::UnicodeString* s, bool is_to_upper,
757 const icu::Locale& locale) {
758 if (is_to_upper)
759 s->toUpper(locale);
760 else
761 s->toLower(locale);
762 }
763 void ConvertCaseWithTransliterator(icu::UnicodeString* input,
764 const char* transliterator_id) {
765 UErrorCode status = U_ZERO_ERROR;
766 base::SmartPointer<icu::Transliterator> translit(
767 icu::Transliterator::createInstance(
768 icu::UnicodeString(transliterator_id, -1, US_INV), UTRANS_FORWARD,
769 status));
770 if (U_FAILURE(status)) return;
771 translit->transliterate(*input);
772 }
773
774 MUST_USE_RESULT Object* LocaleConvertCase(Handle<String> s, Isolate* isolate,
775 bool is_to_upper, int locale_id) {
776 static const char* conversion_locales[] = {
777 "az", "el", "lt", "tr",
778 };
779 RUNTIME_ASSERT(locale_id >= -1 &&
780 locale_id < static_cast<int>(arraysize(conversion_locales)));
781 int32_t length = s->length();
782 icu::UnicodeString converted;
783
784 DisallowHeapAllocation no_gc;
jungshik at Google 2016/04/13 17:34:34 Getting this out of an isolated block to avoid an
Dan Ehrenberg 2016/04/13 17:43:43 I believe DisallowHeapAllocation's function is to
785 DCHECK(s->IsFlat());
786 String::FlatContent flat = s->GetFlatContent();
787
788 const UChar* src;
789 base::SmartArrayPointer<uc16> sap;
790 if (flat.IsOneByte()) {
791 sap = s->ToWideCString();
792 src = reinterpret_cast<const UChar*>(sap.get());
793 } else {
794 src = reinterpret_cast<const UChar*>(flat.ToUC16Vector().start());
795 }
796 converted = icu::UnicodeString(false, src, length);
797
798 if (locale_id == -1) {
799 LocaleConvertCaseHelper(&converted, is_to_upper, icu::Locale::getRoot());
800 } else if (V8_UNLIKELY(locale_id == 1 && is_to_upper)) {
801 // TODO(jshin): Once http://bugs.icu-project.org/trac/ticket/10582 is
802 // fixed, remove this special-casing for uppercasing in Greek(el) locale.
803 // This is ~500 times slower than using the case conversion API.
804 ConvertCaseWithTransliterator(&converted, "el-Upper");
805 } else {
806 LocaleConvertCaseHelper(&converted, is_to_upper,
807 icu::Locale(conversion_locales[locale_id]));
808 }
809
810 Handle<String> result;
811 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
812 isolate, result,
813 isolate->factory()->NewStringFromTwoByte(Vector<const uint16_t>(
814 reinterpret_cast<const uint16_t*>(converted.getBuffer()),
815 converted.length())));
816 return *result;
817 }
818
819 inline bool IsASCIIUpper(uint16_t ch) { return ch >= 'A' && ch <= 'Z'; }
820
821 inline uint16_t ToASCIILower(uint16_t ch) {
822 return ch | ((ch >= 'A' && ch <= 'Z') << 5);
823 }
824
825 inline uint16_t ToASCIIUpper(uint16_t ch) {
826 return ch & ~((ch >= 'a' && ch <= 'z') << 5);
827 }
828
829 } // namespace
830
831 RUNTIME_FUNCTION(Runtime_StringToLowerCaseI18N) {
832 HandleScope scope(isolate);
833 DCHECK_EQ(args.length(), 1);
834 CONVERT_ARG_HANDLE_CHECKED(String, s, 0);
835
836 int length = s->length();
837 s = String::Flatten(s);
838 // First scan the string for uppercase and non-ASCII characters:
839 if (s->HasOnlyOneByteChars()) {
840 unsigned first_index_to_lower = length;
841 for (int index = 0; index < length; ++index) {
842 // Blink specializes this path for one-byte strings, so it
843 // does not need to do a generic get, but can do the equivalent
844 // of SeqOneByteStringGet.
845 uint16_t ch = s->Get(index);
846 if (V8_UNLIKELY(IsASCIIUpper(ch) || ch & ~0x7F)) {
847 first_index_to_lower = index;
848 break;
849 }
850 }
851
852 // Nothing to do if the string is all ASCII with no uppercase.
853 if (first_index_to_lower == length) return *s;
854
855 // We depend here on the invariant that the length of a Latin1
856 // string is invariant under ToLowerCase, and the result always
857 // fits in the Latin1 range (untrue for ToUpperCase, and might
858 // be untrue in some locales, but this is the root locale)
859 Handle<SeqOneByteString> result;
860 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
861 isolate, result, isolate->factory()->NewRawOneByteString(length));
862 if (s->IsSeqOneByteString()) {
863 SeqOneByteString* source = SeqOneByteString::cast(*s);
864 CopyChars(result->GetChars(), source->GetChars(), first_index_to_lower);
865 } else {
866 // Do we have to worry about External{One,Two}ByteString?
867 DCHECK(s->IsSeqTwoByteString());
868 SeqTwoByteString* source = SeqTwoByteString::cast(*s);
869 CopyChars(result->GetChars(), source->GetChars(), first_index_to_lower);
870 }
871
872 for (int index = first_index_to_lower; index < length; ++index) {
873 uint16_t ch = s->Get(index);
874 result->SeqOneByteStringSet(
875 index, V8_UNLIKELY(ch & ~0x7F) ? static_cast<uint16_t>(u_tolower(ch))
876 : ToASCIILower(ch));
877 }
878
879 return *result;
880 }
881
882 // Blink had an additional case here for ASCII 2-byte strings, but
883 // that is subsumed by the above code (assuming there isn't a false
884 // negative for HasOnlyOneByteChars).
885
886 // Do a slower implementation for cases that include non-ASCII characters.
887 return LocaleConvertCase(s, isolate, false, -1);
888 }
889
890 RUNTIME_FUNCTION(Runtime_StringToUpperCaseI18N) {
891 HandleScope scope(isolate);
892 DCHECK_EQ(args.length(), 1);
893 CONVERT_ARG_HANDLE_CHECKED(String, s, 0);
894
895 // This function could be optimized for no-op cases the way lowercase
896 // counterpart is, but in empirical testing, few actual calls to upper()
897 // are no-ops. So, it wouldn't be worth the extra time for pre-scanning.
898
899 int32_t length = s->length();
900 s = String::Flatten(s);
901 static const uint16_t sharp_s = 0x00DFu;
902
903 if (s->HasOnlyOneByteChars()) {
904 Handle<SeqOneByteString> result;
905 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
906 isolate, result, isolate->factory()->NewRawOneByteString(length));
907
908 // Do a faster loop for the case where all the characters are ASCII.
909 uint16_t ored = 0;
910 for (int index = 0; index < length; ++index) {
911 uint16_t ch = s->Get(index);
912 ored |= ch;
913 result->SeqOneByteStringSet(index, ToASCIIUpper(ch));
914 }
915 if (!(ored & ~0x7F)) return *result;
916
917 // Do a slower implementation for cases that include non-ASCII Latin-1
918 // characters.
919 int sharp_s_count = 0;
920
921 // There are two special cases.
922 // 1. latin-1 characters when converted to upper case are 16 bit
923 // characters.
924 // 2. Lower case sharp-S converts to "SS" (two characters)
925 for (int32_t index = 0; index < length; ++index) {
926 uint16_t ch = s->Get(index);
927 if (V8_UNLIKELY(ch == sharp_s)) {
928 ++sharp_s_count;
929 continue;
930 }
931 uint16_t upper = static_cast<uint16_t>(u_toupper(static_cast<UChar>(ch)));
932 if (V8_UNLIKELY(upper > 0xff)) {
933 // Since this upper-cased character does not fit in an 8-bit string, we
934 // need to take the 16-bit path.
935 return LocaleConvertCase(s, isolate, true, -1);
936 }
937 result->SeqOneByteStringSet(index, upper);
938 }
939
940 if (sharp_s_count == 0) return *result;
941
942 // We have sharp_s_count sharp-s characters, but none of the other special
943 // characters.
944 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
945 isolate, result,
946 isolate->factory()->NewRawOneByteString(length + sharp_s_count));
947 for (int32_t index = 0, dest_index = 0; index < length; ++index) {
948 uint16_t ch = s->Get(index);
949 if (ch == sharp_s) {
950 result->SeqOneByteStringSet(dest_index++, 'S');
951 result->SeqOneByteStringSet(dest_index++, 'S');
952 } else {
953 uint16_t upper =
954 static_cast<uint16_t>(u_toupper(static_cast<UChar>(ch)));
955 result->SeqOneByteStringSet(dest_index++, upper);
956 }
957 }
958
959 return *result;
960 }
961
962 return LocaleConvertCase(s, isolate, true, -1);
963 }
964
965 RUNTIME_FUNCTION(Runtime_StringLocaleConvertCase) {
966 HandleScope scope(isolate);
967 DCHECK_EQ(args.length(), 3);
968 CONVERT_ARG_HANDLE_CHECKED(String, s, 0);
969 CONVERT_BOOLEAN_ARG_CHECKED(is_upper, 1);
970 CONVERT_NUMBER_CHECKED(int, lang_id, Int32, args[2]);
971
972 return LocaleConvertCase(s, isolate, is_upper, lang_id);
973 }
974
752 } // namespace internal 975 } // namespace internal
753 } // namespace v8 976 } // namespace v8
754 977
755 #endif // V8_I18N_SUPPORT 978 #endif // V8_I18N_SUPPORT
OLDNEW
« no previous file with comments | « src/runtime/runtime.h ('k') | src/runtime/runtime-strings.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698