#include #include 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(const 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(const 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=(const 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); } } const char* String::c_str() const { if (ptr == 0) return ""; return ptr; } 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==(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) 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; } 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; } 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) const { 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); } 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; }