aboutsummaryrefslogtreecommitdiff
path: root/include/StringView.hpp
blob: 32933587765e5e89e06248186fd53f4415c627d7 (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
55
56
57
58
59
60
61
62
#pragma once

#include "types.hpp"
#include <cstring>
#include <cassert>

namespace dchat
{
    class StringView
    {
    public:
        StringView() : data(nullptr), size(0)
        {
            
        }

        StringView(const StringView &other) : data(other.data), size(other.size)
        {

        }
        
        StringView(const char *_data) : data(_data), size(strlen(_data))
        {
            
        }
        
        StringView(const char *_data, usize _size) : data(_data), size(_size)
        {
            
        }
        
        StringView operator = (const StringView &other)
        {
            StringView result(other.data, other.size);
            return result;
        }
        
        StringView(StringView &&other)
        {
            data = other.data;
            size = other.size;
            
            other.data = nullptr;
            other.size = 0;
        }
        
        bool equals(const StringView &other) const
        {
            if(size != other.size) return false;
            return memcmp(data, other.data, size) == 0;
        }
        
        char operator [] (usize index) const
        {
            assert(index < size);
            return data[index];
        }
        
        const char *data;
        usize size;
    };
}