[Intl] Remove intl.(h|cc)

Fold methods from intl.* to objects/intl-objects.*
Move Isolate* to the first parameter for some method
Move ICUSerice type under Intl
Hide ICUTimeZoneCache under a CreateTimeZoneCache factory method.

Bug: v8:5751
Cq-Include-Trybots: luci.v8.try:v8_linux_noi18n_rel_ng
Change-Id: Ie6f6a1ceee789333a077c1965de8e11d8c15c175
Reviewed-on: https://chromium-review.googlesource.com/c/1293109
Commit-Queue: Frank Tang <ftang@chromium.org>
Reviewed-by: Sathya Gunasekaran <gsathya@chromium.org>
Cr-Commit-Position: refs/heads/master@{#56873}
This commit is contained in:
Frank Tang 2018-10-23 06:02:57 +08:00 committed by Commit Bot
parent 66daabcca9
commit c4311e52f7
19 changed files with 553 additions and 625 deletions

View File

@ -2150,8 +2150,6 @@ v8_source_set("v8_base") {
"src/interpreter/interpreter-intrinsics.h",
"src/interpreter/interpreter.cc",
"src/interpreter/interpreter.h",
"src/intl.cc",
"src/intl.h",
"src/isolate-data.h",
"src/isolate-inl.h",
"src/isolate.cc",
@ -2976,8 +2974,6 @@ v8_source_set("v8_base") {
sources -= [
"src/builtins/builtins-intl.cc",
"src/char-predicates.cc",
"src/intl.cc",
"src/intl.h",
"src/objects/intl-objects.cc",
"src/objects/intl-objects.h",
"src/objects/js-break-iterator-inl.h",

View File

@ -14,7 +14,6 @@
#include "src/builtins/builtins.h"
#include "src/date.h"
#include "src/elements.h"
#include "src/intl.h"
#include "src/objects-inl.h"
#include "src/objects/intl-objects.h"
#include "src/objects/js-array-inl.h"
@ -50,7 +49,7 @@ BUILTIN(StringPrototypeToUpperCaseIntl) {
HandleScope scope(isolate);
TO_THIS_STRING(string, "String.prototype.toUpperCase");
string = String::Flatten(isolate, string);
RETURN_RESULT_OR_FAILURE(isolate, ConvertCase(string, true, isolate));
RETURN_RESULT_OR_FAILURE(isolate, Intl::ConvertToUpper(isolate, string));
}
BUILTIN(StringPrototypeNormalizeIntl) {
@ -98,11 +97,7 @@ BUILTIN(StringPrototypeNormalizeIntl) {
icu::UnicodeString result;
std::unique_ptr<uc16[]> sap;
UErrorCode status = U_ZERO_ERROR;
{
DisallowHeapAllocation no_gc;
String::FlatContent flat = string->GetFlatContent();
const UChar* src = GetUCharBufferFromFlat(flat, &sap, length);
icu::UnicodeString input(false, src, length);
icu::UnicodeString input = Intl::ToICUUnicodeString(isolate, string);
// Getting a singleton. Should not free it.
const icu::Normalizer2* normalizer =
icu::Normalizer2::getInstance(nullptr, form_name, form_mode, status);
@ -118,7 +113,6 @@ BUILTIN(StringPrototypeNormalizeIntl) {
result.setTo(false, input.getBuffer(), normalized_prefix_length);
// copy-on-write; normalize the suffix and append to |result|.
normalizer->normalizeSecondAndAppend(result, unnormalized, status);
}
if (U_FAILURE(status)) {
THROW_NEW_ERROR_RETURN_FAILURE(isolate,
@ -137,7 +131,8 @@ BUILTIN(V8BreakIteratorSupportedLocalesOf) {
Handle<Object> options = args.atOrUndefined(isolate, 2);
RETURN_RESULT_OR_FAILURE(
isolate, Intl::SupportedLocalesOf(isolate, ICUService::kBreakIterator,
isolate,
Intl::SupportedLocalesOf(isolate, Intl::ICUService::kBreakIterator,
locales, options));
}
@ -147,8 +142,8 @@ BUILTIN(NumberFormatSupportedLocalesOf) {
Handle<Object> options = args.atOrUndefined(isolate, 2);
RETURN_RESULT_OR_FAILURE(
isolate, Intl::SupportedLocalesOf(isolate, ICUService::kNumberFormat,
locales, options));
isolate, Intl::SupportedLocalesOf(
isolate, Intl::ICUService::kNumberFormat, locales, options));
}
BUILTIN(NumberFormatPrototypeFormatToParts) {
@ -189,7 +184,7 @@ BUILTIN(DateTimeFormatSupportedLocalesOf) {
Handle<Object> options = args.atOrUndefined(isolate, 2);
RETURN_RESULT_OR_FAILURE(
isolate, Intl::SupportedLocalesOf(isolate, ICUService::kDateFormat,
isolate, Intl::SupportedLocalesOf(isolate, Intl::ICUService::kDateFormat,
locales, options));
}
@ -540,7 +535,8 @@ BUILTIN(ListFormatSupportedLocalesOf) {
Handle<Object> options = args.atOrUndefined(isolate, 2);
RETURN_RESULT_OR_FAILURE(
isolate, Intl::SupportedLocalesOf(isolate, ICUService::kListFormatter,
isolate,
Intl::SupportedLocalesOf(isolate, Intl::ICUService::kListFormatter,
locales, options));
}
@ -647,8 +643,8 @@ BUILTIN(RelativeTimeFormatSupportedLocalesOf) {
Handle<Object> options = args.atOrUndefined(isolate, 2);
RETURN_RESULT_OR_FAILURE(
isolate,
Intl::SupportedLocalesOf(isolate, ICUService::kRelativeDateTimeFormatter,
isolate, Intl::SupportedLocalesOf(
isolate, Intl::ICUService::kRelativeDateTimeFormatter,
locales, options));
}
@ -911,7 +907,7 @@ BUILTIN(PluralRulesSupportedLocalesOf) {
Handle<Object> options = args.atOrUndefined(isolate, 2);
RETURN_RESULT_OR_FAILURE(
isolate, Intl::SupportedLocalesOf(isolate, ICUService::kPluralRules,
isolate, Intl::SupportedLocalesOf(isolate, Intl::ICUService::kPluralRules,
locales, options));
}
@ -961,8 +957,8 @@ BUILTIN(CollatorSupportedLocalesOf) {
Handle<Object> options = args.atOrUndefined(isolate, 2);
RETURN_RESULT_OR_FAILURE(
isolate, Intl::SupportedLocalesOf(isolate, ICUService::kCollator, locales,
options));
isolate, Intl::SupportedLocalesOf(isolate, Intl::ICUService::kCollator,
locales, options));
}
BUILTIN(CollatorPrototypeCompare) {
@ -1117,7 +1113,7 @@ BUILTIN(SegmenterSupportedLocalesOf) {
Handle<Object> options = args.atOrUndefined(isolate, 2);
RETURN_RESULT_OR_FAILURE(
isolate, Intl::SupportedLocalesOf(isolate, ICUService::kSegmenter,
isolate, Intl::SupportedLocalesOf(isolate, Intl::ICUService::kSegmenter,
locales, options));
}

View File

@ -6,11 +6,10 @@
#include "src/conversions.h"
#include "src/objects-inl.h"
#include "src/objects.h"
#ifdef V8_INTL_SUPPORT
#include "src/intl.h"
#include "src/objects/intl-objects.h"
#endif
#include "src/objects.h"
namespace v8 {
namespace internal {
@ -30,8 +29,7 @@ DateCache::DateCache()
: stamp_(nullptr),
tz_cache_(
#ifdef V8_INTL_SUPPORT
FLAG_icu_timezone_data ? new ICUTimezoneCache()
: base::OS::CreateTimezoneCache()
Intl::CreateTimeZoneCache()
#else
base::OS::CreateTimezoneCache()
#endif

View File

@ -47,7 +47,7 @@
#endif // V8_INTERPRETED_REGEXP
#ifdef V8_INTL_SUPPORT
#include "src/intl.h"
#include "src/objects/intl-objects.h"
#endif // V8_INTL_SUPPORT
namespace v8 {
@ -815,11 +815,12 @@ ExternalReference ExternalReference::check_object_type() {
#ifdef V8_INTL_SUPPORT
ExternalReference ExternalReference::intl_convert_one_byte_to_lower() {
return ExternalReference(Redirect(FUNCTION_ADDR(ConvertOneByteToLower)));
return ExternalReference(
Redirect(FUNCTION_ADDR(Intl::ConvertOneByteToLower)));
}
ExternalReference ExternalReference::intl_to_latin1_lower_table() {
uint8_t* ptr = const_cast<uint8_t*>(ToLatin1LowerTable());
uint8_t* ptr = const_cast<uint8_t*>(Intl::ToLatin1LowerTable());
return ExternalReference(reinterpret_cast<Address>(ptr));
}
#endif // V8_INTL_SUPPORT

View File

@ -1,420 +0,0 @@
// Copyright 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_INTL_SUPPORT
#error Internationalization is expected to be enabled.
#endif // V8_INTL_SUPPORT
#include "src/intl.h"
#include <memory>
#include "src/heap/factory.h"
#include "src/isolate.h"
#include "src/objects-inl.h"
#include "src/string-case.h"
#include "unicode/basictz.h"
#include "unicode/calendar.h"
#include "unicode/gregocal.h"
#include "unicode/timezone.h"
#include "unicode/ustring.h"
#include "unicode/uvernum.h"
#include "unicode/uversion.h"
namespace v8 {
namespace internal {
namespace {
inline bool IsASCIIUpper(uint16_t ch) { return ch >= 'A' && ch <= 'Z'; }
const uint8_t kToLower[256] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23,
0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,
0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73,
0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B,
0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x82, 0x83,
0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B,
0x9C, 0x9D, 0x9E, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3,
0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB,
0xEC, 0xED, 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xD7,
0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xDF, 0xE0, 0xE1, 0xE2, 0xE3,
0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB,
0xFC, 0xFD, 0xFE, 0xFF,
};
inline uint16_t ToLatin1Lower(uint16_t ch) {
return static_cast<uint16_t>(kToLower[ch]);
}
inline uint16_t ToASCIIUpper(uint16_t ch) {
return ch & ~((ch >= 'a' && ch <= 'z') << 5);
}
// Does not work for U+00DF (sharp-s), U+00B5 (micron), U+00FF.
inline uint16_t ToLatin1Upper(uint16_t ch) {
DCHECK(ch != 0xDF && ch != 0xB5 && ch != 0xFF);
return ch &
~(((ch >= 'a' && ch <= 'z') || (((ch & 0xE0) == 0xE0) && ch != 0xF7))
<< 5);
}
template <typename Char>
bool ToUpperFastASCII(const Vector<const Char>& src,
Handle<SeqOneByteString> result) {
// Do a faster loop for the case where all the characters are ASCII.
uint16_t ored = 0;
int32_t index = 0;
for (auto it = src.begin(); it != src.end(); ++it) {
uint16_t ch = static_cast<uint16_t>(*it);
ored |= ch;
result->SeqOneByteStringSet(index++, ToASCIIUpper(ch));
}
return !(ored & ~0x7F);
}
const uint16_t sharp_s = 0xDF;
template <typename Char>
bool ToUpperOneByte(const Vector<const Char>& src, uint8_t* dest,
int* sharp_s_count) {
// Still pretty-fast path for the input with non-ASCII Latin-1 characters.
// There are two special cases.
// 1. U+00B5 and U+00FF are mapped to a character beyond U+00FF.
// 2. Lower case sharp-S converts to "SS" (two characters)
*sharp_s_count = 0;
for (auto it = src.begin(); it != src.end(); ++it) {
uint16_t ch = static_cast<uint16_t>(*it);
if (V8_UNLIKELY(ch == sharp_s)) {
++(*sharp_s_count);
continue;
}
if (V8_UNLIKELY(ch == 0xB5 || ch == 0xFF)) {
// Since this upper-cased character does not fit in an 8-bit string, we
// need to take the 16-bit path.
return false;
}
*dest++ = ToLatin1Upper(ch);
}
return true;
}
template <typename Char>
void ToUpperWithSharpS(const Vector<const Char>& src,
Handle<SeqOneByteString> result) {
int32_t dest_index = 0;
for (auto it = src.begin(); it != src.end(); ++it) {
uint16_t ch = static_cast<uint16_t>(*it);
if (ch == sharp_s) {
result->SeqOneByteStringSet(dest_index++, 'S');
result->SeqOneByteStringSet(dest_index++, 'S');
} else {
result->SeqOneByteStringSet(dest_index++, ToLatin1Upper(ch));
}
}
}
inline int FindFirstUpperOrNonAscii(String* s, int length) {
for (int index = 0; index < length; ++index) {
uint16_t ch = s->Get(index);
if (V8_UNLIKELY(IsASCIIUpper(ch) || ch & ~0x7F)) {
return index;
}
}
return length;
}
} // namespace
const uint8_t* ToLatin1LowerTable() { return &kToLower[0]; }
const UChar* GetUCharBufferFromFlat(const String::FlatContent& flat,
std::unique_ptr<uc16[]>* dest,
int32_t length) {
DCHECK(flat.IsFlat());
if (flat.IsOneByte()) {
if (!*dest) {
dest->reset(NewArray<uc16>(length));
CopyChars(dest->get(), flat.ToOneByteVector().start(), length);
}
return reinterpret_cast<const UChar*>(dest->get());
} else {
return reinterpret_cast<const UChar*>(flat.ToUC16Vector().start());
}
}
MaybeHandle<String> LocaleConvertCase(Handle<String> s, Isolate* isolate,
bool is_to_upper, const char* lang) {
auto case_converter = is_to_upper ? u_strToUpper : u_strToLower;
int32_t src_length = s->length();
int32_t dest_length = src_length;
UErrorCode status;
Handle<SeqTwoByteString> result;
std::unique_ptr<uc16[]> sap;
if (dest_length == 0) return ReadOnlyRoots(isolate).empty_string_handle();
// This is not a real loop. It'll be executed only once (no overflow) or
// twice (overflow).
for (int i = 0; i < 2; ++i) {
// Case conversion can increase the string length (e.g. sharp-S => SS) so
// that we have to handle RangeError exceptions here.
ASSIGN_RETURN_ON_EXCEPTION(
isolate, result, isolate->factory()->NewRawTwoByteString(dest_length),
String);
DisallowHeapAllocation no_gc;
DCHECK(s->IsFlat());
String::FlatContent flat = s->GetFlatContent();
const UChar* src = GetUCharBufferFromFlat(flat, &sap, src_length);
status = U_ZERO_ERROR;
dest_length = case_converter(reinterpret_cast<UChar*>(result->GetChars()),
dest_length, src, src_length, lang, &status);
if (status != U_BUFFER_OVERFLOW_ERROR) break;
}
// In most cases, the output will fill the destination buffer completely
// leading to an unterminated string (U_STRING_NOT_TERMINATED_WARNING).
// Only in rare cases, it'll be shorter than the destination buffer and
// |result| has to be truncated.
DCHECK(U_SUCCESS(status));
if (V8_LIKELY(status == U_STRING_NOT_TERMINATED_WARNING)) {
DCHECK(dest_length == result->length());
return result;
}
DCHECK(dest_length < result->length());
return SeqString::Truncate(result, dest_length);
}
// A stripped-down version of ConvertToLower that can only handle flat one-byte
// strings and does not allocate. Note that {src} could still be, e.g., a
// one-byte sliced string with a two-byte parent string.
// Called from TF builtins.
V8_WARN_UNUSED_RESULT String* ConvertOneByteToLower(String* src, String* dst) {
DCHECK_EQ(src->length(), dst->length());
DCHECK(src->HasOnlyOneByteChars());
DCHECK(src->IsFlat());
DCHECK(dst->IsSeqOneByteString());
DisallowHeapAllocation no_gc;
const int length = src->length();
String::FlatContent src_flat = src->GetFlatContent();
uint8_t* dst_data = SeqOneByteString::cast(dst)->GetChars();
if (src_flat.IsOneByte()) {
const uint8_t* src_data = src_flat.ToOneByteVector().start();
bool has_changed_character = false;
int index_to_first_unprocessed =
FastAsciiConvert<true>(reinterpret_cast<char*>(dst_data),
reinterpret_cast<const char*>(src_data), length,
&has_changed_character);
if (index_to_first_unprocessed == length) {
return has_changed_character ? dst : src;
}
// If not ASCII, we keep the result up to index_to_first_unprocessed and
// process the rest.
for (int index = index_to_first_unprocessed; index < length; ++index) {
dst_data[index] = ToLatin1Lower(static_cast<uint16_t>(src_data[index]));
}
} else {
DCHECK(src_flat.IsTwoByte());
int index_to_first_unprocessed = FindFirstUpperOrNonAscii(src, length);
if (index_to_first_unprocessed == length) return src;
const uint16_t* src_data = src_flat.ToUC16Vector().start();
CopyChars(dst_data, src_data, index_to_first_unprocessed);
for (int index = index_to_first_unprocessed; index < length; ++index) {
dst_data[index] = ToLatin1Lower(static_cast<uint16_t>(src_data[index]));
}
}
return dst;
}
MaybeHandle<String> ConvertToLower(Handle<String> s, Isolate* isolate) {
if (!s->HasOnlyOneByteChars()) {
// Use a slower implementation for strings with characters beyond U+00FF.
return LocaleConvertCase(s, isolate, false, "");
}
int length = s->length();
// We depend here on the invariant that the length of a Latin1
// string is invariant under ToLowerCase, and the result always
// fits in the Latin1 range in the *root locale*. It does not hold
// for ToUpperCase even in the root locale.
// Scan the string for uppercase and non-ASCII characters for strings
// shorter than a machine-word without any memory allocation overhead.
// TODO(jshin): Apply this to a longer input by breaking FastAsciiConvert()
// to two parts, one for scanning the prefix with no change and the other for
// handling ASCII-only characters.
bool is_short = length < static_cast<int>(sizeof(uintptr_t));
if (is_short) {
bool is_lower_ascii = FindFirstUpperOrNonAscii(*s, length) == length;
if (is_lower_ascii) return s;
}
Handle<SeqOneByteString> result =
isolate->factory()->NewRawOneByteString(length).ToHandleChecked();
return Handle<String>(ConvertOneByteToLower(*s, *result), isolate);
}
MaybeHandle<String> ConvertToUpper(Handle<String> s, Isolate* isolate) {
int32_t length = s->length();
if (s->HasOnlyOneByteChars() && length > 0) {
Handle<SeqOneByteString> result =
isolate->factory()->NewRawOneByteString(length).ToHandleChecked();
DCHECK(s->IsFlat());
int sharp_s_count;
bool is_result_single_byte;
{
DisallowHeapAllocation no_gc;
String::FlatContent flat = s->GetFlatContent();
uint8_t* dest = result->GetChars();
if (flat.IsOneByte()) {
Vector<const uint8_t> src = flat.ToOneByteVector();
bool has_changed_character = false;
int index_to_first_unprocessed =
FastAsciiConvert<false>(reinterpret_cast<char*>(result->GetChars()),
reinterpret_cast<const char*>(src.start()),
length, &has_changed_character);
if (index_to_first_unprocessed == length) {
return has_changed_character ? result : s;
}
// If not ASCII, we keep the result up to index_to_first_unprocessed and
// process the rest.
is_result_single_byte =
ToUpperOneByte(src.SubVector(index_to_first_unprocessed, length),
dest + index_to_first_unprocessed, &sharp_s_count);
} else {
DCHECK(flat.IsTwoByte());
Vector<const uint16_t> src = flat.ToUC16Vector();
if (ToUpperFastASCII(src, result)) return result;
is_result_single_byte = ToUpperOneByte(src, dest, &sharp_s_count);
}
}
// Go to the full Unicode path if there are characters whose uppercase
// is beyond the Latin-1 range (cannot be represented in OneByteString).
if (V8_UNLIKELY(!is_result_single_byte)) {
return LocaleConvertCase(s, isolate, true, "");
}
if (sharp_s_count == 0) return result;
// We have sharp_s_count sharp-s characters, but the result is still
// in the Latin-1 range.
ASSIGN_RETURN_ON_EXCEPTION(
isolate, result,
isolate->factory()->NewRawOneByteString(length + sharp_s_count),
String);
DisallowHeapAllocation no_gc;
String::FlatContent flat = s->GetFlatContent();
if (flat.IsOneByte()) {
ToUpperWithSharpS(flat.ToOneByteVector(), result);
} else {
ToUpperWithSharpS(flat.ToUC16Vector(), result);
}
return result;
}
return LocaleConvertCase(s, isolate, true, "");
}
MaybeHandle<String> ConvertCase(Handle<String> s, bool is_upper,
Isolate* isolate) {
return is_upper ? ConvertToUpper(s, isolate) : ConvertToLower(s, isolate);
}
ICUTimezoneCache::ICUTimezoneCache() : timezone_(nullptr) { Clear(); }
ICUTimezoneCache::~ICUTimezoneCache() { Clear(); }
const char* ICUTimezoneCache::LocalTimezone(double time_ms) {
bool is_dst = DaylightSavingsOffset(time_ms) != 0;
std::string* name = is_dst ? &dst_timezone_name_ : &timezone_name_;
if (name->empty()) {
icu::UnicodeString result;
GetTimeZone()->getDisplayName(is_dst, icu::TimeZone::LONG, result);
result += '\0';
icu::StringByteSink<std::string> byte_sink(name);
result.toUTF8(byte_sink);
}
DCHECK(!name->empty());
return name->c_str();
}
icu::TimeZone* ICUTimezoneCache::GetTimeZone() {
if (timezone_ == nullptr) {
timezone_ = icu::TimeZone::createDefault();
}
return timezone_;
}
bool ICUTimezoneCache::GetOffsets(double time_ms, bool is_utc,
int32_t* raw_offset, int32_t* dst_offset) {
UErrorCode status = U_ZERO_ERROR;
// TODO(jshin): ICU TimeZone class handles skipped time differently from
// Ecma 262 (https://github.com/tc39/ecma262/pull/778) and icu::TimeZone
// class does not expose the necessary API. Fixing
// http://bugs.icu-project.org/trac/ticket/13268 would make it easy to
// implement the proposed spec change. A proposed fix for ICU is
// https://chromium-review.googlesource.com/851265 .
// In the meantime, use an internal (still public) API of icu::BasicTimeZone.
// Once it's accepted by the upstream, get rid of cast. Note that casting
// TimeZone to BasicTimeZone is safe because we know that icu::TimeZone used
// here is a BasicTimeZone.
if (is_utc) {
GetTimeZone()->getOffset(time_ms, false, *raw_offset, *dst_offset, status);
} else {
static_cast<const icu::BasicTimeZone*>(GetTimeZone())
->getOffsetFromLocal(time_ms, icu::BasicTimeZone::kFormer,
icu::BasicTimeZone::kFormer, *raw_offset,
*dst_offset, status);
}
return U_SUCCESS(status);
}
double ICUTimezoneCache::DaylightSavingsOffset(double time_ms) {
int32_t raw_offset, dst_offset;
if (!GetOffsets(time_ms, true, &raw_offset, &dst_offset)) return 0;
return dst_offset;
}
double ICUTimezoneCache::LocalTimeOffset(double time_ms, bool is_utc) {
int32_t raw_offset, dst_offset;
if (!GetOffsets(time_ms, is_utc, &raw_offset, &dst_offset)) return 0;
return raw_offset + dst_offset;
}
void ICUTimezoneCache::Clear() {
delete timezone_;
timezone_ = nullptr;
timezone_name_.clear();
dst_timezone_name_.clear();
}
} // namespace internal
} // namespace v8

View File

@ -1,82 +0,0 @@
// Copyright 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_INTL_SUPPORT
#error Internationalization is expected to be enabled.
#endif // V8_INTL_SUPPORT
#ifndef V8_INTL_H_
#define V8_INTL_H_
#include <string>
#include "src/base/timezone-cache.h"
#include "src/objects.h"
#include "src/objects/string.h"
#include "unicode/uversion.h"
namespace U_ICU_NAMESPACE {
class TimeZone;
}
namespace v8 {
namespace internal {
enum class ICUService {
kBreakIterator,
kCollator,
kDateFormat,
kNumberFormat,
kPluralRules,
kRelativeDateTimeFormatter,
kListFormatter,
kSegmenter
};
const UChar* GetUCharBufferFromFlat(const String::FlatContent& flat,
std::unique_ptr<uc16[]>* dest,
int32_t length);
MaybeHandle<String> LocaleConvertCase(Handle<String> s, Isolate* isolate,
bool is_to_upper, const char* lang);
MaybeHandle<String> ConvertToLower(Handle<String> s, Isolate* isolate);
MaybeHandle<String> ConvertToUpper(Handle<String> s, Isolate* isolate);
MaybeHandle<String> ConvertCase(Handle<String> s, bool is_upper,
Isolate* isolate);
V8_WARN_UNUSED_RESULT String* ConvertOneByteToLower(String* src, String* dst);
const uint8_t* ToLatin1LowerTable();
// ICUTimezoneCache calls out to ICU for TimezoneCache
// functionality in a straightforward way.
class ICUTimezoneCache : public base::TimezoneCache {
public:
ICUTimezoneCache();
~ICUTimezoneCache() override;
const char* LocalTimezone(double time_ms) override;
double DaylightSavingsOffset(double time_ms) override;
double LocalTimeOffset(double time_ms, bool is_utc) override;
void Clear() override;
private:
icu::TimeZone* GetTimeZone();
bool GetOffsets(double time_ms, bool is_utc, int32_t* raw_offset,
int32_t* dst_offset);
icu::TimeZone* timezone_;
std::string timezone_name_;
std::string dst_timezone_name_;
};
} // namespace internal
} // namespace v8
#endif // V8_INTL_H_

View File

@ -16,7 +16,6 @@
#include "src/api-inl.h"
#include "src/global-handles.h"
#include "src/heap/factory.h"
#include "src/intl.h"
#include "src/isolate.h"
#include "src/objects-inl.h"
#include "src/objects/js-collator-inl.h"
@ -24,6 +23,8 @@
#include "src/objects/js-number-format-inl.h"
#include "src/objects/string.h"
#include "src/property-descriptor.h"
#include "src/string-case.h"
#include "unicode/basictz.h"
#include "unicode/brkiter.h"
#include "unicode/coll.h"
#include "unicode/decimfmt.h"
@ -35,12 +36,344 @@
#include "unicode/timezone.h"
#include "unicode/ucol.h"
#include "unicode/ures.h"
#include "unicode/ustring.h"
#include "unicode/uvernum.h"
#include "unicode/uversion.h"
namespace v8 {
namespace internal {
namespace {
inline bool IsASCIIUpper(uint16_t ch) { return ch >= 'A' && ch <= 'Z'; }
const uint8_t kToLower[256] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23,
0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,
0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73,
0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B,
0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x82, 0x83,
0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B,
0x9C, 0x9D, 0x9E, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3,
0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB,
0xEC, 0xED, 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xD7,
0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xDF, 0xE0, 0xE1, 0xE2, 0xE3,
0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB,
0xFC, 0xFD, 0xFE, 0xFF,
};
inline uint16_t ToLatin1Lower(uint16_t ch) {
return static_cast<uint16_t>(kToLower[ch]);
}
inline uint16_t ToASCIIUpper(uint16_t ch) {
return ch & ~((ch >= 'a' && ch <= 'z') << 5);
}
// Does not work for U+00DF (sharp-s), U+00B5 (micron), U+00FF.
inline uint16_t ToLatin1Upper(uint16_t ch) {
DCHECK(ch != 0xDF && ch != 0xB5 && ch != 0xFF);
return ch &
~(((ch >= 'a' && ch <= 'z') || (((ch & 0xE0) == 0xE0) && ch != 0xF7))
<< 5);
}
template <typename Char>
bool ToUpperFastASCII(const Vector<const Char>& src,
Handle<SeqOneByteString> result) {
// Do a faster loop for the case where all the characters are ASCII.
uint16_t ored = 0;
int32_t index = 0;
for (auto it = src.begin(); it != src.end(); ++it) {
uint16_t ch = static_cast<uint16_t>(*it);
ored |= ch;
result->SeqOneByteStringSet(index++, ToASCIIUpper(ch));
}
return !(ored & ~0x7F);
}
const uint16_t sharp_s = 0xDF;
template <typename Char>
bool ToUpperOneByte(const Vector<const Char>& src, uint8_t* dest,
int* sharp_s_count) {
// Still pretty-fast path for the input with non-ASCII Latin-1 characters.
// There are two special cases.
// 1. U+00B5 and U+00FF are mapped to a character beyond U+00FF.
// 2. Lower case sharp-S converts to "SS" (two characters)
*sharp_s_count = 0;
for (auto it = src.begin(); it != src.end(); ++it) {
uint16_t ch = static_cast<uint16_t>(*it);
if (V8_UNLIKELY(ch == sharp_s)) {
++(*sharp_s_count);
continue;
}
if (V8_UNLIKELY(ch == 0xB5 || ch == 0xFF)) {
// Since this upper-cased character does not fit in an 8-bit string, we
// need to take the 16-bit path.
return false;
}
*dest++ = ToLatin1Upper(ch);
}
return true;
}
template <typename Char>
void ToUpperWithSharpS(const Vector<const Char>& src,
Handle<SeqOneByteString> result) {
int32_t dest_index = 0;
for (auto it = src.begin(); it != src.end(); ++it) {
uint16_t ch = static_cast<uint16_t>(*it);
if (ch == sharp_s) {
result->SeqOneByteStringSet(dest_index++, 'S');
result->SeqOneByteStringSet(dest_index++, 'S');
} else {
result->SeqOneByteStringSet(dest_index++, ToLatin1Upper(ch));
}
}
}
inline int FindFirstUpperOrNonAscii(String* s, int length) {
for (int index = 0; index < length; ++index) {
uint16_t ch = s->Get(index);
if (V8_UNLIKELY(IsASCIIUpper(ch) || ch & ~0x7F)) {
return index;
}
}
return length;
}
const UChar* GetUCharBufferFromFlat(const String::FlatContent& flat,
std::unique_ptr<uc16[]>* dest,
int32_t length) {
DCHECK(flat.IsFlat());
if (flat.IsOneByte()) {
if (!*dest) {
dest->reset(NewArray<uc16>(length));
CopyChars(dest->get(), flat.ToOneByteVector().start(), length);
}
return reinterpret_cast<const UChar*>(dest->get());
} else {
return reinterpret_cast<const UChar*>(flat.ToUC16Vector().start());
}
}
} // namespace
const uint8_t* Intl::ToLatin1LowerTable() { return &kToLower[0]; }
icu::UnicodeString Intl::ToICUUnicodeString(Isolate* isolate,
Handle<String> string) {
string = String::Flatten(isolate, string);
{
DisallowHeapAllocation no_gc;
std::unique_ptr<uc16[]> sap;
return icu::UnicodeString(GetUCharBufferFromFlat(string->GetFlatContent(),
&sap, string->length()),
string->length());
}
}
namespace {
MaybeHandle<String> LocaleConvertCase(Isolate* isolate, Handle<String> s,
bool is_to_upper, const char* lang) {
auto case_converter = is_to_upper ? u_strToUpper : u_strToLower;
int32_t src_length = s->length();
int32_t dest_length = src_length;
UErrorCode status;
Handle<SeqTwoByteString> result;
std::unique_ptr<uc16[]> sap;
if (dest_length == 0) return ReadOnlyRoots(isolate).empty_string_handle();
// This is not a real loop. It'll be executed only once (no overflow) or
// twice (overflow).
for (int i = 0; i < 2; ++i) {
// Case conversion can increase the string length (e.g. sharp-S => SS) so
// that we have to handle RangeError exceptions here.
ASSIGN_RETURN_ON_EXCEPTION(
isolate, result, isolate->factory()->NewRawTwoByteString(dest_length),
String);
DisallowHeapAllocation no_gc;
DCHECK(s->IsFlat());
String::FlatContent flat = s->GetFlatContent();
const UChar* src = GetUCharBufferFromFlat(flat, &sap, src_length);
status = U_ZERO_ERROR;
dest_length = case_converter(reinterpret_cast<UChar*>(result->GetChars()),
dest_length, src, src_length, lang, &status);
if (status != U_BUFFER_OVERFLOW_ERROR) break;
}
// In most cases, the output will fill the destination buffer completely
// leading to an unterminated string (U_STRING_NOT_TERMINATED_WARNING).
// Only in rare cases, it'll be shorter than the destination buffer and
// |result| has to be truncated.
DCHECK(U_SUCCESS(status));
if (V8_LIKELY(status == U_STRING_NOT_TERMINATED_WARNING)) {
DCHECK(dest_length == result->length());
return result;
}
DCHECK(dest_length < result->length());
return SeqString::Truncate(result, dest_length);
}
} // namespace
// A stripped-down version of ConvertToLower that can only handle flat one-byte
// strings and does not allocate. Note that {src} could still be, e.g., a
// one-byte sliced string with a two-byte parent string.
// Called from TF builtins.
String* Intl::ConvertOneByteToLower(String* src, String* dst) {
DCHECK_EQ(src->length(), dst->length());
DCHECK(src->HasOnlyOneByteChars());
DCHECK(src->IsFlat());
DCHECK(dst->IsSeqOneByteString());
DisallowHeapAllocation no_gc;
const int length = src->length();
String::FlatContent src_flat = src->GetFlatContent();
uint8_t* dst_data = SeqOneByteString::cast(dst)->GetChars();
if (src_flat.IsOneByte()) {
const uint8_t* src_data = src_flat.ToOneByteVector().start();
bool has_changed_character = false;
int index_to_first_unprocessed =
FastAsciiConvert<true>(reinterpret_cast<char*>(dst_data),
reinterpret_cast<const char*>(src_data), length,
&has_changed_character);
if (index_to_first_unprocessed == length) {
return has_changed_character ? dst : src;
}
// If not ASCII, we keep the result up to index_to_first_unprocessed and
// process the rest.
for (int index = index_to_first_unprocessed; index < length; ++index) {
dst_data[index] = ToLatin1Lower(static_cast<uint16_t>(src_data[index]));
}
} else {
DCHECK(src_flat.IsTwoByte());
int index_to_first_unprocessed = FindFirstUpperOrNonAscii(src, length);
if (index_to_first_unprocessed == length) return src;
const uint16_t* src_data = src_flat.ToUC16Vector().start();
CopyChars(dst_data, src_data, index_to_first_unprocessed);
for (int index = index_to_first_unprocessed; index < length; ++index) {
dst_data[index] = ToLatin1Lower(static_cast<uint16_t>(src_data[index]));
}
}
return dst;
}
MaybeHandle<String> Intl::ConvertToLower(Isolate* isolate, Handle<String> s) {
if (!s->HasOnlyOneByteChars()) {
// Use a slower implementation for strings with characters beyond U+00FF.
return LocaleConvertCase(isolate, s, false, "");
}
int length = s->length();
// We depend here on the invariant that the length of a Latin1
// string is invariant under ToLowerCase, and the result always
// fits in the Latin1 range in the *root locale*. It does not hold
// for ToUpperCase even in the root locale.
// Scan the string for uppercase and non-ASCII characters for strings
// shorter than a machine-word without any memory allocation overhead.
// TODO(jshin): Apply this to a longer input by breaking FastAsciiConvert()
// to two parts, one for scanning the prefix with no change and the other for
// handling ASCII-only characters.
bool is_short = length < static_cast<int>(sizeof(uintptr_t));
if (is_short) {
bool is_lower_ascii = FindFirstUpperOrNonAscii(*s, length) == length;
if (is_lower_ascii) return s;
}
Handle<SeqOneByteString> result =
isolate->factory()->NewRawOneByteString(length).ToHandleChecked();
return Handle<String>(Intl::ConvertOneByteToLower(*s, *result), isolate);
}
MaybeHandle<String> Intl::ConvertToUpper(Isolate* isolate, Handle<String> s) {
int32_t length = s->length();
if (s->HasOnlyOneByteChars() && length > 0) {
Handle<SeqOneByteString> result =
isolate->factory()->NewRawOneByteString(length).ToHandleChecked();
DCHECK(s->IsFlat());
int sharp_s_count;
bool is_result_single_byte;
{
DisallowHeapAllocation no_gc;
String::FlatContent flat = s->GetFlatContent();
uint8_t* dest = result->GetChars();
if (flat.IsOneByte()) {
Vector<const uint8_t> src = flat.ToOneByteVector();
bool has_changed_character = false;
int index_to_first_unprocessed =
FastAsciiConvert<false>(reinterpret_cast<char*>(result->GetChars()),
reinterpret_cast<const char*>(src.start()),
length, &has_changed_character);
if (index_to_first_unprocessed == length) {
return has_changed_character ? result : s;
}
// If not ASCII, we keep the result up to index_to_first_unprocessed and
// process the rest.
is_result_single_byte =
ToUpperOneByte(src.SubVector(index_to_first_unprocessed, length),
dest + index_to_first_unprocessed, &sharp_s_count);
} else {
DCHECK(flat.IsTwoByte());
Vector<const uint16_t> src = flat.ToUC16Vector();
if (ToUpperFastASCII(src, result)) return result;
is_result_single_byte = ToUpperOneByte(src, dest, &sharp_s_count);
}
}
// Go to the full Unicode path if there are characters whose uppercase
// is beyond the Latin-1 range (cannot be represented in OneByteString).
if (V8_UNLIKELY(!is_result_single_byte)) {
return LocaleConvertCase(isolate, s, true, "");
}
if (sharp_s_count == 0) return result;
// We have sharp_s_count sharp-s characters, but the result is still
// in the Latin-1 range.
ASSIGN_RETURN_ON_EXCEPTION(
isolate, result,
isolate->factory()->NewRawOneByteString(length + sharp_s_count),
String);
DisallowHeapAllocation no_gc;
String::FlatContent flat = s->GetFlatContent();
if (flat.IsOneByte()) {
ToUpperWithSharpS(flat.ToOneByteVector(), result);
} else {
ToUpperWithSharpS(flat.ToUC16Vector(), result);
}
return result;
}
return LocaleConvertCase(isolate, s, true, "");
}
std::string Intl::GetNumberingSystem(const icu::Locale& icu_locale) {
// Ugly hack. ICU doesn't expose numbering system in any way, so we have
// to assume that for given locale NumberingSystem constructor produces the
@ -177,27 +510,27 @@ bool RemoveLocaleScriptTag(const std::string& icu_locale,
} // namespace
std::set<std::string> Intl::GetAvailableLocales(const ICUService service) {
std::set<std::string> Intl::GetAvailableLocales(Intl::ICUService service) {
const icu::Locale* icu_available_locales = nullptr;
int32_t count = 0;
std::set<std::string> locales;
switch (service) {
case ICUService::kBreakIterator:
case ICUService::kSegmenter:
case Intl::ICUService::kBreakIterator:
case Intl::ICUService::kSegmenter:
icu_available_locales = icu::BreakIterator::getAvailableLocales(count);
break;
case ICUService::kCollator:
case Intl::ICUService::kCollator:
icu_available_locales = icu::Collator::getAvailableLocales(count);
break;
case ICUService::kRelativeDateTimeFormatter:
case ICUService::kDateFormat:
case Intl::ICUService::kRelativeDateTimeFormatter:
case Intl::ICUService::kDateFormat:
icu_available_locales = icu::DateFormat::getAvailableLocales(count);
break;
case ICUService::kNumberFormat:
case Intl::ICUService::kNumberFormat:
icu_available_locales = icu::NumberFormat::getAvailableLocales(count);
break;
case ICUService::kPluralRules:
case Intl::ICUService::kPluralRules:
// TODO(littledan): For PluralRules, filter out locales that
// don't support PluralRules.
// PluralRules is missing an appropriate getAvailableLocales method,
@ -205,7 +538,7 @@ std::set<std::string> Intl::GetAvailableLocales(const ICUService service) {
// https://ssl.icu-project.org/trac/ticket/12756
icu_available_locales = icu::Locale::getAvailableLocales(count);
break;
case ICUService::kListFormatter: {
case Intl::ICUService::kListFormatter: {
// TODO(ftang): for now just use
// icu::Locale::getAvailableLocales(count) until we migrate to
// Intl::GetAvailableLocales().
@ -243,23 +576,23 @@ std::set<std::string> Intl::GetAvailableLocales(const ICUService service) {
namespace {
const char* ICUServiceToString(ICUService service) {
const char* ICUServiceToString(Intl::ICUService service) {
switch (service) {
case ICUService::kCollator:
case Intl::ICUService::kCollator:
return "Intl.Collator";
case ICUService::kNumberFormat:
case Intl::ICUService::kNumberFormat:
return "Intl.NumberFormat";
case ICUService::kDateFormat:
case Intl::ICUService::kDateFormat:
return "Intl.DateFormat";
case ICUService::kBreakIterator:
case Intl::ICUService::kBreakIterator:
return "Intl.v8BreakIterator";
case ICUService::kPluralRules:
case Intl::ICUService::kPluralRules:
return "Intl.PluralRules";
case ICUService::kRelativeDateTimeFormatter:
case Intl::ICUService::kRelativeDateTimeFormatter:
return "Intl.RelativeTimeFormat";
case ICUService::kListFormatter:
case Intl::ICUService::kListFormatter:
return "Intl.kListFormat";
case ICUService::kSegmenter:
case Intl::ICUService::kSegmenter:
return "Intl.kSegmenter";
}
UNREACHABLE();
@ -857,7 +1190,10 @@ MaybeHandle<String> Intl::StringLocaleConvertCase(Isolate* isolate,
// tags (x-foo) or grandfathered irregular tags (e.g. i-enochian) would have
// only 'x' or 'i' when they get here.
if (V8_UNLIKELY(requested_locale.length() != 2)) {
return ConvertCase(s, to_upper, isolate);
if (to_upper) {
return ConvertToUpper(isolate, s);
}
return ConvertToLower(isolate, s);
}
// TODO(jshin): Consider adding a fast path for ASCII or Latin-1. The fastpath
// in the root locale needs to be adjusted for az, lt and tr because even case
@ -865,9 +1201,12 @@ MaybeHandle<String> Intl::StringLocaleConvertCase(Isolate* isolate,
// Greek (el) does not require any adjustment.
if (V8_UNLIKELY((requested_locale == "tr") || (requested_locale == "el") ||
(requested_locale == "lt") || (requested_locale == "az"))) {
return LocaleConvertCase(s, isolate, to_upper, requested_locale.c_str());
return LocaleConvertCase(isolate, s, to_upper, requested_locale.c_str());
} else {
return ConvertCase(s, to_upper, isolate);
if (to_upper) {
return ConvertToUpper(isolate, s);
}
return ConvertToLower(isolate, s);
}
}
@ -902,20 +1241,9 @@ Handle<Object> Intl::CompareStrings(Isolate* isolate,
UCollationResult result;
UErrorCode status = U_ZERO_ERROR;
{
DisallowHeapAllocation no_gc;
int32_t length1 = string1->length();
int32_t length2 = string2->length();
String::FlatContent flat1 = string1->GetFlatContent();
String::FlatContent flat2 = string2->GetFlatContent();
std::unique_ptr<uc16[]> sap1;
std::unique_ptr<uc16[]> sap2;
icu::UnicodeString string_val1(
FALSE, GetUCharBufferFromFlat(flat1, &sap1, length1), length1);
icu::UnicodeString string_val2(
FALSE, GetUCharBufferFromFlat(flat2, &sap2, length2), length2);
icu::UnicodeString string_val1 = Intl::ToICUUnicodeString(isolate, string1);
icu::UnicodeString string_val2 = Intl::ToICUUnicodeString(isolate, string2);
result = icu_collator->compare(string_val1, string_val2, status);
}
DCHECK(U_SUCCESS(status));
return factory->NewNumberFromInt(result);
@ -1291,7 +1619,7 @@ MaybeHandle<JSObject> CreateReadOnlyArray(Isolate* isolate,
// ECMA 402 9.2.9 SupportedLocales(availableLocales, requestedLocales, options)
// https://tc39.github.io/ecma402/#sec-supportedlocales
MaybeHandle<JSObject> SupportedLocales(
Isolate* isolate, ICUService service,
Isolate* isolate, Intl::ICUService service,
const std::set<std::string>& available_locales,
const std::vector<std::string>& requested_locales, Handle<Object> options) {
std::vector<std::string> supported_locales;
@ -1370,7 +1698,7 @@ MaybeHandle<JSArray> Intl::GetCanonicalLocales(Isolate* isolate,
// ECMA 402 Intl.*.supportedLocalesOf
MaybeHandle<JSObject> Intl::SupportedLocalesOf(Isolate* isolate,
ICUService service,
Intl::ICUService service,
Handle<Object> locales,
Handle<Object> options) {
// Let availableLocales be %Collator%.[[AvailableLocales]].
@ -1518,16 +1846,8 @@ Intl::ResolvedLocale Intl::ResolveLocale(
Managed<icu::UnicodeString>* Intl::SetTextToBreakIterator(
Isolate* isolate, Handle<String> text, icu::BreakIterator* break_iterator) {
icu::UnicodeString* u_text;
int length = text->length();
text = String::Flatten(isolate, text);
{
DisallowHeapAllocation no_gc;
String::FlatContent flat = text->GetFlatContent();
std::unique_ptr<uc16[]> sap;
const UChar* text_value = GetUCharBufferFromFlat(flat, &sap, length);
u_text = new icu::UnicodeString(text_value, length);
}
icu::UnicodeString* u_text =
(icu::UnicodeString*)(Intl::ToICUUnicodeString(isolate, text).clone());
Handle<Managed<icu::UnicodeString>> new_u_text =
Managed<icu::UnicodeString>::FromRawPtr(isolate, 0, u_text);
@ -1536,5 +1856,104 @@ Managed<icu::UnicodeString>* Intl::SetTextToBreakIterator(
return *new_u_text;
}
// ICUTimezoneCache calls out to ICU for TimezoneCache
// functionality in a straightforward way.
class ICUTimezoneCache : public base::TimezoneCache {
public:
ICUTimezoneCache() : timezone_(nullptr) { Clear(); }
~ICUTimezoneCache() override { Clear(); };
const char* LocalTimezone(double time_ms) override;
double DaylightSavingsOffset(double time_ms) override;
double LocalTimeOffset(double time_ms, bool is_utc) override;
void Clear() override;
private:
icu::TimeZone* GetTimeZone();
bool GetOffsets(double time_ms, bool is_utc, int32_t* raw_offset,
int32_t* dst_offset);
icu::TimeZone* timezone_;
std::string timezone_name_;
std::string dst_timezone_name_;
};
const char* ICUTimezoneCache::LocalTimezone(double time_ms) {
bool is_dst = DaylightSavingsOffset(time_ms) != 0;
std::string* name = is_dst ? &dst_timezone_name_ : &timezone_name_;
if (name->empty()) {
icu::UnicodeString result;
GetTimeZone()->getDisplayName(is_dst, icu::TimeZone::LONG, result);
result += '\0';
icu::StringByteSink<std::string> byte_sink(name);
result.toUTF8(byte_sink);
}
DCHECK(!name->empty());
return name->c_str();
}
icu::TimeZone* ICUTimezoneCache::GetTimeZone() {
if (timezone_ == nullptr) {
timezone_ = icu::TimeZone::createDefault();
}
return timezone_;
}
bool ICUTimezoneCache::GetOffsets(double time_ms, bool is_utc,
int32_t* raw_offset, int32_t* dst_offset) {
UErrorCode status = U_ZERO_ERROR;
// TODO(jshin): ICU TimeZone class handles skipped time differently from
// Ecma 262 (https://github.com/tc39/ecma262/pull/778) and icu::TimeZone
// class does not expose the necessary API. Fixing
// http://bugs.icu-project.org/trac/ticket/13268 would make it easy to
// implement the proposed spec change. A proposed fix for ICU is
// https://chromium-review.googlesource.com/851265 .
// In the meantime, use an internal (still public) API of icu::BasicTimeZone.
// Once it's accepted by the upstream, get rid of cast. Note that casting
// TimeZone to BasicTimeZone is safe because we know that icu::TimeZone used
// here is a BasicTimeZone.
if (is_utc) {
GetTimeZone()->getOffset(time_ms, false, *raw_offset, *dst_offset, status);
} else {
static_cast<const icu::BasicTimeZone*>(GetTimeZone())
->getOffsetFromLocal(time_ms, icu::BasicTimeZone::kFormer,
icu::BasicTimeZone::kFormer, *raw_offset,
*dst_offset, status);
}
return U_SUCCESS(status);
}
double ICUTimezoneCache::DaylightSavingsOffset(double time_ms) {
int32_t raw_offset, dst_offset;
if (!GetOffsets(time_ms, true, &raw_offset, &dst_offset)) return 0;
return dst_offset;
}
double ICUTimezoneCache::LocalTimeOffset(double time_ms, bool is_utc) {
int32_t raw_offset, dst_offset;
if (!GetOffsets(time_ms, is_utc, &raw_offset, &dst_offset)) return 0;
return raw_offset + dst_offset;
}
void ICUTimezoneCache::Clear() {
delete timezone_;
timezone_ = nullptr;
timezone_name_.clear();
dst_timezone_name_.clear();
}
base::TimezoneCache* Intl::CreateTimeZoneCache() {
return FLAG_icu_timezone_data ? new ICUTimezoneCache()
: base::OS::CreateTimezoneCache();
}
} // namespace internal
} // namespace v8

View File

@ -13,8 +13,8 @@
#include <set>
#include <string>
#include "src/base/timezone-cache.h"
#include "src/contexts.h"
#include "src/intl.h"
#include "src/objects.h"
#include "src/objects/managed.h"
#include "unicode/locid.h"
@ -41,6 +41,17 @@ class Intl {
kLength
};
enum class ICUService {
kBreakIterator,
kCollator,
kDateFormat,
kNumberFormat,
kPluralRules,
kRelativeDateTimeFormatter,
kListFormatter,
kSegmenter
};
// Gets the ICU locales for a given service. If there is a locale with a
// script tag then the locales also include a locale without the script; eg,
// pa_Guru_IN (language=Panjabi, script=Gurmukhi, country-India) would include
@ -117,6 +128,12 @@ class Intl {
Isolate* isolate, Handle<String> s, bool is_upper,
Handle<Object> locales);
V8_WARN_UNUSED_RESULT static MaybeHandle<String> ConvertToUpper(
Isolate* isolate, Handle<String> s);
V8_WARN_UNUSED_RESULT static MaybeHandle<String> ConvertToLower(
Isolate* isolate, Handle<String> s);
V8_WARN_UNUSED_RESULT static MaybeHandle<Object> StringLocaleCompare(
Isolate* isolate, Handle<String> s1, Handle<String> s2,
Handle<Object> locales, Handle<Object> options);
@ -198,6 +215,16 @@ class Intl {
static Managed<icu::UnicodeString>* SetTextToBreakIterator(
Isolate* isolate, Handle<String> text,
icu::BreakIterator* break_iterator);
static base::TimezoneCache* CreateTimeZoneCache();
// Convert a Handle<String> to icu::UnicodeString
static icu::UnicodeString ToICUUnicodeString(Isolate* isolate,
Handle<String> string);
static const uint8_t* ToLatin1LowerTable();
static String* ConvertOneByteToLower(String* src, String* dst);
};
} // namespace internal

View File

@ -60,7 +60,7 @@ MaybeHandle<JSV8BreakIterator> JSV8BreakIterator::Initialize(
}
}
std::set<std::string> available_locales =
Intl::GetAvailableLocales(ICUService::kBreakIterator);
Intl::GetAvailableLocales(Intl::ICUService::kBreakIterator);
Intl::ResolvedLocale r = Intl::ResolveLocale(isolate, available_locales,
requested_locales, matcher, {});

View File

@ -309,7 +309,7 @@ MaybeHandle<JSCollator> JSCollator::Initialize(Isolate* isolate,
// requestedLocales, opt, %Collator%.[[RelevantExtensionKeys]],
// localeData).
std::set<std::string> available_locales =
Intl::GetAvailableLocales(ICUService::kCollator);
Intl::GetAvailableLocales(Intl::ICUService::kCollator);
Intl::ResolvedLocale r =
Intl::ResolveLocale(isolate, available_locales, requested_locales,
matcher, relevant_extension_keys);

View File

@ -785,7 +785,7 @@ MaybeHandle<JSDateTimeFormat> JSDateTimeFormat::Initialize(
// requestedLocales, opt, %DateTimeFormat%.[[RelevantExtensionKeys]],
// localeData).
std::set<std::string> available_locales =
Intl::GetAvailableLocales(ICUService::kDateFormat);
Intl::GetAvailableLocales(Intl::ICUService::kDateFormat);
Intl::ResolvedLocale r = Intl::ResolveLocale(
isolate, available_locales, requested_locales, locale_matcher, {"nu"});

View File

@ -203,7 +203,7 @@ MaybeHandle<JSListFormat> JSListFormat::Initialize(
// 15. Let r be ResolveLocale(%ListFormat%.[[AvailableLocales]],
// requestedLocales, opt, undefined, localeData).
std::set<std::string> available_locales =
Intl::GetAvailableLocales(ICUService::kListFormatter);
Intl::GetAvailableLocales(Intl::ICUService::kListFormatter);
Intl::ResolvedLocale r = Intl::ResolveLocale(isolate, available_locales,
requested_locales, matcher, {});
@ -353,13 +353,7 @@ Maybe<bool> ToUnicodeStringArray(Isolate* isolate, Handle<JSArray> array,
}
for (uint32_t i = 0; i < length; i++) {
Handle<String> string = Handle<String>::cast(accessor->Get(array, i));
DisallowHeapAllocation no_gc;
string = String::Flatten(isolate, string);
std::unique_ptr<uc16[]> sap;
items[i] =
icu::UnicodeString(GetUCharBufferFromFlat(string->GetFlatContent(),
&sap, string->length()),
string->length());
items[i] = Intl::ToICUUnicodeString(isolate, string);
}
return Just(true);
}

View File

@ -235,7 +235,7 @@ MaybeHandle<JSNumberFormat> JSNumberFormat::Initialize(
// requestedLocales, opt, %NumberFormat%.[[RelevantExtensionKeys]],
// localeData).
std::set<std::string> available_locales =
Intl::GetAvailableLocales(ICUService::kNumberFormat);
Intl::GetAvailableLocales(Intl::ICUService::kNumberFormat);
std::set<std::string> relevant_extension_keys{"nu"};
Intl::ResolvedLocale r =
Intl::ResolveLocale(isolate, available_locales, requested_locales,

View File

@ -152,7 +152,7 @@ MaybeHandle<JSPluralRules> JSPluralRules::Initialize(
// requestedLocales, opt, %PluralRules%.[[RelevantExtensionKeys]],
// localeData).
std::set<std::string> available_locales =
Intl::GetAvailableLocales(ICUService::kPluralRules);
Intl::GetAvailableLocales(Intl::ICUService::kPluralRules);
Intl::ResolvedLocale r = Intl::ResolveLocale(isolate, available_locales,
requested_locales, matcher, {});

View File

@ -103,7 +103,7 @@ MaybeHandle<JSRelativeTimeFormat> JSRelativeTimeFormat::Initialize(
// requestedLocales, opt,
// %RelativeTimeFormat%.[[RelevantExtensionKeys]], localeData).
std::set<std::string> available_locales =
Intl::GetAvailableLocales(ICUService::kRelativeDateTimeFormatter);
Intl::GetAvailableLocales(Intl::ICUService::kRelativeDateTimeFormatter);
Intl::ResolvedLocale r = Intl::ResolveLocale(isolate, available_locales,
requested_locales, matcher, {});

View File

@ -13,7 +13,6 @@
#include <string>
#include "src/heap/factory.h"
#include "src/intl.h"
#include "src/isolate.h"
#include "src/objects-inl.h"
#include "src/objects/intl-objects.h"

View File

@ -86,7 +86,7 @@ MaybeHandle<JSSegmenter> JSSegmenter::Initialize(
// 9. Let r be ResolveLocale(%Segmenter%.[[AvailableLocales]],
// requestedLocales, opt, %Segmenter%.[[RelevantExtensionKeys]]).
std::set<std::string> available_locales =
Intl::GetAvailableLocales(ICUService::kSegmenter);
Intl::GetAvailableLocales(Intl::ICUService::kSegmenter);
Intl::ResolvedLocale r = Intl::ResolveLocale(isolate, available_locales,
requested_locales, matcher, {});

View File

@ -15,7 +15,6 @@
#include "src/date.h"
#include "src/global-handles.h"
#include "src/heap/factory.h"
#include "src/intl.h"
#include "src/isolate-inl.h"
#include "src/objects/intl-objects.h"
#include "src/objects/js-array-inl.h"
@ -103,7 +102,7 @@ RUNTIME_FUNCTION(Runtime_StringToLowerCaseIntl) {
DCHECK_EQ(args.length(), 1);
CONVERT_ARG_HANDLE_CHECKED(String, s, 0);
s = String::Flatten(isolate, s);
RETURN_RESULT_OR_FAILURE(isolate, ConvertToLower(s, isolate));
RETURN_RESULT_OR_FAILURE(isolate, Intl::ConvertToLower(isolate, s));
}
RUNTIME_FUNCTION(Runtime_StringToUpperCaseIntl) {
@ -111,7 +110,7 @@ RUNTIME_FUNCTION(Runtime_StringToUpperCaseIntl) {
DCHECK_EQ(args.length(), 1);
CONVERT_ARG_HANDLE_CHECKED(String, s, 0);
s = String::Flatten(isolate, s);
RETURN_RESULT_OR_FAILURE(isolate, ConvertToUpper(s, isolate));
RETURN_RESULT_OR_FAILURE(isolate, Intl::ConvertToUpper(isolate, s));
}
RUNTIME_FUNCTION(Runtime_DateCacheVersion) {

View File

@ -211,23 +211,24 @@ TEST(GetBoolOption) {
TEST(GetAvailableLocales) {
std::set<std::string> locales;
locales = Intl::GetAvailableLocales(ICUService::kBreakIterator);
locales = Intl::GetAvailableLocales(Intl::ICUService::kBreakIterator);
CHECK(locales.count("en-US"));
CHECK(!locales.count("abcdefg"));
locales = Intl::GetAvailableLocales(ICUService::kCollator);
locales = Intl::GetAvailableLocales(Intl::ICUService::kCollator);
CHECK(locales.count("en-US"));
locales = Intl::GetAvailableLocales(ICUService::kDateFormat);
locales = Intl::GetAvailableLocales(Intl::ICUService::kDateFormat);
CHECK(locales.count("en-US"));
locales = Intl::GetAvailableLocales(ICUService::kNumberFormat);
locales = Intl::GetAvailableLocales(Intl::ICUService::kNumberFormat);
CHECK(locales.count("en-US"));
locales = Intl::GetAvailableLocales(ICUService::kPluralRules);
locales = Intl::GetAvailableLocales(Intl::ICUService::kPluralRules);
CHECK(locales.count("en-US"));
locales = Intl::GetAvailableLocales(ICUService::kRelativeDateTimeFormatter);
locales =
Intl::GetAvailableLocales(Intl::ICUService::kRelativeDateTimeFormatter);
CHECK(locales.count("en-US"));
}