Thread UTF-16 string input through JSON, script parsing, Date parsing, Intl option parsing, Temporal parsing, and the helper library boundaries that feed those parsers. Preserve ASCII fast paths where the source data is known to be ASCII.
65 lines
2.9 KiB
C++
65 lines
2.9 KiB
C++
/*
|
|
* Copyright (c) 2022-2025, Tim Flynn <trflynn89@ladybird.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/String.h>
|
|
#include <AK/StringView.h>
|
|
#include <AK/Utf16View.h>
|
|
#include <LibJS/Runtime/Completion.h>
|
|
#include <LibJS/Runtime/Intl/IntlObject.h>
|
|
#include <LibUnicode/Locale.h>
|
|
#include <LibUnicode/RelativeTimeFormat.h>
|
|
|
|
namespace JS::Intl {
|
|
|
|
class RelativeTimeFormat final : public IntlObject {
|
|
JS_OBJECT(RelativeTimeFormat, IntlObject);
|
|
GC_DECLARE_ALLOCATOR(RelativeTimeFormat);
|
|
|
|
public:
|
|
virtual ~RelativeTimeFormat() override = default;
|
|
|
|
virtual ReadonlySpan<StringView> relevant_extension_keys() const override;
|
|
virtual ReadonlySpan<ResolutionOptionDescriptor> resolution_option_descriptors(VM&) const override;
|
|
|
|
String const& locale() const { return m_locale; }
|
|
void set_locale(String locale) { m_locale = move(locale); }
|
|
|
|
String const& numbering_system() const { return m_numbering_system; }
|
|
void set_numbering_system(String numbering_system) { m_numbering_system = move(numbering_system); }
|
|
|
|
Unicode::Style style() const { return m_style; }
|
|
void set_style(StringView style) { m_style = Unicode::style_from_string(style); }
|
|
void set_style(Utf16View style) { m_style = Unicode::style_from_string(style); }
|
|
StringView style_string() const { return Unicode::style_to_string(m_style); }
|
|
|
|
Unicode::NumericDisplay numeric() const { return m_numeric; }
|
|
void set_numeric(StringView numeric) { m_numeric = Unicode::numeric_display_from_string(numeric); }
|
|
void set_numeric(Utf16View numeric) { m_numeric = Unicode::numeric_display_from_string(numeric); }
|
|
StringView numeric_string() const { return Unicode::numeric_display_to_string(m_numeric); }
|
|
|
|
Unicode::RelativeTimeFormat const& formatter() const { return *m_formatter; }
|
|
void set_formatter(NonnullOwnPtr<Unicode::RelativeTimeFormat> formatter) { m_formatter = move(formatter); }
|
|
|
|
private:
|
|
explicit RelativeTimeFormat(Object& prototype);
|
|
|
|
String m_locale; // [[Locale]]
|
|
String m_numbering_system; // [[NumberingSystem]]
|
|
Unicode::Style m_style { Unicode::Style::Long }; // [[Style]]
|
|
Unicode::NumericDisplay m_numeric { Unicode::NumericDisplay::Always }; // [[Numeric]]
|
|
|
|
// Non-standard. Stores the ICU relative-time formatter for the Intl object's formatting options.
|
|
OwnPtr<Unicode::RelativeTimeFormat> m_formatter;
|
|
};
|
|
|
|
ThrowCompletionOr<Unicode::TimeUnit> singular_relative_time_unit(VM&, Utf16View unit);
|
|
ThrowCompletionOr<Vector<Unicode::RelativeTimeFormat::Partition>> partition_relative_time_pattern(VM&, RelativeTimeFormat&, double value, Utf16View unit);
|
|
ThrowCompletionOr<Utf16String> format_relative_time(VM&, RelativeTimeFormat&, double value, Utf16View unit);
|
|
ThrowCompletionOr<GC::Ref<Array>> format_relative_time_to_parts(VM&, RelativeTimeFormat&, double value, Utf16View unit);
|
|
|
|
}
|