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.
23 #include "arch/low/general.h"
30 const buffer
buffer::empty
= buffer((void*)0, 0);
34 string::string(const char *text
) : error('\0') {
35 data
= new char[strlen(text
) + 1];
40 void string::save(const char *text
) {
41 data
= new char[strlen(text
) + 1];
45 /* Consider copy on write strategy */
46 string::string(const string
&x
) : error('\0') {
47 assert("string: copying null string", x
.null());
48 data
= new char[x
.length() + 1];
53 string::string() : data((char*)0) {}
55 string::string(int _val
) {
56 unsigned int val
= _val
;
59 for (int i
= 0; i
< 20; i
++)
64 data
[i
++] = val
% 10 + '0';
65 } while ((val
/= 10) > 0);
72 string::string(const unsigned int _val
) {
73 unsigned int val
= _val
;
76 for (int i
= 0; i
< 20; i
++)
81 data
[i
++] = val
% 10 + '0';
82 } while ((val
/= 10) > 0);
90 string::string(const unsigned int _val
, bool) {
91 const char translate
[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
92 unsigned int val
= _val
;
96 for (int i
= 0; i
< 20; i
++)
101 data
[i
++] = translate
[val
& 0xf];
102 } while ((val
>>= 4) != 0);
115 int string::length() const {
122 void string::reverse() {
123 assert("string: operations on a null string", null());
127 for (int i
= 0, j
= length() - 1; i
< j
; i
++, j
--) {
136 char &string::operator[](const int index
) {
137 assert("string: operations on a null string", null());
138 if (index
< length() + 1)
144 const char &string::operator[](const int index
) const {
145 assert("string: operations on a null string", null());
146 if (index
< length() + 1)
152 list
<string
> string::split(char sign
) const {
153 char *text
= new char[strlen(data
) + 1];
160 for (; text
[i
] && text
[i
] != sign
; i
++);
177 bool string::operator==(const char *x
) const {
178 assert("string: operations on a null string", null());
179 assert("string: operations on a null string", x
== 0 || strlen(x
) == 0);
181 if (!strcmp(data
, x
))
187 bool string::operator==(const string
&x
) const {
188 assert("string: operations on a null string", null());
189 // assert("string: operations on a null string", x.null());
191 if (!strcmp(data
, x
.data
))
197 string
string::operator+(const int x
) const {
198 assert("string: operations on a null string", null());
200 return operator+(str
);
203 string
string::operator+(const string
&x
) const {
204 assert("string: operations on a null string", null());
206 char *temp
= new char[length() + x
.length() + 1];
214 temp
[length() + x
.length()] = 0;
221 void string::operator+=(const string
&x
) {
222 char *temp
= new char[length() + x
.length() + 1];
231 temp
[length() + x
.length()] = 0;
239 string::operator const char*() const {
240 assert("string: operations on a null string", null());
244 const char *string::to_ascii() const {
245 assert("string: operations on a null string", null());
249 string
string::from_utf16(char *_data
) {
250 u16
*utf_data
= (u16
*)_data
;
252 for (size
= 0; utf_data
[size
]; size
++);
254 u8
*data
= new u8
[size
];
255 for (int i
= 0; i
< size
; i
++)
256 data
[i
] = utf_data
[i
];
258 return string((char*)data
);
261 bool string::null() const {
262 return length() == 0;
265 void string::operator=(const string
&x
) {
269 assert("string: copying null string", x
.null());
270 data
= new char[x
.length() + 1];
272 strcpy(data
, x
.data
);
275 buffer
string::to_mem() const {
276 return buffer(data
, strlen(data
) + 1);
279 bool string::is_digit(char x
) {
280 return (x
>= '0' && x
<= '9');
283 int strlen(const char *a
) {
285 for (i
= 0; a
[i
]; i
++);
289 int strcmp(const char *a
,const char *b
) {
291 for (i
= 0; a
[i
] && b
[i
] && a
[i
] == b
[i
]; i
++);
292 if (a
[i
] != b
[i
]) return 1;
296 int strncmp(const char *a
,const char *b
, int n
) {
298 for (i
= 0; a
[i
] && b
[i
] && a
[i
] == b
[i
] && i
< n
; i
++);
299 if (a
[i
] != b
[i
] && i
!= n
) return 1;
303 char *strcat(char *a
, const char *b
) {
304 return strcpy(&(a
[strlen(a
)]), b
);
307 char *strcpy(char *a
, const char *b
) {
308 return (char*)memcpy((void*)a
, (const void *)b
, strlen(b
) + 1);
311 char *strncpy(char *a
, const char *b
, int n
) {
312 return (char*)memcpy((void*)a
, (const void *)b
, n
);
315 void *memcpy(void * a
, const void * b
, int count
) {
316 for (int i
= 0; i
< count
; i
++)
317 ((char *)a
)[i
] = ((const char *)b
)[i
];
321 void *memset(void *a
, int sign
, int count
) {
322 for (int i
= 0; i
< count
; i
++)
323 ((unsigned char *)a
)[i
] = (unsigned char)sign
;