LCOV - code coverage report
Current view: top level - src/objects - intl-objects.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 540 569 94.9 %
Date: 2019-02-19 Functions: 73 77 94.8 %

          Line data    Source code
       1             : // Copyright 2013 the V8 project authors. All rights reserved.
       2             : // Use of this source code is governed by a BSD-style license that can be
       3             : // found in the LICENSE file.
       4             : 
       5             : #ifndef V8_INTL_SUPPORT
       6             : #error Internationalization is expected to be enabled.
       7             : #endif  // V8_INTL_SUPPORT
       8             : 
       9             : #include "src/objects/intl-objects.h"
      10             : 
      11             : #include <algorithm>
      12             : #include <memory>
      13             : #include <string>
      14             : #include <vector>
      15             : 
      16             : #include "src/api-inl.h"
      17             : #include "src/global-handles.h"
      18             : #include "src/heap/factory.h"
      19             : #include "src/isolate.h"
      20             : #include "src/objects-inl.h"
      21             : #include "src/objects/js-collator-inl.h"
      22             : #include "src/objects/js-date-time-format-inl.h"
      23             : #include "src/objects/js-locale-inl.h"
      24             : #include "src/objects/js-number-format-inl.h"
      25             : #include "src/objects/string.h"
      26             : #include "src/property-descriptor.h"
      27             : #include "src/string-case.h"
      28             : #include "unicode/basictz.h"
      29             : #include "unicode/brkiter.h"
      30             : #include "unicode/calendar.h"
      31             : #include "unicode/coll.h"
      32             : #include "unicode/datefmt.h"
      33             : #include "unicode/decimfmt.h"
      34             : #include "unicode/locid.h"
      35             : #include "unicode/normalizer2.h"
      36             : #include "unicode/numfmt.h"
      37             : #include "unicode/numsys.h"
      38             : #include "unicode/timezone.h"
      39             : #include "unicode/ustring.h"
      40             : #include "unicode/uvernum.h"  // U_ICU_VERSION_MAJOR_NUM
      41             : 
      42             : #define XSTR(s) STR(s)
      43             : #define STR(s) #s
      44             : static_assert(
      45             :     V8_MINIMUM_ICU_VERSION <= U_ICU_VERSION_MAJOR_NUM,
      46             :     "v8 is required to build with ICU " XSTR(V8_MINIMUM_ICU_VERSION) " and up");
      47             : #undef STR
      48             : #undef XSTR
      49             : 
      50             : namespace v8 {
      51             : namespace internal {
      52             : 
      53             : namespace {
      54         355 : inline bool IsASCIIUpper(uint16_t ch) { return ch >= 'A' && ch <= 'Z'; }
      55             : 
      56             : const uint8_t kToLower[256] = {
      57             :     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
      58             :     0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
      59             :     0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23,
      60             :     0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
      61             :     0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,
      62             :     0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
      63             :     0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73,
      64             :     0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
      65             :     0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B,
      66             :     0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
      67             :     0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x82, 0x83,
      68             :     0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
      69             :     0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B,
      70             :     0x9C, 0x9D, 0x9E, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
      71             :     0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3,
      72             :     0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
      73             :     0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB,
      74             :     0xEC, 0xED, 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xD7,
      75             :     0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xDF, 0xE0, 0xE1, 0xE2, 0xE3,
      76             :     0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
      77             :     0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB,
      78             :     0xFC, 0xFD, 0xFE, 0xFF,
      79             : };
      80             : 
      81             : inline uint16_t ToLatin1Lower(uint16_t ch) {
      82        1710 :   return static_cast<uint16_t>(kToLower[ch]);
      83             : }
      84             : 
      85             : inline uint16_t ToASCIIUpper(uint16_t ch) {
      86           0 :   return ch & ~((ch >= 'a' && ch <= 'z') << 5);
      87             : }
      88             : 
      89             : // Does not work for U+00DF (sharp-s), U+00B5 (micron), U+00FF.
      90             : inline uint16_t ToLatin1Upper(uint16_t ch) {
      91             :   DCHECK(ch != 0xDF && ch != 0xB5 && ch != 0xFF);
      92             :   return ch &
      93        1337 :          ~(((ch >= 'a' && ch <= 'z') || (((ch & 0xE0) == 0xE0) && ch != 0xF7))
      94        1337 :            << 5);
      95             : }
      96             : 
      97             : template <typename Char>
      98           0 : bool ToUpperFastASCII(const Vector<const Char>& src,
      99             :                       Handle<SeqOneByteString> result) {
     100             :   // Do a faster loop for the case where all the characters are ASCII.
     101             :   uint16_t ored = 0;
     102             :   int32_t index = 0;
     103           0 :   for (auto it = src.begin(); it != src.end(); ++it) {
     104           0 :     uint16_t ch = static_cast<uint16_t>(*it);
     105           0 :     ored |= ch;
     106           0 :     result->SeqOneByteStringSet(index++, ToASCIIUpper(ch));
     107             :   }
     108           0 :   return !(ored & ~0x7F);
     109             : }
     110             : 
     111             : const uint16_t sharp_s = 0xDF;
     112             : 
     113             : template <typename Char>
     114         424 : bool ToUpperOneByte(const Vector<const Char>& src, uint8_t* dest,
     115             :                     int* sharp_s_count) {
     116             :   // Still pretty-fast path for the input with non-ASCII Latin-1 characters.
     117             : 
     118             :   // There are two special cases.
     119             :   //  1. U+00B5 and U+00FF are mapped to a character beyond U+00FF.
     120             :   //  2. Lower case sharp-S converts to "SS" (two characters)
     121         212 :   *sharp_s_count = 0;
     122        2306 :   for (auto it = src.begin(); it != src.end(); ++it) {
     123        1004 :     uint16_t ch = static_cast<uint16_t>(*it);
     124        1004 :     if (V8_UNLIKELY(ch == sharp_s)) {
     125          99 :       ++(*sharp_s_count);
     126          99 :       continue;
     127             :     }
     128         905 :     if (V8_UNLIKELY(ch == 0xB5 || ch == 0xFF)) {
     129             :       // Since this upper-cased character does not fit in an 8-bit string, we
     130             :       // need to take the 16-bit path.
     131             :       return false;
     132             :     }
     133        1684 :     *dest++ = ToLatin1Upper(ch);
     134             :   }
     135             : 
     136             :   return true;
     137             : }
     138             : 
     139             : template <typename Char>
     140          81 : void ToUpperWithSharpS(const Vector<const Char>& src,
     141             :                        Handle<SeqOneByteString> result) {
     142             :   int32_t dest_index = 0;
     143        1350 :   for (auto it = src.begin(); it != src.end(); ++it) {
     144         594 :     uint16_t ch = static_cast<uint16_t>(*it);
     145         594 :     if (ch == sharp_s) {
     146          99 :       result->SeqOneByteStringSet(dest_index++, 'S');
     147          99 :       result->SeqOneByteStringSet(dest_index++, 'S');
     148             :     } else {
     149         495 :       result->SeqOneByteStringSet(dest_index++, ToLatin1Upper(ch));
     150             :     }
     151             :   }
     152          81 : }
     153             : 
     154          41 : inline int FindFirstUpperOrNonAscii(String s, int length) {
     155         396 :   for (int index = 0; index < length; ++index) {
     156             :     uint16_t ch = s->Get(index);
     157         355 :     if (V8_UNLIKELY(IsASCIIUpper(ch) || ch & ~0x7F)) {
     158             :       return index;
     159             :     }
     160             :   }
     161             :   return length;
     162             : }
     163             : 
     164      693166 : const UChar* GetUCharBufferFromFlat(const String::FlatContent& flat,
     165             :                                     std::unique_ptr<uc16[]>* dest,
     166             :                                     int32_t length) {
     167             :   DCHECK(flat.IsFlat());
     168      346619 :   if (flat.IsOneByte()) {
     169      338029 :     if (!*dest) {
     170      337957 :       dest->reset(NewArray<uc16>(length));
     171             :       CopyChars(dest->get(), flat.ToOneByteVector().start(), length);
     172             :     }
     173      338029 :     return reinterpret_cast<const UChar*>(dest->get());
     174             :   } else {
     175             :     return reinterpret_cast<const UChar*>(flat.ToUC16Vector().start());
     176             :   }
     177             : }
     178             : 
     179             : template <typename T>
     180        6127 : MaybeHandle<T> New(Isolate* isolate, Handle<JSFunction> constructor,
     181             :                    Handle<Object> locales, Handle<Object> options) {
     182             :   Handle<JSObject> result;
     183       12254 :   ASSIGN_RETURN_ON_EXCEPTION(
     184             :       isolate, result,
     185             :       JSObject::New(constructor, constructor, Handle<AllocationSite>::null()),
     186             :       T);
     187        6127 :   return T::Initialize(isolate, Handle<T>::cast(result), locales, options);
     188             : }
     189             : }  // namespace
     190             : 
     191       61104 : const uint8_t* Intl::ToLatin1LowerTable() { return &kToLower[0]; }
     192             : 
     193      344409 : icu::UnicodeString Intl::ToICUUnicodeString(Isolate* isolate,
     194             :                                             Handle<String> string) {
     195      344409 :   string = String::Flatten(isolate, string);
     196             :   {
     197             :     DisallowHeapAllocation no_gc;
     198      344409 :     std::unique_ptr<uc16[]> sap;
     199             :     return icu::UnicodeString(
     200      688818 :         GetUCharBufferFromFlat(string->GetFlatContent(no_gc), &sap,
     201             :                                string->length()),
     202     1033227 :         string->length());
     203             :   }
     204             : }
     205             : 
     206             : namespace {
     207        2030 : MaybeHandle<String> LocaleConvertCase(Isolate* isolate, Handle<String> s,
     208             :                                       bool is_to_upper, const char* lang) {
     209        2030 :   auto case_converter = is_to_upper ? u_strToUpper : u_strToLower;
     210             :   int32_t src_length = s->length();
     211             :   int32_t dest_length = src_length;
     212             :   UErrorCode status;
     213             :   Handle<SeqTwoByteString> result;
     214        2030 :   std::unique_ptr<uc16[]> sap;
     215             : 
     216        2030 :   if (dest_length == 0) return ReadOnlyRoots(isolate).empty_string_handle();
     217             : 
     218             :   // This is not a real loop. It'll be executed only once (no overflow) or
     219             :   // twice (overflow).
     220         180 :   for (int i = 0; i < 2; ++i) {
     221             :     // Case conversion can increase the string length (e.g. sharp-S => SS) so
     222             :     // that we have to handle RangeError exceptions here.
     223        4420 :     ASSIGN_RETURN_ON_EXCEPTION(
     224             :         isolate, result, isolate->factory()->NewRawTwoByteString(dest_length),
     225             :         String);
     226             :     DisallowHeapAllocation no_gc;
     227             :     DCHECK(s->IsFlat());
     228        2210 :     String::FlatContent flat = s->GetFlatContent(no_gc);
     229        2210 :     const UChar* src = GetUCharBufferFromFlat(flat, &sap, src_length);
     230        2210 :     status = U_ZERO_ERROR;
     231             :     dest_length =
     232             :         case_converter(reinterpret_cast<UChar*>(result->GetChars(no_gc)),
     233        2210 :                        dest_length, src, src_length, lang, &status);
     234        2210 :     if (status != U_BUFFER_OVERFLOW_ERROR) break;
     235             :   }
     236             : 
     237             :   // In most cases, the output will fill the destination buffer completely
     238             :   // leading to an unterminated string (U_STRING_NOT_TERMINATED_WARNING).
     239             :   // Only in rare cases, it'll be shorter than the destination buffer and
     240             :   // |result| has to be truncated.
     241             :   DCHECK(U_SUCCESS(status));
     242        2030 :   if (V8_LIKELY(status == U_STRING_NOT_TERMINATED_WARNING)) {
     243             :     DCHECK(dest_length == result->length());
     244        1895 :     return result;
     245             :   }
     246             :   DCHECK(dest_length < result->length());
     247         135 :   return SeqString::Truncate(result, dest_length);
     248             : }
     249             : 
     250             : }  // namespace
     251             : 
     252             : // A stripped-down version of ConvertToLower that can only handle flat one-byte
     253             : // strings and does not allocate. Note that {src} could still be, e.g., a
     254             : // one-byte sliced string with a two-byte parent string.
     255             : // Called from TF builtins.
     256        2301 : String Intl::ConvertOneByteToLower(String src, String dst) {
     257             :   DCHECK_EQ(src->length(), dst->length());
     258             :   DCHECK(src->HasOnlyOneByteChars());
     259             :   DCHECK(src->IsFlat());
     260             :   DCHECK(dst->IsSeqOneByteString());
     261             : 
     262             :   DisallowHeapAllocation no_gc;
     263             : 
     264             :   const int length = src->length();
     265        2301 :   String::FlatContent src_flat = src->GetFlatContent(no_gc);
     266             :   uint8_t* dst_data = SeqOneByteString::cast(dst)->GetChars(no_gc);
     267             : 
     268        2301 :   if (src_flat.IsOneByte()) {
     269             :     const uint8_t* src_data = src_flat.ToOneByteVector().start();
     270             : 
     271        2296 :     bool has_changed_character = false;
     272             :     int index_to_first_unprocessed =
     273             :         FastAsciiConvert<true>(reinterpret_cast<char*>(dst_data),
     274             :                                reinterpret_cast<const char*>(src_data), length,
     275        2296 :                                &has_changed_character);
     276             : 
     277        2296 :     if (index_to_first_unprocessed == length) {
     278        2278 :       return has_changed_character ? dst : src;
     279             :     }
     280             : 
     281             :     // If not ASCII, we keep the result up to index_to_first_unprocessed and
     282             :     // process the rest.
     283        1710 :     for (int index = index_to_first_unprocessed; index < length; ++index) {
     284        3420 :       dst_data[index] = ToLatin1Lower(static_cast<uint16_t>(src_data[index]));
     285             :     }
     286             :   } else {
     287             :     DCHECK(src_flat.IsTwoByte());
     288           5 :     int index_to_first_unprocessed = FindFirstUpperOrNonAscii(src, length);
     289           5 :     if (index_to_first_unprocessed == length) return src;
     290             : 
     291             :     const uint16_t* src_data = src_flat.ToUC16Vector().start();
     292           0 :     CopyChars(dst_data, src_data, index_to_first_unprocessed);
     293           0 :     for (int index = index_to_first_unprocessed; index < length; ++index) {
     294           0 :       dst_data[index] = ToLatin1Lower(static_cast<uint16_t>(src_data[index]));
     295             :     }
     296             :   }
     297             : 
     298          18 :   return dst;
     299             : }
     300             : 
     301        2836 : MaybeHandle<String> Intl::ConvertToLower(Isolate* isolate, Handle<String> s) {
     302        2836 :   if (!s->HasOnlyOneByteChars()) {
     303             :     // Use a slower implementation for strings with characters beyond U+00FF.
     304        1310 :     return LocaleConvertCase(isolate, s, false, "");
     305             :   }
     306             : 
     307             :   int length = s->length();
     308             : 
     309             :   // We depend here on the invariant that the length of a Latin1
     310             :   // string is invariant under ToLowerCase, and the result always
     311             :   // fits in the Latin1 range in the *root locale*. It does not hold
     312             :   // for ToUpperCase even in the root locale.
     313             : 
     314             :   // Scan the string for uppercase and non-ASCII characters for strings
     315             :   // shorter than a machine-word without any memory allocation overhead.
     316             :   // TODO(jshin): Apply this to a longer input by breaking FastAsciiConvert()
     317             :   // to two parts, one for scanning the prefix with no change and the other for
     318             :   // handling ASCII-only characters.
     319             : 
     320             :   bool is_short = length < static_cast<int>(sizeof(uintptr_t));
     321        1526 :   if (is_short) {
     322          36 :     bool is_lower_ascii = FindFirstUpperOrNonAscii(*s, length) == length;
     323          36 :     if (is_lower_ascii) return s;
     324             :   }
     325             : 
     326             :   Handle<SeqOneByteString> result =
     327        2980 :       isolate->factory()->NewRawOneByteString(length).ToHandleChecked();
     328             : 
     329        4470 :   return Handle<String>(Intl::ConvertOneByteToLower(*s, *result), isolate);
     330             : }
     331             : 
     332        4828 : MaybeHandle<String> Intl::ConvertToUpper(Isolate* isolate, Handle<String> s) {
     333             :   int32_t length = s->length();
     334        4828 :   if (s->HasOnlyOneByteChars() && length > 0) {
     335             :     Handle<SeqOneByteString> result =
     336        9116 :         isolate->factory()->NewRawOneByteString(length).ToHandleChecked();
     337             : 
     338             :     DCHECK(s->IsFlat());
     339             :     int sharp_s_count;
     340             :     bool is_result_single_byte;
     341             :     {
     342             :       DisallowHeapAllocation no_gc;
     343        4558 :       String::FlatContent flat = s->GetFlatContent(no_gc);
     344             :       uint8_t* dest = result->GetChars(no_gc);
     345        4558 :       if (flat.IsOneByte()) {
     346             :         Vector<const uint8_t> src = flat.ToOneByteVector();
     347        4558 :         bool has_changed_character = false;
     348             :         int index_to_first_unprocessed = FastAsciiConvert<false>(
     349             :             reinterpret_cast<char*>(result->GetChars(no_gc)),
     350             :             reinterpret_cast<const char*>(src.start()), length,
     351        4558 :             &has_changed_character);
     352        4558 :         if (index_to_first_unprocessed == length) {
     353        4346 :           return has_changed_character ? result : s;
     354             :         }
     355             :         // If not ASCII, we keep the result up to index_to_first_unprocessed and
     356             :         // process the rest.
     357             :         is_result_single_byte =
     358         212 :             ToUpperOneByte(src.SubVector(index_to_first_unprocessed, length),
     359         424 :                            dest + index_to_first_unprocessed, &sharp_s_count);
     360             :       } else {
     361             :         DCHECK(flat.IsTwoByte());
     362           0 :         Vector<const uint16_t> src = flat.ToUC16Vector();
     363           0 :         if (ToUpperFastASCII(src, result)) return result;
     364           0 :         is_result_single_byte = ToUpperOneByte(src, dest, &sharp_s_count);
     365             :       }
     366             :     }
     367             : 
     368             :     // Go to the full Unicode path if there are characters whose uppercase
     369             :     // is beyond the Latin-1 range (cannot be represented in OneByteString).
     370         212 :     if (V8_UNLIKELY(!is_result_single_byte)) {
     371          63 :       return LocaleConvertCase(isolate, s, true, "");
     372             :     }
     373             : 
     374         149 :     if (sharp_s_count == 0) return result;
     375             : 
     376             :     // We have sharp_s_count sharp-s characters, but the result is still
     377             :     // in the Latin-1 range.
     378         162 :     ASSIGN_RETURN_ON_EXCEPTION(
     379             :         isolate, result,
     380             :         isolate->factory()->NewRawOneByteString(length + sharp_s_count),
     381             :         String);
     382             :     DisallowHeapAllocation no_gc;
     383          81 :     String::FlatContent flat = s->GetFlatContent(no_gc);
     384          81 :     if (flat.IsOneByte()) {
     385          81 :       ToUpperWithSharpS(flat.ToOneByteVector(), result);
     386             :     } else {
     387           0 :       ToUpperWithSharpS(flat.ToUC16Vector(), result);
     388             :     }
     389             : 
     390          81 :     return result;
     391             :   }
     392             : 
     393         270 :   return LocaleConvertCase(isolate, s, true, "");
     394             : }
     395             : 
     396        3771 : std::string Intl::GetNumberingSystem(const icu::Locale& icu_locale) {
     397             :   // Ugly hack. ICU doesn't expose numbering system in any way, so we have
     398             :   // to assume that for given locale NumberingSystem constructor produces the
     399             :   // same digits as NumberFormat/Calendar would.
     400        3771 :   UErrorCode status = U_ZERO_ERROR;
     401             :   std::unique_ptr<icu::NumberingSystem> numbering_system(
     402        3771 :       icu::NumberingSystem::createInstance(icu_locale, status));
     403        3771 :   if (U_SUCCESS(status)) return numbering_system->getName();
     404           0 :   return "latn";
     405             : }
     406             : 
     407       31470 : icu::Locale Intl::CreateICULocale(const std::string& bcp47_locale) {
     408             :   DisallowHeapAllocation no_gc;
     409             : 
     410             :   // Convert BCP47 into ICU locale format.
     411       15735 :   UErrorCode status = U_ZERO_ERROR;
     412             : 
     413       15735 :   icu::Locale icu_locale = icu::Locale::forLanguageTag(bcp47_locale, status);
     414       31470 :   CHECK(U_SUCCESS(status));
     415       15735 :   if (icu_locale.isBogus()) {
     416           0 :     FATAL("Failed to create ICU locale, are ICU data files missing?");
     417             :   }
     418             : 
     419       15735 :   return icu_locale;
     420             : }
     421             : 
     422             : // static
     423             : 
     424       41598 : MaybeHandle<String> Intl::ToString(Isolate* isolate,
     425             :                                    const icu::UnicodeString& string) {
     426             :   return isolate->factory()->NewStringFromTwoByte(Vector<const uint16_t>(
     427       83196 :       reinterpret_cast<const uint16_t*>(string.getBuffer()), string.length()));
     428             : }
     429             : 
     430       35811 : MaybeHandle<String> Intl::ToString(Isolate* isolate,
     431             :                                    const icu::UnicodeString& string,
     432             :                                    int32_t begin, int32_t end) {
     433       35811 :   return Intl::ToString(isolate, string.tempSubStringBetween(begin, end));
     434             : }
     435             : 
     436             : namespace {
     437             : 
     438       18900 : Handle<JSObject> InnerAddElement(Isolate* isolate, Handle<JSArray> array,
     439             :                                  int index, Handle<String> field_type_string,
     440             :                                  Handle<String> value) {
     441             :   // let element = $array[$index] = {
     442             :   //   type: $field_type_string,
     443             :   //   value: $value
     444             :   // }
     445             :   // return element;
     446             :   Factory* factory = isolate->factory();
     447       18900 :   Handle<JSObject> element = factory->NewJSObject(isolate->object_function());
     448             :   JSObject::AddProperty(isolate, element, factory->type_string(),
     449       18900 :                         field_type_string, NONE);
     450             : 
     451       18900 :   JSObject::AddProperty(isolate, element, factory->value_string(), value, NONE);
     452       37800 :   JSObject::AddDataElement(array, index, element, NONE);
     453       18900 :   return element;
     454             : }
     455             : 
     456             : }  // namespace
     457             : 
     458       18450 : void Intl::AddElement(Isolate* isolate, Handle<JSArray> array, int index,
     459             :                       Handle<String> field_type_string, Handle<String> value) {
     460             :   // Same as $array[$index] = {type: $field_type_string, value: $value};
     461       18450 :   InnerAddElement(isolate, array, index, field_type_string, value);
     462       18450 : }
     463             : 
     464         450 : void Intl::AddElement(Isolate* isolate, Handle<JSArray> array, int index,
     465             :                       Handle<String> field_type_string, Handle<String> value,
     466             :                       Handle<String> additional_property_name,
     467             :                       Handle<String> additional_property_value) {
     468             :   // Same as $array[$index] = {
     469             :   //   type: $field_type_string, value: $value,
     470             :   //   $additional_property_name: $additional_property_value
     471             :   // }
     472             :   Handle<JSObject> element =
     473         450 :       InnerAddElement(isolate, array, index, field_type_string, value);
     474             :   JSObject::AddProperty(isolate, element, additional_property_name,
     475         450 :                         additional_property_value, NONE);
     476         450 : }
     477             : 
     478             : namespace {
     479             : 
     480             : // Build the shortened locale; eg, convert xx_Yyyy_ZZ  to xx_ZZ.
     481             : //
     482             : // If locale has a script tag then return true and the locale without the
     483             : // script else return false and an empty string.
     484      317906 : bool RemoveLocaleScriptTag(const std::string& icu_locale,
     485             :                            std::string* locale_less_script) {
     486      317906 :   icu::Locale new_locale = icu::Locale::createCanonical(icu_locale.c_str());
     487             :   const char* icu_script = new_locale.getScript();
     488      317906 :   if (icu_script == nullptr || strlen(icu_script) == 0) {
     489      302658 :     *locale_less_script = std::string();
     490      302658 :     return false;
     491             :   }
     492             : 
     493             :   const char* icu_language = new_locale.getLanguage();
     494             :   const char* icu_country = new_locale.getCountry();
     495       30496 :   icu::Locale short_locale = icu::Locale(icu_language, icu_country);
     496       15248 :   *locale_less_script = short_locale.getName();
     497      317906 :   return true;
     498             : }
     499             : 
     500             : }  // namespace
     501             : 
     502         984 : std::set<std::string> Intl::BuildLocaleSet(
     503             :     const icu::Locale* icu_available_locales, int32_t count) {
     504             :   std::set<std::string> locales;
     505      318890 :   for (int32_t i = 0; i < count; ++i) {
     506             :     std::string locale =
     507      635812 :         Intl::ToLanguageTag(icu_available_locales[i]).FromJust();
     508             :     locales.insert(locale);
     509             : 
     510             :     std::string shortened_locale;
     511      317906 :     if (RemoveLocaleScriptTag(locale, &shortened_locale)) {
     512       15248 :       std::replace(shortened_locale.begin(), shortened_locale.end(), '_', '-');
     513             :       locales.insert(shortened_locale);
     514             :     }
     515             :   }
     516             : 
     517         984 :   return locales;
     518             : }
     519             : 
     520      596579 : Maybe<std::string> Intl::ToLanguageTag(const icu::Locale& locale) {
     521      596579 :   UErrorCode status = U_ZERO_ERROR;
     522      596579 :   std::string res = locale.toLanguageTag<std::string>(status);
     523      596579 :   if (U_FAILURE(status)) {
     524             :     return Nothing<std::string>();
     525             :   }
     526      596570 :   CHECK(U_SUCCESS(status));
     527             : 
     528             :   // Hack to remove -true and -yes from unicode extensions
     529             :   // Address https://crbug.com/v8/8565
     530             :   // TODO(ftang): Move the following "remove true" logic into ICU toLanguageTag
     531             :   // by fixing ICU-20310.
     532      596570 :   size_t u_ext_start = res.find("-u-");
     533      596570 :   if (u_ext_start != std::string::npos) {
     534             :     // remove "-true" and "-yes" after -u-
     535       17818 :     const std::vector<std::string> remove_items({"-true", "-yes"});
     536       35636 :     for (auto item = remove_items.begin(); item != remove_items.end(); item++) {
     537       35669 :       for (size_t sep_remove =
     538       17818 :                res.find(*item, u_ext_start + 5 /* strlen("-u-xx") == 5 */);
     539             :            sep_remove != std::string::npos; sep_remove = res.find(*item)) {
     540          33 :         size_t end_of_sep_remove = sep_remove + item->length();
     541          51 :         if (res.length() == end_of_sep_remove ||
     542          18 :             res.at(end_of_sep_remove) == '-') {
     543          33 :           res.erase(sep_remove, item->length());
     544             :         }
     545             :       }
     546        8909 :     }
     547             :   }
     548             :   return Just(res);
     549             : }
     550             : 
     551             : namespace {
     552        2008 : std::string DefaultLocale(Isolate* isolate) {
     553        2008 :   if (isolate->default_locale().empty()) {
     554         474 :     icu::Locale default_locale;
     555             :     // Translate ICU's fallback locale to a well-known locale.
     556         474 :     if (strcmp(default_locale.getName(), "en_US_POSIX") == 0) {
     557           0 :       isolate->set_default_locale("en-US");
     558             :     } else {
     559             :       // Set the locale
     560             :       isolate->set_default_locale(
     561         474 :           default_locale.isBogus()
     562             :               ? "und"
     563        1422 :               : Intl::ToLanguageTag(default_locale).FromJust());
     564             :     }
     565         474 :     DCHECK(!isolate->default_locale().empty());
     566             :   }
     567        2008 :   return isolate->default_locale();
     568             : }
     569             : }  // namespace
     570             : 
     571             : // See ecma402/#legacy-constructor.
     572        5994 : MaybeHandle<Object> Intl::LegacyUnwrapReceiver(Isolate* isolate,
     573             :                                                Handle<JSReceiver> receiver,
     574             :                                                Handle<JSFunction> constructor,
     575             :                                                bool has_initialized_slot) {
     576             :   Handle<Object> obj_is_instance_of;
     577       11988 :   ASSIGN_RETURN_ON_EXCEPTION(isolate, obj_is_instance_of,
     578             :                              Object::InstanceOf(isolate, receiver, constructor),
     579             :                              Object);
     580        5994 :   bool is_instance_of = obj_is_instance_of->BooleanValue(isolate);
     581             : 
     582             :   // 2. If receiver does not have an [[Initialized...]] internal slot
     583             :   //    and ? InstanceofOperator(receiver, constructor) is true, then
     584        5994 :   if (!has_initialized_slot && is_instance_of) {
     585             :     // 2. a. Let new_receiver be ? Get(receiver, %Intl%.[[FallbackSymbol]]).
     586             :     Handle<Object> new_receiver;
     587         180 :     ASSIGN_RETURN_ON_EXCEPTION(
     588             :         isolate, new_receiver,
     589             :         JSReceiver::GetProperty(isolate, receiver,
     590             :                                 isolate->factory()->intl_fallback_symbol()),
     591             :         Object);
     592          90 :     return new_receiver;
     593             :   }
     594             : 
     595        5904 :   return receiver;
     596             : }
     597             : 
     598      505322 : Maybe<bool> Intl::GetStringOption(Isolate* isolate, Handle<JSReceiver> options,
     599             :                                   const char* property,
     600       13496 :                                   std::vector<const char*> values,
     601             :                                   const char* service,
     602             :                                   std::unique_ptr<char[]>* result) {
     603             :   Handle<String> property_str =
     604      505322 :       isolate->factory()->NewStringFromAsciiChecked(property);
     605             : 
     606             :   // 1. Let value be ? Get(options, property).
     607             :   Handle<Object> value;
     608     1010644 :   ASSIGN_RETURN_ON_EXCEPTION_VALUE(
     609             :       isolate, value,
     610             :       Object::GetPropertyOrElement(isolate, options, property_str),
     611             :       Nothing<bool>());
     612             : 
     613     1010446 :   if (value->IsUndefined(isolate)) {
     614             :     return Just(false);
     615             :   }
     616             : 
     617             :   // 2. c. Let value be ? ToString(value).
     618             :   Handle<String> value_str;
     619       27010 :   ASSIGN_RETURN_ON_EXCEPTION_VALUE(
     620             :       isolate, value_str, Object::ToString(isolate, value), Nothing<bool>());
     621       13496 :   std::unique_ptr<char[]> value_cstr = value_str->ToCString();
     622             : 
     623             :   // 2. d. if values is not undefined, then
     624       13496 :   if (values.size() > 0) {
     625             :     // 2. d. i. If values does not contain an element equal to value,
     626             :     // throw a RangeError exception.
     627       53445 :     for (size_t i = 0; i < values.size(); i++) {
     628       32725 :       if (strcmp(values.at(i), value_cstr.get()) == 0) {
     629             :         // 2. e. return value
     630             :         *result = std::move(value_cstr);
     631             :         return Just(true);
     632             :       }
     633             :     }
     634             : 
     635             :     Handle<String> service_str =
     636         365 :         isolate->factory()->NewStringFromAsciiChecked(service);
     637         730 :     THROW_NEW_ERROR_RETURN_VALUE(
     638             :         isolate,
     639             :         NewRangeError(MessageTemplate::kValueOutOfRange, value, service_str,
     640             :                       property_str),
     641             :         Nothing<bool>());
     642             :   }
     643             : 
     644             :   // 2. e. return value
     645             :   *result = std::move(value_cstr);
     646             :   return Just(true);
     647             : }
     648             : 
     649       70685 : V8_WARN_UNUSED_RESULT Maybe<bool> Intl::GetBoolOption(
     650             :     Isolate* isolate, Handle<JSReceiver> options, const char* property,
     651             :     const char* service, bool* result) {
     652             :   Handle<String> property_str =
     653       70685 :       isolate->factory()->NewStringFromAsciiChecked(property);
     654             : 
     655             :   // 1. Let value be ? Get(options, property).
     656             :   Handle<Object> value;
     657      141370 :   ASSIGN_RETURN_ON_EXCEPTION_VALUE(
     658             :       isolate, value,
     659             :       Object::GetPropertyOrElement(isolate, options, property_str),
     660             :       Nothing<bool>());
     661             : 
     662             :   // 2. If value is not undefined, then
     663      141352 :   if (!value->IsUndefined(isolate)) {
     664             :     // 2. b. i. Let value be ToBoolean(value).
     665         100 :     *result = value->BooleanValue(isolate);
     666             : 
     667             :     // 2. e. return value
     668             :     return Just(true);
     669             :   }
     670             : 
     671             :   return Just(false);
     672             : }
     673             : 
     674             : namespace {
     675             : 
     676      768346 : char AsciiToLower(char c) {
     677      768346 :   if (c < 'A' || c > 'Z') {
     678             :     return c;
     679             :   }
     680      205470 :   return c | (1 << 5);
     681             : }
     682             : 
     683       51564 : bool IsLowerAscii(char c) { return c >= 'a' && c < 'z'; }
     684             : 
     685             : bool IsTwoLetterLanguage(const std::string& locale) {
     686             :   // Two letters, both in range 'a'-'z'...
     687      171896 :   return locale.length() == 2 && IsLowerAscii(locale[0]) &&
     688       25566 :          IsLowerAscii(locale[1]);
     689             : }
     690             : 
     691       25008 : bool IsDeprecatedLanguage(const std::string& locale) {
     692             :   //  Check if locale is one of the deprecated language tags:
     693      100005 :   return locale == "in" || locale == "iw" || locale == "ji" || locale == "jw";
     694             : }
     695             : 
     696             : // Reference:
     697             : // https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry
     698       95162 : bool IsGrandfatheredTagWithoutPreferredVaule(const std::string& locale) {
     699      190297 :   if (V8_UNLIKELY(locale == "zh-min" || locale == "cel-gaulish")) return true;
     700      147871 :   if (locale.length() > 6 /* i-mingo is 7 chars long */ &&
     701       52745 :       V8_UNLIKELY(locale[0] == 'i' && locale[1] == '-')) {
     702         486 :     return locale.substr(2) == "default" || locale.substr(2) == "enochian" ||
     703         135 :            locale.substr(2) == "mingo";
     704             :   }
     705             :   return false;
     706             : }
     707             : 
     708             : }  // anonymous namespace
     709             : 
     710       67484 : Maybe<std::string> Intl::CanonicalizeLanguageTag(Isolate* isolate,
     711             :                                                  Handle<Object> locale_in) {
     712             :   Handle<String> locale_str;
     713             :   // This does part of the validity checking spec'ed in CanonicalizeLocaleList:
     714             :   // 7c ii. If Type(kValue) is not String or Object, throw a TypeError
     715             :   // exception.
     716             :   // 7c iii. Let tag be ? ToString(kValue).
     717             :   // 7c iv. If IsStructurallyValidLanguageTag(tag) is false, throw a
     718             :   // RangeError exception.
     719             : 
     720      134968 :   if (locale_in->IsString()) {
     721       67448 :     locale_str = Handle<String>::cast(locale_in);
     722          72 :   } else if (locale_in->IsJSReceiver()) {
     723           0 :     ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, locale_str,
     724             :                                      Object::ToString(isolate, locale_in),
     725             :                                      Nothing<std::string>());
     726             :   } else {
     727          72 :     THROW_NEW_ERROR_RETURN_VALUE(isolate,
     728             :                                  NewTypeError(MessageTemplate::kLanguageID),
     729             :                                  Nothing<std::string>());
     730             :   }
     731      202344 :   std::string locale(locale_str->ToCString().get());
     732             : 
     733       67448 :   return Intl::CanonicalizeLanguageTag(isolate, locale);
     734             : }
     735             : 
     736      120332 : Maybe<std::string> Intl::CanonicalizeLanguageTag(Isolate* isolate,
     737             :                                                  const std::string& locale_in) {
     738      120332 :   std::string locale = locale_in;
     739             : 
     740      240664 :   if (locale.length() == 0 ||
     741      120332 :       !String::IsAscii(locale.data(), static_cast<int>(locale.length()))) {
     742           0 :     THROW_NEW_ERROR_RETURN_VALUE(
     743             :         isolate,
     744             :         NewRangeError(
     745             :             MessageTemplate::kInvalidLanguageTag,
     746             :             isolate->factory()->NewStringFromAsciiChecked(locale.c_str())),
     747             :         Nothing<std::string>());
     748             :   }
     749             : 
     750             :   // Optimize for the most common case: a 2-letter language code in the
     751             :   // canonical form/lowercase that is not one of the deprecated codes
     752             :   // (in, iw, ji, jw). Don't check for ~70 of 3-letter deprecated language
     753             :   // codes. Instead, let them be handled by ICU in the slow path. However,
     754             :   // fast-track 'fil' (3-letter canonical code).
     755      215674 :   if ((IsTwoLetterLanguage(locale) && !IsDeprecatedLanguage(locale)) ||
     756             :       locale == "fil") {
     757             :     return Just(locale);
     758             :   }
     759             : 
     760             :   // Because per BCP 47 2.1.1 language tags are case-insensitive, lowercase
     761             :   // the input before any more check.
     762       95162 :   std::transform(locale.begin(), locale.end(), locale.begin(), AsciiToLower);
     763             : 
     764             :   // ICU maps a few grandfathered tags to what looks like a regular language
     765             :   // tag even though IANA language tag registry does not have a preferred
     766             :   // entry map for them. Return them as they're with lowercasing.
     767       95162 :   if (IsGrandfatheredTagWithoutPreferredVaule(locale)) {
     768             :     return Just(locale);
     769             :   }
     770             : 
     771             :   // // ECMA 402 6.2.3
     772             :   // TODO(jshin): uloc_{for,to}TanguageTag can fail even for a structually valid
     773             :   // language tag if it's too long (much longer than 100 chars). Even if we
     774             :   // allocate a longer buffer, ICU will still fail if it's too long. Either
     775             :   // propose to Ecma 402 to put a limit on the locale length or change ICU to
     776             :   // handle long locale names better. See
     777             :   // https://unicode-org.atlassian.net/browse/ICU-13417
     778       95036 :   UErrorCode error = U_ZERO_ERROR;
     779             :   // uloc_forLanguageTag checks the structrual validity. If the input BCP47
     780             :   // language tag is parsed all the way to the end, it indicates that the input
     781             :   // is structurally valid. Due to a couple of bugs, we can't use it
     782             :   // without Chromium patches or ICU 62 or earlier.
     783      190072 :   icu::Locale icu_locale = icu::Locale::forLanguageTag(locale.c_str(), error);
     784       95036 :   if (U_FAILURE(error) || icu_locale.isBogus()) {
     785         378 :     THROW_NEW_ERROR_RETURN_VALUE(
     786             :         isolate,
     787             :         NewRangeError(
     788             :             MessageTemplate::kInvalidLanguageTag,
     789             :             isolate->factory()->NewStringFromAsciiChecked(locale.c_str())),
     790             :         Nothing<std::string>());
     791             :   }
     792       94910 :   Maybe<std::string> maybe_to_language_tag = Intl::ToLanguageTag(icu_locale);
     793       94910 :   if (maybe_to_language_tag.IsNothing()) {
     794          27 :     THROW_NEW_ERROR_RETURN_VALUE(
     795             :         isolate,
     796             :         NewRangeError(
     797             :             MessageTemplate::kInvalidLanguageTag,
     798             :             isolate->factory()->NewStringFromAsciiChecked(locale.c_str())),
     799             :         Nothing<std::string>());
     800             :   }
     801             : 
     802             :   return maybe_to_language_tag;
     803             : }
     804             : 
     805       16437 : Maybe<std::vector<std::string>> Intl::CanonicalizeLocaleList(
     806             :     Isolate* isolate, Handle<Object> locales, bool only_return_one_result) {
     807             :   // 1. If locales is undefined, then
     808       32874 :   if (locales->IsUndefined(isolate)) {
     809             :     // 1a. Return a new empty List.
     810        1612 :     return Just(std::vector<std::string>());
     811             :   }
     812             :   // 2. Let seen be a new empty List.
     813             :   std::vector<std::string> seen;
     814             :   // 3. If Type(locales) is String or locales has an [[InitializedLocale]]
     815             :   // internal slot,  then
     816       29650 :   if (locales->IsJSLocale()) {
     817             :     // Since this value came from JSLocale, which is already went though the
     818             :     // CanonializeLanguageTag process once, therefore there are no need to
     819             :     // call CanonializeLanguageTag again.
     820          54 :     seen.push_back(JSLocale::ToString(Handle<JSLocale>::cast(locales)));
     821             :     return Just(seen);
     822             :   }
     823       29596 :   if (locales->IsString()) {
     824             :     // 3a. Let O be CreateArrayFromList(« locales »).
     825             :     // Instead of creating a one-element array and then iterating over it,
     826             :     // we inline the body of the iteration:
     827             :     std::string canonicalized_tag;
     828       20450 :     if (!CanonicalizeLanguageTag(isolate, locales).To(&canonicalized_tag)) {
     829             :       return Nothing<std::vector<std::string>>();
     830             :     }
     831       10126 :     seen.push_back(canonicalized_tag);
     832             :     return Just(seen);
     833             :   }
     834             :   // 4. Else,
     835             :   // 4a. Let O be ? ToObject(locales).
     836             :   Handle<JSReceiver> o;
     837        9146 :   ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, o,
     838             :                                    Object::ToObject(isolate, locales),
     839             :                                    Nothing<std::vector<std::string>>());
     840             :   // 5. Let len be ? ToLength(? Get(O, "length")).
     841             :   Handle<Object> length_obj;
     842        9146 :   ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, length_obj,
     843             :                                    Object::GetLengthFromArrayLike(isolate, o),
     844             :                                    Nothing<std::vector<std::string>>());
     845             :   // TODO(jkummerow): Spec violation: strictly speaking, we have to iterate
     846             :   // up to 2^53-1 if {length_obj} says so. Since cases above 2^32 probably
     847             :   // don't happen in practice (and would be very slow if they do), we'll keep
     848             :   // the code simple for now by using a saturating to-uint32 conversion.
     849        4573 :   double raw_length = length_obj->Number();
     850             :   uint32_t len =
     851        4573 :       raw_length >= kMaxUInt32 ? kMaxUInt32 : static_cast<uint32_t>(raw_length);
     852             :   // 6. Let k be 0.
     853             :   // 7. Repeat, while k < len
     854        8840 :   for (uint32_t k = 0; k < len; k++) {
     855             :     // 7a. Let Pk be ToString(k).
     856             :     // 7b. Let kPresent be ? HasProperty(O, Pk).
     857        4420 :     LookupIterator it(isolate, o, k);
     858        4420 :     Maybe<bool> maybe_found = JSReceiver::HasProperty(&it);
     859        4573 :     MAYBE_RETURN(maybe_found, Nothing<std::vector<std::string>>());
     860             :     // 7c. If kPresent is true, then
     861        4420 :     if (!maybe_found.FromJust()) continue;
     862             :     // 7c i. Let kValue be ? Get(O, Pk).
     863             :     Handle<Object> k_value;
     864        8840 :     ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, k_value, Object::GetProperty(&it),
     865             :                                      Nothing<std::vector<std::string>>());
     866             :     // 7c ii. If Type(kValue) is not String or Object, throw a TypeError
     867             :     // exception.
     868             :     // 7c iii. If Type(kValue) is Object and kValue has an [[InitializedLocale]]
     869             :     // internal slot, then
     870             :     std::string canonicalized_tag;
     871        8840 :     if (k_value->IsJSLocale()) {
     872             :       // 7c iii. 1. Let tag be kValue.[[Locale]].
     873          90 :       canonicalized_tag = JSLocale::ToString(Handle<JSLocale>::cast(k_value));
     874             :       // 7c iv. Else,
     875             :     } else {
     876             :       // 7c iv 1. Let tag be ? ToString(kValue).
     877             :       // 7c v. If IsStructurallyValidLanguageTag(tag) is false, throw a
     878             :       // RangeError exception.
     879             :       // 7c vi. Let canonicalizedTag be CanonicalizeLanguageTag(tag).
     880        8750 :       if (!CanonicalizeLanguageTag(isolate, k_value).To(&canonicalized_tag)) {
     881             :         return Nothing<std::vector<std::string>>();
     882             :       }
     883             :     }
     884             :     // 7c vi. If canonicalizedTag is not an element of seen, append
     885             :     // canonicalizedTag as the last element of seen.
     886        4348 :     if (std::find(seen.begin(), seen.end(), canonicalized_tag) == seen.end()) {
     887        4348 :       seen.push_back(canonicalized_tag);
     888             :     }
     889             :     // 7d. Increase k by 1. (See loop header.)
     890             :     // Optimization: some callers only need one result.
     891        4348 :     if (only_return_one_result) return Just(seen);
     892             :   }
     893             :   // 8. Return seen.
     894       14825 :   return Just(seen);
     895             : }
     896             : 
     897             : // ecma402 #sup-string.prototype.tolocalelowercase
     898             : // ecma402 #sup-string.prototype.tolocaleuppercase
     899         801 : MaybeHandle<String> Intl::StringLocaleConvertCase(Isolate* isolate,
     900             :                                                   Handle<String> s,
     901             :                                                   bool to_upper,
     902             :                                                   Handle<Object> locales) {
     903             :   std::vector<std::string> requested_locales;
     904        1602 :   if (!CanonicalizeLocaleList(isolate, locales, true).To(&requested_locales)) {
     905          36 :     return MaybeHandle<String>();
     906             :   }
     907         765 :   std::string requested_locale = requested_locales.size() == 0
     908             :                                      ? DefaultLocale(isolate)
     909         765 :                                      : requested_locales[0];
     910         765 :   size_t dash = requested_locale.find('-');
     911         765 :   if (dash != std::string::npos) {
     912         360 :     requested_locale = requested_locale.substr(0, dash);
     913             :   }
     914             : 
     915             :   // Primary language tag can be up to 8 characters long in theory.
     916             :   // https://tools.ietf.org/html/bcp47#section-2.2.1
     917             :   DCHECK_LE(requested_locale.length(), 8);
     918         765 :   s = String::Flatten(isolate, s);
     919             : 
     920             :   // All the languages requiring special-handling have two-letter codes.
     921             :   // Note that we have to check for '!= 2' here because private-use language
     922             :   // tags (x-foo) or grandfathered irregular tags (e.g. i-enochian) would have
     923             :   // only 'x' or 'i' when they get here.
     924         765 :   if (V8_UNLIKELY(requested_locale.length() != 2)) {
     925         135 :     if (to_upper) {
     926          45 :       return ConvertToUpper(isolate, s);
     927             :     }
     928          90 :     return ConvertToLower(isolate, s);
     929             :   }
     930             :   // TODO(jshin): Consider adding a fast path for ASCII or Latin-1. The fastpath
     931             :   // in the root locale needs to be adjusted for az, lt and tr because even case
     932             :   // mapping of ASCII range characters are different in those locales.
     933             :   // Greek (el) does not require any adjustment.
     934        1728 :   if (V8_UNLIKELY((requested_locale == "tr") || (requested_locale == "el") ||
     935             :                   (requested_locale == "lt") || (requested_locale == "az"))) {
     936         387 :     return LocaleConvertCase(isolate, s, to_upper, requested_locale.c_str());
     937             :   } else {
     938         243 :     if (to_upper) {
     939          81 :       return ConvertToUpper(isolate, s);
     940             :     }
     941         162 :     return ConvertToLower(isolate, s);
     942         801 :   }
     943             : }
     944             : 
     945       68751 : MaybeHandle<Object> Intl::StringLocaleCompare(Isolate* isolate,
     946             :                                               Handle<String> string1,
     947             :                                               Handle<String> string2,
     948             :                                               Handle<Object> locales,
     949             :                                               Handle<Object> options) {
     950             :   // We only cache the instance when both locales and options are undefined,
     951             :   // as that is the only case when the specified side-effects of examining
     952             :   // those arguments are unobservable.
     953             :   bool can_cache =
     954      264176 :       locales->IsUndefined(isolate) && options->IsUndefined(isolate);
     955       68751 :   if (can_cache) {
     956             :     // Both locales and options are undefined, check the cache.
     957             :     icu::Collator* cached_icu_collator =
     958             :         static_cast<icu::Collator*>(isolate->get_cached_icu_object(
     959       63337 :             Isolate::ICUObjectCacheType::kDefaultCollator));
     960             :     // We may use the cached icu::Collator for a fast path.
     961       63337 :     if (cached_icu_collator != nullptr) {
     962             :       return Intl::CompareStrings(isolate, *cached_icu_collator, string1,
     963       63298 :                                   string2);
     964             :     }
     965             :   }
     966             : 
     967             :   Handle<JSFunction> constructor = Handle<JSFunction>(
     968             :       JSFunction::cast(
     969       10906 :           isolate->context()->native_context()->intl_collator_function()),
     970       10906 :       isolate);
     971             : 
     972             :   Handle<JSCollator> collator;
     973       10906 :   ASSIGN_RETURN_ON_EXCEPTION(
     974             :       isolate, collator,
     975             :       New<JSCollator>(isolate, constructor, locales, options), Object);
     976        5453 :   if (can_cache) {
     977             :     isolate->set_icu_object_in_cache(
     978             :         Isolate::ICUObjectCacheType::kDefaultCollator,
     979             :         std::static_pointer_cast<icu::UObject>(
     980         156 :             collator->icu_collator()->get()));
     981             :   }
     982       10906 :   icu::Collator* icu_collator = collator->icu_collator()->raw();
     983        5453 :   return Intl::CompareStrings(isolate, *icu_collator, string1, string2);
     984             : }
     985             : 
     986             : // ecma402/#sec-collator-comparestrings
     987       74930 : Handle<Object> Intl::CompareStrings(Isolate* isolate,
     988             :                                     const icu::Collator& icu_collator,
     989             :                                     Handle<String> string1,
     990             :                                     Handle<String> string2) {
     991             :   Factory* factory = isolate->factory();
     992             : 
     993       74930 :   string1 = String::Flatten(isolate, string1);
     994       74930 :   string2 = String::Flatten(isolate, string2);
     995             : 
     996             :   UCollationResult result;
     997       74930 :   UErrorCode status = U_ZERO_ERROR;
     998       74930 :   icu::UnicodeString string_val1 = Intl::ToICUUnicodeString(isolate, string1);
     999      149860 :   icu::UnicodeString string_val2 = Intl::ToICUUnicodeString(isolate, string2);
    1000       74930 :   result = icu_collator.compare(string_val1, string_val2, status);
    1001             :   DCHECK(U_SUCCESS(status));
    1002             : 
    1003      149860 :   return factory->NewNumberFromInt(result);
    1004             : }
    1005             : 
    1006             : // ecma402/#sup-properties-of-the-number-prototype-object
    1007        1899 : MaybeHandle<String> Intl::NumberToLocaleString(Isolate* isolate,
    1008             :                                                Handle<Object> num,
    1009             :                                                Handle<Object> locales,
    1010             :                                                Handle<Object> options) {
    1011             :   Handle<Object> numeric_obj;
    1012        1899 :   if (FLAG_harmony_intl_bigint) {
    1013        1188 :     ASSIGN_RETURN_ON_EXCEPTION(isolate, numeric_obj,
    1014             :                                Object::ToNumeric(isolate, num), String);
    1015             :   } else {
    1016        2610 :     ASSIGN_RETURN_ON_EXCEPTION(isolate, numeric_obj,
    1017             :                                Object::ToNumber(isolate, num), String);
    1018             :   }
    1019             : 
    1020             :   // We only cache the instance when both locales and options are undefined,
    1021             :   // as that is the only case when the specified side-effects of examining
    1022             :   // those arguments are unobservable.
    1023             :   bool can_cache =
    1024        6318 :       locales->IsUndefined(isolate) && options->IsUndefined(isolate);
    1025        1899 :   if (can_cache) {
    1026             :     icu::NumberFormat* cached_number_format =
    1027             :         static_cast<icu::NumberFormat*>(isolate->get_cached_icu_object(
    1028        1260 :             Isolate::ICUObjectCacheType::kDefaultNumberFormat));
    1029             :     // We may use the cached icu::NumberFormat for a fast path.
    1030        1260 :     if (cached_number_format != nullptr) {
    1031             :       return JSNumberFormat::FormatNumeric(isolate, *cached_number_format,
    1032        1225 :                                            numeric_obj);
    1033             :     }
    1034             :   }
    1035             : 
    1036             :   Handle<JSFunction> constructor = Handle<JSFunction>(
    1037             :       JSFunction::cast(
    1038        1348 :           isolate->context()->native_context()->intl_number_format_function()),
    1039        1348 :       isolate);
    1040             :   Handle<JSNumberFormat> number_format;
    1041             :   // 2. Let numberFormat be ? Construct(%NumberFormat%, « locales, options »).
    1042        1348 :   ASSIGN_RETURN_ON_EXCEPTION(
    1043             :       isolate, number_format,
    1044             :       New<JSNumberFormat>(isolate, constructor, locales, options), String);
    1045             : 
    1046         665 :   if (can_cache) {
    1047             :     isolate->set_icu_object_in_cache(
    1048             :         Isolate::ICUObjectCacheType::kDefaultNumberFormat,
    1049             :         std::static_pointer_cast<icu::UObject>(
    1050         140 :             number_format->icu_number_format()->get()));
    1051             :   }
    1052             : 
    1053             :   // Return FormatNumber(numberFormat, x).
    1054             :   icu::NumberFormat* icu_number_format =
    1055        1330 :       number_format->icu_number_format()->raw();
    1056             :   return JSNumberFormat::FormatNumeric(isolate, *icu_number_format,
    1057         665 :                                        numeric_obj);
    1058             : }
    1059             : 
    1060             : namespace {
    1061             : 
    1062             : // ecma402/#sec-defaultnumberoption
    1063        6423 : Maybe<int> DefaultNumberOption(Isolate* isolate, Handle<Object> value, int min,
    1064             :                                int max, int fallback, Handle<String> property) {
    1065             :   // 2. Else, return fallback.
    1066       12846 :   if (value->IsUndefined()) return Just(fallback);
    1067             : 
    1068             :   // 1. If value is not undefined, then
    1069             :   // a. Let value be ? ToNumber(value).
    1070             :   Handle<Object> value_num;
    1071         936 :   ASSIGN_RETURN_ON_EXCEPTION_VALUE(
    1072             :       isolate, value_num, Object::ToNumber(isolate, value), Nothing<int>());
    1073             :   DCHECK(value_num->IsNumber());
    1074             : 
    1075             :   // b. If value is NaN or less than minimum or greater than maximum, throw a
    1076             :   // RangeError exception.
    1077        2277 :   if (value_num->IsNaN() || value_num->Number() < min ||
    1078         873 :       value_num->Number() > max) {
    1079         216 :     THROW_NEW_ERROR_RETURN_VALUE(
    1080             :         isolate,
    1081             :         NewRangeError(MessageTemplate::kPropertyValueOutOfRange, property),
    1082             :         Nothing<int>());
    1083             :   }
    1084             : 
    1085             :   // The max and min arguments are integers and the above check makes
    1086             :   // sure that we are within the integer range making this double to
    1087             :   // int conversion safe.
    1088             :   //
    1089             :   // c. Return floor(value).
    1090         720 :   return Just(FastD2I(floor(value_num->Number())));
    1091             : }
    1092             : 
    1093             : // ecma402/#sec-getnumberoption
    1094        6297 : Maybe<int> GetNumberOption(Isolate* isolate, Handle<JSReceiver> options,
    1095             :                            Handle<String> property, int min, int max,
    1096             :                            int fallback) {
    1097             :   // 1. Let value be ? Get(options, property).
    1098             :   Handle<Object> value;
    1099       12594 :   ASSIGN_RETURN_ON_EXCEPTION_VALUE(
    1100             :       isolate, value, JSReceiver::GetProperty(isolate, options, property),
    1101             :       Nothing<int>());
    1102             : 
    1103             :   // Return ? DefaultNumberOption(value, minimum, maximum, fallback).
    1104        6297 :   return DefaultNumberOption(isolate, value, min, max, fallback, property);
    1105             : }
    1106             : 
    1107        6297 : Maybe<int> GetNumberOption(Isolate* isolate, Handle<JSReceiver> options,
    1108             :                            const char* property, int min, int max,
    1109             :                            int fallback) {
    1110             :   Handle<String> property_str =
    1111        6297 :       isolate->factory()->NewStringFromAsciiChecked(property);
    1112        6297 :   return GetNumberOption(isolate, options, property_str, min, max, fallback);
    1113             : }
    1114             : 
    1115             : }  // namespace
    1116             : 
    1117        2132 : Maybe<bool> Intl::SetNumberFormatDigitOptions(Isolate* isolate,
    1118             :                                               icu::DecimalFormat* number_format,
    1119             :                                               Handle<JSReceiver> options,
    1120             :                                               int mnfd_default,
    1121             :                                               int mxfd_default) {
    1122        2132 :   CHECK_NOT_NULL(number_format);
    1123             : 
    1124             :   // 5. Let mnid be ? GetNumberOption(options, "minimumIntegerDigits,", 1, 21,
    1125             :   // 1).
    1126             :   int mnid;
    1127        2132 :   if (!GetNumberOption(isolate, options, "minimumIntegerDigits", 1, 21, 1)
    1128        4264 :            .To(&mnid)) {
    1129             :     return Nothing<bool>();
    1130             :   }
    1131             : 
    1132             :   // 6. Let mnfd be ? GetNumberOption(options, "minimumFractionDigits", 0, 20,
    1133             :   // mnfdDefault).
    1134             :   int mnfd;
    1135        2087 :   if (!GetNumberOption(isolate, options, "minimumFractionDigits", 0, 20,
    1136             :                        mnfd_default)
    1137        4174 :            .To(&mnfd)) {
    1138             :     return Nothing<bool>();
    1139             :   }
    1140             : 
    1141             :   // 7. Let mxfdActualDefault be max( mnfd, mxfdDefault ).
    1142        2078 :   int mxfd_actual_default = std::max(mnfd, mxfd_default);
    1143             : 
    1144             :   // 8. Let mxfd be ? GetNumberOption(options,
    1145             :   // "maximumFractionDigits", mnfd, 20, mxfdActualDefault).
    1146             :   int mxfd;
    1147        2078 :   if (!GetNumberOption(isolate, options, "maximumFractionDigits", mnfd, 20,
    1148             :                        mxfd_actual_default)
    1149        4156 :            .To(&mxfd)) {
    1150             :     return Nothing<bool>();
    1151             :   }
    1152             : 
    1153             :   // 9.  Let mnsd be ? Get(options, "minimumSignificantDigits").
    1154             :   Handle<Object> mnsd_obj;
    1155             :   Handle<String> mnsd_str =
    1156             :       isolate->factory()->minimumSignificantDigits_string();
    1157        4138 :   ASSIGN_RETURN_ON_EXCEPTION_VALUE(
    1158             :       isolate, mnsd_obj, JSReceiver::GetProperty(isolate, options, mnsd_str),
    1159             :       Nothing<bool>());
    1160             : 
    1161             :   // 10. Let mxsd be ? Get(options, "maximumSignificantDigits").
    1162             :   Handle<Object> mxsd_obj;
    1163             :   Handle<String> mxsd_str =
    1164             :       isolate->factory()->maximumSignificantDigits_string();
    1165        4138 :   ASSIGN_RETURN_ON_EXCEPTION_VALUE(
    1166             :       isolate, mxsd_obj, JSReceiver::GetProperty(isolate, options, mxsd_str),
    1167             :       Nothing<bool>());
    1168             : 
    1169             :   // 11. Set intlObj.[[MinimumIntegerDigits]] to mnid.
    1170        2069 :   number_format->setMinimumIntegerDigits(mnid);
    1171             : 
    1172             :   // 12. Set intlObj.[[MinimumFractionDigits]] to mnfd.
    1173        2069 :   number_format->setMinimumFractionDigits(mnfd);
    1174             : 
    1175             :   // 13. Set intlObj.[[MaximumFractionDigits]] to mxfd.
    1176        2069 :   number_format->setMaximumFractionDigits(mxfd);
    1177             : 
    1178             :   bool significant_digits_used = false;
    1179             :   // 14. If mnsd is not undefined or mxsd is not undefined, then
    1180        8186 :   if (!mnsd_obj->IsUndefined(isolate) || !mxsd_obj->IsUndefined(isolate)) {
    1181             :     // 14. a. Let mnsd be ? DefaultNumberOption(mnsd, 1, 21, 1).
    1182             :     int mnsd;
    1183         144 :     if (!DefaultNumberOption(isolate, mnsd_obj, 1, 21, 1, mnsd_str).To(&mnsd)) {
    1184             :       return Nothing<bool>();
    1185             :     }
    1186             : 
    1187             :     // 14. b. Let mxsd be ? DefaultNumberOption(mxsd, mnsd, 21, 21).
    1188             :     int mxsd;
    1189          54 :     if (!DefaultNumberOption(isolate, mxsd_obj, mnsd, 21, 21, mxsd_str)
    1190         108 :              .To(&mxsd)) {
    1191             :       return Nothing<bool>();
    1192             :     }
    1193             : 
    1194             :     significant_digits_used = true;
    1195             : 
    1196             :     // 14. c. Set intlObj.[[MinimumSignificantDigits]] to mnsd.
    1197          27 :     number_format->setMinimumSignificantDigits(mnsd);
    1198             : 
    1199             :     // 14. d. Set intlObj.[[MaximumSignificantDigits]] to mxsd.
    1200          27 :     number_format->setMaximumSignificantDigits(mxsd);
    1201             :   }
    1202             : 
    1203        2024 :   number_format->setSignificantDigitsUsed(significant_digits_used);
    1204        2024 :   number_format->setRoundingMode(icu::DecimalFormat::kRoundHalfUp);
    1205             :   return Just(true);
    1206             : }
    1207             : 
    1208             : namespace {
    1209             : 
    1210             : // ecma402/#sec-bestavailablelocale
    1211       13286 : std::string BestAvailableLocale(const std::set<std::string>& available_locales,
    1212             :                                 const std::string& locale) {
    1213             :   // 1. Let candidate be locale.
    1214       13286 :   std::string candidate = locale;
    1215             : 
    1216             :   // 2. Repeat,
    1217             :   while (true) {
    1218             :     // 2.a. If availableLocales contains an element equal to candidate, return
    1219             :     //      candidate.
    1220       13718 :     if (available_locales.find(candidate) != available_locales.end()) {
    1221             :       return candidate;
    1222             :     }
    1223             : 
    1224             :     // 2.b. Let pos be the character index of the last occurrence of "-"
    1225             :     //      (U+002D) within candidate. If that character does not occur, return
    1226             :     //      undefined.
    1227         675 :     size_t pos = candidate.rfind('-');
    1228         675 :     if (pos == std::string::npos) {
    1229             :       return std::string();
    1230             :     }
    1231             : 
    1232             :     // 2.c. If pos ≥ 2 and the character "-" occurs at index pos-2 of candidate,
    1233             :     //      decrease pos by 2.
    1234         810 :     if (pos >= 2 && candidate[pos - 2] == '-') {
    1235          90 :       pos -= 2;
    1236             :     }
    1237             : 
    1238             :     // 2.d. Let candidate be the substring of candidate from position 0,
    1239             :     //      inclusive, to position pos, exclusive.
    1240         864 :     candidate = candidate.substr(0, pos);
    1241             :   }
    1242             : }
    1243             : 
    1244       26572 : struct ParsedLocale {
    1245             :   std::string no_extensions_locale;
    1246             :   std::string extension;
    1247             : };
    1248             : 
    1249             : // Returns a struct containing a bcp47 tag without unicode extensions
    1250             : // and the removed unicode extensions.
    1251             : //
    1252             : // For example, given 'en-US-u-co-emoji' returns 'en-US' and
    1253             : // 'u-co-emoji'.
    1254       13286 : ParsedLocale ParseBCP47Locale(const std::string& locale) {
    1255             :   size_t length = locale.length();
    1256             :   ParsedLocale parsed_locale;
    1257             : 
    1258             :   // Privateuse or grandfathered locales have no extension sequences.
    1259       26572 :   if ((length > 1) && (locale[1] == '-')) {
    1260             :     // Check to make sure that this really is a grandfathered or
    1261             :     // privateuse extension. ICU can sometimes mess up the
    1262             :     // canonicalization.
    1263          63 :     CHECK(locale[0] == 'x' || locale[0] == 'i');
    1264          63 :     parsed_locale.no_extensions_locale = locale;
    1265             :     return parsed_locale;
    1266             :   }
    1267             : 
    1268       13223 :   size_t unicode_extension_start = locale.find("-u-");
    1269             : 
    1270             :   // No unicode extensions found.
    1271       13223 :   if (unicode_extension_start == std::string::npos) {
    1272       10194 :     parsed_locale.no_extensions_locale = locale;
    1273             :     return parsed_locale;
    1274             :   }
    1275             : 
    1276        3029 :   size_t private_extension_start = locale.find("-x-");
    1277             : 
    1278             :   // Unicode extensions found within privateuse subtags don't count.
    1279        6058 :   if (private_extension_start != std::string::npos &&
    1280        3029 :       private_extension_start < unicode_extension_start) {
    1281          36 :     parsed_locale.no_extensions_locale = locale;
    1282             :     return parsed_locale;
    1283             :   }
    1284             : 
    1285        2993 :   const std::string beginning = locale.substr(0, unicode_extension_start);
    1286             :   size_t unicode_extension_end = length;
    1287             :   DCHECK_GT(length, 2);
    1288             : 
    1289             :   // Find the end of the extension production as per the bcp47 grammar
    1290             :   // by looking for '-' followed by 2 chars and then another '-'.
    1291       24315 :   for (size_t i = unicode_extension_start + 1; i < length - 2; i++) {
    1292       21331 :     if (locale[i] != '-') continue;
    1293             : 
    1294       19902 :     if (locale[i + 2] == '-') {
    1295             :       unicode_extension_end = i;
    1296             :       break;
    1297             :     }
    1298             : 
    1299             :     i += 2;
    1300             :   }
    1301             : 
    1302        2993 :   const std::string end = locale.substr(unicode_extension_end);
    1303        5986 :   parsed_locale.no_extensions_locale = beginning + end;
    1304        5986 :   parsed_locale.extension = locale.substr(
    1305             :       unicode_extension_start, unicode_extension_end - unicode_extension_start);
    1306             :   return parsed_locale;
    1307             : }
    1308             : 
    1309             : // ecma402/#sec-lookupsupportedlocales
    1310         585 : std::vector<std::string> LookupSupportedLocales(
    1311             :     const std::set<std::string>& available_locales,
    1312             :     const std::vector<std::string>& requested_locales) {
    1313             :   // 1. Let subset be a new empty List.
    1314             :   std::vector<std::string> subset;
    1315             : 
    1316             :   // 2. For each element locale of requestedLocales in List order, do
    1317        1719 :   for (const std::string& locale : requested_locales) {
    1318             :     // 2. a. Let noExtensionsLocale be the String value that is locale
    1319             :     //       with all Unicode locale extension sequences removed.
    1320             :     std::string no_extension_locale =
    1321        1098 :         ParseBCP47Locale(locale).no_extensions_locale;
    1322             : 
    1323             :     // 2. b. Let availableLocale be
    1324             :     //       BestAvailableLocale(availableLocales, noExtensionsLocale).
    1325             :     std::string available_locale =
    1326         549 :         BestAvailableLocale(available_locales, no_extension_locale);
    1327             : 
    1328             :     // 2. c. If availableLocale is not undefined, append locale to the
    1329             :     //       end of subset.
    1330         549 :     if (!available_locale.empty()) {
    1331         441 :       subset.push_back(locale);
    1332             :     }
    1333             :   }
    1334             : 
    1335             :   // 3. Return subset.
    1336         585 :   return subset;
    1337             : }
    1338             : 
    1339             : // ECMA 402 9.2.8 BestFitSupportedLocales(availableLocales, requestedLocales)
    1340             : // https://tc39.github.io/ecma402/#sec-bestfitsupportedlocales
    1341             : std::vector<std::string> BestFitSupportedLocales(
    1342             :     const std::set<std::string>& available_locales,
    1343             :     const std::vector<std::string>& requested_locales) {
    1344         549 :   return LookupSupportedLocales(available_locales, requested_locales);
    1345             : }
    1346             : 
    1347             : // ecma262 #sec-createarrayfromlist
    1348         918 : Handle<JSArray> CreateArrayFromList(Isolate* isolate,
    1349        1710 :                                     std::vector<std::string> elements,
    1350             :                                     PropertyAttributes attr) {
    1351             :   Factory* factory = isolate->factory();
    1352             :   // Let array be ! ArrayCreate(0).
    1353         918 :   Handle<JSArray> array = factory->NewJSArray(0);
    1354             : 
    1355         918 :   uint32_t length = static_cast<uint32_t>(elements.size());
    1356             :   // 3. Let n be 0.
    1357             :   // 4. For each element e of elements, do
    1358        1710 :   for (uint32_t i = 0; i < length; i++) {
    1359             :     // a. Let status be CreateDataProperty(array, ! ToString(n), e).
    1360         792 :     const std::string& part = elements[i];
    1361             :     Handle<String> value =
    1362        1584 :         factory->NewStringFromUtf8(CStrVector(part.c_str())).ToHandleChecked();
    1363         792 :     JSObject::AddDataElement(array, i, value, attr);
    1364             :   }
    1365             :   // 5. Return array.
    1366         918 :   return array;
    1367             : }
    1368             : 
    1369             : // ECMA 402 9.2.9 SupportedLocales(availableLocales, requestedLocales, options)
    1370             : // https://tc39.github.io/ecma402/#sec-supportedlocales
    1371         621 : MaybeHandle<JSObject> SupportedLocales(
    1372             :     Isolate* isolate, const char* method,
    1373             :     const std::set<std::string>& available_locales,
    1374             :     const std::vector<std::string>& requested_locales, Handle<Object> options) {
    1375             :   std::vector<std::string> supported_locales;
    1376             : 
    1377             :   // 2. Else, let matcher be "best fit".
    1378             :   Intl::MatcherOption matcher = Intl::MatcherOption::kBestFit;
    1379             : 
    1380             :   // 1. If options is not undefined, then
    1381        1242 :   if (!options->IsUndefined(isolate)) {
    1382             :     // 1. a. Let options be ? ToObject(options).
    1383             :     Handle<JSReceiver> options_obj;
    1384         216 :     ASSIGN_RETURN_ON_EXCEPTION(isolate, options_obj,
    1385             :                                Object::ToObject(isolate, options), JSObject);
    1386             : 
    1387             :     // 1. b. Let matcher be ? GetOption(options, "localeMatcher", "string",
    1388             :     //       « "lookup", "best fit" », "best fit").
    1389             :     Maybe<Intl::MatcherOption> maybe_locale_matcher =
    1390         108 :         Intl::GetLocaleMatcher(isolate, options_obj, method);
    1391         108 :     MAYBE_RETURN(maybe_locale_matcher, MaybeHandle<JSObject>());
    1392             :     matcher = maybe_locale_matcher.FromJust();
    1393             :   }
    1394             : 
    1395             :   // 3. If matcher is "best fit", then
    1396             :   //    a. Let supportedLocales be BestFitSupportedLocales(availableLocales,
    1397             :   //       requestedLocales).
    1398         585 :   if (matcher == Intl::MatcherOption::kBestFit) {
    1399         549 :     supported_locales =
    1400             :         BestFitSupportedLocales(available_locales, requested_locales);
    1401             :   } else {
    1402             :     // 4. Else,
    1403             :     //    a. Let supportedLocales be LookupSupportedLocales(availableLocales,
    1404             :     //       requestedLocales).
    1405             :     DCHECK_EQ(matcher, Intl::MatcherOption::kLookup);
    1406          72 :     supported_locales =
    1407             :         LookupSupportedLocales(available_locales, requested_locales);
    1408             :   }
    1409             : 
    1410             :   // 5. Return CreateArrayFromList(supportedLocales).
    1411             :   PropertyAttributes attr = static_cast<PropertyAttributes>(NONE);
    1412         585 :   return CreateArrayFromList(isolate, supported_locales, attr);
    1413             : }
    1414             : 
    1415             : }  // namespace
    1416             : 
    1417             : // ecma-402 #sec-intl.getcanonicallocales
    1418         342 : MaybeHandle<JSArray> Intl::GetCanonicalLocales(Isolate* isolate,
    1419             :                                                Handle<Object> locales) {
    1420             :   // 1. Let ll be ? CanonicalizeLocaleList(locales).
    1421             :   Maybe<std::vector<std::string>> maybe_ll =
    1422         342 :       CanonicalizeLocaleList(isolate, locales, false);
    1423         342 :   MAYBE_RETURN(maybe_ll, MaybeHandle<JSArray>());
    1424             : 
    1425             :   // 2. Return CreateArrayFromList(ll).
    1426             :   PropertyAttributes attr = static_cast<PropertyAttributes>(NONE);
    1427         333 :   return CreateArrayFromList(isolate, maybe_ll.FromJust(), attr);
    1428             : }
    1429             : 
    1430             : // ECMA 402 Intl.*.supportedLocalesOf
    1431         657 : MaybeHandle<JSObject> Intl::SupportedLocalesOf(
    1432             :     Isolate* isolate, const char* method,
    1433             :     const std::set<std::string>& available_locales, Handle<Object> locales,
    1434             :     Handle<Object> options) {
    1435             :   // Let availableLocales be %Collator%.[[AvailableLocales]].
    1436             : 
    1437             :   // Let requestedLocales be ? CanonicalizeLocaleList(locales).
    1438             :   Maybe<std::vector<std::string>> requested_locales =
    1439         657 :       CanonicalizeLocaleList(isolate, locales, false);
    1440         657 :   MAYBE_RETURN(requested_locales, MaybeHandle<JSObject>());
    1441             : 
    1442             :   // Return ? SupportedLocales(availableLocales, requestedLocales, options).
    1443             :   return SupportedLocales(isolate, method, available_locales,
    1444         621 :                           requested_locales.FromJust(), options);
    1445             : }
    1446             : 
    1447             : namespace {
    1448             : template <typename T>
    1449         720 : bool IsValidExtension(const icu::Locale& locale, const char* key,
    1450             :                       const std::string& value) {
    1451         720 :   UErrorCode status = U_ZERO_ERROR;
    1452             :   std::unique_ptr<icu::StringEnumeration> enumeration(
    1453             :       T::getKeywordValuesForLocale(key, icu::Locale(locale.getBaseName()),
    1454         720 :                                    false, status));
    1455         720 :   if (U_SUCCESS(status)) {
    1456             :     int32_t length;
    1457         720 :     std::string legacy_type(uloc_toLegacyType(key, value.c_str()));
    1458        5868 :     for (const char* item = enumeration->next(&length, status); item != nullptr;
    1459        5148 :          item = enumeration->next(&length, status)) {
    1460       11610 :       if (U_SUCCESS(status) && legacy_type == item) {
    1461             :         return true;
    1462             :       }
    1463             :     }
    1464             :   }
    1465             :   return false;
    1466             : }
    1467             : 
    1468             : bool IsValidCalendar(const icu::Locale& locale, const std::string& value) {
    1469         603 :   return IsValidExtension<icu::Calendar>(locale, "calendar", value);
    1470             : }
    1471             : 
    1472         189 : bool IsValidCollation(const icu::Locale& locale, const std::string& value) {
    1473         378 :   std::set<std::string> invalid_values = {"standard", "search"};
    1474         189 :   if (invalid_values.find(value) != invalid_values.end()) return false;
    1475         117 :   return IsValidExtension<icu::Collator>(locale, "collation", value);
    1476             : }
    1477             : 
    1478        1386 : bool IsValidNumberingSystem(const std::string& value) {
    1479        2772 :   std::set<std::string> invalid_values = {"native", "traditio", "finance"};
    1480        1386 :   if (invalid_values.find(value) != invalid_values.end()) return false;
    1481        1332 :   UErrorCode status = U_ZERO_ERROR;
    1482             :   std::unique_ptr<icu::NumberingSystem> numbering_system(
    1483        1332 :       icu::NumberingSystem::createInstanceByName(value.c_str(), status));
    1484        1332 :   return U_SUCCESS(status) && numbering_system.get() != nullptr;
    1485             : }
    1486             : 
    1487       14493 : std::map<std::string, std::string> LookupAndValidateUnicodeExtensions(
    1488             :     icu::Locale* icu_locale, const std::set<std::string>& relevant_keys) {
    1489             :   std::map<std::string, std::string> extensions;
    1490             : 
    1491       14493 :   UErrorCode status = U_ZERO_ERROR;
    1492             :   std::unique_ptr<icu::StringEnumeration> keywords(
    1493       14493 :       icu_locale->createKeywords(status));
    1494       14493 :   if (U_FAILURE(status)) return extensions;
    1495             : 
    1496       14493 :   if (!keywords) return extensions;
    1497             :   char value[ULOC_FULLNAME_CAPACITY];
    1498             : 
    1499             :   int32_t length;
    1500        2957 :   status = U_ZERO_ERROR;
    1501        7645 :   for (const char* keyword = keywords->next(&length, status);
    1502        4688 :        keyword != nullptr; keyword = keywords->next(&length, status)) {
    1503             :     // Ignore failures in ICU and skip to the next keyword.
    1504             :     //
    1505             :     // This is fine.™
    1506        4688 :     if (U_FAILURE(status)) {
    1507           0 :       status = U_ZERO_ERROR;
    1508           0 :       continue;
    1509             :     }
    1510             : 
    1511        4688 :     icu_locale->getKeywordValue(keyword, value, ULOC_FULLNAME_CAPACITY, status);
    1512             : 
    1513             :     // Ignore failures in ICU and skip to the next keyword.
    1514             :     //
    1515             :     // This is fine.™
    1516        4688 :     if (U_FAILURE(status)) {
    1517           0 :       status = U_ZERO_ERROR;
    1518           0 :       continue;
    1519             :     }
    1520             : 
    1521        4688 :     const char* bcp47_key = uloc_toUnicodeLocaleKey(keyword);
    1522             : 
    1523       18725 :     if (bcp47_key && (relevant_keys.find(bcp47_key) != relevant_keys.end())) {
    1524        2426 :       const char* bcp47_value = uloc_toUnicodeLocaleType(bcp47_key, value);
    1525             :       bool is_valid_value = false;
    1526             :       // 8.h.ii.1.a If keyLocaleData contains requestedValue, then
    1527        2426 :       if (strcmp("ca", bcp47_key) == 0) {
    1528        1206 :         is_valid_value = IsValidCalendar(*icu_locale, bcp47_value);
    1529        1823 :       } else if (strcmp("co", bcp47_key) == 0) {
    1530         378 :         is_valid_value = IsValidCollation(*icu_locale, bcp47_value);
    1531        1634 :       } else if (strcmp("hc", bcp47_key) == 0) {
    1532             :         // https://www.unicode.org/repos/cldr/tags/latest/common/bcp47/calendar.xml
    1533         396 :         std::set<std::string> valid_values = {"h11", "h12", "h23", "h24"};
    1534         396 :         is_valid_value = valid_values.find(bcp47_value) != valid_values.end();
    1535        1436 :       } else if (strcmp("lb", bcp47_key) == 0) {
    1536             :         // https://www.unicode.org/repos/cldr/tags/latest/common/bcp47/segmentation.xml
    1537           0 :         std::set<std::string> valid_values = {"strict", "normal", "loose"};
    1538           0 :         is_valid_value = valid_values.find(bcp47_value) != valid_values.end();
    1539        1436 :       } else if (strcmp("kn", bcp47_key) == 0) {
    1540             :         // https://www.unicode.org/repos/cldr/tags/latest/common/bcp47/collation.xml
    1541          50 :         std::set<std::string> valid_values = {"true", "false"};
    1542          50 :         is_valid_value = valid_values.find(bcp47_value) != valid_values.end();
    1543        1411 :       } else if (strcmp("kf", bcp47_key) == 0) {
    1544             :         // https://www.unicode.org/repos/cldr/tags/latest/common/bcp47/collation.xml
    1545          50 :         std::set<std::string> valid_values = {"upper", "lower", "false"};
    1546          50 :         is_valid_value = valid_values.find(bcp47_value) != valid_values.end();
    1547        1386 :       } else if (strcmp("nu", bcp47_key) == 0) {
    1548        2772 :         is_valid_value = IsValidNumberingSystem(bcp47_value);
    1549             :       }
    1550        2426 :       if (is_valid_value) {
    1551             :         extensions.insert(
    1552        4034 :             std::pair<std::string, std::string>(bcp47_key, bcp47_value));
    1553        2017 :         continue;
    1554             :       }
    1555             :     }
    1556        2671 :     status = U_ZERO_ERROR;
    1557        2671 :     icu_locale->setKeywordValue(keyword, nullptr, status);
    1558        5342 :     CHECK(U_SUCCESS(status));
    1559             :   }
    1560             : 
    1561             :   return extensions;
    1562             : }
    1563             : 
    1564             : // ecma402/#sec-lookupmatcher
    1565       14493 : std::string LookupMatcher(Isolate* isolate,
    1566             :                           const std::set<std::string>& available_locales,
    1567             :                           const std::vector<std::string>& requested_locales) {
    1568             :   // 1. Let result be a new Record.
    1569             :   std::string result;
    1570             : 
    1571             :   // 2. For each element locale of requestedLocales in List order, do
    1572       29121 :   for (const std::string& locale : requested_locales) {
    1573             :     // 2. a. Let noExtensionsLocale be the String value that is locale
    1574             :     //       with all Unicode locale extension sequences removed.
    1575       12737 :     ParsedLocale parsed_locale = ParseBCP47Locale(locale);
    1576       12737 :     std::string no_extensions_locale = parsed_locale.no_extensions_locale;
    1577             : 
    1578             :     // 2. b. Let availableLocale be
    1579             :     //       BestAvailableLocale(availableLocales, noExtensionsLocale).
    1580             :     std::string available_locale =
    1581       12737 :         BestAvailableLocale(available_locales, no_extensions_locale);
    1582             : 
    1583             :     // 2. c. If availableLocale is not undefined, append locale to the
    1584             :     //       end of subset.
    1585       12737 :     if (!available_locale.empty()) {
    1586             :       // Note: The following steps are not performed here because we
    1587             :       // can use ICU to parse the unicode locale extension sequence
    1588             :       // as part of Intl::ResolveLocale.
    1589             :       //
    1590             :       // There's no need to separate the unicode locale extensions
    1591             :       // right here. Instead just return the available locale with the
    1592             :       // extensions.
    1593             :       //
    1594             :       // 2. c. i. Set result.[[locale]] to availableLocale.
    1595             :       // 2. c. ii. If locale and noExtensionsLocale are not the same
    1596             :       // String value, then
    1597             :       // 2. c. ii. 1. Let extension be the String value consisting of
    1598             :       // the first substring of locale that is a Unicode locale
    1599             :       // extension sequence.
    1600             :       // 2. c. ii. 2. Set result.[[extension]] to extension.
    1601             :       // 2. c. iii. Return result.
    1602             :       return available_locale + parsed_locale.extension;
    1603             :     }
    1604         135 :   }
    1605             : 
    1606             :   // 3. Let defLocale be DefaultLocale();
    1607             :   // 4. Set result.[[locale]] to defLocale.
    1608             :   // 5. Return result.
    1609        1891 :   return DefaultLocale(isolate);
    1610             : }
    1611             : 
    1612             : }  // namespace
    1613             : 
    1614             : // This function doesn't correspond exactly with the spec. Instead
    1615             : // we use ICU to do all the string manipulations that the spec
    1616             : // peforms.
    1617             : //
    1618             : // The spec uses this function to normalize values for various
    1619             : // relevant extension keys (such as disallowing "search" for
    1620             : // collation). Instead of doing this here, we let the callers of
    1621             : // this method perform such normalization.
    1622             : //
    1623             : // ecma402/#sec-resolvelocale
    1624       14493 : Intl::ResolvedLocale Intl::ResolveLocale(
    1625             :     Isolate* isolate, const std::set<std::string>& available_locales,
    1626             :     const std::vector<std::string>& requested_locales, MatcherOption matcher,
    1627             :     const std::set<std::string>& relevant_extension_keys) {
    1628             :   std::string locale;
    1629       14493 :   if (matcher == Intl::MatcherOption::kLookup) {
    1630       28662 :     locale = LookupMatcher(isolate, available_locales, requested_locales);
    1631         162 :   } else if (matcher == Intl::MatcherOption::kBestFit) {
    1632             :     // TODO(intl): Implement better lookup algorithm.
    1633         324 :     locale = LookupMatcher(isolate, available_locales, requested_locales);
    1634             :   }
    1635             : 
    1636       28986 :   icu::Locale icu_locale = CreateICULocale(locale);
    1637             :   std::map<std::string, std::string> extensions =
    1638       14493 :       LookupAndValidateUnicodeExtensions(&icu_locale, relevant_extension_keys);
    1639             : 
    1640       28986 :   std::string canonicalized_locale = Intl::ToLanguageTag(icu_locale).FromJust();
    1641             : 
    1642             :   // TODO(gsathya): Remove privateuse subtags from extensions.
    1643             : 
    1644       28986 :   return Intl::ResolvedLocale{canonicalized_locale, icu_locale, extensions};
    1645             : }
    1646             : 
    1647        2813 : Managed<icu::UnicodeString> Intl::SetTextToBreakIterator(
    1648             :     Isolate* isolate, Handle<String> text, icu::BreakIterator* break_iterator) {
    1649             :   icu::UnicodeString* u_text =
    1650        2813 :       (icu::UnicodeString*)(Intl::ToICUUnicodeString(isolate, text).clone());
    1651             : 
    1652             :   Handle<Managed<icu::UnicodeString>> new_u_text =
    1653        2813 :       Managed<icu::UnicodeString>::FromRawPtr(isolate, 0, u_text);
    1654             : 
    1655        2813 :   break_iterator->setText(*u_text);
    1656        2813 :   return *new_u_text;
    1657             : }
    1658             : 
    1659             : // ecma262 #sec-string.prototype.normalize
    1660        2313 : MaybeHandle<String> Intl::Normalize(Isolate* isolate, Handle<String> string,
    1661             :                                     Handle<Object> form_input) {
    1662             :   const char* form_name;
    1663             :   UNormalization2Mode form_mode;
    1664        4626 :   if (form_input->IsUndefined(isolate)) {
    1665             :     // default is FNC
    1666             :     form_name = "nfc";
    1667             :     form_mode = UNORM2_COMPOSE;
    1668             :   } else {
    1669             :     Handle<String> form;
    1670        3780 :     ASSIGN_RETURN_ON_EXCEPTION(isolate, form,
    1671             :                                Object::ToString(isolate, form_input), String);
    1672             : 
    1673        1890 :     if (String::Equals(isolate, form, isolate->factory()->NFC_string())) {
    1674             :       form_name = "nfc";
    1675             :       form_mode = UNORM2_COMPOSE;
    1676        1476 :     } else if (String::Equals(isolate, form,
    1677             :                               isolate->factory()->NFD_string())) {
    1678             :       form_name = "nfc";
    1679             :       form_mode = UNORM2_DECOMPOSE;
    1680        1071 :     } else if (String::Equals(isolate, form,
    1681             :                               isolate->factory()->NFKC_string())) {
    1682             :       form_name = "nfkc";
    1683             :       form_mode = UNORM2_COMPOSE;
    1684         657 :     } else if (String::Equals(isolate, form,
    1685             :                               isolate->factory()->NFKD_string())) {
    1686             :       form_name = "nfkc";
    1687             :       form_mode = UNORM2_DECOMPOSE;
    1688             :     } else {
    1689             :       Handle<String> valid_forms =
    1690          99 :           isolate->factory()->NewStringFromStaticChars("NFC, NFD, NFKC, NFKD");
    1691          99 :       THROW_NEW_ERROR(
    1692             :           isolate,
    1693             :           NewRangeError(MessageTemplate::kNormalizationForm, valid_forms),
    1694             :           String);
    1695             :     }
    1696             :   }
    1697             : 
    1698             :   int length = string->length();
    1699        2214 :   string = String::Flatten(isolate, string);
    1700             :   icu::UnicodeString result;
    1701             :   std::unique_ptr<uc16[]> sap;
    1702        2214 :   UErrorCode status = U_ZERO_ERROR;
    1703        4428 :   icu::UnicodeString input = ToICUUnicodeString(isolate, string);
    1704             :   // Getting a singleton. Should not free it.
    1705             :   const icu::Normalizer2* normalizer =
    1706        2214 :       icu::Normalizer2::getInstance(nullptr, form_name, form_mode, status);
    1707             :   DCHECK(U_SUCCESS(status));
    1708        2214 :   CHECK_NOT_NULL(normalizer);
    1709             :   int32_t normalized_prefix_length =
    1710        2214 :       normalizer->spanQuickCheckYes(input, status);
    1711             :   // Quick return if the input is already normalized.
    1712        2214 :   if (length == normalized_prefix_length) return string;
    1713             :   icu::UnicodeString unnormalized =
    1714        1926 :       input.tempSubString(normalized_prefix_length);
    1715             :   // Read-only alias of the normalized prefix.
    1716        1926 :   result.setTo(false, input.getBuffer(), normalized_prefix_length);
    1717             :   // copy-on-write; normalize the suffix and append to |result|.
    1718         963 :   normalizer->normalizeSecondAndAppend(result, unnormalized, status);
    1719             : 
    1720         963 :   if (U_FAILURE(status)) {
    1721           0 :     THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kIcuError), String);
    1722             :   }
    1723             : 
    1724        3177 :   return Intl::ToString(isolate, result);
    1725             : }
    1726             : 
    1727             : // ICUTimezoneCache calls out to ICU for TimezoneCache
    1728             : // functionality in a straightforward way.
    1729             : class ICUTimezoneCache : public base::TimezoneCache {
    1730             :  public:
    1731      122086 :   ICUTimezoneCache() : timezone_(nullptr) { Clear(TimeZoneDetection::kSkip); }
    1732             : 
    1733      183087 :   ~ICUTimezoneCache() override { Clear(TimeZoneDetection::kSkip); }
    1734             : 
    1735             :   const char* LocalTimezone(double time_ms) override;
    1736             : 
    1737             :   double DaylightSavingsOffset(double time_ms) override;
    1738             : 
    1739             :   double LocalTimeOffset(double time_ms, bool is_utc) override;
    1740             : 
    1741             :   void Clear(TimeZoneDetection time_zone_detection) override;
    1742             : 
    1743             :  private:
    1744             :   icu::TimeZone* GetTimeZone();
    1745             : 
    1746             :   bool GetOffsets(double time_ms, bool is_utc, int32_t* raw_offset,
    1747             :                   int32_t* dst_offset);
    1748             : 
    1749             :   icu::TimeZone* timezone_;
    1750             : 
    1751             :   std::string timezone_name_;
    1752             :   std::string dst_timezone_name_;
    1753             : };
    1754             : 
    1755         130 : const char* ICUTimezoneCache::LocalTimezone(double time_ms) {
    1756         130 :   bool is_dst = DaylightSavingsOffset(time_ms) != 0;
    1757         130 :   std::string* name = is_dst ? &dst_timezone_name_ : &timezone_name_;
    1758         130 :   if (name->empty()) {
    1759             :     icu::UnicodeString result;
    1760         260 :     GetTimeZone()->getDisplayName(is_dst, icu::TimeZone::LONG, result);
    1761             :     result += '\0';
    1762             : 
    1763             :     icu::StringByteSink<std::string> byte_sink(name);
    1764         260 :     result.toUTF8(byte_sink);
    1765             :   }
    1766             :   DCHECK(!name->empty());
    1767         130 :   return name->c_str();
    1768             : }
    1769             : 
    1770           0 : icu::TimeZone* ICUTimezoneCache::GetTimeZone() {
    1771      135312 :   if (timezone_ == nullptr) {
    1772         215 :     timezone_ = icu::TimeZone::createDefault();
    1773             :   }
    1774      135312 :   return timezone_;
    1775             : }
    1776             : 
    1777      135182 : bool ICUTimezoneCache::GetOffsets(double time_ms, bool is_utc,
    1778             :                                   int32_t* raw_offset, int32_t* dst_offset) {
    1779      135182 :   UErrorCode status = U_ZERO_ERROR;
    1780             :   // TODO(jshin): ICU TimeZone class handles skipped time differently from
    1781             :   // Ecma 262 (https://github.com/tc39/ecma262/pull/778) and icu::TimeZone
    1782             :   // class does not expose the necessary API. Fixing
    1783             :   // http://bugs.icu-project.org/trac/ticket/13268 would make it easy to
    1784             :   // implement the proposed spec change. A proposed fix for ICU is
    1785             :   //    https://chromium-review.googlesource.com/851265 .
    1786             :   // In the meantime, use an internal (still public) API of icu::BasicTimeZone.
    1787             :   // Once it's accepted by the upstream, get rid of cast. Note that casting
    1788             :   // TimeZone to BasicTimeZone is safe because we know that icu::TimeZone used
    1789             :   // here is a BasicTimeZone.
    1790      135182 :   if (is_utc) {
    1791      132310 :     GetTimeZone()->getOffset(time_ms, false, *raw_offset, *dst_offset, status);
    1792             :   } else {
    1793             :     static_cast<const icu::BasicTimeZone*>(GetTimeZone())
    1794             :         ->getOffsetFromLocal(time_ms, icu::BasicTimeZone::kFormer,
    1795             :                              icu::BasicTimeZone::kFormer, *raw_offset,
    1796        2872 :                              *dst_offset, status);
    1797             :   }
    1798             : 
    1799      270364 :   return U_SUCCESS(status);
    1800             : }
    1801             : 
    1802       19216 : double ICUTimezoneCache::DaylightSavingsOffset(double time_ms) {
    1803             :   int32_t raw_offset, dst_offset;
    1804       19216 :   if (!GetOffsets(time_ms, true, &raw_offset, &dst_offset)) return 0;
    1805       19216 :   return dst_offset;
    1806             : }
    1807             : 
    1808      115966 : double ICUTimezoneCache::LocalTimeOffset(double time_ms, bool is_utc) {
    1809             :   int32_t raw_offset, dst_offset;
    1810      115966 :   if (!GetOffsets(time_ms, is_utc, &raw_offset, &dst_offset)) return 0;
    1811      115966 :   return raw_offset + dst_offset;
    1812             : }
    1813             : 
    1814      183116 : void ICUTimezoneCache::Clear(TimeZoneDetection time_zone_detection) {
    1815      183116 :   delete timezone_;
    1816      183116 :   timezone_ = nullptr;
    1817      183116 :   timezone_name_.clear();
    1818      183114 :   dst_timezone_name_.clear();
    1819      183116 :   if (time_zone_detection == TimeZoneDetection::kRedetect) {
    1820           0 :     icu::TimeZone::adoptDefault(icu::TimeZone::detectHostTimeZone());
    1821             :   }
    1822      183116 : }
    1823             : 
    1824       61052 : base::TimezoneCache* Intl::CreateTimeZoneCache() {
    1825             :   return FLAG_icu_timezone_data ? new ICUTimezoneCache()
    1826       61062 :                                 : base::OS::CreateTimezoneCache();
    1827             : }
    1828             : 
    1829        6075 : Maybe<Intl::CaseFirst> Intl::GetCaseFirst(Isolate* isolate,
    1830             :                                           Handle<JSReceiver> options,
    1831             :                                           const char* method) {
    1832             :   return Intl::GetStringOption<Intl::CaseFirst>(
    1833             :       isolate, options, "caseFirst", method, {"upper", "lower", "false"},
    1834             :       {Intl::CaseFirst::kUpper, Intl::CaseFirst::kLower,
    1835             :        Intl::CaseFirst::kFalse},
    1836       18225 :       Intl::CaseFirst::kUndefined);
    1837             : }
    1838             : 
    1839        3693 : Maybe<Intl::HourCycle> Intl::GetHourCycle(Isolate* isolate,
    1840             :                                           Handle<JSReceiver> options,
    1841             :                                           const char* method) {
    1842             :   return Intl::GetStringOption<Intl::HourCycle>(
    1843             :       isolate, options, "hourCycle", method, {"h11", "h12", "h23", "h24"},
    1844             :       {Intl::HourCycle::kH11, Intl::HourCycle::kH12, Intl::HourCycle::kH23,
    1845             :        Intl::HourCycle::kH24},
    1846       11079 :       Intl::HourCycle::kUndefined);
    1847             : }
    1848             : 
    1849       14637 : Maybe<Intl::MatcherOption> Intl::GetLocaleMatcher(Isolate* isolate,
    1850             :                                                   Handle<JSReceiver> options,
    1851             :                                                   const char* method) {
    1852             :   return Intl::GetStringOption<Intl::MatcherOption>(
    1853             :       isolate, options, "localeMatcher", method, {"best fit", "lookup"},
    1854             :       {Intl::MatcherOption::kLookup, Intl::MatcherOption::kBestFit},
    1855       43911 :       Intl::MatcherOption::kLookup);
    1856             : }
    1857             : 
    1858         108 : Intl::HourCycle Intl::ToHourCycle(const std::string& hc) {
    1859         108 :   if (hc == "h11") return Intl::HourCycle::kH11;
    1860          81 :   if (hc == "h12") return Intl::HourCycle::kH12;
    1861          54 :   if (hc == "h23") return Intl::HourCycle::kH23;
    1862          27 :   if (hc == "h24") return Intl::HourCycle::kH24;
    1863           0 :   return Intl::HourCycle::kUndefined;
    1864             : }
    1865             : 
    1866        1221 : const std::set<std::string>& Intl::GetAvailableLocalesForLocale() {
    1867             :   static base::LazyInstance<Intl::AvailableLocales<icu::Locale>>::type
    1868             :       available_locales = LAZY_INSTANCE_INITIALIZER;
    1869        1221 :   return available_locales.Pointer()->Get();
    1870             : }
    1871             : 
    1872        4621 : const std::set<std::string>& Intl::GetAvailableLocalesForDateFormat() {
    1873             :   static base::LazyInstance<Intl::AvailableLocales<icu::DateFormat>>::type
    1874             :       available_locales = LAZY_INSTANCE_INITIALIZER;
    1875        4621 :   return available_locales.Pointer()->Get();
    1876             : }
    1877             : 
    1878             : }  // namespace internal
    1879      178779 : }  // namespace v8

Generated by: LCOV version 1.10