2019-04-15 12:56:37 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-16 00:39:16 +00:00
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
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() {}
|
|
|
|
StringView(const char* characters, int length)
|
|
|
|
: m_characters(characters)
|
|
|
|
, m_length(length)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
StringView(const unsigned char* characters, int length)
|
|
|
|
: 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-08 21:55:13 +00:00
|
|
|
StringView(const String& 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; }
|
|
|
|
const char* characters() const { return m_characters; }
|
|
|
|
int length() const { return m_length; }
|
|
|
|
char operator[](int index) const { return m_characters[index]; }
|
|
|
|
|
2019-04-16 00:39:16 +00:00
|
|
|
StringView substring_view(int start, int length) const;
|
|
|
|
Vector<StringView> split_view(char) const;
|
|
|
|
unsigned to_uint(bool& ok) 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-06-07 17:22:58 +00:00
|
|
|
int other_length = strlen(cstring);
|
|
|
|
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-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 };
|
|
|
|
int m_length { 0 };
|
|
|
|
};
|
2019-04-16 00:39:16 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::StringView;
|