5 * Copyright (C) 2008-2009 Pawel Dziepak
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 string::string(const char *text
) : error('\0') {
28 data
= new char[strlen(text
) + 1];
33 void string::save(const char *text
) {
34 data
= new char[strlen(text
) + 1];
38 /* Consider copy on write strategy */
39 string::string(const string
&x
) : error('\0') {
40 assert("string: copying null string", x
.null());
41 data
= new char[x
.length() + 1];
46 string::string() : data((char*)0) {}
48 string::string(int _val
) {
49 unsigned int val
= _val
;
52 for (int i
= 0; i
< 20; i
++)
57 data
[i
++] = val
% 10 + '0';
58 } while ((val
/= 10) > 0);
65 string::string(const unsigned int _val
) {
66 unsigned int val
= _val
;
69 for (int i
= 0; i
< 20; i
++)
74 data
[i
++] = val
% 10 + '0';
75 } while ((val
/= 10) > 0);
83 string::string(const unsigned int _val
, bool) {
84 const char translate
[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
85 unsigned int val
= _val
;
89 for (int i
= 0; i
< 20; i
++)
94 data
[i
++] = translate
[val
& 0xf];
95 } while ((val
>>= 4) != 0);
108 int string::length() const {
115 void string::reverse() {
116 assert("string: operations on a null string", null());
120 for (int i
= 0, j
= length() - 1; i
< j
; i
++, j
--) {
129 char &string::operator[](const int index
) {
130 assert("string: operations on a null string", null());
131 if (index
< length() + 1)
137 const char &string::operator[](const int index
) const {
138 assert("string: operations on a null string", null());
139 if (index
< length() + 1)
145 bool string::operator==(const char *x
) const {
146 assert("string: operations on a null string", null());
147 assert("string: operations on a null string", x
== 0 || strlen(x
) == 0);
149 if (!strcmp(data
, x
))
155 bool string::operator==(const string
&x
) const {
156 assert("string: operations on a null string", null());
157 // assert("string: operations on a null string", x.null());
159 if (!strcmp(data
, x
.data
))
165 string
string::operator+(const int x
) const {
166 assert("string: operations on a null string", null());
168 return operator+(str
);
171 string
string::operator+(const string
&x
) const {
172 assert("string: operations on a null string", null());
174 char *temp
= new char[length() + x
.length() + 1];
182 temp
[length() + x
.length()] = 0;
189 void string::operator+=(const string
&x
) {
190 char *temp
= new char[length() + x
.length() + 1];
199 temp
[length() + x
.length()] = 0;
207 string::operator const char*() const {
208 assert("string: operations on a null string", null());
212 bool string::null() const {
213 return length() == 0;
216 void string::operator=(const string
&x
) {
220 assert("string: copying null string", x
.null());
221 data
= new char[x
.length() + 1];
223 strcpy(data
, x
.data
);
226 buffer
string::to_mem() const {
227 return buffer(data
, strlen(data
));
230 bool string::is_digit(char x
) {
231 return (x
>= '0' && x
<= '9');
234 int strlen(const char *a
) {
236 for (i
= 0; a
[i
]; i
++);
240 int strcmp(const char *a
,const char *b
) {
242 for (i
= 0; a
[i
] && b
[i
] && a
[i
] == b
[i
]; i
++);
243 if (a
[i
] != b
[i
]) return 1;
247 int strncmp(const char *a
,const char *b
, int n
) {
249 for (i
= 0; a
[i
] && b
[i
] && a
[i
] == b
[i
] && i
< n
; i
++);
250 if (a
[i
] != b
[i
] && i
!= n
) return 1;
254 char *strcat(char *a
, const char *b
) {
255 return strcpy(&(a
[strlen(a
)]), b
);
258 char *strcpy(char *a
, const char *b
) {
259 return (char*)memcpy((void*)a
, (const void *)b
, strlen(b
) + 1);
262 char *strncpy(char *a
, const char *b
, int n
) {
263 return (char*)memcpy((void*)a
, (const void *)b
, n
);
266 void *memcpy(void * a
, const void * b
, int count
) {
267 for (int i
= 0; i
< count
; i
++)
268 ((char *)a
)[i
] = ((const char *)b
)[i
];
272 void *memset(void *a
, int sign
, int count
) {
273 for (int i
= 0; i
< count
; i
++)
274 ((unsigned char *)a
)[i
] = (unsigned char)sign
;