-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfixed_string.hpp
226 lines (194 loc) · 6.34 KB
/
fixed_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
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
#pragma once
//=====================================================================//
/*! @file
@brief 固定サイズ文字列クラス
@author 平松邦仁 ([email protected])
@copyright Copyright (C) 2017 Kunihito Hiramatsu @n
Released under the MIT license @n
https://github.com/hirakuni45/RX/blob/master/LICENSE
*/
//=====================================================================//
#include <algorithm>
#include <cstring>
namespace utils {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief 固定サイズ文字列クラス
@param[in] SIZE 文字列サイズ
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <uint32_t SIZE>
class fixed_string {
char text_[SIZE];
uint32_t pos_;
public:
//-----------------------------------------------------------------//
/*!
@brief コンストラクタ
@param[in] str 初期設定文字列
*/
//-----------------------------------------------------------------//
fixed_string(const char* str = nullptr) noexcept : pos_(0) {
if(str != nullptr) {
std::strcpy(text_, str);
pos_ = std::strlen(text_);
} else {
text_[pos_] = 0;
}
}
//-----------------------------------------------------------------//
/*!
@brief 格納可能な最大サイズを返す(終端の数を除外)
@return 格納可能な最大サイズ
*/
//-----------------------------------------------------------------//
uint32_t capacity() const noexcept { return SIZE - 1; }
//-----------------------------------------------------------------//
/*!
@brief 現在のサイズを返す
@return 現在のサイズ
*/
//-----------------------------------------------------------------//
uint32_t size() const noexcept { return pos_; }
//-----------------------------------------------------------------//
/*!
@brief 文字列をクリア(リセット)
*/
//-----------------------------------------------------------------//
void clear() noexcept {
pos_ = 0;
text_[pos_] = 0;
}
//-----------------------------------------------------------------//
/*!
@brief 文字列を返す
@return 文字列
*/
//-----------------------------------------------------------------//
const char* c_str() const noexcept { return text_; }
//-----------------------------------------------------------------//
/*!
@brief 交換
@param[in] src ソース
@return 自分
*/
//-----------------------------------------------------------------//
void swap(fixed_string& src) noexcept {
std::swap(src.text_, text_);
std::swap(src.pos_, pos_);
}
//-----------------------------------------------------------------//
/*!
@brief 代入
@param[in] src ソース
@return 自分
*/
//-----------------------------------------------------------------//
fixed_string& operator = (const fixed_string& src) {
std::strcpy(text_, src.c_str());
pos_ = src.pos_;
return *this;
}
//-----------------------------------------------------------------//
/*!
@brief 文字を追加
@param[in] ch 文字
@return 自分
*/
//-----------------------------------------------------------------//
fixed_string& operator += (char ch) {
if(pos_ < (SIZE - 1)) {
text_[pos_] = ch;
++pos_;
text_[pos_] = 0;
}
return *this;
}
//-----------------------------------------------------------------//
/*!
@brief 文字列を追加
@param[in] str 文字列
@return 自分
*/
//-----------------------------------------------------------------//
fixed_string& operator += (const char* str) {
if(str == nullptr) {
return *this;
}
uint32_t l = std::strlen(str);
if((pos_ + l) < (SIZE - 1)) {
std::strcpy(&text_[pos_], str);
pos_ += l;
} else { // バッファが許す範囲でコピー
l = SIZE - pos_ - 1;
std::strncpy(&text_[pos_], str, l);
pos_ = SIZE - 1;
}
text_[pos_] = 0;
return *this;
}
//-----------------------------------------------------------------//
/*!
@brief 文字参照
@param[in] pos 配列位置
@return 文字
*/
//-----------------------------------------------------------------//
char& operator [] (uint32_t pos) noexcept {
if(pos >= pos_) {
static char tmp = 0;
return tmp;
}
return text_[pos];
}
//-----------------------------------------------------------------//
/*!
@brief 一致比較
@param[in] text 文字列
@return 同じなら「true」
*/
//-----------------------------------------------------------------//
bool cmp(const char* text) const noexcept {
if(text == nullptr) {
return pos_ == 0;
}
return std::strcmp(c_str(), text) == 0;
}
//-----------------------------------------------------------------//
/*!
@brief 一致比較(オペレーター)
@param[in] text 文字列
@return 同じなら「true」
*/
//-----------------------------------------------------------------//
bool operator == (const char* text) const { return cmp(text); }
//-----------------------------------------------------------------//
/*!
@brief 不一致比較(オペレーター)
@param[in] text 文字列
@return 同じなら「true」
*/
//-----------------------------------------------------------------//
bool operator != (const char* text) const { return !cmp(text); }
//-----------------------------------------------------------------//
/*!
@brief クラス、一致比較(オペレーター)
@param[in] th 比較対象
@return 同じなら「true」
*/
//-----------------------------------------------------------------//
bool operator == (const fixed_string& th) const {
return std::strcmp(c_str(), th.c_str()) == 0;
}
//-----------------------------------------------------------------//
/*!
@brief クラス、不一致比較(オペレーター)
@param[in] th 比較対象
@return 同じなら「true」
*/
//-----------------------------------------------------------------//
bool operator != (const fixed_string& th) const {
return std::strcmp(c_str(), th.c_str()) != 0;
}
};
}