/* * Copyright (c) 2018-2022, Andreas Kling * Copyright (c) 2023-2024, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace AK { // String is a strongly owned sequence of Unicode code points encoded as UTF-8. // The data may or may not be heap-allocated, and may or may not be reference counted. // There is no guarantee that the underlying bytes are null-terminated. class String : public Detail::StringBase { AK_MAKE_DEFAULT_COPYABLE(String); AK_MAKE_DEFAULT_MOVABLE(String); public: // NOTE: For short strings, we avoid heap allocations by storing them in the data pointer slot. static constexpr size_t MAX_SHORT_STRING_BYTE_COUNT = Detail::MAX_SHORT_STRING_BYTE_COUNT; using StringBase::StringBase; // Creates a new String from a sequence of UTF-8 encoded code points. static ErrorOr from_utf8(StringView); enum class WithBOMHandling { Yes, No, }; // Creates a new String using the replacement character for invalid bytes [[nodiscard]] static String from_utf8_with_replacement_character(StringView, WithBOMHandling = WithBOMHandling::Yes); template requires(IsOneOf, ByteString, FlyString, String>) static ErrorOr from_utf8(T&&) = delete; [[nodiscard]] static String from_utf8_without_validation(ReadonlyBytes); [[nodiscard]] static String from_ascii_without_validation(ReadonlyBytes); [[nodiscard]] static constexpr String from_ascii_short_string_without_validation(char const* data, size_t length) { VERIFY(length <= Detail::MAX_SHORT_STRING_BYTE_COUNT); auto short_string = Detail::ShortString::create_with_byte_count(length); for (size_t i = 0; i < length; ++i) short_string.storage[i] = static_cast(data[i]); return String { StringBase { short_string } }; } static ErrorOr from_string_builder(Badge, StringBuilder&); [[nodiscard]] static String from_string_builder_without_validation(Badge, StringBuilder&); // Creates a new String from a sequence of UTF-16 encoded code points. static ErrorOr from_utf16_le_with_replacement_character(ReadonlyBytes); static ErrorOr from_utf16_be_with_replacement_character(ReadonlyBytes); // Creates a new String by reading byte_count bytes from a UTF-8 encoded Stream. static ErrorOr from_stream(Stream&, size_t byte_count); // Creates a new String from a single code point. static constexpr String from_code_point(u32 code_point) { VERIFY(is_unicode(code_point)); String string; string.replace_with_new_short_string(UnicodeUtils::bytes_to_store_code_point_in_utf8(code_point), [&](Bytes buffer) { size_t i = 0; (void)UnicodeUtils::code_point_to_utf8(code_point, [&](auto byte) { buffer[i++] = static_cast(byte); }); }); return string; } // Creates a new String with a single code point repeated N times. static ErrorOr repeated(u32 code_point, size_t count); // Creates a new String from another string, repeated N times. static ErrorOr repeated(String const&, size_t count); enum class Case { Upper, Lower, }; [[nodiscard]] static String bijective_base_from(size_t value, Case, unsigned base = 26, Optional map = {}); [[nodiscard]] static String greek_letter_from(size_t value); [[nodiscard]] static String roman_number_from(size_t value, Case); // Creates a new String by case-transforming this String. Using these methods require linking LibUnicode into your application. ErrorOr to_lowercase(Optional const& locale = {}) const; ErrorOr to_uppercase(Optional const& locale = {}) const; ErrorOr to_titlecase(Optional const& locale = {}, TrailingCodePointTransformation trailing_code_point_transformation = TrailingCodePointTransformation::Lowercase) const; ErrorOr to_casefold() const; ErrorOr to_fullwidth() const; [[nodiscard]] String to_ascii_lowercase() const; [[nodiscard]] String to_ascii_uppercase() const; [[nodiscard]] bool is_ascii() const { return bytes_as_string_view().is_ascii(); } // Compare this String against another string with caseless matching. Using this method requires linking LibUnicode into your application. [[nodiscard]] bool equals_ignoring_case(String const&) const; [[nodiscard]] bool equals_ignoring_ascii_case(String const&) const; [[nodiscard]] bool equals_ignoring_ascii_case(StringView) const; [[nodiscard]] bool starts_with(u32 code_point) const; [[nodiscard]] bool starts_with_bytes(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const; [[nodiscard]] bool ends_with(u32 code_point) const; [[nodiscard]] bool ends_with_bytes(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const; // Creates a substring with a deep copy of the specified data window. ErrorOr substring_from_byte_offset(size_t start, size_t byte_count) const; ErrorOr substring_from_byte_offset(size_t start) const; // Creates a substring that strongly references the origin superstring instead of making a deep copy of the data. ErrorOr substring_from_byte_offset_with_shared_superstring(size_t start, size_t byte_count) const; ErrorOr substring_from_byte_offset_with_shared_superstring(size_t start) const; // Returns an iterable view over the Unicode code points. [[nodiscard]] Utf8View code_points() const&; [[nodiscard]] Utf8View code_points() const&& = delete; // Returns true if the String is zero-length. [[nodiscard]] bool is_empty() const { return byte_count() == 0; } // Returns a StringView covering the full length of the string. Note that iterating this will go byte-at-a-time, not code-point-at-a-time. [[nodiscard]] StringView bytes_as_string_view() const& LIFETIME_BOUND { return StringView(bytes()); } [[nodiscard]] StringView bytes_as_string_view() const&& = delete; [[nodiscard]] size_t count(StringView needle) const { return StringUtils::count(bytes_as_string_view(), needle); } ErrorOr replace(StringView needle, StringView replacement, ReplaceMode replace_mode) const; ErrorOr reverse() const; ErrorOr trim(Utf8View const& code_points_to_trim, TrimMode mode = TrimMode::Both) const; ErrorOr trim(StringView code_points_to_trim, TrimMode mode = TrimMode::Both) const; ErrorOr trim_whitespace(TrimMode mode = TrimMode::Both) const; ErrorOr trim_ascii_whitespace(TrimMode mode = TrimMode::Both) const; ErrorOr> split_limit(u32 separator, size_t limit, SplitBehavior = SplitBehavior::Nothing) const; ErrorOr> split(u32 separator, SplitBehavior = SplitBehavior::Nothing) const; Optional find_byte_offset(u32 code_point, size_t from_byte_offset = 0) const; Optional find_byte_offset(StringView substring, size_t from_byte_offset = 0) const; // Using this method requires linking LibUnicode into your application. Optional find_byte_offset_ignoring_case(StringView, size_t from_byte_offset = 0) const; [[nodiscard]] bool operator==(String const&) const = default; [[nodiscard]] bool operator==(FlyString const&) const; [[nodiscard]] bool operator==(StringView) const; [[nodiscard]] bool operator==(char const* cstring) const; // NOTE: UTF-8 is defined in a way that lexicographic ordering of code points is equivalent to lexicographic ordering of bytes. [[nodiscard]] int operator<=>(String const& other) const { return this->bytes_as_string_view().compare(other.bytes_as_string_view()); } template [[nodiscard]] ALWAYS_INLINE constexpr bool is_one_of(Ts&&... strings) const { return (this->operator==(forward(strings)) || ...); } [[nodiscard]] bool contains(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const; [[nodiscard]] bool contains(u32, CaseSensitivity = CaseSensitivity::CaseSensitive) const; [[nodiscard]] u32 ascii_case_insensitive_hash() const; template [[nodiscard]] static String number(T); template [[nodiscard]] static String number(T value) { return MUST(formatted("{}", value)); } template Optional to_number(TrimWhitespace trim_whitespace = TrimWhitespace::Yes, int base = 10) const { return bytes_as_string_view().to_number(trim_whitespace, base); } static ErrorOr vformatted(StringView fmtstr, TypeErasedFormatParams&); template static ErrorOr formatted(CheckedFormatString&& fmtstr, Parameters const&... parameters) { VariadicFormatParams variadic_format_parameters { parameters... }; return vformatted(fmtstr.view(), variadic_format_parameters); } template static ErrorOr join(SeparatorType const& separator, CollectionType const& collection, StringView fmtstr = "{}"sv) { StringBuilder builder; TRY(builder.try_join(separator, collection, fmtstr)); return builder.to_string(); } // FIXME: Remove these once all code has been ported to String [[nodiscard]] ByteString to_byte_string() const; static ErrorOr from_byte_string(ByteString const&); template requires(IsSame, StringView>) static ErrorOr from_byte_string(T&&) = delete; private: friend class ::AK::FlyString; friend struct SentinelOptionalTraits; using ShortString = Detail::ShortString; constexpr bool is_invalid() const { return raw(Badge {}) == 0; } explicit constexpr String(StringBase&& base) : StringBase(move(base)) { } explicit constexpr String(nullptr_t) : StringBase(nullptr) { } }; template<> struct SentinelOptionalTraits { static constexpr String sentinel_value() { return String(nullptr); } static constexpr bool is_sentinel(String const& value) { return value.is_invalid(); } }; template<> class Optional : public SentinelOptional { public: using SentinelOptional::SentinelOptional; }; template<> struct Traits : public DefaultTraits { static unsigned hash(String const&); }; template<> struct Formatter : Formatter { ErrorOr format(FormatBuilder&, String const&); }; struct ASCIICaseInsensitiveStringTraits : public Traits { static unsigned hash(String const& s) { return s.ascii_case_insensitive_hash(); } static bool equals(String const& a, String const& b) { return a.bytes_as_string_view().equals_ignoring_ascii_case(b.bytes_as_string_view()); } }; } [[nodiscard]] ALWAYS_INLINE constexpr AK::String operator""_string(char const* cstring, size_t length) { // OPTIMIZATION: Short ASCII strings become compile-time constants with no runtime validation or heap allocation. if (length <= AK::Detail::MAX_SHORT_STRING_BYTE_COUNT && AK::all_of(cstring, cstring + length, AK::is_ascii)) { return AK::String::from_ascii_short_string_without_validation(cstring, length); } ASSERT(Utf8View(AK::StringView(cstring, length)).validate()); return AK::String::from_utf8_without_validation({ cstring, length }); }