-
Notifications
You must be signed in to change notification settings - Fork 0
/
String.cpp
226 lines (191 loc) · 3.45 KB
/
String.cpp
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
//
// Created by pedro on 27/04/18.
//
#include "String.h"
#include "Memory.h"
String::String()
{
count = 1;
capacity = 8;
data = (char*) malloc(sizeof(char) * capacity);
data[0] = '\0';
}
String::String(const char * str)
{
size_t length = strlen(str);
capacity = length + 1;
count = capacity;
data = (char*) malloc(sizeof(char) * capacity);
Memory::memcpy(data, str, sizeof(char) * (length + 1));
}
String::String(char c, size_t n)
{
count = n + 1;
capacity = count;
data = (char*) malloc(sizeof(char) * capacity);
for(size_t i = 0; i < n; i++) {
data[i] = c;
}
data[n] = '\0';
}
String::String(const String& other)
{
capacity = other.capacity;
count = other.count;
data = (char*) malloc(sizeof(char) * capacity);
Memory::memcpy(data, other.data, sizeof(char) * count);
}
String& String::operator=(const String& other)
{
capacity = other.capacity;
count = other.count;
data = (char*) realloc(data, sizeof(char) * capacity);
Memory::memcpy(data, other.data, sizeof(char) * count);
return *this;
}
String::~String()
{
free(data);
}
void String::push(char c)
{
if(count >= capacity)
{
capacity *= 2;
data = (char*) realloc(data, sizeof(char) * capacity);
}
//Replace '\0' with new char
data[count - 1] = c;
//Don't forget to end string
data[count++] = '\0';
}
void String::push(const String &s)
{
for(size_t i = 0; i < s.getLength(); i++)
{
push(s[i]);
}
}
size_t String::getLength() const
{
return count - 1;
}
char& String::operator[](size_t index)
{
return data[index];
}
const char& String::operator[](size_t index) const
{
return data[index];
}
String operator+(const String& a, const String& b)
{
String result(a);
result.push(b);
return result;
}
String operator+(const String& s, char c)
{
String result(s);
result.push(c);
return result;
}
String operator+(char c, const String& s)
{
String result(s);
//Push arbitrary character
result.push('c');
for(size_t i = result.getLength(); i > 0; i--)
{
result[i] = result[i - 1];
}
result[0] = c;
return result;
}
String& String::operator+=(const String& str)
{
push(str);
return *this;
}
String& String::operator+=(char c)
{
push(c);
return *this;
}
size_t String::strlen(const char* str)
{
size_t size = 0;
while(*str != '\0')
{
size++;
str++;
}
return size;
}
const char* String::getString() const
{
return data;
}
bool String::operator<(const String &other) const
{
size_t index = 0;
while(index < getLength() && index < other.getLength())
{
if(data[index] > other[index])
{
return false;
}
else if(data[index] < other[index])
{
return true;
}
index++;
}
//The shortest string is smaller
return getLength() < other.getLength();
}
bool String::operator==(const String &other) const
{
if(getLength() != other.getLength())
{
return false;
}
for(size_t i = 0; i < getLength(); i++)
{
if(data[i] != other[i])
{
return false;
}
}
return true;
}
bool String::operator<=(const String &other) const
{
size_t index = 0;
while(index < getLength() && index < other.getLength())
{
if(data[index] > other[index])
{
return false;
}
else if(data[index] < other[index])
{
return true;
}
index++;
}
//The shortest string is smaller
return getLength() <= other.getLength();
}
bool String::operator>(const String &other) const
{
return !(*this <= other);
}
bool String::operator>=(const String &other) const
{
return !(*this < other);
}
bool String::operator!=(const String &other) const
{
return !(*this == other);
}