forked from jacobdufault/cquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nt_string.h
43 lines (37 loc) · 1.15 KB
/
nt_string.h
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
#pragma once
#include <string_view.h>
#include <string.h>
#include <memory>
#include <string>
#include <utility>
// Nullable null-terminated string, which is null if default constructed,
// but non-null if assigned.
// This is used in Query{Func,Type,Var}::def to reduce memory footprint.
class NtString {
using size_type = std::string::size_type;
std::unique_ptr<char[]> str;
public:
NtString() = default;
NtString(NtString&& o) = default;
NtString(const NtString& o) { *this = o; }
NtString(std::string_view sv) { *this = sv; }
const char* c_str() const { return str.get(); }
operator std::string_view() const {
if (c_str())
return c_str();
return {};
}
bool empty() const { return !str || *c_str() == '\0'; }
void operator=(std::string_view sv) {
str = std::unique_ptr<char[]>(new char[sv.size() + 1]);
memcpy(str.get(), sv.data(), sv.size());
str.get()[sv.size()] = '\0';
}
void operator=(const NtString& o) {
*this = static_cast<std::string_view>(o);
}
bool operator==(const NtString& o) const {
return str && o.str ? strcmp(c_str(), o.c_str()) == 0
: c_str() == o.c_str();
}
};