Chromium Code Reviews| OLD | NEW | 
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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 // TODO(jshin): Greek and Lithuanian need to use 'transliterator'. | |
| 759 if (is_to_upper) | |
| 760 s->toUpper(locale); | |
| 761 else | |
| 762 s->toLower(locale); | |
| 763 } | |
| 764 void ConvertCaseWithTransliterator(icu::UnicodeString* input, bool is_to_upper, | |
| 765 const char* language) { | |
| 766 #if 0 | |
| 767 char transliteratorId[8]; | |
| 768 | |
| 769 strncpy(transliteratorId, language, 2); | |
| 770 transliteratorId[2] = '-'; | |
| 771 strncpy(transliteratorId + 3, is_to_upper ? "Lower" : "Upper", 5); | |
| 772 #endif | |
| 773 | |
| 774 icu::UnicodeString transliteratorId; | |
| 775 if (strcmp("el", language) == 0) { | |
| 776 transliteratorId = UNICODE_STRING_SIMPLE("el-Upper"); | |
| 777 } else if (strcmp("lt", language) == 0) { | |
| 778 transliteratorId = is_to_upper ? UNICODE_STRING_SIMPLE("lt-Upper") | |
| 779 : UNICODE_STRING_SIMPLE("lt-Lower"); | |
| 780 } | |
| 781 | |
| 782 UErrorCode status = U_ZERO_ERROR; | |
| 783 base::SmartPointer<icu::Transliterator> translit( | |
| 784 icu::Transliterator::createInstance(transliteratorId, UTRANS_FORWARD, | |
| 785 status)); | |
| 786 // RUNTIME_ASSERT(U_SUCCESS(status)); | |
| 787 if (U_FAILURE(status)) return; | |
| 788 translit->transliterate(*input); | |
| 789 } | |
| 790 | |
| 791 MUST_USE_RESULT Object* LocaleConvertCase(Handle<String> s, Isolate* isolate, | |
| 792 bool is_to_upper, int locale_id) { | |
| 793 static const char* case_conversion_languages[] = { | |
| 794 "az", "el", "lt", "tr", | |
| 795 }; | |
| 796 RUNTIME_ASSERT(locale_id >= -1 && | |
| 797 locale_id < | |
| 798 static_cast<int>(arraysize(case_conversion_languages))); | |
| 799 int32_t length = s->length(); | |
| 800 icu::UnicodeString converted; | |
| 801 { | |
| 802 DisallowHeapAllocation no_gc; | |
| 803 DCHECK(s->IsFlat()); | |
| 804 String::FlatContent flat = s->GetFlatContent(); | |
| 805 | |
| 806 const UChar* src; | |
| 807 if (flat.IsOneByte()) { | |
| 808 base::SmartArrayPointer<uc16> sap = s->ToWideCString(); | |
| 809 src = reinterpret_cast<const UChar*>(sap.get()); | |
| 810 converted = icu::UnicodeString(src, length); | |
| 811 } else { | |
| 812 src = reinterpret_cast<const UChar*>(flat.ToUC16Vector().start()); | |
| 813 converted = icu::UnicodeString(src, length); | |
| 814 } | |
| 815 } | |
| 816 #if 0 | |
| 817 v8::String::Value string_value(v8::Utils::ToLocal(s)); | |
| 818 icu::UnicodeString converted( | |
| 819 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
 
 | |
| 820 #endif | |
| 821 | |
| 822 if (locale_id == -1) { | |
| 823 LocaleConvertCaseHelper(&converted, is_to_upper, icu::Locale::getRoot()); | |
| 824 } else { | |
| 825 const char* case_conversion_language = case_conversion_languages[locale_id]; | |
| 826 // el-Upper, lt-Upper, lt-Lower need to be done with transliterator. | |
| 827 if ((locale_id == 1 && is_to_upper) || locale_id == 2) { | |
| 828 ConvertCaseWithTransliterator(&converted, is_to_upper, | |
| 829 case_conversion_language); | |
| 830 } else { | |
| 831 LocaleConvertCaseHelper(&converted, is_to_upper, | |
| 832 icu::Locale(case_conversion_language)); | |
| 833 } | |
| 834 } | |
| 835 | |
| 836 Handle<String> result; | |
| 837 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
| 838 isolate, result, | |
| 839 isolate->factory()->NewStringFromTwoByte(Vector<const uint16_t>( | |
| 840 reinterpret_cast<const uint16_t*>(converted.getBuffer()), | |
| 841 converted.length()))); | |
| 842 return *result; | |
| 843 } | |
| 844 | |
| 845 inline bool IsASCIIUpper(uint16_t ch) { return ch >= 'A' && ch <= 'Z'; } | |
| 846 | |
| 847 inline uint16_t ToASCIILower(uint16_t ch) { | |
| 848 return ch | ((ch >= 'A' && ch <= 'Z') << 5); | |
| 849 } | |
| 850 | |
| 851 inline uint16_t ToASCIIUpper(uint16_t ch) { | |
| 852 return ch & ~((ch >= 'a' && ch <= 'z') << 5); | |
| 853 } | |
| 854 | |
| 855 } // namespace | |
| 856 | |
| 857 RUNTIME_FUNCTION(Runtime_StringToLowerCaseI18N) { | |
| 858 HandleScope scope(isolate); | |
| 859 DCHECK_EQ(args.length(), 1); | |
| 860 CONVERT_ARG_HANDLE_CHECKED(String, s, 0); | |
| 861 | |
| 862 int length = s->length(); | |
| 863 s = String::Flatten(s); | |
| 864 // First scan the string for uppercase and non-ASCII characters: | |
| 865 if (s->HasOnlyOneByteChars()) { | |
| 866 unsigned first_index_to_lower = length; | |
| 867 for (int index = 0; index < length; ++index) { | |
| 868 // Blink specializes this path for one-byte strings, so it | |
| 869 // does not need to do a generic get, but can do the equivalent | |
| 870 // of SeqOneByteStringGet. | |
| 871 uint16_t ch = s->Get(index); | |
| 872 if (V8_UNLIKELY(IsASCIIUpper(ch) || ch & ~0x7F)) { | |
| 873 first_index_to_lower = index; | |
| 874 break; | |
| 875 } | |
| 876 } | |
| 877 | |
| 878 // Nothing to do if the string is all ASCII with no uppercase. | |
| 879 if (first_index_to_lower == length) return *s; | |
| 880 | |
| 881 // We depend here on the invariant that the length of a Latin1 | |
| 882 // string is invariant under ToLowerCase, and the result always | |
| 883 // fits in the Latin1 range (untrue for ToUpperCase, and might | |
| 884 // be untrue in some locales, but this is the root locale) | |
| 885 Handle<SeqOneByteString> result; | |
| 886 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
| 887 isolate, result, isolate->factory()->NewRawOneByteString(length)); | |
| 888 if (s->IsSeqOneByteString()) { | |
| 889 SeqOneByteString* source = SeqOneByteString::cast(*s); | |
| 890 CopyChars(result->GetChars(), source->GetChars(), first_index_to_lower); | |
| 891 } else { | |
| 892 // Do we have to worry about External{One,Two}ByteString? | |
| 893 DCHECK(s->IsSeqTwoByteString()); | |
| 894 SeqTwoByteString* source = SeqTwoByteString::cast(*s); | |
| 895 CopyChars(result->GetChars(), source->GetChars(), first_index_to_lower); | |
| 896 } | |
| 897 | |
| 898 for (int index = first_index_to_lower; index < length; ++index) { | |
| 899 uint16_t ch = s->Get(index); | |
| 900 result->SeqOneByteStringSet( | |
| 901 index, V8_UNLIKELY(ch & ~0x7F) ? static_cast<uint16_t>(u_tolower(ch)) | |
| 902 : ToASCIILower(ch)); | |
| 903 } | |
| 904 | |
| 905 return *result; | |
| 906 } | |
| 907 | |
| 908 // Blink had an additional case here for ASCII 2-byte strings, but | |
| 909 // that is subsumed by the above code (assuming there isn't a false | |
| 910 // negative for HasOnlyOneByteChars). | |
| 911 | |
| 912 // Do a slower implementation for cases that include non-ASCII characters. | |
| 913 return LocaleConvertCase(s, isolate, false, -1); | |
| 914 } | |
| 915 | |
| 916 RUNTIME_FUNCTION(Runtime_StringToUpperCaseI18N) { | |
| 917 HandleScope scope(isolate); | |
| 918 DCHECK_EQ(args.length(), 1); | |
| 919 CONVERT_ARG_HANDLE_CHECKED(String, s, 0); | |
| 920 | |
| 921 // This function could be optimized for no-op cases the way lowercase | |
| 922 // counterpart is, but in empirical testing, few actual calls to upper() | |
| 923 // are no-ops. So, it wouldn't be worth the extra time for pre-scanning. | |
| 924 | |
| 925 int32_t length = s->length(); | |
| 926 s = String::Flatten(s); | |
| 927 static const uint16_t sharp_s = 0x00DFu; | |
| 928 | |
| 929 if (s->HasOnlyOneByteChars()) { | |
| 930 Handle<SeqOneByteString> result; | |
| 931 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
| 932 isolate, result, isolate->factory()->NewRawOneByteString(length)); | |
| 933 | |
| 934 // Do a faster loop for the case where all the characters are ASCII. | |
| 935 uint16_t ored = 0; | |
| 936 for (int index = 0; index < length; ++index) { | |
| 937 uint16_t ch = s->Get(index); | |
| 938 ored |= ch; | |
| 939 result->SeqOneByteStringSet(index, ToASCIIUpper(ch)); | |
| 940 } | |
| 941 if (!(ored & ~0x7F)) return *result; | |
| 942 | |
| 943 // Do a slower implementation for cases that include non-ASCII Latin-1 | |
| 944 // characters. | |
| 945 int sharp_s_count = 0; | |
| 946 | |
| 947 // There are two special cases. | |
| 948 // 1. latin-1 characters when converted to upper case are 16 bit | |
| 949 // characters. | |
| 950 // 2. Lower case sharp-S converts to "SS" (two characters) | |
| 951 for (int32_t index = 0; index < length; ++index) { | |
| 952 uint16_t ch = s->Get(index); | |
| 953 if (V8_UNLIKELY(ch == sharp_s)) { | |
| 954 ++sharp_s_count; | |
| 955 continue; | |
| 956 } | |
| 957 uint16_t upper = static_cast<uint16_t>(u_toupper(static_cast<UChar>(ch))); | |
| 958 if (V8_UNLIKELY(upper > 0xff)) { | |
| 959 // Since this upper-cased character does not fit in an 8-bit string, we | |
| 960 // need to take the 16-bit path. | |
| 961 return LocaleConvertCase(s, isolate, true, -1); | |
| 962 } | |
| 963 result->SeqOneByteStringSet(index, upper); | |
| 964 } | |
| 965 | |
| 966 if (sharp_s_count == 0) return *result; | |
| 967 | |
| 968 // We have sharp_s_count sharp-s characters, but none of the other special | |
| 969 // characters.) | |
| 970 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
| 971 isolate, result, | |
| 972 isolate->factory()->NewRawOneByteString(length + sharp_s_count)); | |
| 973 for (int32_t index = 0, dest_index = 0; index < length; ++index) { | |
| 974 uint16_t ch = s->Get(index); | |
| 975 if (ch == sharp_s) { | |
| 976 result->SeqOneByteStringSet(dest_index++, 'S'); | |
| 977 result->SeqOneByteStringSet(dest_index++, 'S'); | |
| 978 } else { | |
| 979 uint16_t upper = | |
| 980 static_cast<uint16_t>(u_toupper(static_cast<UChar>(ch))); | |
| 981 result->SeqOneByteStringSet(dest_index++, upper); | |
| 982 } | |
| 983 } | |
| 984 | |
| 985 return *result; | |
| 986 } | |
| 987 | |
| 988 return LocaleConvertCase(s, isolate, true, -1); | |
| 989 } | |
| 990 | |
| 991 RUNTIME_FUNCTION(Runtime_StringLocaleConvertCase) { | |
| 992 HandleScope scope(isolate); | |
| 993 DCHECK_EQ(args.length(), 3); | |
| 994 CONVERT_ARG_HANDLE_CHECKED(String, s, 0); | |
| 995 CONVERT_BOOLEAN_ARG_CHECKED(is_upper, 1); | |
| 996 CONVERT_NUMBER_CHECKED(int, lang_id, Int32, args[2]); | |
| 997 | |
| 998 return LocaleConvertCase(s, isolate, is_upper, lang_id); | |
| 999 } | |
| 1000 | |
| 752 } // namespace internal | 1001 } // namespace internal | 
| 753 } // namespace v8 | 1002 } // namespace v8 | 
| 754 | 1003 | 
| 755 #endif // V8_I18N_SUPPORT | 1004 #endif // V8_I18N_SUPPORT | 
| OLD | NEW |