This repository has been archived by the owner on Jul 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstr.hpp
321 lines (264 loc) · 11.3 KB
/
str.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright 2022 The Jule Programming Language.
// Use of this source code is governed by a BSD 3-Clause
// license that can be found in the LICENSE file.
#ifndef __JULE_STR_HPP
#define __JULE_STR_HPP
#include <sstream>
#include <ostream>
#include <string>
#include <cstring>
#include "panic.hpp"
#include "utf8.hpp"
#include "slice.hpp"
#include "types.hpp"
#include "error.hpp"
namespace jule {
// Built-in str type.
class Str;
// Libraries uses this function for UTf-8 encoded Jule strings.
// Also it is builtin str type constructor.
template<typename T>
jule::Str to_str(const T &obj) noexcept;
jule::Str to_str(const jule::Str &s) noexcept;
class Str {
public:
jule::Int _len{};
std::basic_string<jule::U8> buffer{};
Str(void) noexcept {}
Str(const char *src, const jule::Int &len) noexcept {
if (!src)
return;
this->_len = len;
this->buffer = std::basic_string<jule::U8>(&src[0],
&src[this->_len]);
}
Str(const char *src) noexcept {
if (!src)
return;
this->_len = std::strlen(src);
this->buffer = std::basic_string<jule::U8>(&src[0],
&src[this->_len]);
}
Str(const std::initializer_list<jule::U8> &src) noexcept {
this->_len = src.size();
this->buffer = src;
}
Str(const jule::I32 &rune) noexcept
: Str( jule::utf8_rune_to_bytes(rune) ) {}
Str(const std::basic_string<jule::U8> &src) noexcept {
this->_len = src.length();
this->buffer = src;
}
Str(const std::string &src) noexcept {
this->_len = src.length();
this->buffer = std::basic_string<jule::U8>(src.begin(), src.end());
}
Str(const jule::Str &src) noexcept {
this->_len = src._len;
this->buffer = src.buffer;
}
Str(const jule::Slice<U8> &src) noexcept {
this->_len = src.len();
this->buffer = std::basic_string<jule::U8>(src.begin(), src.end());
}
Str(const jule::Slice<jule::I32> &src) noexcept {
for (const jule::I32 &r: src) {
const jule::Slice<jule::U8> bytes{ jule::utf8_rune_to_bytes(r) };
this->_len += bytes.len();
for (const jule::U8 _byte: bytes)
this->buffer += _byte;
}
}
typedef jule::U8 *Iterator;
typedef const jule::U8 *ConstIterator;
inline Iterator begin(void) noexcept
{ return reinterpret_cast<Iterator>(&this->buffer[0]); }
inline ConstIterator begin(void) const noexcept
{ return reinterpret_cast<ConstIterator>(&this->buffer[0]); }
inline Iterator end(void) noexcept
{ return reinterpret_cast<Iterator>(&this->buffer[this->len()]); }
inline ConstIterator end(void) const noexcept
{ return reinterpret_cast<ConstIterator>(&this->buffer[this->len()]); }
inline jule::Str slice(const jule::Int &start,
const jule::Int &end) const noexcept {
if (start < 0 || end < 0 || start > end || end > this->len()) {
std::stringstream sstream;
__JULEC_WRITE_ERROR_SLICING_INDEX_OUT_OF_RANGE(
sstream, start, end);
jule::panic(sstream.str().c_str());
} else if (start == end)
return jule::Str();
const jule::Int n{ end-start };
return this->buffer.substr(start, n);
}
inline jule::Str slice(const jule::Int &start) const noexcept
{ return this->slice(start, this->len()); }
inline jule::Str slice(void) const noexcept
{ return this->slice(0, this->len()); }
inline jule::Int len(void) const noexcept
{ return this->_len; }
inline jule::Bool empty(void) const noexcept
{ return this->buffer.empty(); }
inline jule::Bool has_prefix(const jule::Str &sub) const noexcept {
return this->buffer.find(sub.buffer, 0) == 0;
}
inline jule::Bool has_suffix(const jule::Str &sub) const noexcept {
return this->len() >= sub.len() &&
this->buffer.substr(this->len()-sub.len()) == sub.buffer;
}
inline jule::Int find(const jule::Str &sub) const noexcept
{ return static_cast<jule::Int>(this->buffer.find(sub.buffer) ); }
inline jule::Int rfind(const jule::Str &sub) const noexcept
{ return static_cast<jule::Int>(this->buffer.rfind(sub.buffer)); }
jule::Str trim(const jule::Str &bytes) const noexcept {
ConstIterator it{ this->begin() };
const ConstIterator end{ this->end() };
ConstIterator begin{ this->begin() };
for (; it < end; ++it) {
jule::Bool exist{ false };
ConstIterator bytes_it{ bytes.begin() };
const ConstIterator bytes_end{ bytes.end() };
for (; bytes_it < bytes_end; ++bytes_it) {
if ((exist = *it == *bytes_it))
break;
}
if (!exist)
return this->buffer.substr(it-begin);
}
return jule::Str();
}
jule::Str rtrim(const jule::Str &bytes) const noexcept {
ConstIterator it{ this->end()-1 };
const ConstIterator begin{ this->begin() };
for (; it >= begin; --it) {
jule::Bool exist{ false };
ConstIterator bytes_it{ bytes.begin() };
const ConstIterator bytes_end{ bytes.end() };
for (; bytes_it < bytes_end; ++bytes_it) {
if ((exist = *it == *bytes_it))
break;
}
if (!exist)
return this->buffer.substr(0, it-begin+1);
}
return jule::Str();
}
jule::Slice<jule::Str> split(const jule::Str &sub,
const jule::I64 &n) const noexcept {
jule::Slice<jule::Str> parts;
if (n == 0)
return parts;
const ConstIterator begin{ this->begin() };
std::basic_string<jule::U8> s{ this->buffer };
constexpr jule::Uint npos{ static_cast<jule::Uint>(std::string::npos) };
jule::Uint pos{ npos };
if (n < 0) {
while ((pos = s.find(sub.buffer)) != npos) {
parts.push(s.substr(0, pos));
s = s.substr(pos+sub.len());
}
if (!s.empty())
parts.push(jule::Str(s));
} else {
jule::Uint _n{ 0 };
while ((pos = s.find(sub.buffer)) != npos) {
if (++_n >= n) {
parts.push(jule::Str(s));
break;
}
parts.push(s.substr(0, pos));
s = s.substr(pos+sub.len());
}
if (!parts.empty() && _n < n)
parts.push(jule::Str(s));
else if (parts.empty())
parts.push(jule::Str(s));
}
return parts;
}
jule::Str replace(const jule::Str &sub,
const jule::Str &_new,
const jule::I64 &n) const noexcept {
if (n == 0)
return *this;
std::basic_string<jule::U8> s(this->buffer);
constexpr jule::Uint npos{ static_cast<jule::Uint>(std::string::npos) };
jule::Uint start_pos{ 0 };
if (n < 0) {
while((start_pos = s.find(sub.buffer, start_pos)) != npos) {
s.replace(start_pos, sub.len(), _new.buffer);
start_pos += _new.len();
}
} else {
jule::Uint _n{ 0 };
while((start_pos = s.find(sub.buffer, start_pos)) != npos) {
s.replace(start_pos, sub.len(), _new.buffer);
start_pos += _new.len();
if (++_n >= n)
break;
}
}
return jule::Str(s);
}
inline operator const char*(void) const noexcept
{ return reinterpret_cast<const char*>(this->buffer.c_str()); }
inline operator const std::basic_string<jule::U8>(void) const noexcept
{ return this->buffer; }
inline operator const std::basic_string<char>(void) const noexcept
{ return std::basic_string<char>(this->begin(), this->end()); }
operator jule::Slice<jule::U8>(void) const noexcept {
jule::Slice<jule::U8> slice{ jule::Slice<jule::U8>::alloc(this->len()) };
for (jule::Int index{ 0 }; index < this->len(); ++index)
slice[index] = this->operator[](index);
return slice;
}
operator jule::Slice<jule::I32>(void) const noexcept {
jule::Slice<jule::I32> runes{};
const char *str{ this->operator const char *() };
for (jule::Int index{ 0 }; index < this->len(); ) {
jule::I32 rune;
jule::Int n;
std::tie(rune, n) = jule::utf8_decode_rune_str(str+index ,
this->len()-index);
index += n;
runes.push(rune);
}
return runes;
}
jule::U8 &operator[](const jule::Int &index) {
if (this->empty() || index < 0 || this->len() <= index) {
std::stringstream sstream;
__JULEC_WRITE_ERROR_INDEX_OUT_OF_RANGE(sstream, index);
jule::panic(sstream.str().c_str());
}
return this->buffer[index];
}
inline jule::U8 operator[](const jule::Int &index) const
{ return (*this).buffer[index]; }
inline void operator+=(const jule::Str &str) noexcept {
this->_len += str.len();
this->buffer += str.buffer;
}
inline jule::Str operator+(const jule::Str &str) const noexcept
{ return jule::Str(this->buffer + str.buffer); }
inline jule::Bool operator==(const jule::Str &str) const noexcept
{ return this->buffer == str.buffer; }
inline jule::Bool operator!=(const jule::Str &str) const noexcept
{ return !this->operator==(str); }
friend std::ostream &operator<<(std::ostream &stream,
const jule::Str &src) noexcept {
for (const jule::U8 &b: src)
{ stream << static_cast<char>(b); }
return stream;
}
};
template<typename T>
jule::Str to_str(const T &obj) noexcept {
std::stringstream stream;
stream << obj;
return jule::Str(stream.str());
}
jule::Str to_str(const jule::Str &s) noexcept
{ return s; }
} // namespace jule
#endif // #ifndef __JULE_STR_HPP