summaryrefslogtreecommitdiff
path: root/AK/StringView.h
blob: 51ccd2f67c7f9726d68730fc466c54c813f26ba7 (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
#pragma once

#include <AK/Vector.h>

namespace AK {

class String;

class StringView {
public:
    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) { }
    StringView(const char* cstring)
        : m_characters(cstring)
    {
        if (cstring) {
            while (*(cstring++))
                ++m_length;
        }
    }

    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]; }

    StringView substring_view(int start, int length) const;
    Vector<StringView> split_view(char) const;
    unsigned to_uint(bool& ok) const;

    bool operator==(const char* cstring) const { return !strcmp(m_characters, cstring); }
    bool operator!=(const char* cstring) const { return strcmp(m_characters, cstring); }

    bool operator==(const String&) const;

private:
    const char* m_characters { nullptr };
    int m_length { 0 };
};

}

using AK::StringView;