2020-01-18 11:38:21 +03: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 12:53:07 +03:00
|
|
|
#pragma once
|
|
|
|
|
2020-02-14 23:41:10 +03:00
|
|
|
#include <AK/Forward.h>
|
2019-06-21 19:45:35 +03:00
|
|
|
#include <AK/RefPtr.h>
|
2020-08-05 13:14:44 +03:00
|
|
|
#include <AK/Stream.h>
|
2020-08-27 15:21:53 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2019-04-15 15:56:37 +03:00
|
|
|
#include <AK/StringImpl.h>
|
2020-02-26 10:25:24 +03:00
|
|
|
#include <AK/StringUtils.h>
|
2019-04-15 15:56:37 +03:00
|
|
|
#include <AK/Traits.h>
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-06-02 09:37:01 +03: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 19:37:47 +03:00
|
|
|
// RefPtr<StringImpl>, with a bit of syntactic sugar.
|
2019-06-02 09:37:01 +03: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 19:40:24 +03:00
|
|
|
// retainable and so copying only requires modifying the ref count.
|
2019-06-02 09:37:01 +03: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 12:53:07 +03:00
|
|
|
class String {
|
|
|
|
public:
|
2020-05-25 13:36:41 +03:00
|
|
|
~String() { }
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2020-05-25 13:36:41 +03:00
|
|
|
String() { }
|
2020-03-23 15:45:10 +03:00
|
|
|
String(const StringView&);
|
2019-04-15 15:56:37 +03:00
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
String(const String& other)
|
2019-07-11 16:45:11 +03:00
|
|
|
: m_impl(const_cast<String&>(other).m_impl)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String(String&& other)
|
2018-10-17 11:55:43 +03:00
|
|
|
: m_impl(move(other.m_impl))
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-07 02:19:35 +03:00
|
|
|
String(const char* cstring, ShouldChomp shouldChomp = NoChomp)
|
|
|
|
: m_impl(StringImpl::create(cstring, shouldChomp))
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-12-09 19:45:40 +03:00
|
|
|
String(const char* cstring, size_t length, ShouldChomp shouldChomp = NoChomp)
|
2018-11-07 02:19:35 +03:00
|
|
|
: m_impl(StringImpl::create(cstring, length, shouldChomp))
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-08-05 11:37:34 +03:00
|
|
|
explicit String(ReadonlyBytes bytes, ShouldChomp shouldChomp = NoChomp)
|
|
|
|
: m_impl(StringImpl::create(bytes, shouldChomp))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
String(const StringImpl& impl)
|
|
|
|
: m_impl(const_cast<StringImpl&>(impl))
|
2019-04-12 15:43:44 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String(const StringImpl* impl)
|
|
|
|
: m_impl(const_cast<StringImpl*>(impl))
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
String(RefPtr<StringImpl>&& impl)
|
2018-10-17 11:55:43 +03:00
|
|
|
: m_impl(move(impl))
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
String(NonnullRefPtr<StringImpl>&& impl)
|
2019-02-25 18:04:08 +03:00
|
|
|
: m_impl(move(impl))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-03-22 21:07:02 +03:00
|
|
|
String(const FlyString&);
|
|
|
|
|
2019-12-09 19:45:40 +03:00
|
|
|
static String repeated(char, size_t count);
|
2020-02-26 10:25:24 +03:00
|
|
|
bool matches(const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive) const;
|
2019-04-25 23:56:09 +03:00
|
|
|
|
2020-06-12 22:07:52 +03:00
|
|
|
Optional<int> to_int() const;
|
|
|
|
Optional<unsigned> to_uint() const;
|
2018-10-31 21:49:22 +03:00
|
|
|
|
2020-03-22 21:07:02 +03:00
|
|
|
String to_lowercase() const;
|
|
|
|
String to_uppercase() const;
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2020-05-13 02:20:52 +03:00
|
|
|
enum class TrimMode {
|
|
|
|
Left,
|
|
|
|
Right,
|
|
|
|
Both
|
|
|
|
};
|
|
|
|
String trim_whitespace(TrimMode mode = TrimMode::Both) const;
|
2020-05-11 03:43:33 +03:00
|
|
|
|
2019-12-18 14:43:53 +03:00
|
|
|
bool equals_ignoring_case(const StringView&) const;
|
|
|
|
|
2019-10-28 20:47:48 +03:00
|
|
|
bool contains(const String&) const;
|
2020-07-12 20:36:48 +03:00
|
|
|
Optional<size_t> index_of(const String&, size_t start = 0) const;
|
2019-10-28 20:47:48 +03:00
|
|
|
|
2020-01-22 17:17:27 +03: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;
|
2019-12-09 19:45:40 +03:00
|
|
|
String substring(size_t start, size_t length) const;
|
2019-04-16 03:39:16 +03:00
|
|
|
|
2019-09-21 00:43:37 +03:00
|
|
|
Vector<StringView> split_view(char separator, bool keep_empty = false) const;
|
2019-12-09 19:45:40 +03:00
|
|
|
StringView substring_view(size_t start, size_t length) const;
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-12-21 04:10:45 +03:00
|
|
|
bool is_null() const { return !m_impl; }
|
2020-05-26 23:49:06 +03: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 13:56:46 +03:00
|
|
|
// Includes NUL-terminator, if non-nullptr.
|
2020-05-26 23:49:06 +03:00
|
|
|
ALWAYS_INLINE const char* characters() const { return m_impl ? m_impl->characters() : nullptr; }
|
2020-07-27 15:15:37 +03:00
|
|
|
|
2020-08-25 17:23:18 +03:00
|
|
|
[[nodiscard]] bool copy_characters_to_buffer(char* buffer, size_t buffer_size) const;
|
|
|
|
|
2020-07-27 15:15:37 +03:00
|
|
|
ALWAYS_INLINE ReadonlyBytes bytes() const { return m_impl ? m_impl->bytes() : nullptr; }
|
|
|
|
|
2020-05-26 23:49:06 +03:00
|
|
|
ALWAYS_INLINE const char& operator[](size_t i) const
|
2019-05-28 12:53:16 +03:00
|
|
|
{
|
|
|
|
return (*m_impl)[i];
|
|
|
|
}
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2020-09-06 22:14:08 +03: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 11:13:29 +03:00
|
|
|
|
2020-07-18 19:59:38 +03:00
|
|
|
bool starts_with(const StringView&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
2020-05-26 12:12:18 +03:00
|
|
|
bool ends_with(const StringView&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
2020-02-15 03:04:00 +03:00
|
|
|
bool starts_with(char) const;
|
|
|
|
bool ends_with(char) const;
|
2019-03-22 14:43:29 +03:00
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
bool operator==(const String&) const;
|
|
|
|
bool operator!=(const String& other) const { return !(*this == other); }
|
2019-07-11 13:58:27 +03:00
|
|
|
|
2019-08-24 23:28:42 +03:00
|
|
|
bool operator==(const StringView&) const;
|
|
|
|
bool operator!=(const StringView& other) const { return !(*this == other); }
|
|
|
|
|
2020-03-28 11:11:00 +03:00
|
|
|
bool operator==(const FlyString&) const;
|
|
|
|
bool operator!=(const FlyString& other) const { return !(*this == other); }
|
|
|
|
|
2019-03-09 15:33:52 +03:00
|
|
|
bool operator<(const String&) const;
|
2019-07-04 15:20:48 +03: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 12:53:07 +03:00
|
|
|
|
2019-07-11 13:58:27 +03: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 15:45:10 +03:00
|
|
|
bool operator==(const char* cstring) const;
|
|
|
|
bool operator!=(const char* cstring) const { return !(*this == cstring); }
|
2019-06-08 19:30:40 +03:00
|
|
|
|
2018-12-21 04:10:45 +03:00
|
|
|
String isolated_copy() const;
|
2018-10-26 10:54:29 +03:00
|
|
|
|
2018-10-10 12:53:07 +03: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 15:28:22 +03:00
|
|
|
if (this != &other)
|
2018-10-17 11:55:43 +03:00
|
|
|
m_impl = move(other.m_impl);
|
2018-10-24 15:28:22 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
String& operator=(const String& other)
|
|
|
|
{
|
|
|
|
if (this != &other)
|
2019-07-11 16:45:11 +03:00
|
|
|
m_impl = const_cast<String&>(other).m_impl;
|
2018-10-10 12:53:07 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-08-05 11:37:34 +03: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 18:17:47 +03:00
|
|
|
u32 hash() const
|
|
|
|
{
|
|
|
|
if (!m_impl)
|
|
|
|
return 0;
|
|
|
|
return m_impl->hash();
|
|
|
|
}
|
|
|
|
|
2018-12-21 04:10:45 +03:00
|
|
|
ByteBuffer to_byte_buffer() const;
|
2019-04-20 15:13:40 +03:00
|
|
|
|
|
|
|
template<typename BufferType>
|
|
|
|
static String copy(const BufferType& buffer, ShouldChomp should_chomp = NoChomp)
|
|
|
|
{
|
|
|
|
if (buffer.is_null())
|
2019-05-28 12:53:16 +03:00
|
|
|
return {};
|
2019-04-20 15:13:40 +03:00
|
|
|
if (buffer.is_empty())
|
|
|
|
return empty();
|
|
|
|
return String((const char*)buffer.data(), buffer.size(), should_chomp);
|
|
|
|
}
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2019-01-30 18:28:51 +03:00
|
|
|
static String format(const char*, ...);
|
2020-02-05 21:07:53 +03:00
|
|
|
static String number(unsigned);
|
|
|
|
static String number(unsigned long);
|
|
|
|
static String number(unsigned long long);
|
|
|
|
static String number(int);
|
|
|
|
static String number(long);
|
|
|
|
static String number(long long);
|
2019-01-30 18:28:51 +03:00
|
|
|
|
2020-03-23 15:45:10 +03:00
|
|
|
StringView view() const;
|
2019-04-15 15:56:37 +03:00
|
|
|
|
2020-04-01 22:27:39 +03:00
|
|
|
int replace(const String& needle, const String& replacement, bool all_occurences = false);
|
|
|
|
|
2020-05-25 13:36:41 +03:00
|
|
|
template<typename T, typename... Rest>
|
2020-05-29 22:44:06 +03:00
|
|
|
bool is_one_of(const T& string, Rest... rest) const
|
2020-05-25 13:36:41 +03:00
|
|
|
{
|
2020-07-28 18:40:23 +03:00
|
|
|
if (*this == string)
|
2020-05-25 13:36:41 +03:00
|
|
|
return true;
|
|
|
|
return is_one_of(rest...);
|
|
|
|
}
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
private:
|
2020-05-29 22:44:06 +03:00
|
|
|
bool is_one_of() const { return false; }
|
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
RefPtr<StringImpl> m_impl;
|
2018-10-10 12:53:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2019-06-29 20:14:03 +03:00
|
|
|
struct Traits<String> : public GenericTraits<String> {
|
2018-10-10 12:53:07 +03:00
|
|
|
static unsigned hash(const String& s) { return s.impl() ? s.impl()->hash() : 0; }
|
|
|
|
};
|
|
|
|
|
2019-07-13 12:00:29 +03: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 15:45:10 +03: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 13:58:27 +03:00
|
|
|
|
2020-02-13 10:46:00 +03:00
|
|
|
String escape_html_entities(const StringView& html);
|
|
|
|
|
2020-08-27 15:21:53 +03:00
|
|
|
inline InputStream& operator>>(InputStream& stream, String& string)
|
2020-08-05 13:14:44 +03:00
|
|
|
{
|
2020-08-27 15:21:53 +03:00
|
|
|
StringBuilder builder;
|
|
|
|
|
|
|
|
for (;;) {
|
2020-09-13 13:24:17 +03:00
|
|
|
char next_char;
|
|
|
|
stream >> next_char;
|
2020-08-27 15:21:53 +03:00
|
|
|
|
2020-09-13 13:24:17 +03:00
|
|
|
if (stream.has_any_error()) {
|
2020-08-31 14:12:15 +03:00
|
|
|
stream.set_fatal_error();
|
2020-09-13 13:24:17 +03:00
|
|
|
string = nullptr;
|
2020-08-27 15:21:53 +03:00
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next_char) {
|
|
|
|
builder.append(next_char);
|
|
|
|
} else {
|
|
|
|
string = builder.to_string();
|
|
|
|
return stream;
|
|
|
|
}
|
2020-08-05 13:14:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2019-09-13 15:37:25 +03:00
|
|
|
using AK::CaseInsensitiveStringTraits;
|
2020-02-13 10:46:00 +03:00
|
|
|
using AK::escape_html_entities;
|
2020-03-10 11:13:29 +03:00
|
|
|
using AK::String;
|