Skip to content

Commit

Permalink
[TinyString] Added size_t, added find(char)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsaturn committed Feb 19, 2023
1 parent 31c51ae commit c9130c7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
27 changes: 19 additions & 8 deletions src/TinyString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

const char* TinyString::emptyString = "";

TinyString::TinyString(const char* buffer, uint16_t s)
TinyString::TinyString(const char* buffer, size_t s)
{
dup(buffer, s);
}
Expand Down Expand Up @@ -43,7 +43,7 @@ TinyString& TinyString::operator +=(int i)
return *this;
}

void TinyString::concat(const char* buf, uint16_t len)
void TinyString::concat(const char* buf, size_t len)
{
reserve(size_ + len + 1);
strcpy(str + size_, buf);
Expand All @@ -59,7 +59,7 @@ void TinyString::push_back(const char c)
free_--;
}

void TinyString::erase(uint16_t pos, uint16_t size)
void TinyString::erase(size_t pos, size_t size)
{
if (size == npos) size = size_;
if (pos > size_) return;
Expand Down Expand Up @@ -90,15 +90,15 @@ TinyString& TinyString::operator = (const char c)
return *this;
}

TinyString TinyString::substr(uint16_t pos, uint16_t size)
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, uint16_t size) const
bool TinyString::starts_with(const char* buf, size_t size) const
{
const_iterator it(str);
while(size and it != end() and (*it == *buf))
Expand All @@ -110,15 +110,15 @@ bool TinyString::starts_with(const char* buf, uint16_t size) const
return size == 0;
}

int TinyString::compare(const char* s, uint16_t len) const
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(uint16_t sz, uint8_t extent)
void TinyString::reserve(size_t sz, uint8_t extent)
{
if (sz == 0)
{
Expand Down Expand Up @@ -147,7 +147,18 @@ void TinyString::collect()
}
}

void TinyString::dup(const char* buffer, uint16_t sz, uint8_t extent)
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);
Expand Down
33 changes: 18 additions & 15 deletions src/TinyString.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,38 @@
class TinyString
{
public:
using size_t = uint16_t;
using value_type = char;
static constexpr uint16_t npos = std::numeric_limits<uint16_t>::max();
static constexpr size_t npos = std::numeric_limits<uint16_t>::max();

TinyString() = default;
TinyString(int, int base=10);
TinyString(const TinyString&);
TinyString(const char*, uint16_t size);
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, uint16_t len) const;
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; }
uint16_t length() const { return size_; }
uint16_t size() const { return length(); }
void concat(const char* buf, uint16_t len);
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, uint16_t len) const;
bool starts_with(const char* buf, size_t len) const;
bool starts_with(const char* buf) const { return starts_with(buf, strlen(buf)); }

TinyString substr(uint16_t pos, uint16_t len = npos);
size_t find(const char c, const size_t from=0) const;

char& operator[](uint16_t index) const { assert(index < size_); return str[index]; }
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; }
Expand All @@ -45,11 +48,11 @@ class TinyString

operator const char*() const { return str; }

void reserve(uint16_t size) { reserve(size, 0); }
void reserve(size_t size) { reserve(size, 0); }

void erase(uint16_t pos, uint16_t size = npos);
void erase(size_t pos, size_t size = npos);

void dup(const char* buffer, uint16_t size, uint8_t extent = 4);
void dup(const char* buffer, size_t size, uint8_t extent = 4);

void push_back(const char c);
void clear();
Expand All @@ -61,15 +64,15 @@ class TinyString
iterator begin() const { return str; }
iterator end() const { return str + size_; }

uint16_t capacity() const { return size_ + free_; }
size_t capacity() const { return size_ + free_; }
void collect(); // Save memory

private:
void reserve(uint16_t new_size, uint8_t extent);
void reserve(size_t new_size, uint8_t extent);
void copy(const TinyString& t) { dup(t.str, t.size_); };

char* str = const_cast<char *>(emptyString);
uint16_t size_ = 0; // if size_ == 0 no allocation, but str = 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;
Expand Down

0 comments on commit c9130c7

Please sign in to comment.