Repository navigation
Expand file tree
/
Copy pathfstring.hpp
More file actions
161 lines (131 loc) · 3.13 KB
/
Copy pathfstring.hpp
File metadata and controls
161 lines (131 loc) · 3.13 KB
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
#ifndef FSTRING_HPP
#define FSTRING_HPP
#include <string>
#include <cstdint>
#include <iostream>
/**
* fstring: fixed length string
*
* > Guaranteed to be null-terminated
* - All mutations append a NUL byte to the end of the buffer
* - All array access (operator[]) marks the size bit as dirty
*
* > Supports assignment, copy, concatenation, and streams.
*/
template<size_t SIZE, typename CHAR = char>
class fstring {
public:
fstring()
: size_(0), sizeDirty_(false)
{
}
fstring(const CHAR* s, size_t l)
: size_(0), sizeDirty_(false)
{
copy(s, l);
}
fstring(const CHAR* s)
: size_(0), sizeDirty_(false)
{
copy(s, maxlen());
}
fstring(const std::string& s)
: size_(0), sizeDirty_(false)
{
copy(s.c_str(), s.length());
}
inline const size_t maxlen() const
{
return SIZE;
}
inline const size_t size()
{
if (sizeDirty_)
{
size_ = strlen(buf_);
sizeDirty_ = false;
}
return size_;
}
inline const size_t length()
{
return size();
}
inline const CHAR* c_str() const
{
return buf_;
}
fstring<SIZE>& operator =(const CHAR* str)
{
size_ = 0;
copy(str, maxlen());
return *this;
}
template<size_t N>
fstring<SIZE>& operator =(const fstring<N>& str)
{
size_ = 0;
copy(str.c_str(), maxlen());
}
template<size_t N>
void operator +=(fstring<N>& str)
{
copy(str.c_str(), maxlen());
}
void operator +=(const CHAR* str)
{
copy(str, maxlen());
}
CHAR& operator [](size_t n)
{
sizeDirty_ = true;
return buf_[n];
}
inline const CHAR operator [](size_t n) const
{
return buf_[n];
}
template<size_t N>
inline fstring<SIZE>&& operator +(const fstring<N>& rhs) const
{
fstring<SIZE> ret;
ret.copy(c_str(), maxlen());
ret.copy(rhs.c_str(), maxlen());
return std::move(ret);
}
inline bool operator==(const CHAR * s) const
{
// Cannot mutate the size -- so we're just going to do an unsafe compare
// as our buffer will always be NUL-terminated.
return strcmp(c_str(), s) == 0;
}
inline bool operator==(const std::string& s) const
{
return strcmp(c_str(), s.c_str()) == 0;
}
template<size_t N>
inline bool operator==(const fstring<N>& s) const
{
return strcmp(c_str(), s.c_str()) == 0;
}
private:
size_t size_;
bool sizeDirty_;
CHAR buf_[SIZE + 1];
template<size_t N, typename C>
friend std::basic_ostream<C>& operator <<(std::basic_ostream<C>& os, const fstring<N, C>& fs);
void copy(const CHAR* s, size_t maxlen)
{
for (; *s && size_ < maxlen; s++, size_++)
{
buf_[size_] = *s;
}
buf_[size_] = 0;
}
};
template<size_t SIZE, typename CHAR>
std::basic_ostream<CHAR>& operator <<(std::basic_ostream<CHAR>& os, const fstring<SIZE, CHAR>& fs)
{
return os << fs.c_str();
}
#endif // FSTRING_HPP