diff --git a/AK/ByteString.cpp b/AK/ByteString.cpp index 97464213f93..0e8b30636cd 100644 --- a/AK/ByteString.cpp +++ b/AK/ByteString.cpp @@ -12,7 +12,6 @@ #include #include #include -#include #include namespace AK { @@ -52,16 +51,6 @@ bool ByteString::copy_characters_to_buffer(char* buffer, size_t buffer_size) con return characters_to_copy == length(); } -ByteString ByteString::isolated_copy() const -{ - if (m_impl->length() == 0) - return empty(); - char* buffer; - auto impl = ByteStringImpl::create_uninitialized(length(), buffer); - memcpy(buffer, m_impl->characters(), m_impl->length()); - return impl; -} - ByteString ByteString::substring(size_t start, size_t length) const { if (!length) @@ -199,88 +188,6 @@ ByteString ByteString::repeated(StringView string, size_t count) return impl; } -ByteString ByteString::bijective_base_from(size_t value, unsigned base, StringView map) -{ - value++; - if (map.is_null()) - map = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"sv; - - VERIFY(base >= 2 && base <= map.length()); - - // The '8 bits per byte' assumption may need to go? - Array buffer; - size_t i = 0; - do { - auto remainder = value % base; - auto new_value = value / base; - if (remainder == 0) { - new_value--; - remainder = map.length(); - } - - buffer[i++] = map[remainder - 1]; - value = new_value; - } while (value > 0); - - for (size_t j = 0; j < i / 2; ++j) - swap(buffer[j], buffer[i - j - 1]); - - return ByteString { ReadonlyBytes(buffer.data(), i) }; -} - -ByteString ByteString::roman_number_from(size_t value) -{ - if (value > 3999) - return ByteString::number(value); - - StringBuilder builder; - - while (value > 0) { - if (value >= 1000) { - builder.append('M'); - value -= 1000; - } else if (value >= 900) { - builder.append("CM"sv); - value -= 900; - } else if (value >= 500) { - builder.append('D'); - value -= 500; - } else if (value >= 400) { - builder.append("CD"sv); - value -= 400; - } else if (value >= 100) { - builder.append('C'); - value -= 100; - } else if (value >= 90) { - builder.append("XC"sv); - value -= 90; - } else if (value >= 50) { - builder.append('L'); - value -= 50; - } else if (value >= 40) { - builder.append("XL"sv); - value -= 40; - } else if (value >= 10) { - builder.append('X'); - value -= 10; - } else if (value == 9) { - builder.append("IX"sv); - value -= 9; - } else if (value >= 5 && value <= 8) { - builder.append('V'); - value -= 5; - } else if (value == 4) { - builder.append("IV"sv); - value -= 4; - } else if (value <= 3) { - builder.append('I'); - value -= 1; - } - } - - return builder.to_byte_string(); -} - bool ByteString::matches(StringView mask, Vector& mask_spans, CaseSensitivity case_sensitivity) const { return StringUtils::matches(*this, mask, case_sensitivity, &mask_spans); @@ -391,16 +298,4 @@ Vector ByteString::find_all(StringView needle) const return StringUtils::find_all(*this, needle); } -Utf8CodePointIterator ByteString::code_points() const& -{ - return Utf8CodePointIterator { reinterpret_cast(characters()), length() }; -} - -ErrorOr ByteString::from_utf8(ReadonlyBytes bytes) -{ - if (!Utf8View(bytes).validate()) - return Error::from_string_literal("ByteString::from_utf8: Input was not valid UTF-8"); - return ByteStringImpl::create(bytes); -} - } diff --git a/AK/ByteString.h b/AK/ByteString.h index 80a2d9ca513..940de438f3b 100644 --- a/AK/ByteString.h +++ b/AK/ByteString.h @@ -88,11 +88,6 @@ public: ByteString(FlyString const&); - static ErrorOr from_utf8(ReadonlyBytes); - static ErrorOr from_utf8(StringView string) { return from_utf8(string.bytes()); } - static ByteString must_from_utf8(StringView string) { return MUST(from_utf8(string)); } - static ByteString from_utf8_without_validation(StringView string) { return ByteString { string }; } - template< typename F, typename PossiblyErrorOr = decltype(declval()(declval())), @@ -113,9 +108,6 @@ public: [[nodiscard]] static ByteString repeated(char, size_t count); [[nodiscard]] static ByteString repeated(StringView, size_t count); - [[nodiscard]] static ByteString bijective_base_from(size_t value, unsigned base = 26, StringView map = {}); - [[nodiscard]] static ByteString roman_number_from(size_t value); - template [[nodiscard]] static ByteString join(SeparatorType const& separator, CollectionType const& collection, StringView fmtstr = "{}"sv) { @@ -127,6 +119,12 @@ public: [[nodiscard]] bool matches(StringView mask, CaseSensitivity = CaseSensitivity::CaseInsensitive) const; [[nodiscard]] bool matches(StringView mask, Vector&, CaseSensitivity = CaseSensitivity::CaseInsensitive) const; + template + [[nodiscard]] static ByteString number(T value) + { + return formatted("{}", value); + } + template Optional to_number(TrimWhitespace trim_whitespace = TrimWhitespace::Yes) const { @@ -139,9 +137,6 @@ public: [[nodiscard]] bool is_whitespace() const { return StringUtils::is_whitespace(*this); } - [[nodiscard]] Utf8CodePointIterator code_points() const&; - [[nodiscard]] Utf8CodePointIterator code_points() const&& = delete; - [[nodiscard]] ByteString trim(StringView characters, TrimMode mode = TrimMode::Both) const { auto trimmed_view = StringUtils::trim(view(), characters, mode); @@ -236,8 +231,6 @@ public: bool operator==(char const* cstring) const; - [[nodiscard]] ByteString isolated_copy() const; - [[nodiscard]] static ByteString empty() { return ByteStringImpl::the_empty_stringimpl(); @@ -290,12 +283,6 @@ public: return vformatted(fmtstr.view(), variadic_format_parameters); } - template - [[nodiscard]] static ByteString number(T value) - { - return formatted("{}", value); - } - [[nodiscard]] StringView view() const& { return { characters(), length() }; } [[nodiscard]] StringView view() const&& = delete; diff --git a/Tests/AK/TestByteString.cpp b/Tests/AK/TestByteString.cpp index a62df767d5c..9158ca191f9 100644 --- a/Tests/AK/TestByteString.cpp +++ b/Tests/AK/TestByteString.cpp @@ -258,36 +258,3 @@ TEST_CASE(find_with_empty_needle) EXPECT_EQ(string.find(""sv), 0u); EXPECT_EQ(string.find_all(""sv), (Vector { 0u, 1u, 2u, 3u })); } - -TEST_CASE(bijective_base) -{ - EXPECT_EQ(ByteString::bijective_base_from(0), "A"); - EXPECT_EQ(ByteString::bijective_base_from(25), "Z"); - EXPECT_EQ(ByteString::bijective_base_from(26), "AA"); - EXPECT_EQ(ByteString::bijective_base_from(52), "BA"); - EXPECT_EQ(ByteString::bijective_base_from(701), "ZZ"); - EXPECT_EQ(ByteString::bijective_base_from(702), "AAA"); - EXPECT_EQ(ByteString::bijective_base_from(730), "ABC"); - EXPECT_EQ(ByteString::bijective_base_from(18277), "ZZZ"); -} - -TEST_CASE(roman_numerals) -{ - auto zero = ByteString::roman_number_from(0); - EXPECT_EQ(zero, ""); - - auto one = ByteString::roman_number_from(1); - EXPECT_EQ(one, "I"); - - auto nine = ByteString::roman_number_from(9); - EXPECT_EQ(nine, "IX"); - - auto fourty_eight = ByteString::roman_number_from(48); - EXPECT_EQ(fourty_eight, "XLVIII"); - - auto one_thousand_nine_hundred_ninety_eight = ByteString::roman_number_from(1998); - EXPECT_EQ(one_thousand_nine_hundred_ninety_eight, "MCMXCVIII"); - - auto four_thousand = ByteString::roman_number_from(4000); - EXPECT_EQ(four_thousand, "4000"); -} diff --git a/Tests/AK/TestString.cpp b/Tests/AK/TestString.cpp index d94dfd80efb..3bf5c38a2bf 100644 --- a/Tests/AK/TestString.cpp +++ b/Tests/AK/TestString.cpp @@ -1528,3 +1528,36 @@ TEST_CASE(is_ascii) EXPECT(!"😀"_string.is_ascii()); EXPECT(!"abcdefghijklmnopqrstuvwxyz😀ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789😀!@#$%^&*()"_string.is_ascii()); } + +TEST_CASE(bijective_base) +{ + EXPECT_EQ(String::bijective_base_from(0, String::Case::Upper), "A"sv); + EXPECT_EQ(String::bijective_base_from(25, String::Case::Upper), "Z"sv); + EXPECT_EQ(String::bijective_base_from(26, String::Case::Upper), "AA"sv); + EXPECT_EQ(String::bijective_base_from(52, String::Case::Upper), "BA"sv); + EXPECT_EQ(String::bijective_base_from(701, String::Case::Upper), "ZZ"sv); + EXPECT_EQ(String::bijective_base_from(702, String::Case::Upper), "AAA"sv); + EXPECT_EQ(String::bijective_base_from(730, String::Case::Upper), "ABC"sv); + EXPECT_EQ(String::bijective_base_from(18277, String::Case::Upper), "ZZZ"sv); +} + +TEST_CASE(roman_numerals) +{ + auto zero = String::roman_number_from(0, String::Case::Upper); + EXPECT_EQ(zero, ""sv); + + auto one = String::roman_number_from(1, String::Case::Upper); + EXPECT_EQ(one, "I"sv); + + auto nine = String::roman_number_from(9, String::Case::Upper); + EXPECT_EQ(nine, "IX"sv); + + auto fourty_eight = String::roman_number_from(48, String::Case::Upper); + EXPECT_EQ(fourty_eight, "XLVIII"sv); + + auto one_thousand_nine_hundred_ninety_eight = String::roman_number_from(1998, String::Case::Upper); + EXPECT_EQ(one_thousand_nine_hundred_ninety_eight, "MCMXCVIII"sv); + + auto four_thousand = String::roman_number_from(4000, String::Case::Upper); + EXPECT_EQ(four_thousand, "4000"sv); +}