2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-09-23 11:21:18 +00:00
|
|
|
#include <AK/Format.h>
|
2020-02-14 20:41:10 +00:00
|
|
|
#include <AK/Forward.h>
|
2019-06-21 16:45:35 +00:00
|
|
|
#include <AK/RefPtr.h>
|
2020-08-05 10:14:44 +00:00
|
|
|
#include <AK/Stream.h>
|
2021-01-15 19:13:14 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2019-04-15 12:56:37 +00:00
|
|
|
#include <AK/StringImpl.h>
|
2020-02-26 07:25:24 +00:00
|
|
|
#include <AK/StringUtils.h>
|
2019-04-15 12:56:37 +00:00
|
|
|
#include <AK/Traits.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-06-02 06:37:01 +00:00
|
|
|
// String is a convenience wrapper around StringImpl, suitable for passing
|
|
|
|
// around as a value type. It's basically the same as passing around a
|
2019-06-21 16:37:47 +00:00
|
|
|
// RefPtr<StringImpl>, with a bit of syntactic sugar.
|
2019-06-02 06:37:01 +00:00
|
|
|
//
|
|
|
|
// 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 String is very efficient, since the internal StringImpl is
|
2019-06-21 16:40:24 +00:00
|
|
|
// retainable and so copying only requires modifying the ref count.
|
2019-06-02 06:37:01 +00:00
|
|
|
//
|
|
|
|
// There are three main ways to construct a new String:
|
|
|
|
//
|
|
|
|
// s = String("some literal");
|
|
|
|
//
|
2021-04-21 21:45:08 +00:00
|
|
|
// s = String::formatted("{} little piggies", m_piggies);
|
2019-06-02 06:37:01 +00:00
|
|
|
//
|
|
|
|
// StringBuilder builder;
|
|
|
|
// builder.append("abc");
|
|
|
|
// builder.append("123");
|
|
|
|
// s = builder.to_string();
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
class String {
|
|
|
|
public:
|
2021-01-10 23:29:28 +00:00
|
|
|
~String() = default;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2021-01-10 23:29:28 +00:00
|
|
|
String() = default;
|
2020-03-23 12:45:10 +00:00
|
|
|
String(const StringView&);
|
2019-04-15 12:56:37 +00:00
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
String(const String& other)
|
2019-07-11 13:45:11 +00:00
|
|
|
: m_impl(const_cast<String&>(other).m_impl)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String(String&& other)
|
2018-10-17 08:55:43 +00:00
|
|
|
: m_impl(move(other.m_impl))
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-06 23:19:35 +00:00
|
|
|
String(const char* cstring, ShouldChomp shouldChomp = NoChomp)
|
|
|
|
: m_impl(StringImpl::create(cstring, shouldChomp))
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-12-09 16:45:40 +00:00
|
|
|
String(const char* cstring, size_t length, ShouldChomp shouldChomp = NoChomp)
|
2018-11-06 23:19:35 +00:00
|
|
|
: m_impl(StringImpl::create(cstring, length, shouldChomp))
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-08-05 08:37:34 +00:00
|
|
|
explicit String(ReadonlyBytes bytes, ShouldChomp shouldChomp = NoChomp)
|
|
|
|
: m_impl(StringImpl::create(bytes, shouldChomp))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
String(const StringImpl& impl)
|
|
|
|
: m_impl(const_cast<StringImpl&>(impl))
|
2019-04-12 12:43:44 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String(const StringImpl* impl)
|
|
|
|
: m_impl(const_cast<StringImpl*>(impl))
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
String(RefPtr<StringImpl>&& impl)
|
2018-10-17 08:55:43 +00:00
|
|
|
: m_impl(move(impl))
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
String(NonnullRefPtr<StringImpl>&& impl)
|
2019-02-25 15:04:08 +00:00
|
|
|
: m_impl(move(impl))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-03-22 18:07:02 +00:00
|
|
|
String(const FlyString&);
|
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] static String repeated(char, size_t count);
|
2021-01-15 19:13:14 +00:00
|
|
|
|
|
|
|
template<class SeparatorType, class CollectionType>
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] static String join(const SeparatorType& separator, const CollectionType& collection)
|
2021-01-15 19:13:14 +00:00
|
|
|
{
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.join(separator, collection);
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] bool matches(const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive) const;
|
|
|
|
[[nodiscard]] bool matches(const StringView& mask, Vector<MaskSpan>&, CaseSensitivity = CaseSensitivity::CaseInsensitive) const;
|
2019-04-25 20:56:09 +00:00
|
|
|
|
2020-12-10 13:17:30 +00:00
|
|
|
template<typename T = int>
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] Optional<T> to_int() const;
|
2020-12-10 13:17:30 +00:00
|
|
|
template<typename T = unsigned>
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] Optional<T> to_uint() const;
|
2018-10-31 18:49:22 +00:00
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] String to_lowercase() const;
|
|
|
|
[[nodiscard]] String to_uppercase() const;
|
|
|
|
[[nodiscard]] String to_snakecase() const;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] bool is_whitespace() const { return StringUtils::is_whitespace(*this); }
|
2021-01-02 23:26:02 +00:00
|
|
|
|
2020-09-20 13:35:04 +00:00
|
|
|
#ifndef KERNEL
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] String trim_whitespace(TrimMode mode = TrimMode::Both) const
|
2020-09-20 13:35:04 +00:00
|
|
|
{
|
|
|
|
return StringUtils::trim_whitespace(StringView { characters(), length() }, mode);
|
|
|
|
}
|
|
|
|
#endif
|
2020-05-11 00:43:33 +00:00
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] bool equals_ignoring_case(const StringView&) const;
|
2019-12-18 11:43:53 +00:00
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] bool contains(const StringView&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
|
|
|
[[nodiscard]] Optional<size_t> index_of(const String&, size_t start = 0) const;
|
2019-10-28 17:47:48 +00:00
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] Vector<String> split_limit(char separator, size_t limit, bool keep_empty = false) const;
|
|
|
|
[[nodiscard]] Vector<String> split(char separator, bool keep_empty = false) const;
|
2021-01-12 19:58:45 +00:00
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] Optional<size_t> find(char) const;
|
|
|
|
[[nodiscard]] Optional<size_t> find(const StringView&) const;
|
2021-01-12 19:58:45 +00:00
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] String substring(size_t start) const;
|
|
|
|
[[nodiscard]] String substring(size_t start, size_t length) const;
|
2019-04-16 00:39:16 +00:00
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] Vector<StringView> split_view(char separator, bool keep_empty = false) const;
|
|
|
|
[[nodiscard]] StringView substring_view(size_t start, size_t length) const;
|
|
|
|
[[nodiscard]] StringView substring_view(size_t start) const;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] bool is_null() const { return !m_impl; }
|
|
|
|
[[nodiscard]] ALWAYS_INLINE bool is_empty() const { return length() == 0; }
|
|
|
|
[[nodiscard]] ALWAYS_INLINE size_t length() const { return m_impl ? m_impl->length() : 0; }
|
2020-08-23 10:56:46 +00:00
|
|
|
// Includes NUL-terminator, if non-nullptr.
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE const char* characters() const { return m_impl ? m_impl->characters() : nullptr; }
|
2020-07-27 12:15:37 +00:00
|
|
|
|
2020-08-25 14:23:18 +00:00
|
|
|
[[nodiscard]] bool copy_characters_to_buffer(char* buffer, size_t buffer_size) const;
|
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE ReadonlyBytes bytes() const
|
2021-01-10 23:29:28 +00:00
|
|
|
{
|
|
|
|
if (m_impl) {
|
|
|
|
return m_impl->bytes();
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
2020-07-27 12:15:37 +00:00
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE const char& operator[](size_t i) const
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
return (*m_impl)[i];
|
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2020-09-06 19:14:08 +00:00
|
|
|
using ConstIterator = SimpleIterator<const String, const char>;
|
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] constexpr ConstIterator begin() const { return ConstIterator::begin(*this); }
|
|
|
|
[[nodiscard]] constexpr ConstIterator end() const { return ConstIterator::end(*this); }
|
2020-03-10 08:13:29 +00:00
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] bool starts_with(const StringView&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
|
|
|
[[nodiscard]] bool ends_with(const StringView&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
|
|
|
[[nodiscard]] bool starts_with(char) const;
|
|
|
|
[[nodiscard]] bool ends_with(char) const;
|
2019-03-22 11:43:29 +00:00
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
bool operator==(const String&) const;
|
|
|
|
bool operator!=(const String& other) const { return !(*this == other); }
|
2019-07-11 10:58:27 +00:00
|
|
|
|
2019-08-24 20:28:42 +00:00
|
|
|
bool operator==(const StringView&) const;
|
|
|
|
bool operator!=(const StringView& other) const { return !(*this == other); }
|
|
|
|
|
2020-03-28 08:11:00 +00:00
|
|
|
bool operator==(const FlyString&) const;
|
|
|
|
bool operator!=(const FlyString& other) const { return !(*this == other); }
|
|
|
|
|
2019-03-09 12:33:52 +00:00
|
|
|
bool operator<(const String&) const;
|
2019-07-04 12:20:48 +00:00
|
|
|
bool operator<(const char*) const;
|
|
|
|
bool operator>=(const String& other) const { return !(*this < other); }
|
|
|
|
bool operator>=(const char* other) const { return !(*this < other); }
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-07-11 10:58:27 +00:00
|
|
|
bool operator>(const String&) const;
|
|
|
|
bool operator>(const char*) const;
|
|
|
|
bool operator<=(const String& other) const { return !(*this > other); }
|
|
|
|
bool operator<=(const char* other) const { return !(*this > other); }
|
|
|
|
|
2020-03-23 12:45:10 +00:00
|
|
|
bool operator==(const char* cstring) const;
|
|
|
|
bool operator!=(const char* cstring) const { return !(*this == cstring); }
|
2019-06-08 16:30:40 +00:00
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] String isolated_copy() const;
|
2018-10-26 07:54:29 +00:00
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] static String empty();
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] StringImpl* impl() { return m_impl.ptr(); }
|
|
|
|
[[nodiscard]] const StringImpl* impl() const { return m_impl.ptr(); }
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
String& operator=(String&& other)
|
|
|
|
{
|
2018-10-24 12:28:22 +00:00
|
|
|
if (this != &other)
|
2018-10-17 08:55:43 +00:00
|
|
|
m_impl = move(other.m_impl);
|
2018-10-24 12:28:22 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
String& operator=(const String& other)
|
|
|
|
{
|
|
|
|
if (this != &other)
|
2019-07-11 13:45:11 +00:00
|
|
|
m_impl = const_cast<String&>(other).m_impl;
|
2018-10-10 09:53:07 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-08-05 08:37:34 +00:00
|
|
|
String& operator=(std::nullptr_t)
|
|
|
|
{
|
|
|
|
m_impl = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
String& operator=(ReadonlyBytes bytes)
|
|
|
|
{
|
|
|
|
m_impl = StringImpl::create(bytes);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] u32 hash() const
|
2019-10-18 15:17:47 +00:00
|
|
|
{
|
|
|
|
if (!m_impl)
|
|
|
|
return 0;
|
|
|
|
return m_impl->hash();
|
|
|
|
}
|
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] ByteBuffer to_byte_buffer() const;
|
2019-04-20 12:13:40 +00:00
|
|
|
|
|
|
|
template<typename BufferType>
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] static String copy(const BufferType& buffer, ShouldChomp should_chomp = NoChomp)
|
2019-04-20 12:13:40 +00:00
|
|
|
{
|
|
|
|
if (buffer.is_null())
|
2019-05-28 09:53:16 +00:00
|
|
|
return {};
|
2019-04-20 12:13:40 +00:00
|
|
|
if (buffer.is_empty())
|
|
|
|
return empty();
|
|
|
|
return String((const char*)buffer.data(), buffer.size(), should_chomp);
|
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] static String vformatted(StringView fmtstr, TypeErasedFormatParams);
|
2020-09-23 11:21:18 +00:00
|
|
|
|
|
|
|
template<typename... Parameters>
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] static String formatted(CheckedFormatString<Parameters...>&& fmtstr, const Parameters&... parameters)
|
2020-09-23 11:21:18 +00:00
|
|
|
{
|
2021-02-23 06:28:15 +00:00
|
|
|
return vformatted(fmtstr.view(), VariadicFormatParams { parameters... });
|
2020-09-23 11:21:18 +00:00
|
|
|
}
|
|
|
|
|
2020-12-30 11:59:14 +00:00
|
|
|
template<typename T>
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] static String number(T value) requires IsArithmetic<T>
|
2021-01-17 08:22:04 +00:00
|
|
|
{
|
|
|
|
return formatted("{}", value);
|
|
|
|
}
|
2019-01-30 15:28:51 +00:00
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] StringView view() const;
|
2019-04-15 12:56:37 +00:00
|
|
|
|
2020-10-02 21:14:37 +00:00
|
|
|
int replace(const String& needle, const String& replacement, bool all_occurrences = false);
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] String reverse() const;
|
2020-04-01 19:27:39 +00:00
|
|
|
|
2020-05-25 10:36:41 +00:00
|
|
|
template<typename T, typename... Rest>
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] bool is_one_of(const T& string, Rest... rest) const
|
2020-05-25 10:36:41 +00:00
|
|
|
{
|
2020-07-28 15:40:23 +00:00
|
|
|
if (*this == string)
|
2020-05-25 10:36:41 +00:00
|
|
|
return true;
|
|
|
|
return is_one_of(rest...);
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
private:
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] bool is_one_of() const { return false; }
|
2020-05-29 19:44:06 +00:00
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr<StringImpl> m_impl;
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2019-06-29 17:14:03 +00:00
|
|
|
struct Traits<String> : public GenericTraits<String> {
|
2018-10-10 09:53:07 +00:00
|
|
|
static unsigned hash(const String& s) { return s.impl() ? s.impl()->hash() : 0; }
|
|
|
|
};
|
|
|
|
|
2021-02-25 20:10:47 +00:00
|
|
|
struct CaseInsensitiveStringTraits : public Traits<String> {
|
2019-07-13 09:00:29 +00:00
|
|
|
static unsigned hash(const String& s) { return s.impl() ? s.to_lowercase().impl()->hash() : 0; }
|
|
|
|
static bool equals(const String& a, const String& b) { return a.to_lowercase() == b.to_lowercase(); }
|
|
|
|
};
|
|
|
|
|
2020-03-23 12:45:10 +00:00
|
|
|
bool operator<(const char*, const String&);
|
|
|
|
bool operator>=(const char*, const String&);
|
|
|
|
bool operator>(const char*, const String&);
|
|
|
|
bool operator<=(const char*, const String&);
|
2019-07-11 10:58:27 +00:00
|
|
|
|
2020-02-13 07:46:00 +00:00
|
|
|
String escape_html_entities(const StringView& html);
|
|
|
|
|
2020-09-23 11:21:18 +00:00
|
|
|
InputStream& operator>>(InputStream& stream, String& string);
|
2020-08-05 10:14:44 +00:00
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2019-09-13 12:37:25 +00:00
|
|
|
using AK::CaseInsensitiveStringTraits;
|
2020-02-13 07:46:00 +00:00
|
|
|
using AK::escape_html_entities;
|
2020-03-10 08:13:29 +00:00
|
|
|
using AK::String;
|