dymokomi/luce-browser-foundation
Source of release 0.6.0
0.6.0 / src / ak / utf16_fly_string.lucb
#==============================================================================================
#
# utf16_fly_string - Utf16FlyString: interned UTF-16 strings that compare by identity
#
# DESCRIPTION:
# Port of AK/Utf16FlyString.h (lines 1-245) and AK/Utf16FlyString.cpp (lines 1-104).
#
# A Utf16FlyString is a Utf16StringBase whose long data is registered in the
# all_utf16_fly_strings table, so equal strings share one Utf16StringData and `==` is a
# comparison of the eight bytes. The table's HashTable calls are gathered in the
# utf16_fly_string_table_* helpers.
#
# The table and every interned Utf16StringData live in `memory.heap`, whatever allocator is
# current, as FlyString's do (see fly_string.lucb): interning copies the string's data
# there, and the table only changes under `with memory.heap:`.
#
#==============================================================================================
# mark: The table ===============================================================================
## Ported from AK/Utf16FlyString.cpp:14 AK::Utf16FlyStringTableHashTraits::hash.
pub func utf16_fly_string_table_hash_traits_hash(string: const Utf16StringData*?) -> u32:
return utf16_string_data_hash(string else trap("VERIFY: null Utf16StringData in the Utf16FlyString table"))
## Ported from AK/Utf16FlyString.cpp:15 AK::Utf16FlyStringTableHashTraits::equals.
pub func utf16_fly_string_table_hash_traits_equals(a: const Utf16StringData*?, b: const Utf16StringData*?) -> bool:
return utf16_string_data_eq_utf16_string_data(a else trap("VERIFY: null Utf16StringData"), b else trap("VERIFY: null Utf16StringData"))
## Ported from AK/Utf16FlyString.cpp:16 AK::Utf16FlyStringTableHashTraits::may_have_slow_equality_check.
pub func utf16_fly_string_table_hash_traits_may_have_slow_equality_check() -> bool:
return true
## `static Singleton<HashTable<Utf16StringData const*, Utf16FlyStringTableHashTraits>> table`.
## Its buckets and entries are in `memory.heap`; nothing removes an entry yet (see fly_string.lucb's
## FIXME), so an interned string lives as long as the process.
var s_all_utf16_fly_strings: HashTable[const Utf16StringData*?, Utf16FlyStringTableHashTraits]
## Ported from AK/Utf16FlyString.cpp:19 AK::all_utf16_fly_strings.
func all_utf16_fly_strings() -> HashTable[const Utf16StringData*?, Utf16FlyStringTableHashTraits]*:
return &s_all_utf16_fly_strings
## Ported from AK/Utf16FlyString.cpp:27 AK::Detail::did_destroy_utf16_fly_string_data: drops a
## Utf16StringData from the table when it dies (the destructor's call; the collector's finalizer
## will make it).
pub func did_destroy_utf16_fly_string_data(data: const Utf16StringData*):
with memory.heap:
_ = hash_table_remove_t(all_utf16_fly_strings(), data)
## Adds a copy of `data` to the table and answers it (addition: the C++ adds `data` itself). The
## copy and the table's buckets come from `memory.heap`, which outlives any allocator scope, so
## an interned string stays valid after the allocator that was current when it was interned is
## gone. The block is flat (header, then the code units), so it is copied byte for byte.
func utf16_fly_string_table_add(data: const Utf16StringData*) -> const Utf16StringData*:
let size = utf16_string_data_allocation_size_for_string_data(utf16_string_data_has_ascii_storage(data), utf16_string_data_length_in_code_units(data))
with memory.heap:
let slot = memory.allocate(size, memory.align_of(Utf16StringData)) else trap("VERIFY: kmalloc")
memory.copy(slot, u8[]((u8*)data, size), size)
let interned = (const Utf16StringData*)slot.data
_ = hash_table_set(all_utf16_fly_strings(), (const Utf16StringData*?)interned)
utf16_string_data_mark_as_fly_string(interned)
return interned
## The captures of the `[&](auto const& entry) { return *entry == string; }` predicate of
## create_fly_string_from_cache for a UTF-8 view.
struct Utf16FlyStringTableStringViewPredicate:
var string: StringView
# The UTF-8 predicate's body.
func utf16_fly_string_table_string_view_predicate_call(context: void*?, entry: const (const Utf16StringData*?)*) -> bool:
let captures = (const Utf16FlyStringTableStringViewPredicate*)(context else trap("Utf16FlyString: no captures"))
return utf16_string_data_eq_string_view(*entry else trap("VERIFY: null Utf16StringData"), captures.string)
## The same predicate's captures for a Utf16View.
struct Utf16FlyStringTableUtf16ViewPredicate:
var string: Utf16View
# The Utf16View predicate's body.
func utf16_fly_string_table_utf16_view_predicate_call(context: void*?, entry: const (const Utf16StringData*?)*) -> bool:
let captures = (const Utf16FlyStringTableUtf16ViewPredicate*)(context else trap("Utf16FlyString: no captures"))
return utf16_string_data_eq_utf16_view(*entry else trap("VERIFY: null Utf16StringData"), &captures.string)
# `all_utf16_fly_strings().find(hash, predicate)`: the interned data the predicate accepts, if any.
func utf16_fly_string_table_find(hash_: u32, predicate: Function1[const (const Utf16StringData*?)*, bool]) -> const Utf16StringData*?:
var it = hash_table_find_u32_tt_unary_predicate(all_utf16_fly_strings(), hash_, predicate)
if hash_table_iterator_eq(&it, hash_table_end(all_utf16_fly_strings())):
return none
return *hash_table_iterator_deref(&it)
## Ported from AK/Utf16FlyString.cpp:35 AK::Utf16FlyString::create_fly_string_from_cache for a
## StringView: a short string, or the interned string equal to `string`, or none.
func utf16_fly_string_create_fly_string_from_cache_string_view(string: StringView) -> Utf16FlyString?:
if string_view_is_empty(&string):
return none
if string_view_length(&string) <= max_short_string_byte_count and string_view_is_ascii(&string):
return utf16_fly_string_from_utf16_string(utf16_string_from_utf8_without_validation(string))
var captures = Utf16FlyStringTableStringViewPredicate(string = string)
let predicate = Function1[const (const Utf16StringData*?)*, bool](call = utf16_fly_string_table_string_view_predicate_call, context = &captures)
let data = utf16_fly_string_table_find(string_view_hash(&string), predicate) else return none
return utf16_fly_string_from_data(data)
## Ported from AK/Utf16FlyString.cpp:35 AK::Utf16FlyString::create_fly_string_from_cache for a
## Utf16View.
func utf16_fly_string_create_fly_string_from_cache_utf16_view(string: Utf16View) -> Utf16FlyString?:
if utf16_view_is_empty(&string):
return none
if utf16_view_length_in_code_units(&string) <= max_short_string_byte_count and utf16_view_is_ascii(&string):
return utf16_fly_string_from_utf16_string(utf16_string_from_utf16(string))
var captures = Utf16FlyStringTableUtf16ViewPredicate(string = string)
let predicate = Function1[const (const Utf16StringData*?)*, bool](call = utf16_fly_string_table_utf16_view_predicate_call, context = &captures)
let data = utf16_fly_string_table_find(utf16_view_hash(&string), predicate) else return none
return utf16_fly_string_from_data(data)
## `Utf16FlyString { Utf16StringBase(data) }`.
func utf16_fly_string_from_data(data: const Utf16StringData*) -> Utf16FlyString:
var result = Utf16FlyString(m_data = Utf16StringBase())
utf16_string_base_construct_utf16_string_data(&result.m_data, data)
return result
# mark: Creation ================================================================================
## Ported from AK/Utf16FlyString.cpp:56 AK::Utf16FlyString::from_utf8(StringView).
pub func utf16_fly_string_from_utf8_string_view(string: StringView) -> Utf16FlyString:
if let result = utf16_fly_string_create_fly_string_from_cache_string_view(string):
return result
return utf16_fly_string_from_utf16_string(utf16_string_from_utf8_string_view(string))
## Ported from AK/Utf16FlyString.h:25 AK::Utf16FlyString::from_utf8(String const&).
pub func utf16_fly_string_from_utf8_string(string: const String*) -> Utf16FlyString:
return utf16_fly_string_from_utf8_without_validation(string_bytes_as_string_view(string))
## Ported from AK/Utf16FlyString.h:26 AK::Utf16FlyString::from_utf8(FlyString const&).
pub func utf16_fly_string_from_utf8_fly_string(string: const FlyString*) -> Utf16FlyString:
return utf16_fly_string_from_utf8_without_validation(fly_string_bytes_as_string_view(string))
## Ported from AK/Utf16FlyString.cpp:63 AK::Utf16FlyString::from_utf8_without_validation.
pub func utf16_fly_string_from_utf8_without_validation(string: StringView) -> Utf16FlyString:
if let result = utf16_fly_string_create_fly_string_from_cache_string_view(string):
return result
return utf16_fly_string_from_utf16_string(utf16_string_from_utf8_without_validation(string))
## Ported from AK/Utf16FlyString.h:28 AK::Utf16FlyString::from_utf8_but_should_be_ported_to_utf16.
pub func utf16_fly_string_from_utf8_but_should_be_ported_to_utf16(string: StringView) -> Utf16FlyString:
return utf16_fly_string_from_utf8_without_validation(string)
## Ported from AK/Utf16FlyString.cpp:70 AK::Utf16FlyString::from_utf16.
pub func utf16_fly_string_from_utf16(string: Utf16View) -> Utf16FlyString:
if let result = utf16_fly_string_create_fly_string_from_cache_utf16_view(string):
return result
return utf16_fly_string_from_utf16_string(utf16_string_from_utf16(string))
## Ported from AK/Utf16FlyString.cpp:77 AK::Utf16FlyString::Utf16FlyString(Utf16String const&):
## interns `string`.
pub func utf16_fly_string_construct_utf16_string(this: Utf16FlyString*, string: const Utf16String*):
if utf16_string_base_has_short_ascii_storage(&string.utf16_string_base):
this.m_data = string.utf16_string_base
return
let data = utf16_string_base_data(&string.utf16_string_base) else trap("VERIFY: Utf16String data")
if utf16_string_data_is_fly_string(data):
this.m_data = string.utf16_string_base
return
var it = hash_table_find_t(all_utf16_fly_strings(), (const Utf16StringData*?)data)
if hash_table_iterator_eq(&it, hash_table_end(all_utf16_fly_strings())):
# The C++ interns `data` itself and marks it; here the table's copy is interned, since
# `data` may come from a shorter-lived allocator.
this.m_data = string.utf16_string_base
utf16_string_base_set_data(&this.m_data, utf16_fly_string_table_add(data))
else:
utf16_string_base_set_data(&this.m_data, *hash_table_iterator_deref(&it) else trap("VERIFY: null Utf16StringData"))
## `Utf16FlyString { string }`: the value form of the Utf16String constructor.
pub func utf16_fly_string_from_utf16_string(string: Utf16String) -> Utf16FlyString:
var result = utf16_fly_string_new()
utf16_fly_string_construct_utf16_string(&result, &string)
return result
## `Utf16FlyString {}`: the empty string.
pub func utf16_fly_string_new() -> Utf16FlyString:
return Utf16FlyString(m_data = utf16_string_base_new())
## `u"text"_utf16_fly_string` from a `str`.
pub func utf16_fly_string_from_str(text: str) -> Utf16FlyString:
return literalutf16_fly_string_u8(text.bytes.data, text.length)
# mark: Views and conversions ===================================================================
## Ported from AK/Utf16FlyString.h:37 AK::Utf16FlyString::view.
pub func utf16_fly_string_view(this: const Utf16FlyString*) -> Utf16View:
return utf16_string_base_utf16_view(&this.m_data)
## Ported from AK/Utf16FlyString.h:39 AK::Utf16FlyString::operator Utf16String.
pub func utf16_fly_string_to_utf16_string_const(this: const Utf16FlyString*) -> Utf16String:
return utf16_fly_string_to_utf16_string_const_2(this)
## Ported from AK/Utf16FlyString.h:41 AK::Utf16FlyString::operator Utf16View.
pub func utf16_fly_string_to_utf16_view(this: const Utf16FlyString*) -> Utf16View:
return utf16_fly_string_view(this)
## Ported from AK/Utf16FlyString.h:44 AK::Utf16FlyString::to_utf16_string.
pub func utf16_fly_string_to_utf16_string_const_2(this: const Utf16FlyString*) -> Utf16String:
return Utf16String(utf16_string_base = this.m_data)
## Ported from AK/Utf16FlyString.h:50 AK::Utf16FlyString::to_ascii_lowercase.
pub func utf16_fly_string_to_ascii_lowercase(this: const Utf16FlyString*) -> Utf16FlyString:
let view = utf16_string_base_utf16_view(&this.m_data)
var has_upper = false
if utf16_view_has_ascii_storage(&view):
for byte in utf16_view_ascii_span(&view):
if is_ascii_upper_alpha(byte):
has_upper = true
break
else:
for code_unit in utf16_view_utf16_span(&view):
if is_ascii_upper_alpha(code_unit):
has_upper = true
break
if not has_upper:
return *this
return utf16_fly_string_from_utf16_string(utf16_view_to_ascii_lowercase(&view))
## Ported from AK/Utf16FlyString.h:65 AK::Utf16FlyString::to_ascii_uppercase.
pub func utf16_fly_string_to_ascii_uppercase(this: const Utf16FlyString*) -> Utf16FlyString:
let view = utf16_string_base_utf16_view(&this.m_data)
var has_lower = false
if utf16_view_has_ascii_storage(&view):
for byte in utf16_view_ascii_span(&view):
if is_ascii_lower_alpha(byte):
has_lower = true
break
else:
for code_unit in utf16_view_utf16_span(&view):
if is_ascii_lower_alpha(code_unit):
has_lower = true
break
if not has_lower:
return *this
return utf16_fly_string_from_utf16_string(utf16_view_to_ascii_uppercase(&view))
## Ported from AK/Utf16FlyString.h:80 AK::Utf16FlyString::to_ascii_titlecase.
pub func utf16_fly_string_to_ascii_titlecase(this: const Utf16FlyString*) -> Utf16FlyString:
let view = utf16_fly_string_view(this)
return utf16_fly_string_from_utf16_string(utf16_view_to_ascii_titlecase(&view))
## Ported from AK/Utf16FlyString.h:87 AK::Utf16FlyString::to_number<i8>.
pub func utf16_fly_string_to_number_i8(this: const Utf16FlyString*, trim_whitespace: TrimWhitespace = TrimWhitespace.yes, base_: i32 = 10) -> i8?:
return utf16_string_base_to_number_i8(&this.m_data, trim_whitespace, base_)
## Ported from AK/Utf16FlyString.h:87 AK::Utf16FlyString::to_number<i16>.
pub func utf16_fly_string_to_number_i16(this: const Utf16FlyString*, trim_whitespace: TrimWhitespace = TrimWhitespace.yes, base_: i32 = 10) -> i16?:
return utf16_string_base_to_number_i16(&this.m_data, trim_whitespace, base_)
## Ported from AK/Utf16FlyString.h:87 AK::Utf16FlyString::to_number<i32>.
pub func utf16_fly_string_to_number_i32(this: const Utf16FlyString*, trim_whitespace: TrimWhitespace = TrimWhitespace.yes, base_: i32 = 10) -> i32?:
return utf16_string_base_to_number_i32(&this.m_data, trim_whitespace, base_)
## Ported from AK/Utf16FlyString.h:87 AK::Utf16FlyString::to_number<i64>.
pub func utf16_fly_string_to_number_i64(this: const Utf16FlyString*, trim_whitespace: TrimWhitespace = TrimWhitespace.yes, base_: i32 = 10) -> i64?:
return utf16_string_base_to_number_i64(&this.m_data, trim_whitespace, base_)
## Ported from AK/Utf16FlyString.h:87 AK::Utf16FlyString::to_number<u8>.
pub func utf16_fly_string_to_number_u8(this: const Utf16FlyString*, trim_whitespace: TrimWhitespace = TrimWhitespace.yes, base_: i32 = 10) -> u8?:
return utf16_string_base_to_number_u8(&this.m_data, trim_whitespace, base_)
## Ported from AK/Utf16FlyString.h:87 AK::Utf16FlyString::to_number<u16>.
pub func utf16_fly_string_to_number_u16(this: const Utf16FlyString*, trim_whitespace: TrimWhitespace = TrimWhitespace.yes, base_: i32 = 10) -> u16?:
return utf16_string_base_to_number_u16(&this.m_data, trim_whitespace, base_)
## Ported from AK/Utf16FlyString.h:87 AK::Utf16FlyString::to_number<u32>.
pub func utf16_fly_string_to_number_u32(this: const Utf16FlyString*, trim_whitespace: TrimWhitespace = TrimWhitespace.yes, base_: i32 = 10) -> u32?:
return utf16_string_base_to_number_u32(&this.m_data, trim_whitespace, base_)
## Ported from AK/Utf16FlyString.h:87 AK::Utf16FlyString::to_number<u64>.
pub func utf16_fly_string_to_number_u64(this: const Utf16FlyString*, trim_whitespace: TrimWhitespace = TrimWhitespace.yes, base_: i32 = 10) -> u64?:
return utf16_string_base_to_number_u64(&this.m_data, trim_whitespace, base_)
## Ported from AK/Utf16FlyString.h:87 AK::Utf16FlyString::to_number<f32>.
pub func utf16_fly_string_to_number_f32(this: const Utf16FlyString*, trim_whitespace: TrimWhitespace = TrimWhitespace.yes, base_: i32 = 10) -> f32?:
return utf16_string_base_to_number_f32(&this.m_data, trim_whitespace, base_)
## Ported from AK/Utf16FlyString.h:87 AK::Utf16FlyString::to_number<f64>.
pub func utf16_fly_string_to_number_f64(this: const Utf16FlyString*, trim_whitespace: TrimWhitespace = TrimWhitespace.yes, base_: i32 = 10) -> f64?:
return utf16_string_base_to_number_f64(&this.m_data, trim_whitespace, base_)
# mark: Comparison and queries ==================================================================
## Ported from AK/Utf16FlyString.h:97 AK::Utf16FlyString::operator==(Utf16FlyString const&).
pub func utf16_fly_string_eq_utf16_fly_string(this: const Utf16FlyString*, other: const Utf16FlyString*) -> bool:
return utf16_string_base_raw_utf16_fly_string(&this.m_data) == utf16_string_base_raw_utf16_fly_string(&other.m_data)
## Ported from AK/Utf16FlyString.h:98 AK::Utf16FlyString::operator==(Utf16String const&).
pub func utf16_fly_string_eq_utf16_string(this: const Utf16FlyString*, other: const Utf16String*) -> bool:
return utf16_string_base_eq_utf16_string_base(&this.m_data, &other.utf16_string_base)
## Ported from AK/Utf16FlyString.h:99 AK::Utf16FlyString::operator==(Utf16View const&).
pub func utf16_fly_string_eq_utf16_view(this: const Utf16FlyString*, other: const Utf16View*) -> bool:
return utf16_string_base_eq_utf16_view(&this.m_data, other)
## Ported from AK/Utf16FlyString.h:100 AK::Utf16FlyString::operator==(StringView).
pub func utf16_fly_string_eq_string_view(this: const Utf16FlyString*, other: StringView) -> bool:
return utf16_string_base_eq_string_view(&this.m_data, other)
## Ported from AK/Utf16FlyString.h:102 AK::Utf16FlyString::operator<=>(Utf16FlyString const&).
pub func utf16_fly_string_cmp_utf16_fly_string(this: const Utf16FlyString*, other: const Utf16FlyString*) -> i32:
return utf16_string_base_cmp_utf16_string_base(&this.m_data, &other.m_data)
## Ported from AK/Utf16FlyString.h:103 AK::Utf16FlyString::operator<=>(Utf16View const&).
pub func utf16_fly_string_cmp_utf16_view(this: const Utf16FlyString*, other: const Utf16View*) -> i32:
return utf16_string_base_cmp_utf16_view(&this.m_data, other)
## Ported from AK/Utf16FlyString.h:105 AK::Utf16FlyString::equals_ignoring_ascii_case(Utf16FlyString const&).
pub func utf16_fly_string_equals_ignoring_ascii_case_utf16_fly_string(this: const Utf16FlyString*, other: const Utf16FlyString*) -> bool:
if utf16_fly_string_eq_utf16_fly_string(this, other):
return true
return utf16_string_base_equals_ignoring_ascii_case_utf16_string_base(&this.m_data, &other.m_data)
## Ported from AK/Utf16FlyString.h:112 AK::Utf16FlyString::equals_ignoring_ascii_case(Utf16View const&).
pub func utf16_fly_string_equals_ignoring_ascii_case_utf16_view(this: const Utf16FlyString*, other: const Utf16View*) -> bool:
return utf16_string_base_equals_ignoring_ascii_case_utf16_view(&this.m_data, other)
## Ported from AK/Utf16FlyString.h:126 AK::Utf16FlyString::hash.
pub func utf16_fly_string_hash(this: const Utf16FlyString*) -> u32:
return utf16_string_base_hash(&this.m_data)
## Ported from AK/Utf16FlyString.h:127 AK::Utf16FlyString::is_empty.
pub func utf16_fly_string_is_empty(this: const Utf16FlyString*) -> bool:
return utf16_string_base_is_empty(&this.m_data)
## Ported from AK/Utf16FlyString.h:128 AK::Utf16FlyString::is_ascii.
pub func utf16_fly_string_is_ascii(this: const Utf16FlyString*) -> bool:
return utf16_string_base_is_ascii(&this.m_data)
## Ported from AK/Utf16FlyString.h:130 AK::Utf16FlyString::length_in_code_units.
pub func utf16_fly_string_length_in_code_units(this: const Utf16FlyString*) -> usize:
return utf16_string_base_length_in_code_units(&this.m_data)
## Ported from AK/Utf16FlyString.h:131 AK::Utf16FlyString::length_in_code_points.
pub func utf16_fly_string_length_in_code_points(this: const Utf16FlyString*) -> usize:
return utf16_string_base_length_in_code_points(&this.m_data)
## Ported from AK/Utf16FlyString.h:133 AK::Utf16FlyString::code_unit_at.
pub func utf16_fly_string_code_unit_at(this: const Utf16FlyString*, code_unit_offset: usize) -> u16:
return utf16_string_base_code_unit_at(&this.m_data, code_unit_offset)
## Ported from AK/Utf16FlyString.h:134 AK::Utf16FlyString::code_point_at.
pub func utf16_fly_string_code_point_at(this: const Utf16FlyString*, code_unit_offset: usize) -> u32:
return utf16_string_base_code_point_at(&this.m_data, code_unit_offset)
## Ported from AK/Utf16FlyString.h:136 AK::Utf16FlyString::code_unit_offset_of.
pub func utf16_fly_string_code_unit_offset_of(this: const Utf16FlyString*, code_point_offset: usize) -> usize:
return utf16_string_base_code_unit_offset_of(&this.m_data, code_point_offset)
## Ported from AK/Utf16FlyString.h:137 AK::Utf16FlyString::code_point_offset_of.
pub func utf16_fly_string_code_point_offset_of(this: const Utf16FlyString*, code_unit_offset: usize) -> usize:
return utf16_string_base_code_point_offset_of(&this.m_data, code_unit_offset)
## Ported from AK/Utf16FlyString.cpp:100 AK::Utf16FlyString::number_of_utf16_fly_strings. This
## is primarily interesting to unit tests.
pub func utf16_fly_string_number_of_utf16_fly_strings() -> usize:
return hash_table_size(all_utf16_fly_strings())
## Ported from AK/Utf16FlyString.h:150 AK::Utf16FlyString::from_ascii_short_string_without_validation(char const*, size_t).
pub func utf16_fly_string_from_ascii_short_string_without_validation_u8(data: const u8[]) -> Utf16FlyString:
let string = utf16_string_from_ascii_short_string_without_validation_u8(data)
return Utf16FlyString(m_data = string.utf16_string_base)
## Ported from AK/Utf16FlyString.h:159 AK::Utf16FlyString::from_ascii_short_string_without_validation(char16_t const*, size_t).
pub func utf16_fly_string_from_ascii_short_string_without_validation_u16(data: const u16[]) -> Utf16FlyString:
let string = utf16_string_from_ascii_short_string_without_validation_u16(data)
return Utf16FlyString(m_data = string.utf16_string_base)
## Ported from AK/Utf16FlyString.h:169 AK::Utf16FlyString::Utf16FlyString(Utf16StringBase).
pub func utf16_fly_string_construct_utf16_string_base(this: Utf16FlyString*, data: Utf16StringBase):
this.m_data = data
# mark: Literals ================================================================================
## Ported from AK/Utf16FlyString.h:223 operator""_utf16_fly_string(char const*, size_t).
pub func literalutf16_fly_string_u8(string: const u8*?, length: usize) -> Utf16FlyString:
let view = literalsv_u8(string, length)
# OPTIMIZATION: Short ASCII strings become compile-time constants with no runtime validation
# or table lookup.
if length <= max_short_string_byte_count and string_view_is_ascii(&view):
return utf16_fly_string_from_ascii_short_string_without_validation_u8(string_view_bytes(&view))
let utf8 = Utf8View(m_string = view)
assert(utf8_view_validate_allow_lonely_surrogates(&utf8))
return utf16_fly_string_from_utf8_without_validation(view)
## Ported from AK/Utf16FlyString.h:237 operator""_utf16_fly_string(char16_t const*, size_t).
pub func literalutf16_fly_string_u16(string: const u16*?, length: usize) -> Utf16FlyString:
let view = literalsv_u16(string, length)
# OPTIMIZATION: Short ASCII strings become compile-time constants with no runtime work.
if length <= max_short_string_byte_count and utf16_view_is_ascii(&view):
return utf16_fly_string_from_ascii_short_string_without_validation_u16(utf16_view_utf16_span(&view))
return utf16_fly_string_from_utf16(view)