-
Notifications
You must be signed in to change notification settings - Fork 0
/
atyp_String.h
310 lines (244 loc) · 6.65 KB
/
atyp_String.h
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
/**
* @file atyp_String.h
*
* @brief A header only String Implementaion
*
* @author Christopher-Robin Ebbinghaus
* Issues: https://github.com/CEbbinghaus/StandardLibary
* Contact: [email protected]
*/
#pragma once;
#include <stdio.h>
#include <stdarg.h>
#include <malloc.h>
#include <stdexcept>
using uint = unsigned int;
class String{
char* data;
uint length;
static int findMin(int x, int y, int z) {
if (x <= y && x <= z)
return x;
else if (y <= x && y <= z)
return y;
else
return z;
}
void allocateData(){
data = (char*)malloc(length + 1);
if (data == nullptr) throw std::bad_alloc();
}
static char* allocateData(uint length){
char* tmp = (char*)malloc(length + 1);
if (tmp == nullptr) throw std::bad_alloc();
return tmp;
}
void copyData(const char* source){
memcpy((void*)data, source, length);
data[length] = '\0';
}
static char* copyData(char* dest, char* src, uint length){
memcpy((void*)dest, src, length);
dest[length] = '\0';
return dest;
}
void destroyData() {
if (data != nullptr)
free((void*)data);
data = nullptr;
}
public:
static String format(const char* base, ...) {
va_list argptr;
va_start(argptr, base);
size_t needed = vsnprintf(NULL, 0, base, argptr) + 1;
char* buffer = (char*)malloc(needed);
vsprintf(buffer, base, argptr);
va_end(argptr);
String result = String(buffer);
free(buffer);
return result;
}
static int LevinSteinDistance(const String& a, const String& b) {
// Declaring a 2D array on the heap dynamically:
int len_a = strlen(a.data) + 1;
int len_b = strlen(b.data) + 1;
int** d = new int*[len_a];
for (int i = 0; i < len_a; i++)
d[i] = new int[len_b];
// Initialising first column:
for (int i = 0; i < len_a; i++)
d[i][0] = i;
// Initialising first row:
for (int j = 0; j < len_b; j++)
d[0][j] = j;
// Applying the algorithm:
int insertion, deletion, replacement;
for (int i = 1; i < len_a; i++) {
for (int j = 1; j < len_b; j++) {
if (a.data[i - 1] == b.data[j - 1]) {
d[i][j] = d[i - 1][j - 1];
} else {
// Choosing the best option:
insertion = d[i][j - 1];
deletion = d[i - 1][j];
replacement = d[i - 1][j - 1];
d[i][j] = 1 + findMin(insertion, deletion, replacement);
}
}
}
int answer = d[len_a - 1][len_b - 1];
for (int i = 0; i < len_a; ++i) {
delete[] d[i];
}
delete[] d;
return answer;
}
String(){
length = 1;
allocateData();
data[0] = '\0';
}
String(const char* inData){
length = strlen(inData);
allocateData();
copyData(inData);
}
String(const String& source){
length = source.length;
allocateData();
copyData(source.data);
}
String(String&& source) noexcept{
length = source.length;
data = source.data;
source.data = nullptr;
}
~String() {
//Delete Data if it Exists
destroyData();
}
String& operator=(const String& other) {
destroyData();
//Assign and Allocate local Memory
length = other.length;
allocateData();
copyData(other.data);
return *this;
}
String& operator=(String&& other) noexcept {
destroyData();
//Assign and Allocate local Memory
length = other.length;
data = other.data;
other.data = nullptr;
return *this;
}
String& operator=(const char* inData) {
destroyData();
length = strlen(inData);
allocateData();
copyData(inData);
return *this;
}
bool operator ==(const String& other) const {
return strcmp(this->data, other.data) == 0;
}
bool operator ==(const char* other) const {
return strcmp(this->data, other) == 0;
}
bool operator !=(const String& other) const {
return strcmp(this->data, other.data) != 0;
}
bool operator !=(const char* other) const {
return strcmp(this->data, other) != 0;
}
String operator+(const String& other) const {
//Allocates the Combined Length of Both strings + 1 for the Null Terminator
uint totalLength = length + other.length;
char* strData = allocateData(totalLength);
//Copies the Data into the String and Sets the last character to Null
memcpy(strData, data, length);
memcpy(strData + length, other.data, other.length);
strData[totalLength] = '\0';
String result = String(strData);
free(strData);
return result;
}
String operator+(const char* other) const {
//Allocates the Combined Length of Both strings + 1 for the Null Terminator
uint otherLength = strlen(other);
uint totalLength = length + otherLength;
char* strData = allocateData(totalLength);
//Copies the Data into the String and Sets the last character to Null
memcpy(strData, data, length);
memcpy(strData + length, other, otherLength);
strData[totalLength] = '\0';
String result = String(strData);
free(strData);
return result;
}
String& operator+=(const String& other){
//Allocates the Combined Length of Both strings + 1 for the Null Terminator
uint totalLength = length + other.length;
char* strData = allocateData(totalLength);
//Copies the Data into the String and Sets the last character to Null
memcpy(strData, data, length);
memcpy(strData + length, other.data, other.length);
strData[totalLength] = '\0';
//Update Values
destroyData();
data = strData;
length = totalLength;
return *this;
}
String& operator+=(const char* other) {
//Allocates the Combined Length of Both strings + 1 for the Null Terminator
uint otherLength = strlen(other);
uint totalLength = length + otherLength;
char* strData = allocateData(totalLength);
//Copies the Data into the String and Sets the last character to Null
memcpy(strData, data, length);
memcpy(strData + length, other, otherLength);
strData[totalLength] = '\0';
//Update Values
destroyData();
data = strData;
length = totalLength;
return *this;
}
const char& operator[](int index) const {
int len = (int)length;
if(index > len || abs(index) > length)
throw std::out_of_range("Index was out of Range");
if(index < 0)
return data[len + ((index % len) ? (index % len) : -len)];
else
return data[index % len];
}
int Length() const{
return length;
}
const char* ToCString() const{
return data;
}
operator const char*() const{
return data;
}
const char* operator&()const{
return data;
}
};
inline String operator+(const char* lhs, const String& rhs) {
//Allocates the Combined Length of Both strings + 1 for the Null Terminator
uint otherLength = strlen(lhs);
uint totalLength = rhs.Length() + otherLength;
char* strData = (char*)malloc(totalLength + 1);
//Copies the Data into the String and Sets the last character to Null
memcpy(strData, lhs, otherLength);
memcpy(strData + otherLength, rhs, rhs.Length());
strData[totalLength] = '\0';
String result = String(strData);
free(strData);
return result;
}