6#error Internationalization is expected to be enabled.
24#include "unicode/fieldpos.h"
25#include "unicode/fpositer.h"
26#include "unicode/listformatter.h"
27#include "unicode/ulistformatter.h"
37 return ULISTFMT_WIDTH_WIDE;
39 return ULISTFMT_WIDTH_SHORT;
41 return ULISTFMT_WIDTH_NARROW;
49 return ULISTFMT_TYPE_AND;
51 return ULISTFMT_TYPE_OR;
53 return ULISTFMT_TYPE_UNITS;
67 std::vector<std::string> requested_locales =
71 const char* service =
"Intl.ListFormat";
92 requested_locales, matcher, {});
98 isolate->factory()->NewStringFromAsciiChecked(
r.locale.c_str());
103 isolate, options,
"type", service, {
"conjunction",
"disjunction",
"unit"},
111 isolate, options,
"style", service, {
"long",
"short",
"narrow"},
116 icu::Locale icu_locale =
r.icu_locale;
117 UErrorCode status = U_ZERO_ERROR;
118 std::shared_ptr<icu::ListFormatter> formatter{
119 icu::ListFormatter::createInstance(icu_locale, GetIcuType(type_enum),
120 GetIcuWidth(style_enum), status)};
121 if (U_FAILURE(status) || formatter ==
nullptr) {
132 list_format->set_flags(0);
133 list_format->set_icu_formatter(*managed_formatter);
136 list_format->set_locale(*locale_str);
139 list_format->set_type(type_enum);
142 list_format->set_style(style_enum);
150 Factory* factory = isolate->factory();
165 format->TypeAsString(isolate),
NONE);
167 format->StyleAsString(isolate),
NONE);
175 return isolate->factory()->long_string();
177 return isolate->factory()->short_string();
179 return isolate->factory()->narrow_string();
187 return isolate->factory()->conjunction_string();
189 return isolate->factory()->disjunction_string();
191 return isolate->factory()->unit_string();
201 int length = array->length();
202 std::vector<icu::UnicodeString>
result;
207 if (!item_str->IsFlat()) item_str =
String::Flatten(isolate, item_str);
214MaybeDirectHandle<T> FormatListCommon(
215 Isolate* isolate, DirectHandle<JSListFormat> format,
216 DirectHandle<FixedArray> list,
217 const std::function<MaybeDirectHandle<T>(
218 Isolate*,
const icu::FormattedValue&)>& formatToResult) {
219 DCHECK(!IsUndefined(*list));
220 Maybe<std::vector<icu::UnicodeString>> maybe_array =
221 ToUnicodeStringArray(isolate, list);
223 std::vector<icu::UnicodeString> array = maybe_array.FromJust();
225 icu::ListFormatter* formatter = format->icu_formatter()->raw();
228 UErrorCode status = U_ZERO_ERROR;
229 icu::FormattedList
formatted = formatter->formatStringsToValue(
230 array.data(),
static_cast<int32_t
>(array.size()), status);
231 if (U_FAILURE(status)) {
234 return formatToResult(isolate,
formatted);
237DirectHandle<String> IcuFieldIdToType(Isolate* isolate, int32_t field_id) {
239 case ULISTFMT_LITERAL_FIELD:
240 return isolate->factory()->literal_string();
241 case ULISTFMT_ELEMENT_FIELD:
242 return isolate->factory()->element_string();
251 Isolate* isolate,
const icu::FormattedValue&
formatted) {
252 DirectHandle<JSArray> array = isolate->factory()->NewJSArray(0);
253 icu::ConstrainedFieldPosition cfpos;
254 cfpos.constrainCategory(UFIELD_CATEGORY_LIST);
256 UErrorCode status = U_ZERO_ERROR;
257 icu::UnicodeString
string =
formatted.toString(status);
259 while (
formatted.nextPosition(cfpos, status) && U_SUCCESS(status)) {
262 Intl::ToString(isolate,
string, cfpos.getStart(), cfpos.getLimit()));
264 IcuFieldIdToType(isolate, cfpos.getField()), substring);
266 if (U_FAILURE(status)) {
279 return FormatListCommon<String>(isolate, format, list,
287 return FormatListCommon<JSArray>(isolate, format, list,
293struct CheckListPattern {
294 static const char*
key() {
return "listPattern"; }
295 static const char* path() {
return nullptr; }
303 return available_locales.Pointer()->Get();
V8_INLINE T FromJust() const &
V8_INLINE bool IsNothing() const
Handle< JSObject > NewJSObject(DirectHandle< JSFunction > constructor, AllocationType allocation=AllocationType::kYoung, NewJSObjectType=NewJSObjectType::kNoAPIWrapper)
static void AddElement(Isolate *isolate, DirectHandle< JSArray > array, int index, DirectHandle< String > field_type_string, DirectHandle< String > value)
static V8_WARN_UNUSED_RESULT MaybeHandle< String > ToString(Isolate *isolate, const icu::UnicodeString &string)
static Maybe< std::vector< std::string > > CanonicalizeLocaleList(Isolate *isolate, DirectHandle< Object > locales, bool only_return_one_result=false)
static Maybe< ResolvedLocale > ResolveLocale(Isolate *isolate, const std::set< std::string > &available_locales, const std::vector< std::string > &requested_locales, MatcherOption options, const std::set< std::string > &relevant_extension_keys)
static icu::UnicodeString ToICUUnicodeString(Isolate *isolate, DirectHandle< String > string, int offset=0)
static V8_WARN_UNUSED_RESULT Maybe< MatcherOption > GetLocaleMatcher(Isolate *isolate, DirectHandle< JSReceiver > options, const char *method_name)
static V8_WARN_UNUSED_RESULT MaybeDirectHandle< String > FormattedToString(Isolate *isolate, const icu::FormattedValue &formatted)
static V8_EXPORT_PRIVATE void AddProperty(Isolate *isolate, DirectHandle< JSObject > object, DirectHandle< Name > name, DirectHandle< Object > value, PropertyAttributes attributes)
static void ValidateElements(Tagged< JSObject > object)
static DirectHandle< Managed< CppType > > From(Isolate *isolate, size_t estimated_size, std::shared_ptr< CppType > shared_ptr, AllocationType allocation_type=AllocationType::kYoung)
static V8_INLINE HandleType< String > Flatten(Isolate *isolate, HandleType< T > string, AllocationType allocation=AllocationType::kYoung)
void push_back(const T &value)
#define ASSIGN_RETURN_ON_EXCEPTION(isolate, dst, call)
#define THROW_NEW_ERROR(isolate, call)
#define MAYBE_RETURN(call, value)
DirectHandle< JSReceiver > options
ZoneVector< RpoNumber > & result
#define LAZY_INSTANCE_INITIALIZER
MaybeDirectHandle< JSReceiver > GetOptionsObject(Isolate *isolate, DirectHandle< Object > options, const char *method_name)
MaybeDirectHandle< JSArray > FormattedListToJSArray(Isolate *isolate, const icu::FormattedValue &formatted, const std::vector< std::vector< Part > > *parts, JSDurationFormat::Separator separator)
Maybe< bool > GetStringOption(Isolate *isolate, DirectHandle< JSReceiver > options, const char *property, const std::vector< const char * > &values, const char *method_name, std::unique_ptr< char[]> *result)
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Maybe< T > Just(const T &t)
#define DCHECK_NOT_NULL(val)
#define DCHECK(condition)