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
|
/*
* ========================================================
*
* CompilerKit
* Copyright (C) 2024-2025 Amlal El Mahrouss, Licensed under Apache 2.0.
*
* ========================================================
*/
/**
* @file StringKit.cc
* @author Amlal (amlal@nekernel.org)
* @brief C++ string manipulation API.
* @version 0.2
* @date 2024-01-23
*
* @copyright Copyright (c) Amlal El Mahrouss
*
*/
#include <CompilerKit/StringKit.h>
namespace CompilerKit {
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 {
const SizeType len = Length();
if (rhs.Length() != len) return false;
return memcmp(m_Data, rhs.m_Data, len) == 0;
}
bool BasicString::operator==(const Char* rhs) const {
const SizeType rhs_len = string_length(rhs);
const SizeType len = Length();
if (rhs_len != len) return false;
return memcmp(m_Data, rhs, len) == 0;
}
bool BasicString::operator!=(const BasicString& rhs) const {
return !(*this == rhs);
}
bool BasicString::operator!=(const Char* rhs) const {
return !(*this == rhs);
}
BasicString StringBuilder::Construct(const Char* data) {
if (!data || *data == 0) return BasicString(0);
BasicString view(strlen(data));
view += data;
return view;
}
BasicString StringBuilder::FromInt(const char* fmt, int i) {
if (!fmt) return BasicString(0);
Char result[sizeof(int64_t)] = {0};
if (!to_str(result, sizeof(int64_t), i)) return BasicString(0);
const SizeType fmt_len = string_length(fmt);
const SizeType res_len = string_length(result);
BasicString output(fmt_len + res_len);
bool inserted = false;
for (SizeType idx = 0; idx < fmt_len; ++idx) {
if (!inserted && fmt[idx] == '%') {
output += result;
inserted = true;
continue;
}
output += Char{fmt[idx]};
}
return output;
}
BasicString StringBuilder::FromBool(const char* fmt, bool val) {
if (!fmt) return BasicString(0);
const Char* boolean_expr = val ? "true" : "false";
const SizeType fmt_len = string_length(fmt);
const SizeType res_len = string_length(boolean_expr);
BasicString output(fmt_len + res_len);
bool inserted = false;
for (SizeType idx = 0; idx < fmt_len; ++idx) {
if (!inserted && fmt[idx] == '%') {
output += boolean_expr;
inserted = true;
continue;
}
output += Char{fmt[idx]};
}
return output;
}
bool StringBuilder::Equals(const char* lhs, const char* rhs) {
const SizeType lhs_len = string_length(lhs);
const SizeType rhs_len = string_length(rhs);
if (lhs_len != rhs_len) return false;
return memcmp(lhs, rhs, lhs_len) == 0;
}
BasicString StringBuilder::Format(const char* fmt, const char* fmtRight) {
if (!fmt || !fmtRight) return BasicString(0);
const SizeType fmt_len = string_length(fmt);
const SizeType rhs_len = string_length(fmtRight);
BasicString output(fmt_len + rhs_len);
bool inserted = false;
for (SizeType idx = 0; idx < fmt_len; ++idx) {
if (!inserted && fmt[idx] == '%') {
output += fmtRight;
inserted = true;
continue;
}
output += Char{fmt[idx]};
}
return output;
}
BasicString& BasicString::operator+=(const Char* rhs) {
const SizeType rhs_len = strlen(rhs);
if (this->m_Cur + rhs_len >= this->m_Sz) {
throw std::runtime_error("out_of_bounds: BasicString");
}
memcpy(this->m_Data + this->m_Cur, rhs, rhs_len);
this->m_Cur += rhs_len;
this->m_Data[this->m_Cur] = '\0';
return *this;
}
BasicString& BasicString::operator+=(const BasicString& rhs) {
if (this->m_Cur + rhs.m_Cur >= this->m_Sz) {
throw std::runtime_error("out_of_bounds: BasicString");
}
memcpy(this->m_Data + this->m_Cur, rhs.CData(), rhs.m_Cur);
this->m_Cur += rhs.m_Cur;
this->m_Data[this->m_Cur] = '\0';
return *this;
}
BasicString& BasicString::operator+=(const Char ch) {
if (this->m_Cur + 1 >= this->m_Sz) {
throw std::runtime_error("out_of_bounds..");
}
this->m_Data[this->m_Cur++] = ch;
this->m_Data[this->m_Cur] = '\0';
return *this;
}
} // namespace CompilerKit
|