| 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 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, int locale_id) { |
| 769 static const char* conversion_locales[] = { |
| 770 "az", "el", "lt", "tr", |
| 771 }; |
| 772 RUNTIME_ASSERT(locale_id >= -1 && |
| 773 locale_id < static_cast<int>(arraysize(conversion_locales))); |
| 774 int32_t src_length = s->length(); |
| 775 const UChar* src = nullptr; |
| 776 |
| 777 base::SmartArrayPointer<uc16> sap; |
| 778 if (s->IsOneByteRepresentationUnderneath()) { |
| 779 sap = s->ToWideCString(); |
| 780 src = reinterpret_cast<const UChar*>(sap.get()); |
| 781 } |
| 782 |
| 783 // Greek (id == 1) uppercasing has to be done via transliteration. |
| 784 // TODO(jshin): Drop this special-casing once ICU's regular case conversion |
| 785 // API supports Greek uppercasing. See |
| 786 // http://bugs.icu-project.org/trac/ticket/10582 . |
| 787 // ICU's C API for transliteration is nasty and we just use C++ API. |
| 788 if (V8_UNLIKELY(locale_id == 1 && is_to_upper)) { |
| 789 icu::UnicodeString converted; |
| 790 { |
| 791 DisallowHeapAllocation no_gc; |
| 792 String::FlatContent flat = s->GetFlatContent(); |
| 793 if (src == nullptr) { |
| 794 DCHECK(flat.IsTwoByte()); |
| 795 src = reinterpret_cast<const UChar*>(flat.ToUC16Vector().start()); |
| 796 } |
| 797 // Starts with the source string and will be replaced by the converted |
| 798 // result. |
| 799 converted.fastCopyFrom(icu::UnicodeString(false, src, src_length)); |
| 800 ConvertCaseWithTransliterator(&converted, "el-Upper"); |
| 801 } |
| 802 Handle<String> result; |
| 803 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 804 isolate, result, |
| 805 isolate->factory()->NewStringFromTwoByte(Vector<const uint16_t>( |
| 806 reinterpret_cast<const uint16_t*>(converted.getBuffer()), |
| 807 converted.length()))); |
| 808 return *result; |
| 809 } |
| 810 |
| 811 typedef int32_t (*case_conversion_fn)( |
| 812 UChar * dest, int32_t destCapacity, const UChar* src, int32_t srcLength, |
| 813 const char* locale, UErrorCode* pErrorCode); |
| 814 case_conversion_fn fn = is_to_upper ? u_strToUpper : u_strToLower; |
| 815 const char* locale = locale_id == -1 ? "" : conversion_locales[locale_id]; |
| 816 |
| 817 int32_t dest_length = src_length; |
| 818 UErrorCode error; |
| 819 Handle<SeqTwoByteString> result; |
| 820 |
| 821 // This is not a real loop. It'll be executed only once (no overflow) or |
| 822 // twice (overflow). |
| 823 do { |
| 824 result = |
| 825 isolate->factory()->NewRawTwoByteString(dest_length).ToHandleChecked(); |
| 826 base::SmartArrayPointer<uc16> sap; |
| 827 DisallowHeapAllocation no_gc; |
| 828 String::FlatContent flat = s->GetFlatContent(); |
| 829 // For OneByteString, |src| is already obtained with |sap| outside the loop. |
| 830 if (flat.IsTwoByte()) |
| 831 src = reinterpret_cast<const UChar*>(flat.ToUC16Vector().start()); |
| 832 error = U_ZERO_ERROR; |
| 833 dest_length = fn(reinterpret_cast<UChar*>(result->GetChars()), dest_length, |
| 834 src, src_length, locale, &error); |
| 835 } while (error == U_BUFFER_OVERFLOW_ERROR); |
| 836 |
| 837 // In most cases, the output will fill the destination buffer completely |
| 838 // leading to an unterminated string (U_STRING_NOT_TERMINATED_WARNING). |
| 839 // Only in rare cases, it'll be shorter than the destination buffer and |
| 840 // |result| has to be truncated. |
| 841 DCHECK(U_SUCCESS(error)); |
| 842 if (V8_LIKELY(error == U_STRING_NOT_TERMINATED_WARNING)) |
| 843 return *result; |
| 844 if (U_SUCCESS(error)) { |
| 845 return *Handle<SeqTwoByteString>::cast( |
| 846 SeqString::Truncate(result, dest_length)); |
| 847 } |
| 848 return *s; |
| 849 } |
| 850 |
| 851 inline bool IsASCIIUpper(uint16_t ch) { return ch >= 'A' && ch <= 'Z'; } |
| 852 |
| 853 inline uint16_t ToASCIILower(uint16_t ch) { |
| 854 return ch | ((ch >= 'A' && ch <= 'Z') << 5); |
| 855 } |
| 856 |
| 857 inline uint16_t ToASCIIUpper(uint16_t ch) { |
| 858 return ch & ~((ch >= 'a' && ch <= 'z') << 5); |
| 859 } |
| 860 |
| 861 } // namespace |
| 862 |
| 863 RUNTIME_FUNCTION(Runtime_StringToLowerCaseI18N) { |
| 864 HandleScope scope(isolate); |
| 865 DCHECK_EQ(args.length(), 1); |
| 866 CONVERT_ARG_HANDLE_CHECKED(String, s, 0); |
| 867 |
| 868 int length = s->length(); |
| 869 s = String::Flatten(s); |
| 870 // First scan the string for uppercase and non-ASCII characters: |
| 871 if (s->HasOnlyOneByteChars()) { |
| 872 unsigned first_index_to_lower = length; |
| 873 for (int index = 0; index < length; ++index) { |
| 874 // Blink specializes this path for one-byte strings, so it |
| 875 // does not need to do a generic get, but can do the equivalent |
| 876 // of SeqOneByteStringGet. |
| 877 uint16_t ch = s->Get(index); |
| 878 if (V8_UNLIKELY(IsASCIIUpper(ch) || ch & ~0x7F)) { |
| 879 first_index_to_lower = index; |
| 880 break; |
| 881 } |
| 882 } |
| 883 |
| 884 // Nothing to do if the string is all ASCII with no uppercase. |
| 885 if (first_index_to_lower == length) return *s; |
| 886 |
| 887 // We depend here on the invariant that the length of a Latin1 |
| 888 // string is invariant under ToLowerCase, and the result always |
| 889 // fits in the Latin1 range (untrue for ToUpperCase, and might |
| 890 // be untrue in some locales, but this is the root locale) |
| 891 Handle<SeqOneByteString> result; |
| 892 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 893 isolate, result, isolate->factory()->NewRawOneByteString(length)); |
| 894 if (s->IsSeqOneByteString()) { |
| 895 SeqOneByteString* source = SeqOneByteString::cast(*s); |
| 896 CopyChars(result->GetChars(), source->GetChars(), first_index_to_lower); |
| 897 } else { |
| 898 // Do we have to worry about External{One,Two}ByteString? |
| 899 DCHECK(s->IsSeqTwoByteString()); |
| 900 SeqTwoByteString* source = SeqTwoByteString::cast(*s); |
| 901 CopyChars(result->GetChars(), source->GetChars(), first_index_to_lower); |
| 902 } |
| 903 |
| 904 for (int index = first_index_to_lower; index < length; ++index) { |
| 905 uint16_t ch = s->Get(index); |
| 906 result->SeqOneByteStringSet( |
| 907 index, V8_UNLIKELY(ch & ~0x7F) ? static_cast<uint16_t>(u_tolower(ch)) |
| 908 : ToASCIILower(ch)); |
| 909 } |
| 910 |
| 911 return *result; |
| 912 } |
| 913 |
| 914 // Blink had an additional case here for ASCII 2-byte strings, but |
| 915 // that is subsumed by the above code (assuming there isn't a false |
| 916 // negative for HasOnlyOneByteChars). |
| 917 |
| 918 // Do a slower implementation for cases that include non-ASCII characters. |
| 919 return LocaleConvertCase(s, isolate, false, -1); |
| 920 } |
| 921 |
| 922 RUNTIME_FUNCTION(Runtime_StringToUpperCaseI18N) { |
| 923 HandleScope scope(isolate); |
| 924 DCHECK_EQ(args.length(), 1); |
| 925 CONVERT_ARG_HANDLE_CHECKED(String, s, 0); |
| 926 |
| 927 // This function could be optimized for no-op cases the way lowercase |
| 928 // counterpart is, but in empirical testing, few actual calls to upper() |
| 929 // are no-ops. So, it wouldn't be worth the extra time for pre-scanning. |
| 930 |
| 931 int32_t length = s->length(); |
| 932 s = String::Flatten(s); |
| 933 static const uint16_t sharp_s = 0x00DFu; |
| 934 |
| 935 if (s->HasOnlyOneByteChars()) { |
| 936 Handle<SeqOneByteString> result; |
| 937 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 938 isolate, result, isolate->factory()->NewRawOneByteString(length)); |
| 939 |
| 940 // Do a faster loop for the case where all the characters are ASCII. |
| 941 uint16_t ored = 0; |
| 942 for (int index = 0; index < length; ++index) { |
| 943 uint16_t ch = s->Get(index); |
| 944 ored |= ch; |
| 945 result->SeqOneByteStringSet(index, ToASCIIUpper(ch)); |
| 946 } |
| 947 if (!(ored & ~0x7F)) return *result; |
| 948 |
| 949 // Do a slower implementation for cases that include non-ASCII Latin-1 |
| 950 // characters. |
| 951 int sharp_s_count = 0; |
| 952 |
| 953 // There are two special cases. |
| 954 // 1. latin-1 characters when converted to upper case are 16 bit |
| 955 // characters. |
| 956 // 2. Lower case sharp-S converts to "SS" (two characters) |
| 957 for (int32_t index = 0; index < length; ++index) { |
| 958 uint16_t ch = s->Get(index); |
| 959 if (V8_UNLIKELY(ch == sharp_s)) { |
| 960 ++sharp_s_count; |
| 961 continue; |
| 962 } |
| 963 uint16_t upper = static_cast<uint16_t>(u_toupper(static_cast<UChar>(ch))); |
| 964 if (V8_UNLIKELY(upper > 0xff)) { |
| 965 // Since this upper-cased character does not fit in an 8-bit string, we |
| 966 // need to take the 16-bit path. |
| 967 return LocaleConvertCase(s, isolate, true, -1); |
| 968 } |
| 969 result->SeqOneByteStringSet(index, upper); |
| 970 } |
| 971 |
| 972 if (sharp_s_count == 0) return *result; |
| 973 |
| 974 // We have sharp_s_count sharp-s characters, but none of the other special |
| 975 // characters. |
| 976 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 977 isolate, result, |
| 978 isolate->factory()->NewRawOneByteString(length + sharp_s_count)); |
| 979 for (int32_t index = 0, dest_index = 0; index < length; ++index) { |
| 980 uint16_t ch = s->Get(index); |
| 981 if (ch == sharp_s) { |
| 982 result->SeqOneByteStringSet(dest_index++, 'S'); |
| 983 result->SeqOneByteStringSet(dest_index++, 'S'); |
| 984 } else { |
| 985 uint16_t upper = |
| 986 static_cast<uint16_t>(u_toupper(static_cast<UChar>(ch))); |
| 987 result->SeqOneByteStringSet(dest_index++, upper); |
| 988 } |
| 989 } |
| 990 |
| 991 return *result; |
| 992 } |
| 993 |
| 994 return LocaleConvertCase(s, isolate, true, -1); |
| 995 } |
| 996 |
| 997 RUNTIME_FUNCTION(Runtime_StringLocaleConvertCase) { |
| 998 HandleScope scope(isolate); |
| 999 DCHECK_EQ(args.length(), 3); |
| 1000 CONVERT_ARG_HANDLE_CHECKED(String, s, 0); |
| 1001 CONVERT_BOOLEAN_ARG_CHECKED(is_upper, 1); |
| 1002 CONVERT_NUMBER_CHECKED(int, lang_id, Int32, args[2]); |
| 1003 |
| 1004 return LocaleConvertCase(s, isolate, is_upper, lang_id); |
| 1005 } |
| 1006 |
| 752 } // namespace internal | 1007 } // namespace internal |
| 753 } // namespace v8 | 1008 } // namespace v8 |
| 754 | 1009 |
| 755 #endif // V8_I18N_SUPPORT | 1010 #endif // V8_I18N_SUPPORT |
| OLD | NEW |