blob: 06fe6752e501019a36ae4e37bc425cbe9a858331 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#include <AK/StringView.h>
#include <AK/AKString.h>
namespace AK {
Vector<StringView> StringView::split_view(const char separator) const
{
if (is_empty())
return { };
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)
v.append(String::empty().view());
return v;
}
StringView StringView::substring_view(int start, int length) const
{
if (!length)
return { };
ASSERT(start + length <= m_length);
return { m_characters + start, length };
}
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;
}
}
|