File: const_stringref.h

package info (click to toggle)
intel-compute-runtime 20.44.18297-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 34,780 kB
  • sloc: cpp: 379,729; lisp: 4,931; python: 299; sh: 196; makefile: 8
file content (226 lines) | stat: -rw-r--r-- 5,438 bytes parent folder | download
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * Copyright (C) 2019-2020 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#pragma once

#include <cinttypes>
#include <cstddef>
#include <cstring>
#include <string>

namespace NEO {

constexpr size_t constLength(const char *string) {
    if (nullptr == string) {
        return 0U;
    }
    auto it = string;
    for (; *it != '\0'; ++it) {
    }
    return it - string;
}

class ConstStringRef {
  public:
    constexpr ConstStringRef() {
    }

    constexpr ConstStringRef(const ConstStringRef &rhs) : ptr(rhs.ptr), len(rhs.len) {
    }

    ConstStringRef &operator=(const ConstStringRef &rhs) {
        this->ptr = rhs.ptr;
        this->len = rhs.len;
        return *this;
    }

    constexpr ConstStringRef(const char &c) noexcept
        : ptr(&c), len(1) {
    }

    constexpr ConstStringRef(const char *const ptr) noexcept
        : ptr(ptr), len(constLength(ptr)) {
    }

    constexpr ConstStringRef(const char *const ptr, const size_t length) noexcept
        : ptr(ptr), len(length) {
    }

    template <size_t Length>
    static constexpr ConstStringRef fromArray(const char (&array)[Length]) noexcept {
        return ConstStringRef(array, Length);
    }

    ConstStringRef(const std::string &str) noexcept
        : ptr(str.data()), len(str.length()) {
    }

    constexpr ConstStringRef substr(int offset, int len) const noexcept {
        if (len >= 0) {
            return ConstStringRef(this->ptr + offset, len);
        } else {
            return ConstStringRef(this->ptr + offset, this->len + len - offset);
        }
    }

    constexpr ConstStringRef substr(int offset) const noexcept {
        return ConstStringRef(this->ptr + offset, this->len - offset);
    }

    constexpr ConstStringRef truncated(int len) const noexcept {
        if (len >= 0) {
            return ConstStringRef(this->ptr, len);
        } else {
            return ConstStringRef(this->ptr, this->len + len);
        }
    }

    constexpr const char *data() const noexcept {
        return ptr;
    }

    constexpr char operator[](size_t pos) const noexcept {
        return ptr[pos];
    }

    constexpr char operator[](int pos) const noexcept {
        return ptr[pos];
    }

    explicit operator std::string() const {
        return str();
    }

    std::string str() const {
        return std::string(ptr, len);
    }

    constexpr size_t size() const noexcept {
        return len;
    }

    constexpr size_t length() const noexcept {
        return len;
    }

    constexpr bool empty() const noexcept {
        return len == 0;
    }

    constexpr const char *begin() const noexcept {
        return ptr;
    }

    constexpr const char *end() const noexcept {
        return ptr + len;
    }

    constexpr bool contains(const char *subString) const noexcept {
        const char *findBeg = ptr;
        const char *findEnd = ptr + len;
        while (findBeg != findEnd) {
            const char *lhs = findBeg;
            const char *rhs = subString;
            while ((lhs < findEnd) && (*lhs == *rhs) && ('\0' != *rhs)) {
                ++lhs;
                ++rhs;
            }
            if ('\0' == *rhs) {
                return true;
            }
            ++findBeg;
        }
        return false;
    }

    constexpr bool startsWith(const char *subString) const noexcept {
        const char *findEnd = ptr + len;
        const char *lhs = ptr;
        const char *rhs = subString;
        while ((lhs < findEnd) && (*lhs == *rhs) && ('\0' != *rhs)) {
            ++lhs;
            ++rhs;
        }
        return ('\0' == *rhs);
    }

  protected:
    ConstStringRef(std::nullptr_t) = delete;

    const char *ptr = nullptr;
    size_t len = 0U;
};

constexpr bool equals(const ConstStringRef &lhs, const ConstStringRef &rhs) {
    if (lhs.size() != rhs.size()) {
        return false;
    }

    for (size_t i = 0, e = lhs.size(); i < e; ++i) {
        if (lhs[i] != rhs[i]) {
            return false;
        }
    }

    return true;
}

constexpr bool equals(const ConstStringRef &lhs, const char *rhs) {
    size_t i = 0;
    for (size_t e = lhs.size(); i < e; ++i) {
        if (lhs[i] != rhs[i]) {
            return false;
        }
        if ((rhs[i] == '\0') && (i + 1 < e)) {
            return false;
        }
    }

    return (rhs[i] == '\0');
}

constexpr bool operator==(const ConstStringRef &lhs, const ConstStringRef &rhs) {
    return equals(lhs, rhs);
}

constexpr bool operator==(const ConstStringRef &lhs, const char *rhs) {
    return equals(lhs, rhs);
}

constexpr bool operator==(const char *lhs, const ConstStringRef &rhs) {
    return equals(rhs, lhs);
}

constexpr bool operator!=(const ConstStringRef &lhs, const ConstStringRef &rhs) {
    return false == equals(lhs, rhs);
}

constexpr bool operator!=(const ConstStringRef &lhs, const char *rhs) {
    return false == equals(lhs, rhs);
}

constexpr bool operator!=(const char *lhs, const ConstStringRef &rhs) {
    return false == equals(rhs, lhs);
}

constexpr bool equalsCaseInsesitive(const ConstStringRef &lhs, const ConstStringRef &rhs) {
    if (lhs.size() != rhs.size()) {
        return false;
    }

    constexpr auto caseDiff = 'a' - 'A';
    for (size_t i = 0, e = lhs.size(); i < e; ++i) {

        if ((lhs[i] != rhs[i]) & (lhs[i] + caseDiff != rhs[i]) & (lhs[i] != rhs[i] + caseDiff)) {
            return false;
        }
    }

    return true;
}

} // namespace NEO