1 #include "string.h"
2 #include <cstring>
3 #include <cstdlib>
4 #include <cstdio>
5 #include "ParseTools.h"
6 #include "lineFileUtilities.h"
7
string(size_t capacity)8 string::string(size_t capacity)
9 : _buffer(NULL),
10 _currCapacity(capacity),
11 _currSize(0)
12 {
13 build();
14 }
15
string(const string & qs)16 string::string(const string &qs)
17 : _buffer(NULL),
18 _currCapacity(qs._currCapacity),
19 _currSize(0)
20 {
21 build();
22 set(qs._buffer, qs._currSize);
23 }
24
string(const char * inBuf)25 string::string(const char *inBuf)
26 {
27 size_t len = strlen(inBuf);
28 _currCapacity = len +1;
29
30 build();
31 set(inBuf, len);
32 }
33
string(const string & inString)34 string::string(const string &inString)
35 {
36 size_t len = (int)inString.size();
37 _currCapacity = len +1;
38
39 build();
40 set(inString.c_str(), len);
41 }
42
string(char c)43 string::string(char c)
44 {
45 _currCapacity =2;
46
47 build();
48
49 char buffer[2];
50 buffer[0] = c;
51 buffer[1] = 0;
52
53 set(buffer, 1);
54 }
55
build()56 void string::build() {
57 _buffer = (char *)malloc(_currCapacity);
58 clear();
59 }
60
~string()61 string::~string(){
62 free(_buffer);
63 }
64
clear()65 void string::clear() {
66 memset(_buffer, 0, _currCapacity);
67 _currSize = 0;
68 }
69
release()70 void string::release() {
71 free(_buffer);
72 _currCapacity = DEFAULT_CAPACITY;
73 build();
74 }
75
operator =(const char * inBuf)76 string &string::operator = (const char *inBuf){
77 set(inBuf, strlen(inBuf));
78 return *this;
79 }
80
operator =(const string & inBuf)81 string &string::operator = (const string & inBuf){
82 set(inBuf.c_str(), (int)inBuf.size());
83 return *this;
84 }
85
operator =(const string & inBuf)86 string &string::operator = (const string & inBuf){
87 set(inBuf._buffer, (int)inBuf._currSize);
88 return *this;
89 }
90
operator =(char val)91 string &string::operator = (char val) {
92 clear();
93 append(val);
94 return *this;
95 }
operator =(int val)96 string &string::operator = (int val) {
97 clear();
98 append(val);
99 return *this;
100 }
101
operator =(uint32_t val)102 string &string::operator = (uint32_t val) {
103 clear();
104 append(val);
105 return *this;
106 }
107
108 // string &string::operator = (size_t val) {
109 // clear();
110 // append(val);
111 // return *this;
112 // }
113
operator =(float val)114 string &string::operator = (float val) {
115 clear();
116 append(val);
117 return *this;
118 }
119
operator =(double val)120 string &string::operator = (double val) {
121 clear();
122 append(val);
123 return *this;
124 }
125
126
operator +=(const string & inBuf)127 string &string::operator += (const string & inBuf)
128 {
129 append(inBuf._buffer, (int)inBuf._currSize);
130 return *this;
131 }
132
operator +=(const string & inBuf)133 string &string::operator +=(const string &inBuf)
134 {
135 append(inBuf.c_str(), (int)inBuf.size());
136 return *this;
137 }
138
operator +=(char c)139 string &string::operator +=(char c) {
140
141 append(c);
142 return *this;
143 }
144
operator +=(const char * inBuf)145 string &string::operator += (const char *inBuf)
146 {
147 append(inBuf, strlen(inBuf));
148 return *this;
149 }
150
operator +=(int num)151 string &string::operator += (int num) {
152 append(num);
153 return *this;
154 }
155
operator +=(uint32_t num)156 string &string::operator += (uint32_t num) {
157 append(num);
158 return *this;
159 }
160
161 // string &string::operator += (size_t num) {
162 // append(num);
163 // return *this;
164 // }
165
operator +=(float num)166 string &string::operator += (float num) {
167 append(num);
168 return *this;
169 }
170
operator +=(double num)171 string &string::operator += (double num) {
172 append(num);
173 return *this;
174 }
175
operator ==(const string & qs) const176 bool string::operator == (const string &qs) const {
177 if ( _currSize != qs._currSize) {
178 return false;
179 }
180 for (int i= _currSize-1; i > -1; i--) {
181 if (_buffer[i] != qs._buffer[i]) return false;
182 }
183 return true;
184 }
185
operator ==(const string & str) const186 bool string::operator == (const string &str) const {
187 if ( _currSize != str.size()) {
188 return false;
189 }
190 for (int i= _currSize-1; i > -1; i--) {
191 if (_buffer[i] != str[i]) return false;
192 }
193 return true;
194
195 }
196
stricmp(const string & str) const197 bool string::stricmp(const string &str) const {
198 if (str.size() != _currSize) {
199 return true;
200 }
201 for (size_t i=0; i < _currSize; i++) {
202 if (tolower(str[i]) != tolower(_buffer[i])) {
203 return true;
204 }
205 }
206 return false;
207 }
208
operator ==(const char * str) const209 bool string::operator == (const char *str) const {
210 size_t inLen = strlen(str);
211 if (inLen != _currSize) {
212 return false;
213 }
214 for (int i= _currSize-1; i > -1; i--) {
215 if (_buffer[i] != str[i]) return false;
216 }
217 return true;
218 }
219
220
operator !=(const string & qs) const221 bool string::operator != (const string &qs) const {
222 return !(*this == qs);
223 }
224
operator <(const string & qs) const225 bool string::operator < (const string &qs) const {
226 return (memcmp(_buffer, qs._buffer, max(_currSize, qs._currSize)) < 0);
227 }
228
operator >(const string & qs) const229 bool string::operator > (const string &qs) const {
230 return (memcmp(_buffer, qs._buffer, max(_currSize, qs._currSize))> 0);
231 }
232
set(const char * inBuf,size_t newLen)233 void string::set(const char *inBuf, size_t newLen) {
234 reserve(newLen);
235 clear();
236 memcpy(_buffer, inBuf, newLen);
237 _currSize = newLen;
238 }
239
reserve(size_t newLen)240 void string::reserve(size_t newLen) {
241 newLen++; //always leave room for a null termninator.
242 if (_currCapacity <= newLen) {
243 while (_currCapacity <= newLen) {
244 _currCapacity = _currCapacity << 1;
245 }
246 _buffer = (char *)realloc(_buffer, _currCapacity );
247 if (_buffer == NULL) {
248 fprintf(stderr, "Error: failed to reallocate string.\n");
249 _currSize = 0;
250 _currCapacity = 0;
251 exit(1);
252 }
253 //initialize newly reserved memory.
254 memset(_buffer + _currSize, 0, _currCapacity - _currSize);
255 }
256 }
257
append(char c)258 void string::append(char c)
259 {
260 reserve(_currSize +1);
261 _buffer[_currSize] = c;
262 _currSize++;
263 }
264
append(const char * inBuf,size_t inBufLen)265 void string::append(const char *inBuf, size_t inBufLen)
266 {
267 reserve(_currSize + inBufLen);
268 memcpy(_buffer + _currSize, inBuf, inBufLen);
269 _currSize += inBufLen;
270 }
271
append(int num)272 void string::append(int num) {
273 int2str(num, *this, true);
274 }
275
append(uint32_t num)276 void string::append(uint32_t num) {
277 int2str((int)num, *this, true);
278 }
279
280 // void string::append(size_t num) {
281 // int2str((int)num, *this, true);
282 // }
283
append(float num)284 void string::append(float num) {
285 append(ToString(num));
286 }
287
append(double num)288 void string::append(double num) {
289 append(ToString(num));
290 }
291
292
293
assign(const char * inBuf,size_t inBufLen)294 string &string::assign(const char *inBuf, size_t inBufLen)
295 {
296 clear();
297 append(inBuf, inBufLen);
298 return *this;
299 }
300
resize(size_t newSize,char fillChar)301 void string::resize(size_t newSize, char fillChar)
302 {
303 if (newSize > _currSize) { //grow the string, pad with fillChar
304 reserve(newSize);
305 memset(_buffer + _currSize, fillChar, newSize -_currSize);
306 } else if (newSize < _currSize) { //cut off characters from the end
307 memset(_buffer + newSize, 0, _currSize - newSize);
308 }
309 _currSize = newSize;
310 }
311
312
substr(string & newStr,size_t pos,size_t len) const313 void string::substr (string &newStr, size_t pos, size_t len) const
314 {
315 if (pos >= _currSize) {
316 return;
317 }
318 if (pos + len >= _currSize) {
319 len = _currSize - pos;
320 }
321 newStr.set(_buffer + pos, len);
322 }
323
operator <<(ostream & out,const string & str)324 ostream &operator << (ostream &out, const string &str) {
325 out << str._buffer;
326 return out;
327 }
328