-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString.h
More file actions
305 lines (264 loc) · 7 KB
/
String.h
File metadata and controls
305 lines (264 loc) · 7 KB
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
#ifndef STRING_H
#define STRING_H 1
//Headers
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Replacement string for C++ strings, NUL terminated
struct String
{
private:
//Changes the default size for the string on creation
#define START_SIZE 64
public:
//Variables to keep track of string values
int length;
private:
//Keeps track of how much memory is allocated
int allocated;
//Char array for all the characters in the string
char* characters;
//Initializes the string's memory
bool Init()
{
//Try to allocate memory
characters = (char*)malloc(START_SIZE * sizeof(char));
//Check if allocation was successful
if (characters == NULL)
{
printf("ERROR: Could not allocate %i bytes for String!\n", START_SIZE);
return false;
}
//Set how much space has been allocated
allocated = START_SIZE;
//Return success
return true;
}
//Checks to reallocate space for the string
bool Allocation(int desired)
{
//Keep track of size
int newSize = allocated;
//Get new allocation size, make sure there is room for NUL termination
while(newSize <= desired) newSize *= 2;
//Check if a resize is not needed
if (newSize <= allocated) return true;
//Resizes the memory, and set allocated amount to new
characters = (char*)realloc(characters, newSize);
//Check if reallocation was successful
if (characters == NULL)
{
//Print error
printf("ERROR: Was unable to realloc String from %i to %i bytes!\n", allocated, newSize);
//Return failure
return false;
}
//Set allocated amount to new size
allocated = newSize;
//Return success
return true;
}
public:
//Constructor
String(const char* str = "")
{
//Initially set values
length = 0;
//Allocate initial memory
Init();
//Check the desired size
int desired = strlen(str);
Allocation(desired);
//Insert string into characters
InsertAtEnd(str);
}
//Destructor
~String()
{
//Free memory used for characters
free(characters);
}
//Clears the whole string
void Clear()
{
//Clears the characters of the String, makes them all NUL termination
for (int i = 0; i < length; i++) characters[i] = '\0';
//Sets length to 0
length = 0;
}
//Inserts a string at an index in the string
bool Insert(const char* str, int index)
{
//Check if index is valid
if (index >= length) return false;
//Get length of string
int len = strlen(str);
//Check allocation
if (!Allocation(length + len)) return false;
//Move/Copy the characters backwards to make space for insertion
for (int i = length - index + len; i >= 0; i--)
{
characters[index + len + i] = characters[index + i];
}
//Puts the string at the desired index
for (int i = 0; i < len; i++)
{
characters[index + i] = str[i];
}
//Add desired to length
length += len;
//Make NUL termination
characters[length] = '\0';
//Return success
return true;
}
//Inserts a string on the end of this String
bool InsertAtEnd(const char* str)
{
//Get length of string
int len = strlen(str);
//Check allocation
if (!Allocation(length + len)) return false;
//Puts the string in on the end
for (int i = 0; i < len; i++)
{
characters[length + i] = str[i];
}
//Add desired to length
length += len;
//Make NUL termination
characters[length] = '\0';
//Return success
return true;
}
//Get a substring copy from within the string
String Substring(int start, int end)
{
//Make sure start is before end, return empty string if not
if (start >= end) return String();
//Check if end index is after String length
if (length < end) end = length;
//Allocate space for the substring
char* substr = (char*)malloc(sizeof(char) * (end - start));
//Dump substring char after char
for(int i = 0; i < end - start; i++)
{
substr[i] = characters[start + i];
}
//Create String with array
String result = String(substr);
//Clear memory for substring
free(substr);
//Return result
return result;
}
//Get a substring copy within the string, starting at 0
String Substring(int end)
{
return Substring(0, end);
}
//Finds a string within this string, and returns the index of where it starts
int Find(const char* find)
{
//Storage for the index
int index = -1;
//Get the length of the string
int findLen = strlen(find);
//Search for the first character
for (int i = 0; i < length - findLen; i++)
{
//Check if not the first character of string to find and skip
if (characters[i] != find[0]) continue;
//Set index premptively
index = i;
//Check for the rest of the string
for (int j = 1; j < findLen; j++)
{
//Check if characters don't match string to find, wipe index if they don't
if (characters[i + j] != find[j])
{
index = -1;
break;
}
}
//Check if index is not wiped, break if it's still set
if (index != -1) break;
}
//Return the index, or -1 if we found nothing
return index;
}
//Returns the raw string
char* GetRawString()
{
return characters;
}
//Gets the length of the string
int GetLength()
{
return strlen(characters);
}
//Gets the size of the char allocation in bytes
size_t AllocatedSize()
{
return allocated;
}
//Operator overload for index
char operator[](int index)
{
//Check if valid index, if not return -1
if (index >= length)
{
//Return error
printf("Could not access index \"%i\" in String with size %i!\n", index, length);
return -1;
}
//Return character at index
return characters[index];
}
//= operator overloads
void operator=(const char* rhs)
{
//Clear string contents
Clear();
//Insert new contents
InsertAtEnd(rhs);
}
void operator=(String rhs)
{
//Clear string contents
Clear();
//Insert new contents
InsertAtEnd(rhs.characters);
}
//+ operator overloads
String operator+(const char* rhs)
{
//Make a copy of this string
String result = String(characters);
//Add other string to the end
result.InsertAtEnd(rhs);
//Return result
return result;
}
String operator+(String rhs)
{
//Make a copy of this string
String result = String(characters);
//Add other string to the end
result.InsertAtEnd(rhs.characters);
//Return result
return result;
}
//+= operator overloads (More efficient than +)
void operator+=(const char* rhs)
{
//Inserts String contents at the end
InsertAtEnd(rhs);
}
void operator+=(String rhs)
{
//Inserts String contents at the end
InsertAtEnd(rhs.characters);
}
};
#endif