diff options
Diffstat (limited to 'src/user/lib/fwik/String.cpp')
-rw-r--r-- | src/user/lib/fwik/String.cpp | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/src/user/lib/fwik/String.cpp b/src/user/lib/fwik/String.cpp index 75114c2..982ce4e 100644 --- a/src/user/lib/fwik/String.cpp +++ b/src/user/lib/fwik/String.cpp @@ -1 +1,158 @@ #include <String.h> +#include <string.h> + +String::String() { + ptr = 0; + len = 0; +} + +String::String(const String &other) { + len = other.len; + if (len == 0) { + ptr = 0; + } else { + ptr = (char*)malloc(len + 1); + libc::memcpy(ptr, other.ptr, len + 1); + } +} + +String::String(char* from) { + len = (from == 0 ? 0 : libc::strlen(from)); + if (len == 0) { + ptr = 0; + } else { + ptr = (char*)malloc(len + 1); + libc::memcpy(ptr, from, len + 1); + } +} + +String::String(char* from, int l) { + len = l; + if (len < 0) len = 0; + if (len == 0) { + ptr = 0; + } else { + ptr = (char*)malloc(len + 1); + libc::memcpy(ptr, from, len); + ptr[len] = 0; + } +} + +String::String(char c, int count) { + len = count; + if (len == 0) { + ptr = 0; + } else { + ptr = (char*)malloc(len + 1); + libc::memset(ptr, c, len); + ptr[len] = 0; + } +} + +String::~String() { + if (ptr != 0) free(ptr); +} + +void String::operator=(const String &other) { + if (ptr != 0) free(ptr); + len = other.len; + if (len == 0) { + ptr = 0; + } else { + ptr = (char*)malloc(len + 1); + libc::memcpy(ptr, other.ptr, len + 1); + } +} + +void String::operator=(char* from) { + if (ptr != 0) free(ptr); + len = (from == 0 ? 0 : libc::strlen(from)); + if (len == 0) { + ptr = 0; + } else { + ptr = (char*)malloc(len + 1); + libc::memcpy(ptr, from, len + 1); + } +} + +char* String::c_str() { + if (ptr == 0) return ""; + return ptr; +} + +bool String::operator==(const String& other) { + if (len != other.len) return false; + for (int i = 0; i < len; i++) if (ptr[i] != other.ptr[i]) return false; + return true; +} + +bool String::operator==(char* other) { + if (other == 0) return (len == 0); + if (len != libc::strlen(other)) return false; + for (int i = 0; i < len; i++) if (ptr[i] != other[i]) return false; + return true; +} + +bool String::operator<(const String& other) { + for (int i = 0; i < len && i < other.len; i++) { + if (ptr[i] > other.ptr[i]) return false; + if (ptr[i] < other.ptr[i]) return true; + } + if (len < other.len) return true; + return false; +} + +static char crap; +char &String::operator[](int pos) { + if (pos >= 0 && pos < len) return ptr[pos]; + crap = 0; + return crap; +} + +String String::substr(int start, int count) { + if (start + count > len) count = len - start; + return String(ptr + start, count); +} + +String String::operator+(const String& other) { + String ret(' ', len + other.len); + libc::memcpy(ret.ptr, ptr, len); + libc::memcpy(ret.ptr + len, other.ptr, other.len); + return ret; +} + +void String::operator+=(const String& other) { + if (other.len == 0) return; + int newlen = len + other.len; + char* newptr = (char*)malloc(newlen + 1); + libc::memcpy(newptr, ptr, len); + libc::memcpy(newptr+len, other.ptr, other.len); + newptr[newlen] = 0; + free(ptr); + ptr = newptr; + len = newlen; +} + +void String::operator+=(char c) { + char* newptr = (char*)malloc(len + 2); + libc::memcpy(newptr, ptr, len); + newptr[len] = c; + len++; + newptr[len] = 0; + free(ptr); + ptr = newptr; +} + +String String::dec(int i) { + char b[16]; + const char *e = libc::format_int(b, i); + return String(b, e - b); +} + +String String::hex(uint32_t v) { + char b[16]; + const char *e = libc::format_hex(b, v); + return String(b, e - b); +} + + |