diff options
Diffstat (limited to 'src/user/lib/fwik/String.cpp')
-rw-r--r-- | src/user/lib/fwik/String.cpp | 36 |
1 files changed, 27 insertions, 9 deletions
diff --git a/src/user/lib/fwik/String.cpp b/src/user/lib/fwik/String.cpp index 982ce4e..e3455e3 100644 --- a/src/user/lib/fwik/String.cpp +++ b/src/user/lib/fwik/String.cpp @@ -16,7 +16,7 @@ String::String(const String &other) { } } -String::String(char* from) { +String::String(const char* from) { len = (from == 0 ? 0 : libc::strlen(from)); if (len == 0) { ptr = 0; @@ -26,7 +26,7 @@ String::String(char* from) { } } -String::String(char* from, int l) { +String::String(const char* from, int l) { len = l; if (len < 0) len = 0; if (len == 0) { @@ -64,7 +64,7 @@ void String::operator=(const String &other) { } } -void String::operator=(char* from) { +void String::operator=(const char* from) { if (ptr != 0) free(ptr); len = (from == 0 ? 0 : libc::strlen(from)); if (len == 0) { @@ -75,25 +75,25 @@ void String::operator=(char* from) { } } -char* String::c_str() { +const char* String::c_str() const { if (ptr == 0) return ""; return ptr; } -bool String::operator==(const String& other) { +bool String::operator==(const String& other) const { 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) { +bool String::operator==(const char* other) const { 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) { +bool String::operator<(const String& other) const { 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; @@ -109,12 +109,17 @@ char &String::operator[](int pos) { return crap; } -String String::substr(int start, int count) { +char String::operator[](int pos) const { + if (pos >= 0 && pos < len) return ptr[pos]; + return 0; +} + +String String::substr(int start, int count) const { if (start + count > len) count = len - start; return String(ptr + start, count); } -String String::operator+(const String& other) { +String String::operator+(const String& other) const { String ret(' ', len + other.len); libc::memcpy(ret.ptr, ptr, len); libc::memcpy(ret.ptr + len, other.ptr, other.len); @@ -155,4 +160,17 @@ String String::hex(uint32_t v) { return String(b, e - b); } +String String::sprintf(const char* format, ...) { + va_list ap; + va_start(ap, format); + va_list ap2; + va_copy(ap2, ap); + String ret; + ret.len = libc::printf_str_len(format, ap2); + va_end(ap2); + ret.ptr = (char*)malloc(ret.len + 1); + ret.len = libc::vsprintf(ret.ptr, format, ap); + va_end(ap); + return ret; +} |