2019-04-16 00:39:16 +00:00
|
|
|
#include <AK/AKString.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-04-16 00:39:16 +00:00
|
|
|
Vector<StringView> StringView::split_view(const char separator) const
|
|
|
|
{
|
|
|
|
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) {
|
|
|
|
char ch = characters()[i];
|
|
|
|
if (ch == separator) {
|
|
|
|
ssize_t sublen = i - substart;
|
|
|
|
if (sublen != 0)
|
|
|
|
v.append(substring_view(substart, sublen));
|
|
|
|
substart = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ssize_t taillen = length() - substart;
|
|
|
|
if (taillen != 0)
|
|
|
|
v.append(substring_view(substart, taillen));
|
|
|
|
if (characters()[length() - 1] == separator)
|
2019-06-02 10:19:21 +00:00
|
|
|
v.append(String::empty());
|
2019-04-16 00:39:16 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
const char* remaining_characters = substring.characters();
|
|
|
|
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
|
|
|
|
{
|
|
|
|
const char* remaining_characters = substring.characters() + substring.length();
|
|
|
|
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-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) {
|
|
|
|
if (characters()[i] < '0' || characters()[i] > '9') {
|
|
|
|
ok = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
value = value * 10;
|
|
|
|
value += characters()[i] - '0';
|
|
|
|
}
|
|
|
|
ok = true;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|