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

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: rebased: dropped unnecesary Wno-c99-extensions from gypi 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
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 void ConvertCaseWithTransliterator(icu::UnicodeString* input,
757 const char* transliterator_id) {
758 UErrorCode status = U_ZERO_ERROR;
759 base::SmartPointer<icu::Transliterator> translit(
760 icu::Transliterator::createInstance(
761 icu::UnicodeString(transliterator_id, -1, US_INV), UTRANS_FORWARD,
762 status));
763 if (U_FAILURE(status)) return;
764 translit->transliterate(*input);
765 }
766
767 MUST_USE_RESULT Object* LocaleConvertCase(Handle<String> s, Isolate* isolate,
768 bool is_to_upper, const char* lang) {
769 int32_t src_length = s->length();
770 const UChar* src = nullptr;
771
772 base::SmartArrayPointer<uc16> sap;
773 if (s->IsOneByteRepresentationUnderneath()) {
774 sap = s->ToWideCString();
775 src = reinterpret_cast<const UChar*>(sap.get());
776 }
777
778 // Greek uppercasing has to be done via transliteration.
779 // TODO(jshin): Drop this special-casing once ICU's regular case conversion
780 // API supports Greek uppercasing. See
781 // http://bugs.icu-project.org/trac/ticket/10582 .
782 // ICU's C API for transliteration is nasty and we just use C++ API.
783 if (V8_UNLIKELY(!strncmp(lang, "el", 2) && is_to_upper)) {
784 icu::UnicodeString converted;
785 {
786 DisallowHeapAllocation no_gc;
787 String::FlatContent flat = s->GetFlatContent();
788 if (src == nullptr) {
789 DCHECK(flat.IsTwoByte());
790 src = reinterpret_cast<const UChar*>(flat.ToUC16Vector().start());
791 }
792 // Starts with the source string and will be replaced by the converted
793 // result.
794 converted.fastCopyFrom(icu::UnicodeString(false, src, src_length));
795 ConvertCaseWithTransliterator(&converted, "el-Upper");
796 }
797 Handle<String> result;
798 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
799 isolate, result,
800 isolate->factory()->NewStringFromTwoByte(Vector<const uint16_t>(
801 reinterpret_cast<const uint16_t*>(converted.getBuffer()),
802 converted.length())));
803 return *result;
804 }
805
806 auto case_converter = is_to_upper ? u_strToUpper : u_strToLower;
807
808 int32_t dest_length = src_length;
809 UErrorCode error;
810 Handle<SeqTwoByteString> result;
811
812 // This is not a real loop. It'll be executed only once (no overflow) or
813 // twice (overflow).
814 for (int i = 0; i < 2; ++i) {
815 result =
816 isolate->factory()->NewRawTwoByteString(dest_length).ToHandleChecked();
817 base::SmartArrayPointer<uc16> sap;
818 DisallowHeapAllocation no_gc;
819 String::FlatContent flat = s->GetFlatContent();
820 // For OneByteString, |src| is already obtained with |sap| outside the loop.
821 if (flat.IsTwoByte())
822 src = reinterpret_cast<const UChar*>(flat.ToUC16Vector().start());
823 error = U_ZERO_ERROR;
824 dest_length = case_converter(reinterpret_cast<UChar*>(result->GetChars()),
825 dest_length, src, src_length, lang, &error);
826 if (error != U_BUFFER_OVERFLOW_ERROR) break;
827 }
828
829 // In most cases, the output will fill the destination buffer completely
830 // leading to an unterminated string (U_STRING_NOT_TERMINATED_WARNING).
831 // Only in rare cases, it'll be shorter than the destination buffer and
832 // |result| has to be truncated.
833 DCHECK(U_SUCCESS(error));
834 // dest_length == result->length()
835 if (V8_LIKELY(error == U_STRING_NOT_TERMINATED_WARNING)) return *result;
836 if (U_SUCCESS(error)) {
837 // dest_length < result->length()
838 return *Handle<SeqTwoByteString>::cast(
839 SeqString::Truncate(result, dest_length));
840 }
841 return *s;
842 }
843
844 inline bool IsASCIIUpper(uint16_t ch) { return ch >= 'A' && ch <= 'Z'; }
845
846 inline uint16_t ToLatin1Lower(uint16_t ch) {
847 return ch |
848 (((ch >= 'A' && ch <= 'Z') || (ch >= 0xC0 && ch <= 0xDE && ch != 0xD7))
849 << 5);
850 }
851
852 inline uint16_t ToASCIIUpper(uint16_t ch) {
853 return ch & ~((ch >= 'a' && ch <= 'z') << 5);
854 }
855
856 // Does not work for U+00DF (sharp-s), U+00B5 (micron), U+00FF.
857 inline uint16_t ToLatin1Upper(uint16_t ch) {
858 DCHECK(ch != 0xDF && ch != 0xB5 && ch != 0xFF);
859 return ch &
860 ~(((ch >= 'a' && ch <= 'z') || (((ch & 0xE0) == 0xE0) && ch != 0xE7))
861 << 5);
862 }
863
864 template <typename Char>
865 bool ToUpperFastASCII(const Vector<const Char>& src,
866 Handle<SeqOneByteString> result) {
867 // Do a faster loop for the case where all the characters are ASCII.
868 uint16_t ored = 0;
869 int32_t index = 0;
870 for (auto it = src.begin(); it != src.end(); ++it) {
871 uint16_t ch = static_cast<uint16_t>(*it);
872 ored |= ch;
873 result->SeqOneByteStringSet(index++, ToASCIIUpper(ch));
874 }
875 return !(ored & ~0x7F);
876 }
877
878 const uint16_t sharp_s = 0xDF;
879
880 template <typename Char>
881 bool ToUpperOneByte(const Vector<const Char>& src,
882 Handle<SeqOneByteString> result, int* sharp_s_count) {
883 // Still pretty-fast path for the input with non-ASCII Latin-1 characters.
884
885 // There are two special cases.
886 // 1. U+00B5 and U+00FF are mapped to a character beyond U+00FF.
887 // 2. Lower case sharp-S converts to "SS" (two characters)
888 *sharp_s_count = 0;
889 int32_t index = 0;
890 for (auto it = src.begin(); it != src.end(); ++it) {
891 uint16_t ch = static_cast<uint16_t>(*it);
892 if (V8_UNLIKELY(ch == sharp_s)) {
893 ++(*sharp_s_count);
894 continue;
895 }
896 if (V8_UNLIKELY(ch == 0xB5 || ch == 0xFF)) {
897 // Since this upper-cased character does not fit in an 8-bit string, we
898 // need to take the 16-bit path.
899 return false;
900 }
901 result->SeqOneByteStringSet(index++, ToLatin1Upper(ch));
902 }
903
904 return true;
905 }
906
907 template <typename Char>
908 void ToUpperWithSharpS(const Vector<const Char>& src,
909 Handle<SeqOneByteString> result) {
910 int32_t dest_index = 0;
911 for (auto it = src.begin(); it != src.end(); ++it) {
912 uint16_t ch = static_cast<uint16_t>(*it);
913 if (ch == sharp_s) {
914 result->SeqOneByteStringSet(dest_index++, 'S');
915 result->SeqOneByteStringSet(dest_index++, 'S');
916 } else {
917 result->SeqOneByteStringSet(dest_index++, ToLatin1Upper(ch));
918 }
919 }
920 }
921
922 } // namespace
923
924 RUNTIME_FUNCTION(Runtime_StringToLowerCaseI18N) {
925 HandleScope scope(isolate);
926 DCHECK_EQ(args.length(), 1);
927 CONVERT_ARG_HANDLE_CHECKED(String, s, 0);
928
929 int length = s->length();
930 s = String::Flatten(s);
931 // First scan the string for uppercase and non-ASCII characters:
932 if (s->HasOnlyOneByteChars()) {
933 unsigned first_index_to_lower = length;
934 for (int index = 0; index < length; ++index) {
935 // Blink specializes this path for one-byte strings, so it
936 // does not need to do a generic get, but can do the equivalent
937 // of SeqOneByteStringGet.
938 uint16_t ch = s->Get(index);
939 if (V8_UNLIKELY(IsASCIIUpper(ch) || ch & ~0x7F)) {
940 first_index_to_lower = index;
941 break;
942 }
943 }
944
945 // Nothing to do if the string is all ASCII with no uppercase.
946 if (first_index_to_lower == length) return *s;
947
948 // We depend here on the invariant that the length of a Latin1
949 // string is invariant under ToLowerCase, and the result always
950 // fits in the Latin1 range in the *root locale*. It does not hold
951 // for ToUpperCase even in the root locale.
952 Handle<SeqOneByteString> result;
953 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
954 isolate, result, isolate->factory()->NewRawOneByteString(length));
955
956 DisallowHeapAllocation no_gc;
957 String::FlatContent flat = s->GetFlatContent();
958 if (flat.IsOneByte()) {
959 const uint8_t* src = flat.ToOneByteVector().start();
960 CopyChars(result->GetChars(), src, first_index_to_lower);
961 for (int index = first_index_to_lower; index < length; ++index) {
962 uint16_t ch = static_cast<uint16_t>(src[index]);
963 result->SeqOneByteStringSet(index, ToLatin1Lower(ch));
964 }
965 } else {
966 const uint16_t* src = flat.ToUC16Vector().start();
967 CopyChars(result->GetChars(), src, first_index_to_lower);
968 for (int index = first_index_to_lower; index < length; ++index) {
969 uint16_t ch = src[index];
970 result->SeqOneByteStringSet(index, ToLatin1Lower(ch));
971 }
972 }
973
974 return *result;
975 }
976
977 // Blink had an additional case here for ASCII 2-byte strings, but
978 // that is subsumed by the above code (assuming there isn't a false
979 // negative for HasOnlyOneByteChars).
980
981 // Do a slower implementation for cases that include non-ASCII characters.
982 return LocaleConvertCase(s, isolate, false, "");
983 }
984
985 RUNTIME_FUNCTION(Runtime_StringToUpperCaseI18N) {
986 HandleScope scope(isolate);
987 DCHECK_EQ(args.length(), 1);
988 CONVERT_ARG_HANDLE_CHECKED(String, s, 0);
989
990 // This function could be optimized for no-op cases the way lowercase
991 // counterpart is, but in empirical testing, few actual calls to upper()
992 // are no-ops. So, it wouldn't be worth the extra time for pre-scanning.
993
994 int32_t length = s->length();
995 s = String::Flatten(s);
996
997 if (s->HasOnlyOneByteChars()) {
998 Handle<SeqOneByteString> result;
999 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
1000 isolate, result, isolate->factory()->NewRawOneByteString(length));
1001
1002 int sharp_s_count;
1003 bool is_result_single_byte;
1004 {
1005 DisallowHeapAllocation no_gc;
1006 String::FlatContent flat = s->GetFlatContent();
1007 // If it was ok to slow down ASCII-only input slightly, ToUpperFastASCII
1008 // could be removed because ToUpperOneByte is pretty fast now (it
1009 // does not call ICU API any more.).
1010 if (flat.IsOneByte()) {
1011 Vector<const uint8_t> src = flat.ToOneByteVector();
1012 if (ToUpperFastASCII(src, result)) return *result;
1013 is_result_single_byte = ToUpperOneByte(src, result, &sharp_s_count);
1014 } else {
1015 DCHECK(flat.IsTwoByte());
1016 Vector<const uint16_t> src = flat.ToUC16Vector();
1017 if (ToUpperFastASCII(src, result)) return *result;
1018 is_result_single_byte = ToUpperOneByte(src, result, &sharp_s_count);
1019 }
1020 }
1021
1022 // Go to the full Unicode path if there are characters whose uppercase
1023 // is beyond the Latin-1 range (cannot be represented in OneByteString).
1024 if (V8_UNLIKELY(!is_result_single_byte))
1025 return LocaleConvertCase(s, isolate, true, "");
1026
1027 if (sharp_s_count == 0) return *result;
1028
1029 // We have sharp_s_count sharp-s characters, but the result is still
1030 // in the Latin-1 range.
1031 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
1032 isolate, result,
1033 isolate->factory()->NewRawOneByteString(length + sharp_s_count));
1034 DisallowHeapAllocation no_gc;
1035 String::FlatContent flat = s->GetFlatContent();
1036 if (flat.IsOneByte())
1037 ToUpperWithSharpS(flat.ToOneByteVector(), result);
1038 else
1039 ToUpperWithSharpS(flat.ToUC16Vector(), result);
1040
1041 return *result;
1042 }
1043
1044 return LocaleConvertCase(s, isolate, true, "");
1045 }
1046
1047 RUNTIME_FUNCTION(Runtime_StringLocaleConvertCase) {
1048 HandleScope scope(isolate);
1049 DCHECK_EQ(args.length(), 3);
1050 CONVERT_ARG_HANDLE_CHECKED(String, s, 0);
1051 CONVERT_BOOLEAN_ARG_CHECKED(is_upper, 1);
1052 CONVERT_ARG_HANDLE_CHECKED(SeqOneByteString, lang, 2);
1053
1054 // All the languages requiring special handling ("az", "el", "lt", "tr")
1055 // have a 2-letter language code.
1056 DCHECK(lang->length() == 2);
1057 uint8_t lang_str[3];
1058 memcpy(lang_str, lang->GetChars(), 2);
1059 lang_str[2] = 0;
1060 return LocaleConvertCase(s, isolate, is_upper,
1061 reinterpret_cast<const char*>(lang_str));
1062 }
1063
752 } // namespace internal 1064 } // namespace internal
753 } // namespace v8 1065 } // namespace v8
754 1066
755 #endif // V8_I18N_SUPPORT 1067 #endif // V8_I18N_SUPPORT
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698