diff --git a/src/TinyString.cpp b/src/TinyString.cpp deleted file mode 100644 index 1f3299e..0000000 --- a/src/TinyString.cpp +++ /dev/null @@ -1,171 +0,0 @@ -#include "TinyString.h" -#include - -const char* TinyString::emptyString = ""; - -TinyString::TinyString(const char* buffer, size_t s) -{ - dup(buffer, s); -} - -TinyString::TinyString(int i, int base) -{ - reserve(sizeof(int)*8+1); - itoa(i, str, base); - size_ = strlen(str); - free_ -= size_; -} - -TinyString::TinyString(const TinyString& m) -{ - copy(m); -} - -TinyString& TinyString::operator=(const TinyString& m) -{ - copy(m); - return *this; -} - -TinyString& TinyString::operator+=(const char c) -{ - push_back(c); - return *this; -} - -TinyString& TinyString::operator +=(int i) -{ - reserve(size_ + sizeof(int)*3+1); - itoa(i, str + size_, 10); - int8_t sz = strlen(str+size_); - size_ += sz; - free_ -= sz; - return *this; -} - -void TinyString::concat(const char* buf, size_t len) -{ - reserve(size_ + len + 1); - strcpy(str + size_, buf); - size_ += len; - free_ -= len; -} - -void TinyString::push_back(const char c) -{ - reserve(size_+1, extent); - str[size_++] = c; - str[size_] = 0; - free_--; -} - -void TinyString::erase(size_t pos, size_t size) -{ - if (size == npos) size = size_; - if (pos > size_) return; - if (pos + size > size_) size = size_ - pos; - memmove(str+pos, str+pos+size, size_ - pos + 1); - if (size_ == size) - { - clear(); - } - else - { - size_ -= size; - free_ += size; - } -} - -void TinyString::clear() -{ - if (size_) - free(str); - str = const_cast(emptyString); // Dangerous str must be left untouched when size_ == 0 - size_ = 0; -} - -TinyString& TinyString::operator = (const char c) -{ - dup(&c, 1); - return *this; -} - -TinyString TinyString::substr(size_t pos, size_t size) -{ - if (size == npos) size = size_; - if (pos > size_) return TinyString(); - if (pos + size > size_) size = size_ - pos; - return TinyString(str+pos, size); -} - -bool TinyString::starts_with(const char* buf, size_t size) const -{ - const_iterator it(str); - while(size and it != end() and (*it == *buf)) - { - it++; - buf++; - size--; - } - return size == 0; -} - -int TinyString::compare(const char* s, size_t len) const -{ - if (len > size_) - return memcmp(str, s, size_ + 1); - else - return memcmp(str, s, len + 1); -} - -void TinyString::reserve(size_t sz, uint8_t extent) -{ - if (sz == 0) - { - clear(); - return; - } - if (size_ == 0) - { - free_ = sz + extent; - str = static_cast(malloc(sz + extent)); - return; - } - if ((sz > size_ + free_) or (extent > size_ + free_ - sz)) - { - free_ = sz + extent - size_; - str = static_cast(realloc(str, sz + extent)); - } -} - -void TinyString::collect() -{ - if (size_ > 0 and free_ > 1) - { - str = static_cast(realloc(str, size_ + 1)); - free_ = 1; - } -} - -TinyString::size_t TinyString::find(const char c, size_t from) const -{ - if (size_ == 0 or from > size_) return npos; - - const char* f = str + from; - const char* end = str + size_; - while(f != end) if (*f++ == c) return f-str-1; - - return npos; -} - -void TinyString::dup(const char* buffer, size_t sz, uint8_t extent) -{ - reserve(sz + 1, extent); - memcpy(str, buffer, sz); - str[sz] = 0; - if (size_ > sz) - free_ += size_ - sz; - else - free_ -= sz - size_; - size_ = sz; -} diff --git a/src/TinyString.h b/src/TinyString.h deleted file mode 100644 index 42a6b1c..0000000 --- a/src/TinyString.h +++ /dev/null @@ -1,93 +0,0 @@ -#pragma once -#include -#include -#include - -#pragma pack(push, 1) - -class TinyString -{ - public: - class size_t - { - public: - using type = uint16_t; - size_t (const size_t& s) : s_(s) {} - size_t (int i) : s_(static_cast) {} - - type operator() const { return s_; } - - private: - type s_; - }; - using value_type = char; - constexpr size_t::type npos = std::numeric_limits::max(); - - TinyString() = default; - TinyString(int, int base=10); - TinyString(const TinyString&); - TinyString(const char*, size_t size); - TinyString(const char* s) : TinyString(s, strlen(s)){}; - TinyString& operator= (const TinyString&); - ~TinyString() { clear(); } - - int compare(const char* buf, size_t len) const; - int compare(const char* buf) const { return compare(buf, strlen(buf)); } - - friend bool operator == (const TinyString& l, const TinyString& r) { return l.compare(r) == 0; } - - friend bool operator < (const TinyString& l, const TinyString& r) { return l.compare(r) <0; } - - const char* c_str() const { return str; } - size_t length() const { return size_; } - size_t size() const { return length(); } - void concat(const char* buf, size_t len); - - bool starts_with(const char* buf, size_t len) const; - bool starts_with(const char* buf) const { return starts_with(buf, strlen(buf)); } - - size_t find(const char c, const size_t from=0) const; - - TinyString substr(size_t pos, size_t len = npos); - - char& operator[](size_t index) const { assert(index < size_); return str[index]; } - TinyString& operator = (const char c); - TinyString& operator +=(const char c); - TinyString& operator +=(const char* buf) { concat(buf, strlen(buf)); return *this; } - TinyString& operator +=(const TinyString& s) { concat(s.str, s.size_); return *this; } - TinyString& operator +=(int32_t); - - operator const char*() const { return str; } - - void reserve(size_t size) { reserve(size, 0); } - - void erase(size_t pos, size_t size = npos); - - void dup(const char* buffer, size_t size, uint8_t extent = 4); - - void push_back(const char c); - void clear(); - - using const_iterator = const char*; - using iterator = char*; - const_iterator cbegin() const { return begin(); } - const_iterator cend() const { return end(); } - iterator begin() const { return str; } - iterator end() const { return str + size_; } - - size_t capacity() const { return size_ + free_; } - void collect(); // Save memory - - private: - void reserve(size_t new_size, uint8_t extent); - void copy(const TinyString& t) { dup(t.str, t.size_); }; - - char* str = const_cast(emptyString); - size_t size_ = 0; // if size_ == 0 no allocation, but str = emptyString - uint8_t free_ = 0; // malloc(str) = size_ + free_ - - static const char* emptyString; - const uint8_t extent = 8; -}; - -#pragma pack(pop)