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.
26 const buffer
buffer::empty
= buffer((void*)0, 0);
30 string::string(const char *text
) : error('\0') {
31 data
= new char[strlen(text
) + 1];
36 void string::save(const char *text
) {
37 data
= new char[strlen(text
) + 1];
41 /* Consider copy on write strategy */
42 string::string(const string
&x
) : error('\0') {
43 assert("string: copying null string", x
.null());
44 data
= new char[x
.length() + 1];
49 string::string() : data((char*)0) {}
51 string::string(int _val
) {
52 unsigned int val
= _val
;
55 for (int i
= 0; i
< 20; i
++)
60 data
[i
++] = val
% 10 + '0';
61 } while ((val
/= 10) > 0);
68 string::string(const unsigned int _val
) {
69 unsigned int val
= _val
;
72 for (int i
= 0; i
< 20; i
++)
77 data
[i
++] = val
% 10 + '0';
78 } while ((val
/= 10) > 0);
86 string::string(const unsigned int _val
, bool) {
87 const char translate
[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
88 unsigned int val
= _val
;
92 for (int i
= 0; i
< 20; i
++)
97 data
[i
++] = translate
[val
& 0xf];
98 } while ((val
>>= 4) != 0);
111 int string::length() const {
118 void string::reverse() {
119 assert("string: operations on a null string", null());
123 for (int i
= 0, j
= length() - 1; i
< j
; i
++, j
--) {
132 char &string::operator[](const int index
) {
133 assert("string: operations on a null string", null());
134 if (index
< length() + 1)
140 const char &string::operator[](const int index
) const {
141 assert("string: operations on a null string", null());
142 if (index
< length() + 1)
148 bool string::operator==(const char *x
) const {
149 assert("string: operations on a null string", null());
150 assert("string: operations on a null string", x
== 0 || strlen(x
) == 0);
152 if (!strcmp(data
, x
))
158 bool string::operator==(const string
&x
) const {
159 assert("string: operations on a null string", null());
160 // assert("string: operations on a null string", x.null());
162 if (!strcmp(data
, x
.data
))
168 string
string::operator+(const int x
) const {
169 assert("string: operations on a null string", null());
171 return operator+(str
);
174 string
string::operator+(const string
&x
) const {
175 assert("string: operations on a null string", null());
177 char *temp
= new char[length() + x
.length() + 1];
185 temp
[length() + x
.length()] = 0;
192 void string::operator+=(const string
&x
) {
193 char *temp
= new char[length() + x
.length() + 1];
202 temp
[length() + x
.length()] = 0;
210 string::operator const char*() const {
211 assert("string: operations on a null string", null());
215 bool string::null() const {
216 return length() == 0;
219 void string::operator=(const string
&x
) {
223 assert("string: copying null string", x
.null());
224 data
= new char[x
.length() + 1];
226 strcpy(data
, x
.data
);
229 buffer
string::to_mem() const {
230 return buffer(data
, strlen(data
));
233 bool string::is_digit(char x
) {
234 return (x
>= '0' && x
<= '9');
237 int strlen(const char *a
) {
239 for (i
= 0; a
[i
]; i
++);
243 int strcmp(const char *a
,const char *b
) {
245 for (i
= 0; a
[i
] && b
[i
] && a
[i
] == b
[i
]; i
++);
246 if (a
[i
] != b
[i
]) return 1;
250 int strncmp(const char *a
,const char *b
, int n
) {
252 for (i
= 0; a
[i
] && b
[i
] && a
[i
] == b
[i
] && i
< n
; i
++);
253 if (a
[i
] != b
[i
] && i
!= n
) return 1;
257 char *strcat(char *a
, const char *b
) {
258 return strcpy(&(a
[strlen(a
)]), b
);
261 char *strcpy(char *a
, const char *b
) {
262 return (char*)memcpy((void*)a
, (const void *)b
, strlen(b
) + 1);
265 char *strncpy(char *a
, const char *b
, int n
) {
266 return (char*)memcpy((void*)a
, (const void *)b
, n
);
269 void *memcpy(void * a
, const void * b
, int count
) {
270 for (int i
= 0; i
< count
; i
++)
271 ((char *)a
)[i
] = ((const char *)b
)[i
];
275 void *memset(void *a
, int sign
, int count
) {
276 for (int i
= 0; i
< count
; i
++)
277 ((unsigned char *)a
)[i
] = (unsigned char)sign
;