mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-03 21:10:30 +00:00
AK: Remove the API to explicitly construct short strings
Now that ""_string is infallible, the only benefit of explicitly constructing a short string is the ability to do it at compile-time. But we never do that, so let's simplify the API and remove this implementation detail from it.
This commit is contained in:
parent
3f35ffb648
commit
fde26c53f0
Notes:
sideshowbarker
2024-07-17 17:38:29 +09:00
Author: https://github.com/LucasChollet Commit: https://github.com/SerenityOS/serenity/commit/fde26c53f0 Pull-request: https://github.com/SerenityOS/serenity/pull/20418
2 changed files with 2 additions and 41 deletions
20
AK/String.h
20
AK/String.h
|
@ -73,21 +73,6 @@ public:
|
|||
// Creates a new String by reading byte_count bytes from a UTF-8 encoded Stream.
|
||||
static ErrorOr<String> from_stream(Stream&, size_t byte_count);
|
||||
|
||||
// Creates a new String from a short sequence of UTF-8 encoded code points. If the provided string
|
||||
// does not fit in the short string storage, a compilation error will be emitted.
|
||||
static AK_SHORT_STRING_CONSTEVAL String from_utf8_short_string(StringView string)
|
||||
{
|
||||
VERIFY(string.length() <= MAX_SHORT_STRING_BYTE_COUNT);
|
||||
VERIFY(Utf8View { string }.validate());
|
||||
|
||||
ShortString short_string;
|
||||
for (size_t i = 0; i < string.length(); ++i)
|
||||
short_string.storage[i] = string.characters_without_null_termination()[i];
|
||||
short_string.byte_count_and_short_string_flag = (string.length() << 1) | SHORT_STRING_FLAG;
|
||||
|
||||
return String { short_string };
|
||||
}
|
||||
|
||||
// Creates a new String from a single code point.
|
||||
static constexpr String from_code_point(u32 code_point)
|
||||
{
|
||||
|
@ -284,8 +269,3 @@ struct Formatter<String> : Formatter<StringView> {
|
|||
{
|
||||
return AK::String::from_utf8(AK::StringView(cstring, length)).release_value();
|
||||
}
|
||||
|
||||
[[nodiscard]] ALWAYS_INLINE AK_SHORT_STRING_CONSTEVAL AK::String operator""_short_string(char const* cstring, size_t length)
|
||||
{
|
||||
return AK::String::from_utf8_short_string(AK::StringView(cstring, length));
|
||||
}
|
||||
|
|
|
@ -47,40 +47,21 @@ TEST_CASE(short_strings)
|
|||
EXPECT_EQ(string1.bytes().size(), 7u);
|
||||
EXPECT_EQ(string1.bytes_as_string_view(), "abcdefg"sv);
|
||||
|
||||
constexpr auto string2 = String::from_utf8_short_string("abcdefg"sv);
|
||||
auto string2 = "abcdefg"_string;
|
||||
EXPECT_EQ(string2.is_short_string(), true);
|
||||
EXPECT_EQ(string2.bytes().size(), 7u);
|
||||
EXPECT_EQ(string2, string1);
|
||||
|
||||
auto string3 = "abcdefg"_string;
|
||||
EXPECT_EQ(string3.is_short_string(), true);
|
||||
EXPECT_EQ(string3.bytes().size(), 7u);
|
||||
EXPECT_EQ(string3, string1);
|
||||
|
||||
constexpr auto string4 = "abcdefg"_short_string;
|
||||
EXPECT_EQ(string4.is_short_string(), true);
|
||||
EXPECT_EQ(string4.bytes().size(), 7u);
|
||||
EXPECT_EQ(string4, string1);
|
||||
#else
|
||||
auto string1 = MUST(String::from_utf8("abc"sv));
|
||||
EXPECT_EQ(string1.is_short_string(), true);
|
||||
EXPECT_EQ(string1.bytes().size(), 3u);
|
||||
EXPECT_EQ(string1.bytes_as_string_view(), "abc"sv);
|
||||
|
||||
constexpr auto string2 = String::from_utf8_short_string("abc"sv);
|
||||
auto string2 = "abc"_string;
|
||||
EXPECT_EQ(string2.is_short_string(), true);
|
||||
EXPECT_EQ(string2.bytes().size(), 3u);
|
||||
EXPECT_EQ(string2, string1);
|
||||
|
||||
auto string3 = "abc"_string;
|
||||
EXPECT_EQ(string3.is_short_string(), true);
|
||||
EXPECT_EQ(string3.bytes().size(), 3u);
|
||||
EXPECT_EQ(string3, string1);
|
||||
|
||||
constexpr auto string4 = "abc"_short_string;
|
||||
EXPECT_EQ(string4.is_short_string(), true);
|
||||
EXPECT_EQ(string4.bytes().size(), 3u);
|
||||
EXPECT_EQ(string4, string1);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue