2019-04-15 12:56:37 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-16 00:39:16 +00:00
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-06-29 10:03:28 +00:00
|
|
|
class ByteBuffer;
|
2019-04-16 00:39:16 +00:00
|
|
|
class String;
|
2019-06-08 21:55:13 +00:00
|
|
|
class StringImpl;
|
2019-04-16 00:39:16 +00:00
|
|
|
|
2019-04-15 12:56:37 +00:00
|
|
|
class StringView {
|
|
|
|
public:
|
2019-05-28 09:53:16 +00:00
|
|
|
StringView() {}
|
2019-12-09 16:45:40 +00:00
|
|
|
StringView(const char* characters, size_t length)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_characters(characters)
|
|
|
|
, m_length(length)
|
|
|
|
{
|
|
|
|
}
|
2019-12-09 16:45:40 +00:00
|
|
|
StringView(const unsigned char* characters, size_t length)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_characters((const char*)characters)
|
|
|
|
, m_length(length)
|
|
|
|
{
|
|
|
|
}
|
2019-04-15 12:56:37 +00:00
|
|
|
StringView(const char* cstring)
|
|
|
|
: m_characters(cstring)
|
|
|
|
{
|
|
|
|
if (cstring) {
|
|
|
|
while (*(cstring++))
|
|
|
|
++m_length;
|
|
|
|
}
|
|
|
|
}
|
2019-06-29 10:03:28 +00:00
|
|
|
|
|
|
|
StringView(const ByteBuffer&);
|
|
|
|
StringView(const String&);
|
2019-04-15 12:56:37 +00:00
|
|
|
|
2019-06-08 16:30:40 +00:00
|
|
|
bool is_null() const { return !m_characters; }
|
2019-04-15 12:56:37 +00:00
|
|
|
bool is_empty() const { return m_length == 0; }
|
2019-07-08 13:38:44 +00:00
|
|
|
const char* characters_without_null_termination() const { return m_characters; }
|
2019-12-09 16:45:40 +00:00
|
|
|
size_t length() const { return m_length; }
|
|
|
|
char operator[](size_t index) const { return m_characters[index]; }
|
2019-04-15 12:56:37 +00:00
|
|
|
|
2019-08-24 20:31:06 +00:00
|
|
|
unsigned hash() const;
|
|
|
|
|
2019-09-12 11:13:07 +00:00
|
|
|
bool starts_with(const StringView&) const;
|
2019-12-29 12:44:30 +00:00
|
|
|
bool ends_with(const StringView&) const;
|
2019-09-12 11:13:07 +00:00
|
|
|
|
2019-12-09 16:45:40 +00:00
|
|
|
StringView substring_view(size_t start, size_t length) const;
|
2019-09-20 21:43:37 +00:00
|
|
|
Vector<StringView> split_view(char, bool keep_empty = false) const;
|
2019-08-04 09:44:20 +00:00
|
|
|
|
2019-12-02 12:42:33 +00:00
|
|
|
// Create a Vector of StringViews split by line endings. As of CommonMark
|
|
|
|
// 0.29, the spec defines a line ending as "a newline (U+000A), a carriage
|
|
|
|
// return (U+000D) not followed by a newline, or a carriage return and a
|
|
|
|
// following newline.".
|
|
|
|
Vector<StringView> lines(bool consider_cr = true) const;
|
|
|
|
|
2019-08-04 09:44:20 +00:00
|
|
|
// FIXME: These should be shared between String and StringView somehow!
|
2019-04-16 00:39:16 +00:00
|
|
|
unsigned to_uint(bool& ok) const;
|
2019-08-04 09:44:20 +00:00
|
|
|
int to_int(bool& ok) const;
|
2019-04-16 00:39:16 +00:00
|
|
|
|
2019-06-13 13:30:55 +00:00
|
|
|
// Create a new substring view of this string view, starting either at the beginning of
|
|
|
|
// the given substring view, or after its end, and continuing until the end of this string
|
|
|
|
// view (that is, for the remaining part of its length). For example,
|
|
|
|
//
|
|
|
|
// StringView str { "foobar" };
|
|
|
|
// StringView substr = str.substring_view(1, 2); // "oo"
|
|
|
|
// StringView substr_from = str.substring_view_starting_from_substring(subst); // "oobar"
|
|
|
|
// StringView substr_after = str.substring_view_starting_after_substring(subst); // "bar"
|
|
|
|
//
|
|
|
|
// Note that this only works if the string view passed as an argument is indeed a substring
|
|
|
|
// view of this string view, such as one created by substring_view() and split_view(). It
|
|
|
|
// does not work for arbitrary strings; for example declaring substr in the example above as
|
|
|
|
//
|
|
|
|
// StringView substr { "oo" };
|
|
|
|
//
|
|
|
|
// would not work.
|
|
|
|
StringView substring_view_starting_from_substring(const StringView& substring) const;
|
|
|
|
StringView substring_view_starting_after_substring(const StringView& substring) const;
|
|
|
|
|
2019-06-07 17:22:58 +00:00
|
|
|
bool operator==(const char* cstring) const
|
|
|
|
{
|
2019-06-08 16:30:40 +00:00
|
|
|
if (is_null())
|
|
|
|
return !cstring;
|
|
|
|
if (!cstring)
|
|
|
|
return false;
|
2019-12-09 16:45:40 +00:00
|
|
|
size_t other_length = strlen(cstring);
|
2019-06-07 17:22:58 +00:00
|
|
|
if (m_length != other_length)
|
|
|
|
return false;
|
|
|
|
return !memcmp(m_characters, cstring, m_length);
|
|
|
|
}
|
|
|
|
bool operator!=(const char* cstring) const
|
|
|
|
{
|
|
|
|
return !(*this == cstring);
|
|
|
|
}
|
2019-04-16 00:39:16 +00:00
|
|
|
|
|
|
|
bool operator==(const String&) const;
|
|
|
|
|
2019-08-15 12:07:23 +00:00
|
|
|
bool operator==(const StringView& other) const
|
|
|
|
{
|
|
|
|
if (is_null())
|
|
|
|
return other.is_null();
|
|
|
|
if (other.is_null())
|
|
|
|
return false;
|
|
|
|
if (length() != other.length())
|
|
|
|
return false;
|
|
|
|
return !memcmp(m_characters, other.m_characters, m_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const StringView& other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
2019-04-15 12:56:37 +00:00
|
|
|
private:
|
2019-06-03 16:27:56 +00:00
|
|
|
friend class String;
|
2019-06-08 21:55:13 +00:00
|
|
|
const StringImpl* m_impl { nullptr };
|
2019-04-15 12:56:37 +00:00
|
|
|
const char* m_characters { nullptr };
|
2019-12-09 16:45:40 +00:00
|
|
|
size_t m_length { 0 };
|
2019-04-15 12:56:37 +00:00
|
|
|
};
|
2019-04-16 00:39:16 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::StringView;
|