/* * Copyright (c) 2018-2020, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include namespace AK { // DeprecatedString is a convenience wrapper around StringImpl, suitable for passing // around as a value type. It's basically the same as passing around a // RefPtr, with a bit of syntactic sugar. // // Note that StringImpl is an immutable object that cannot shrink or grow. // Its allocation size is snugly tailored to the specific string it contains. // Copying a DeprecatedString is very efficient, since the internal StringImpl is // retainable and so copying only requires modifying the ref count. // // There are three main ways to construct a new DeprecatedString: // // s = DeprecatedString("some literal"); // // s = DeprecatedString::formatted("{} little piggies", m_piggies); // // StringBuilder builder; // builder.append("abc"); // builder.append("123"); // s = builder.to_deprecated_string(); class DeprecatedString { public: ~DeprecatedString() = default; DeprecatedString() : m_impl(StringImpl::the_empty_stringimpl()) { } DeprecatedString(StringView view) : m_impl(*StringImpl::create(view.characters_without_null_termination(), view.length())) { } DeprecatedString(DeprecatedString const& other) : m_impl(other.m_impl) { } DeprecatedString(DeprecatedString&& other) : m_impl(move(other.m_impl)) { other.m_impl = StringImpl::the_empty_stringimpl(); } DeprecatedString(char const* cstring, ShouldChomp shouldChomp = NoChomp) : m_impl(*StringImpl::create(cstring, shouldChomp)) { } DeprecatedString(char const* cstring, size_t length, ShouldChomp shouldChomp = NoChomp) : m_impl(*StringImpl::create(cstring, length, shouldChomp)) { } explicit DeprecatedString(ReadonlyBytes bytes, ShouldChomp shouldChomp = NoChomp) : m_impl(*StringImpl::create(bytes, shouldChomp)) { } DeprecatedString(StringImpl const& impl) : m_impl(impl) { } DeprecatedString(NonnullRefPtr&& impl) : m_impl(*move(impl)) { } DeprecatedString(DeprecatedFlyString const&); static ErrorOr from_utf8(ReadonlyBytes); static ErrorOr from_utf8(StringView string) { return from_utf8(string.bytes()); } [[nodiscard]] static DeprecatedString repeated(char, size_t count); [[nodiscard]] static DeprecatedString repeated(StringView, size_t count); [[nodiscard]] static DeprecatedString bijective_base_from(size_t value, unsigned base = 26, StringView map = {}); [[nodiscard]] static DeprecatedString roman_number_from(size_t value); template [[nodiscard]] static DeprecatedString join(SeparatorType const& separator, CollectionType const& collection, StringView fmtstr = "{}"sv) { StringBuilder builder; builder.join(separator, collection, fmtstr); return builder.to_deprecated_string(); } [[nodiscard]] bool matches(StringView mask, CaseSensitivity = CaseSensitivity::CaseInsensitive) const; [[nodiscard]] bool matches(StringView mask, Vector&, CaseSensitivity = CaseSensitivity::CaseInsensitive) const; template [[nodiscard]] Optional to_int(TrimWhitespace = TrimWhitespace::Yes) const; template [[nodiscard]] Optional to_uint(TrimWhitespace = TrimWhitespace::Yes) const; #ifndef KERNEL [[nodiscard]] Optional to_double(TrimWhitespace = TrimWhitespace::Yes) const; [[nodiscard]] Optional to_float(TrimWhitespace = TrimWhitespace::Yes) const; #endif [[nodiscard]] DeprecatedString to_lowercase() const; [[nodiscard]] DeprecatedString to_uppercase() const; [[nodiscard]] DeprecatedString to_snakecase() const; [[nodiscard]] DeprecatedString to_titlecase() const; [[nodiscard]] DeprecatedString invert_case() const; [[nodiscard]] bool is_whitespace() const { return StringUtils::is_whitespace(*this); } [[nodiscard]] DeprecatedStringCodePointIterator code_points() const; [[nodiscard]] DeprecatedString trim(StringView characters, TrimMode mode = TrimMode::Both) const { auto trimmed_view = StringUtils::trim(view(), characters, mode); if (view() == trimmed_view) return *this; return trimmed_view; } [[nodiscard]] DeprecatedString trim_whitespace(TrimMode mode = TrimMode::Both) const { auto trimmed_view = StringUtils::trim_whitespace(view(), mode); if (view() == trimmed_view) return *this; return trimmed_view; } [[nodiscard]] bool equals_ignoring_ascii_case(StringView) const; [[nodiscard]] bool contains(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const; [[nodiscard]] bool contains(char, CaseSensitivity = CaseSensitivity::CaseSensitive) const; [[nodiscard]] Vector split_limit(char separator, size_t limit, SplitBehavior = SplitBehavior::Nothing) const; [[nodiscard]] Vector split(char separator, SplitBehavior = SplitBehavior::Nothing) const; [[nodiscard]] Vector split_view(char separator, SplitBehavior = SplitBehavior::Nothing) const; [[nodiscard]] Vector split_view(Function separator, SplitBehavior = SplitBehavior::Nothing) const; [[nodiscard]] Optional find(char needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); } [[nodiscard]] Optional find(StringView needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); } [[nodiscard]] Optional find_last(char needle) const { return StringUtils::find_last(*this, needle); } [[nodiscard]] Optional find_last(StringView needle) const { return StringUtils::find_last(*this, needle); } Vector find_all(StringView needle) const; using SearchDirection = StringUtils::SearchDirection; [[nodiscard]] Optional find_any_of(StringView needles, SearchDirection direction) const { return StringUtils::find_any_of(*this, needles, direction); } [[nodiscard]] StringView find_last_split_view(char separator) const { return view().find_last_split_view(separator); } [[nodiscard]] DeprecatedString substring(size_t start, size_t length) const; [[nodiscard]] DeprecatedString substring(size_t start) const; [[nodiscard]] StringView substring_view(size_t start, size_t length) const; [[nodiscard]] StringView substring_view(size_t start) const; [[nodiscard]] ALWAYS_INLINE bool is_empty() const { return length() == 0; } [[nodiscard]] ALWAYS_INLINE size_t length() const { return m_impl->length(); } // Includes NUL-terminator. [[nodiscard]] ALWAYS_INLINE char const* characters() const { return m_impl->characters(); } [[nodiscard]] bool copy_characters_to_buffer(char* buffer, size_t buffer_size) const; [[nodiscard]] ALWAYS_INLINE ReadonlyBytes bytes() const { return m_impl->bytes(); } [[nodiscard]] ALWAYS_INLINE char const& operator[](size_t i) const { return (*m_impl)[i]; } [[nodiscard]] ALWAYS_INLINE u8 byte_at(size_t i) const { return bit_cast((*m_impl)[i]); } using ConstIterator = SimpleIterator; [[nodiscard]] constexpr ConstIterator begin() const { return ConstIterator::begin(*this); } [[nodiscard]] constexpr ConstIterator end() const { return ConstIterator::end(*this); } [[nodiscard]] bool starts_with(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const; [[nodiscard]] bool ends_with(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const; [[nodiscard]] bool starts_with(char) const; [[nodiscard]] bool ends_with(char) const; bool operator==(DeprecatedString const&) const; bool operator==(StringView) const; bool operator==(DeprecatedFlyString const&) const; bool operator<(DeprecatedString const&) const; bool operator>=(DeprecatedString const& other) const { return !(*this < other); } bool operator>=(char const* other) const { return !(*this < other); } bool operator>(DeprecatedString const&) const; bool operator<=(DeprecatedString const& other) const { return !(*this > other); } bool operator<=(char const* other) const { return !(*this > other); } bool operator==(char const* cstring) const; [[nodiscard]] DeprecatedString isolated_copy() const; [[nodiscard]] static DeprecatedString empty() { return StringImpl::the_empty_stringimpl(); } [[nodiscard]] StringImpl const* impl() const { return m_impl.ptr(); } DeprecatedString& operator=(DeprecatedString&& other) { if (this != &other) m_impl = move(other.m_impl); return *this; } DeprecatedString& operator=(DeprecatedString const& other) { if (this != &other) m_impl = const_cast(other).m_impl; return *this; } template T> DeprecatedString& operator=(T bytes) { m_impl = *StringImpl::create(bytes); return *this; } [[nodiscard]] u32 hash() const { return m_impl->hash(); } [[nodiscard]] ByteBuffer to_byte_buffer() const; template [[nodiscard]] static DeprecatedString copy(BufferType const& buffer, ShouldChomp should_chomp = NoChomp) { if (buffer.is_empty()) return empty(); return DeprecatedString(reinterpret_cast(buffer.data()), buffer.size(), should_chomp); } [[nodiscard]] static DeprecatedString vformatted(StringView fmtstr, TypeErasedFormatParams&); template [[nodiscard]] static DeprecatedString formatted(CheckedFormatString&& fmtstr, Parameters const&... parameters) { VariadicFormatParams variadic_format_parameters { parameters... }; return vformatted(fmtstr.view(), variadic_format_parameters); } template [[nodiscard]] static DeprecatedString number(T value) { return formatted("{}", value); } [[nodiscard]] StringView view() const { return { characters(), length() }; } [[nodiscard]] DeprecatedString replace(StringView needle, StringView replacement, ReplaceMode replace_mode = ReplaceMode::All) const { return StringUtils::replace(*this, needle, replacement, replace_mode); } [[nodiscard]] size_t count(StringView needle) const { return StringUtils::count(*this, needle); } [[nodiscard]] DeprecatedString reverse() const; template [[nodiscard]] ALWAYS_INLINE constexpr bool is_one_of(Ts&&... strings) const { return (... || this->operator==(forward(strings))); } template [[nodiscard]] ALWAYS_INLINE constexpr bool is_one_of_ignoring_ascii_case(Ts&&... strings) const { return (... || [this, &strings]() -> bool { if constexpr (requires(Ts a) { a.view()->StringView; }) return this->equals_ignoring_ascii_case(forward(strings.view())); else return this->equals_ignoring_ascii_case(forward(strings)); }()); } private: NonnullRefPtr m_impl; }; template<> struct Traits : public GenericTraits { static unsigned hash(DeprecatedString const& s) { return s.impl() ? s.impl()->hash() : 0; } }; // FIXME: Rename this to indicate that it's about ASCII-only case insensitivity. struct CaseInsensitiveStringTraits : public Traits { static unsigned hash(DeprecatedString const& s) { return s.impl() ? s.impl()->case_insensitive_hash() : 0; } static bool equals(DeprecatedString const& a, DeprecatedString const& b) { return a.equals_ignoring_ascii_case(b); } }; DeprecatedString escape_html_entities(StringView html); } #if USING_AK_GLOBALLY using AK::CaseInsensitiveStringTraits; using AK::escape_html_entities; #endif