-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
String.hpp
77 lines (59 loc) · 1.64 KB
/
String.hpp
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
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2022, Benoit BLANCHON
// MIT License
#pragma once
#include <ArduinoJson/Misc/SafeBoolIdiom.hpp>
#if ARDUINOJSON_ENABLE_STD_STREAM
# include <ostream>
#endif
namespace ARDUINOJSON_NAMESPACE {
class String : public SafeBoolIdom<String> {
public:
enum Ownership { Copied, Linked };
String() : _data(0), _size(0), _ownership(Linked) {}
String(const char* data, Ownership ownership = Linked)
: _data(data), _size(data ? ::strlen(data) : 0), _ownership(ownership) {}
String(const char* data, size_t sz, Ownership ownership = Linked)
: _data(data), _size(sz), _ownership(ownership) {}
const char* c_str() const {
return _data;
}
bool isNull() const {
return !_data;
}
bool isLinked() const {
return _ownership == Linked;
}
size_t size() const {
return _size;
}
// safe bool idiom
operator bool_type() const {
return _data ? safe_true() : safe_false();
}
friend bool operator==(String lhs, String rhs) {
if (lhs._size != rhs._size)
return false;
if (lhs._data == rhs._data)
return true;
if (!lhs._data)
return false;
if (!rhs._data)
return false;
return memcmp(lhs._data, rhs._data, lhs._size) == 0;
}
friend bool operator!=(String lhs, String rhs) {
return !(lhs == rhs);
}
#if ARDUINOJSON_ENABLE_STD_STREAM
friend std::ostream& operator<<(std::ostream& lhs, const String& rhs) {
lhs.write(rhs.c_str(), static_cast<std::streamsize>(rhs.size()));
return lhs;
}
#endif
private:
const char* _data;
size_t _size;
Ownership _ownership;
};
} // namespace ARDUINOJSON_NAMESPACE