2019-09-06 13:34:26 +00:00
|
|
|
#include <AK/String.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <AK/StringView.h>
|
2019-04-16 00:39:16 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-06-08 21:55:13 +00:00
|
|
|
StringView::StringView(const String& string)
|
|
|
|
: m_impl(string.impl())
|
2019-06-03 16:27:56 +00:00
|
|
|
, m_characters(string.characters())
|
2019-06-02 10:19:21 +00:00
|
|
|
, m_length(string.length())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-29 10:03:28 +00:00
|
|
|
StringView::StringView(const ByteBuffer& buffer)
|
|
|
|
: m_characters((const char*)buffer.data())
|
|
|
|
, m_length(buffer.size())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-09-20 21:43:37 +00:00
|
|
|
Vector<StringView> StringView::split_view(const char separator, bool keep_empty) const
|
2019-04-16 00:39:16 +00:00
|
|
|
{
|
|
|
|
if (is_empty())
|
2019-05-28 09:53:16 +00:00
|
|
|
return {};
|
2019-04-16 00:39:16 +00:00
|
|
|
|
|
|
|
Vector<StringView> v;
|
|
|
|
ssize_t substart = 0;
|
|
|
|
for (ssize_t i = 0; i < length(); ++i) {
|
2019-07-08 13:38:44 +00:00
|
|
|
char ch = characters_without_null_termination()[i];
|
2019-04-16 00:39:16 +00:00
|
|
|
if (ch == separator) {
|
|
|
|
ssize_t sublen = i - substart;
|
2019-09-20 21:43:37 +00:00
|
|
|
if (sublen != 0 || keep_empty)
|
2019-04-16 00:39:16 +00:00
|
|
|
v.append(substring_view(substart, sublen));
|
|
|
|
substart = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ssize_t taillen = length() - substart;
|
2019-09-20 21:43:37 +00:00
|
|
|
if (taillen != 0 || keep_empty)
|
2019-04-16 00:39:16 +00:00
|
|
|
v.append(substring_view(substart, taillen));
|
2019-09-20 21:43:37 +00:00
|
|
|
if (characters_without_null_termination()[length() - 1] == separator && keep_empty)
|
2019-06-02 10:19:21 +00:00
|
|
|
v.append(String::empty());
|
2019-04-16 00:39:16 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2019-12-02 12:42:33 +00:00
|
|
|
Vector<StringView> StringView::lines(bool consider_cr) const
|
|
|
|
{
|
|
|
|
if (is_empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if (!consider_cr)
|
|
|
|
return split_view('\n', true);
|
|
|
|
|
|
|
|
Vector<StringView> v;
|
|
|
|
ssize_t substart = 0;
|
|
|
|
bool last_ch_was_cr = false;
|
|
|
|
bool split_view = false;
|
|
|
|
for (ssize_t i = 0; i < length(); ++i) {
|
|
|
|
char ch = characters_without_null_termination()[i];
|
|
|
|
if (ch == '\n') {
|
|
|
|
split_view = true;
|
|
|
|
if (last_ch_was_cr) {
|
|
|
|
substart = i + 1;
|
|
|
|
split_view = false;
|
|
|
|
last_ch_was_cr = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ch == '\r') {
|
|
|
|
split_view = true;
|
|
|
|
last_ch_was_cr = true;
|
|
|
|
}
|
|
|
|
if (split_view) {
|
|
|
|
ssize_t sublen = i - substart;
|
|
|
|
if (sublen != 0)
|
|
|
|
v.append(substring_view(substart, sublen));
|
|
|
|
substart = i + 1;
|
|
|
|
}
|
|
|
|
split_view = false;
|
|
|
|
}
|
|
|
|
ssize_t taillen = length() - substart;
|
|
|
|
if (taillen != 0)
|
|
|
|
v.append(substring_view(substart, taillen));
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2019-09-12 11:13:07 +00:00
|
|
|
bool StringView::starts_with(const StringView& str) const
|
|
|
|
{
|
|
|
|
if (str.is_empty())
|
|
|
|
return true;
|
|
|
|
if (is_empty())
|
|
|
|
return false;
|
|
|
|
if (str.length() > length())
|
|
|
|
return false;
|
|
|
|
if (characters_without_null_termination() == str.characters_without_null_termination())
|
|
|
|
return true;
|
|
|
|
return !memcmp(characters_without_null_termination(), str.characters_without_null_termination(), str.length());
|
|
|
|
}
|
|
|
|
|
2019-04-16 00:39:16 +00:00
|
|
|
StringView StringView::substring_view(int start, int length) const
|
|
|
|
{
|
|
|
|
if (!length)
|
2019-05-28 09:53:16 +00:00
|
|
|
return {};
|
2019-04-16 00:39:16 +00:00
|
|
|
ASSERT(start + length <= m_length);
|
|
|
|
return { m_characters + start, length };
|
|
|
|
}
|
|
|
|
|
2019-06-13 13:30:55 +00:00
|
|
|
StringView StringView::substring_view_starting_from_substring(const StringView& substring) const
|
|
|
|
{
|
2019-07-08 13:38:44 +00:00
|
|
|
const char* remaining_characters = substring.characters_without_null_termination();
|
2019-06-13 13:30:55 +00:00
|
|
|
ASSERT(remaining_characters >= m_characters);
|
|
|
|
ASSERT(remaining_characters <= m_characters + m_length);
|
|
|
|
int remaining_length = m_length - (remaining_characters - m_characters);
|
|
|
|
return { remaining_characters, remaining_length };
|
|
|
|
}
|
|
|
|
|
|
|
|
StringView StringView::substring_view_starting_after_substring(const StringView& substring) const
|
|
|
|
{
|
2019-07-08 13:38:44 +00:00
|
|
|
const char* remaining_characters = substring.characters_without_null_termination() + substring.length();
|
2019-06-13 13:30:55 +00:00
|
|
|
ASSERT(remaining_characters >= m_characters);
|
|
|
|
ASSERT(remaining_characters <= m_characters + m_length);
|
|
|
|
int remaining_length = m_length - (remaining_characters - m_characters);
|
|
|
|
return { remaining_characters, remaining_length };
|
|
|
|
}
|
|
|
|
|
2019-08-04 09:44:20 +00:00
|
|
|
int StringView::to_int(bool& ok) const
|
|
|
|
{
|
|
|
|
bool negative = false;
|
|
|
|
int value = 0;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if (is_empty()) {
|
|
|
|
ok = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (characters_without_null_termination()[0] == '-') {
|
|
|
|
i++;
|
|
|
|
negative = true;
|
|
|
|
}
|
|
|
|
for (; i < length(); i++) {
|
|
|
|
if (characters_without_null_termination()[i] < '0' || characters_without_null_termination()[i] > '9') {
|
|
|
|
ok = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
value = value * 10;
|
|
|
|
value += characters_without_null_termination()[i] - '0';
|
|
|
|
}
|
|
|
|
ok = true;
|
|
|
|
|
|
|
|
return negative ? -value : value;
|
|
|
|
}
|
|
|
|
|
2019-04-16 00:39:16 +00:00
|
|
|
unsigned StringView::to_uint(bool& ok) const
|
|
|
|
{
|
|
|
|
unsigned value = 0;
|
|
|
|
for (ssize_t i = 0; i < length(); ++i) {
|
2019-07-08 13:38:44 +00:00
|
|
|
if (characters_without_null_termination()[i] < '0' || characters_without_null_termination()[i] > '9') {
|
2019-04-16 00:39:16 +00:00
|
|
|
ok = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
value = value * 10;
|
2019-07-08 13:38:44 +00:00
|
|
|
value += characters_without_null_termination()[i] - '0';
|
2019-04-16 00:39:16 +00:00
|
|
|
}
|
|
|
|
ok = true;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2019-08-24 20:31:06 +00:00
|
|
|
unsigned StringView::hash() const
|
|
|
|
{
|
|
|
|
if (is_empty())
|
|
|
|
return 0;
|
|
|
|
if (m_impl)
|
|
|
|
return m_impl->hash();
|
|
|
|
return string_hash(characters_without_null_termination(), length());
|
|
|
|
}
|
|
|
|
|
2019-04-16 00:39:16 +00:00
|
|
|
}
|