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
|
/*
* ========================================================
*
* HCore
* Copyright Mahrouss Logic, all rights reserved.
*
* ========================================================
*/
#include <NewKit/String.hpp>
#include <NewKit/Utils.hpp>
namespace HCore {
Char *StringView::Data() { return m_Data; }
const Char *StringView::CData() { return m_Data; }
Size StringView::Length() const { return rt_string_len(m_Data); }
bool StringView::operator==(const StringView &rhs) const {
if (rhs.Length() != this->Length()) return false;
for (Size index = 0; index < this->Length(); ++index) {
if (rhs.m_Data[index] != m_Data[index]) return false;
}
return true;
}
bool StringView::operator==(const Char *rhs) const {
if (rt_string_len(rhs) != this->Length()) return false;
for (Size index = 0; index < rt_string_len(rhs); ++index) {
if (rhs[index] != m_Data[index]) return false;
}
return true;
}
bool StringView::operator!=(const StringView &rhs) const {
if (rhs.Length() != this->Length()) return false;
for (Size index = 0; index < rhs.Length(); ++index) {
if (rhs.m_Data[index] == m_Data[index]) return false;
}
return true;
}
bool StringView::operator!=(const Char *rhs) const {
if (rt_string_len(rhs) != this->Length()) return false;
for (Size index = 0; index < rt_string_len(rhs); ++index) {
if (rhs[index] == m_Data[index]) return false;
}
return true;
}
ErrorOr<StringView> StringBuilder::Construct(const Char *data) {
if (!data || *data == 0) return {};
StringView view(rt_string_len(data));
rt_copy_memory(reinterpret_cast<voidPtr>(const_cast<Char *>(data)),
reinterpret_cast<voidPtr>(view.Data()), view.Length());
return ErrorOr<StringView>(view);
}
const char *StringBuilder::FromInt(const char *fmt, int i) {
if (!fmt) return ("-1");
char *ret = (char *)__alloca(sizeof(char) * 8 + rt_string_len(fmt));
if (!ret) return ("-1");
Char result[8];
if (!rt_to_string(result, sizeof(int), i)) {
return ("-1");
}
const auto fmt_len = rt_string_len(fmt);
const auto res_len = rt_string_len(result);
for (Size idx = 0; idx < fmt_len; ++idx) {
if (fmt[idx] == '%') {
SizeT result_cnt = idx;
for (auto y_idx = idx; y_idx < res_len; ++y_idx) {
ret[result_cnt] = result[y_idx];
++result_cnt;
}
break;
}
ret[idx] = fmt[idx];
}
return ret; /* Copy that ret into a buffer, Alloca allocates to the stack */
}
const char *StringBuilder::FromBool(const char *fmt, bool i) {
if (!fmt) return ("?");
const char *boolean_expr = i ? "true" : "false";
char *ret = (char *)__alloca((sizeof(char) * i) ? 4 : 5 + rt_string_len(fmt));
if (!ret) return ("?");
const auto fmt_len = rt_string_len(fmt);
const auto res_len = rt_string_len(boolean_expr);
for (Size idx = 0; idx < fmt_len; ++idx) {
if (fmt[idx] == '%') {
SizeT result_cnt = idx;
for (auto y_idx = idx; y_idx < res_len; ++y_idx) {
ret[result_cnt] = boolean_expr[y_idx];
++result_cnt;
}
break;
}
ret[idx] = fmt[idx];
}
return ret;
}
bool StringBuilder::Equals(const char *lhs, const char *rhs) {
if (rt_string_len(rhs) != rt_string_len(lhs)) return false;
for (Size index = 0; index < rt_string_len(rhs); ++index) {
if (rhs[index] != lhs[index]) return false;
}
return true;
}
const char *StringBuilder::Format(const char *fmt, const char *fmt2) {
if (!fmt || !fmt2) return ("?");
char *ret =
(char *)alloca(sizeof(char) * rt_string_len(fmt2) + rt_string_len(fmt2));
if (!ret) return ("?");
for (Size idx = 0; idx < rt_string_len(fmt); ++idx) {
if (fmt[idx] == '%') {
Size result_cnt = idx;
for (Size y_idx = 0; y_idx < rt_string_len(fmt2); ++y_idx) {
ret[result_cnt] = fmt2[y_idx];
++result_cnt;
}
break;
}
ret[idx] = fmt[idx];
}
return ret;
}
static void string_append(char *lhs, char *rhs, int cur) {
if (lhs && rhs && cur < rt_string_len(lhs)) {
SizeT sz_rhs = rt_string_len(rhs);
rt_copy_memory(rhs, lhs + cur, sz_rhs);
}
}
StringView &StringView::operator+=(const Char *rhs) {
if (rt_string_len(rhs) > rt_string_len(this->m_Data)) return *this;
string_append(this->m_Data, const_cast<char *>(rhs), this->m_Cur);
this->m_Cur += rt_string_len(rhs);
return *this;
}
StringView &StringView::operator+=(const StringView &rhs) {
if (rt_string_len(rhs.m_Data) > rt_string_len(this->m_Data)) return *this;
string_append(this->m_Data, const_cast<char *>(rhs.m_Data), this->m_Cur);
this->m_Cur += rt_string_len(const_cast<char *>(rhs.m_Data));
return *this;
}
} // namespace HCore
|