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");
|
|
|
|
//
|
|
|
|
// s = String::format("%d little piggies", m_piggies);
|
|
|
|
//
|
|
|
|
// 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&);
|
|
|
|
|
2019-12-09 16:45:40 +00:00
|
|
|
static String repeated(char, size_t count);
|
2021-01-15 19:13:14 +00:00
|
|
|
|
|
|
|
template<class SeparatorType, class CollectionType>
|
|
|
|
static String join(const SeparatorType& separator, const CollectionType& collection)
|
|
|
|
{
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.join(separator, collection);
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
2020-02-26 07:25:24 +00:00
|
|
|
bool matches(const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive) const;
|
2020-10-25 05:34:39 +00:00
|
|
|
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>
|
|
|
|
Optional<T> to_int() const;
|
|
|
|
template<typename T = unsigned>
|
|
|
|
Optional<T> to_uint() const;
|
2018-10-31 18:49:22 +00:00
|
|
|
|
2020-03-22 18:07:02 +00:00
|
|
|
String to_lowercase() const;
|
|
|
|
String to_uppercase() const;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2021-01-02 23:26:02 +00:00
|
|
|
bool is_whitespace() const { return StringUtils::is_whitespace(*this); }
|
|
|
|
|
2020-09-20 13:35:04 +00:00
|
|
|
#ifndef KERNEL
|
|
|
|
String trim_whitespace(TrimMode mode = TrimMode::Both) const
|
|
|
|
{
|
|
|
|
return StringUtils::trim_whitespace(StringView { characters(), length() }, mode);
|
|
|
|
}
|
|
|
|
#endif
|
2020-05-11 00:43:33 +00:00
|
|
|
|
2019-12-18 11:43:53 +00:00
|
|
|
bool equals_ignoring_case(const StringView&) const;
|
|
|
|
|
2020-10-20 21:07:03 +00:00
|
|
|
bool contains(const StringView&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
2020-07-12 17:36:48 +00:00
|
|
|
Optional<size_t> index_of(const String&, size_t start = 0) const;
|
2019-10-28 17:47:48 +00:00
|
|
|
|
2020-01-22 14:17:27 +00:00
|
|
|
Vector<String> split_limit(char separator, size_t limit, bool keep_empty = false) const;
|
|
|
|
Vector<String> split(char separator, bool keep_empty = false) const;
|
2021-01-12 19:58:45 +00:00
|
|
|
|
|
|
|
Optional<size_t> find(char) const;
|
|
|
|
Optional<size_t> find(const StringView&) const;
|
|
|
|
|
2020-12-10 17:57:56 +00:00
|
|
|
String substring(size_t start) const;
|
2019-12-09 16:45:40 +00:00
|
|
|
String substring(size_t start, size_t length) const;
|
2019-04-16 00:39:16 +00:00
|
|
|
|
2019-09-20 21:43:37 +00:00
|
|
|
Vector<StringView> split_view(char separator, bool keep_empty = false) const;
|
2019-12-09 16:45:40 +00:00
|
|
|
StringView substring_view(size_t start, size_t length) const;
|
2020-12-01 22:11:20 +00:00
|
|
|
StringView substring_view(size_t start) const;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-12-21 01:10:45 +00:00
|
|
|
bool is_null() const { return !m_impl; }
|
2020-05-26 20:49:06 +00:00
|
|
|
ALWAYS_INLINE bool is_empty() const { return length() == 0; }
|
|
|
|
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.
|
2020-05-26 20:49:06 +00:00
|
|
|
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-01-10 23:29:28 +00:00
|
|
|
ALWAYS_INLINE ReadonlyBytes bytes() const
|
|
|
|
{
|
|
|
|
if (m_impl) {
|
|
|
|
return m_impl->bytes();
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
2020-07-27 12:15:37 +00:00
|
|
|
|
2020-05-26 20:49:06 +00:00
|
|
|
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>;
|
|
|
|
|
|
|
|
constexpr ConstIterator begin() const { return ConstIterator::begin(*this); }
|
|
|
|
constexpr ConstIterator end() const { return ConstIterator::end(*this); }
|
2020-03-10 08:13:29 +00:00
|
|
|
|
2020-07-18 16:59:38 +00:00
|
|
|
bool starts_with(const StringView&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
2020-05-26 09:12:18 +00:00
|
|
|
bool ends_with(const StringView&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
2020-02-15 00:04:00 +00:00
|
|
|
bool starts_with(char) const;
|
|
|
|
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
|
|
|
|
2018-12-21 01:10:45 +00:00
|
|
|
String isolated_copy() const;
|
2018-10-26 07:54:29 +00:00
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
static String empty();
|
|
|
|
|
|
|
|
StringImpl* impl() { return m_impl.ptr(); }
|
|
|
|
const StringImpl* impl() const { return m_impl.ptr(); }
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-10-18 15:17:47 +00:00
|
|
|
u32 hash() const
|
|
|
|
{
|
|
|
|
if (!m_impl)
|
|
|
|
return 0;
|
|
|
|
return m_impl->hash();
|
|
|
|
}
|
|
|
|
|
2018-12-21 01:10:45 +00:00
|
|
|
ByteBuffer to_byte_buffer() const;
|
2019-04-20 12:13:40 +00:00
|
|
|
|
|
|
|
template<typename BufferType>
|
|
|
|
static String copy(const BufferType& buffer, ShouldChomp should_chomp = NoChomp)
|
|
|
|
{
|
|
|
|
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-01-11 14:02:37 +00:00
|
|
|
static String format(const char*, ...) __attribute__((format(printf, 1, 2)));
|
2020-09-22 11:27:40 +00:00
|
|
|
|
2020-10-02 13:21:30 +00:00
|
|
|
static String vformatted(StringView fmtstr, TypeErasedFormatParams);
|
2020-09-23 11:21:18 +00:00
|
|
|
|
|
|
|
template<typename... Parameters>
|
|
|
|
static String formatted(StringView fmtstr, const Parameters&... parameters)
|
|
|
|
{
|
2020-10-02 13:21:30 +00:00
|
|
|
return vformatted(fmtstr, VariadicFormatParams { parameters... });
|
2020-09-23 11:21:18 +00:00
|
|
|
}
|
|
|
|
|
2020-12-30 11:59:14 +00:00
|
|
|
template<typename T>
|
2021-01-17 08:22:04 +00:00
|
|
|
static String number(T value) requires IsArithmetic<T>::value
|
|
|
|
{
|
|
|
|
return formatted("{}", value);
|
|
|
|
}
|
2019-01-30 15:28:51 +00:00
|
|
|
|
2020-03-23 12:45:10 +00:00
|
|
|
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);
|
2020-12-26 10:30:14 +00:00
|
|
|
String reverse() const;
|
2020-04-01 19:27:39 +00:00
|
|
|
|
2020-05-25 10:36:41 +00:00
|
|
|
template<typename T, typename... Rest>
|
2020-05-29 19:44:06 +00:00
|
|
|
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:
|
2020-05-29 19:44:06 +00:00
|
|
|
bool is_one_of() const { return false; }
|
|
|
|
|
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; }
|
|
|
|
};
|
|
|
|
|
2019-07-13 09:00:29 +00:00
|
|
|
struct CaseInsensitiveStringTraits : public AK::Traits<String> {
|
|
|
|
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;
|