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
|
/*
* ========================================================
*
* LibCompiler
* Copyright (C) 2024-2025 Amlal El Mahrouss, all rights reserved.
*
* ========================================================
*/
/**
* @file BasicString.cxx
* @author Amlal (amlal@el-mahrouss-logic.com)
* @brief C++ string manipulation API.
* @version 0.2
* @date 2024-01-23
*
* @copyright Copyright (c) Amlal El Mahrouss
*
*/
#include <LibCompiler/BasicString.h>
namespace LibCompiler {
Char* BasicString::Data() {
return m_Data;
}
const Char* BasicString::CData() const {
return m_Data;
}
SizeType BasicString::Length() const {
return strlen(m_Data);
}
bool BasicString::operator==(const BasicString& rhs) const {
if (rhs.Length() != Length()) return false;
for (SizeType index = 0; index < Length(); ++index) {
if (rhs.m_Data[index] != m_Data[index]) return false;
}
return true;
}
bool BasicString::operator==(const Char* rhs) const {
if (string_length(rhs) != Length()) return false;
for (SizeType index = 0; index < string_length(rhs); ++index) {
if (rhs[index] != m_Data[index]) return false;
}
return true;
}
bool BasicString::operator!=(const BasicString& rhs) const {
if (rhs.Length() != Length()) return false;
for (SizeType index = 0; index < rhs.Length(); ++index) {
if (rhs.m_Data[index] == m_Data[index]) return false;
}
return true;
}
bool BasicString::operator!=(const Char* rhs) const {
if (string_length(rhs) != Length()) return false;
for (SizeType index = 0; index < string_length(rhs); ++index) {
if (rhs[index] == m_Data[index]) return false;
}
return true;
}
BasicString StringBuilder::Construct(const Char* data) {
if (!data || *data == 0) return BasicString(0);
BasicString view(strlen(data));
view += data;
return view;
}
const char* StringBuilder::FromInt(const char* fmt, int i) {
if (!fmt) return ("-1");
auto ret_len = 8 + string_length(fmt);
char* ret = new char[ret_len];
if (!ret) return ("-1");
memset(ret, 0, ret_len);
Char result[sizeof(int64_t)];
if (!to_str(result, sizeof(int64_t), i)) {
delete[] ret;
return ("-1");
}
const auto fmt_len = string_length(fmt);
const auto res_len = string_length(result);
for (SizeType idx = 0; idx < fmt_len; ++idx) {
if (fmt[idx] == '%') {
SizeType result_cnt = idx;
for (auto y_idx = 0; y_idx < res_len; ++y_idx) {
ret[y_idx] = result[result_cnt];
++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 = new char[i ? 4 : 5 + string_length(fmt)];
if (!ret) return ("?");
const auto fmt_len = string_length(fmt);
const auto res_len = string_length(boolean_expr);
for (SizeType idx = 0; idx < fmt_len; ++idx) {
if (fmt[idx] == '%') {
SizeType 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 (string_length(rhs) != string_length(lhs)) return false;
for (SizeType index = 0; index < string_length(rhs); ++index) {
if (rhs[index] != lhs[index]) return false;
}
return true;
}
const char* StringBuilder::Format(const char* fmt, const char* fmtRight) {
if (!fmt || !fmtRight) return ("?");
char* ret = new char[string_length(fmtRight) + string_length(fmtRight)];
if (!ret) return ("?");
for (SizeType idx = 0; idx < string_length(fmt); ++idx) {
if (fmt[idx] == '%') {
SizeType result_cnt = idx;
for (SizeType y_idx = 0; y_idx < string_length(fmtRight); ++y_idx) {
ret[result_cnt] = fmtRight[y_idx];
++result_cnt;
}
break;
}
ret[idx] = fmt[idx];
}
return ret;
}
BasicString& BasicString::operator+=(const Char* rhs) {
if (strlen(rhs) > this->m_Sz) {
throw std::runtime_error("out_of_bounds: BasicString");
}
memcpy(this->m_Data + this->m_Cur, rhs, strlen(rhs));
this->m_Cur += strlen(rhs);
return *this;
}
BasicString& BasicString::operator+=(const BasicString& rhs) {
if (rhs.m_Cur > this->m_Sz) {
throw std::runtime_error("out_of_bounds: BasicString");
}
memcpy(this->m_Data + this->m_Cur, rhs.CData(), strlen(rhs.CData()));
this->m_Cur += strlen(rhs.CData());
return *this;
}
} // namespace LibCompiler
|