Skip to content

Commit

Permalink
improve fastring
Browse files Browse the repository at this point in the history
  • Loading branch information
idealvin committed Jul 29, 2023
1 parent b9874cf commit a36b649
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
38 changes: 23 additions & 15 deletions include/co/fastring.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,28 +122,28 @@ class __coapi fastring : public fast::stream {
return (fastring&) fast::stream::append(c);
}

fastring& append(signed char c) {
return this->append((char)c);
}
fastring& push_back(char c) { return this->append(c); }

char pop_back() { return _p[--_size]; }

fastring& append(unsigned char c) {
return this->append((char)c);
fastring& operator+=(const fastring& s) {
return this->append(s);
}

fastring& push_back(char c) { return this->append(c); }
fastring& operator+=(const std::string& s) {
return this->append(s);
}

char pop_back() { return _p[--_size]; }
fastring& operator+=(const char* s) {
return this->append(s);
}

template<typename T>
fastring& operator+=(T&& t) {
return this->append(std::forward<T>(t));
fastring& operator+=(char c) {
return this->append(c);
}

fastring& cat() { return *this; }

// concatenate fastring to any number of elements
// - fastring s("hello");
// s.cat(' ', 123); // s -> "hello 123"
template<typename X, typename ...V>
fastring& cat(X&& x, V&& ... v) {
(*this) << std::forward<X>(x);
Expand Down Expand Up @@ -405,9 +405,17 @@ class __coapi fastring : public fast::stream {
return this->trim(std::forward<X>(x)...);
}

// replace @sub in the string with @to, do not apply it to binary strings
fastring& replace(const char* sub, size_t n, const char* to, size_t m, size_t maxreplace=0);

// replace @sub in the string with @to
// @maxreplace: 0 for unlimited
fastring& replace(const char* sub, const char* to, size_t maxreplace=0);
fastring& replace(const char* sub, const char* to, size_t maxreplace=0) {
return this->replace(sub, strlen(sub), to, strlen(to), maxreplace);
}

fastring& replace(const fastring& sub, const fastring& to, size_t maxreplace=0) {
return this->replace(sub.data(), sub.size(), to.data(), to.size(), maxreplace);
}

fastring& tolower();
fastring& toupper();
Expand Down
16 changes: 7 additions & 9 deletions src/fastring.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,23 @@ fastring& fastring::trim(size_t n, char d) {
return *this;
}

fastring& fastring::replace(const char* sub, const char* to, size_t maxreplace) {
if (this->empty()) return *this;
fastring& fastring::replace(const char* sub, size_t n, const char* to, size_t m, size_t maxreplace) {
if (this->empty() || n == 0) return *this;

const char* from = this->c_str();
const char* p = strstr(from, sub);
const char* from = _p;
const char* p = _memmem(_p, _size, sub, n);
if (!p) return *this;

const size_t n = strlen(sub);
const size_t m = strlen(to);

const char* const e = _p + _size;
fastring s(_size);

do {
s.append(from, p - from).append(to, m);
from = p + n;
if (maxreplace && --maxreplace == 0) break;
} while ((p = strstr(from, sub)));
} while ((p = _memmem(from, e - from, sub, n)));

if (from < _p + _size) s.append(from);
if (from < _p + _size) s.append(from, e - from);

this->swap(s);
return *this;
Expand Down

0 comments on commit a36b649

Please sign in to comment.