pstring_t<F> pstring_t<F>::replace(const pstring_t &search, const pstring_t &replace) const { // FIXME: use this pstringbuffer ret = ""; pstring_t ret = ""; const int slen = search.blen(); const int tlen = blen(); if (slen == 0 || tlen < slen ) return *this; int i = 0; while (i < tlen - slen + 1) { if (memcmp(cstr()+i,search.cstr(),slen) == 0) { ret += replace; i += slen; } else { /* avoid adding a code, cat a string ... */ mem_t buf[2] = { *(cstr() + i), 0 }; ret = ret.cat(buf); i++; } } ret = ret.cat(cstr() + i); return ret; }
bool pstring_t<F>::endsWith(const pstring_t &arg) const { if (arg.blen() > blen()) return false; else return std::equal(arg.c_str(), arg.c_str() + arg.blen(), c_str()+this->blen()-arg.blen()); }
void pstring_t<F>::pcat(const pstring_t &s) { int slen = s.blen(); pstr_t *n = salloc(m_ptr->len() + slen); if (m_ptr->len() > 0) std::memcpy(n->str(), m_ptr->str(), m_ptr->len()); if (slen > 0) std::memcpy(n->str() + m_ptr->len(), s.cstr(), slen); *(n->str() + n->len()) = 0; sfree(m_ptr); m_ptr = n; }
void pstring_t<F>::pcat(const pstring_t &s) { std::size_t slen = s.blen(); pstr_t *n = salloc(m_ptr->len() + slen); if (m_ptr->len() > 0) n->copy_from(m_ptr->str(), m_ptr->len()); if (slen > 0) std::copy(s.c_str(), s.c_str() + slen, n->str() + m_ptr->len()); *(n->str() + n->len()) = 0; sfree(m_ptr); m_ptr = n; }
typename pstring_t<F>::iterator pstring_t<F>::find(const pstring_t search, iterator start) const { for (; start != end(); ++start) { iterator itc(start); auto cmp = search.begin(); while (itc != end() && cmp != search.end() && *itc == *cmp) { ++itc; ++cmp; } if (cmp == search.end()) return start; } return end(); }
const pstring_t<F> pstring_t<F>::rpad(const pstring_t &ws, const size_type cnt) const { // FIXME: pstringbuffer ret(*this); pstring_t ret(*this); size_type wsl = ws.len(); for (auto i = ret.len(); i < cnt; i+=wsl) ret += ws; return ret; }
int pstring_t<F>::pcmp(const pstring_t &right) const { std::size_t l = std::min(blen(), right.blen()); if (l == 0) { if (blen() == 0 && right.blen() == 0) return 0; else if (right.blen() == 0) return 1; else return -1; } auto si = this->begin(); auto ri = right.begin(); while (si != this->end() && *si == *ri) { ri++; si++; } int ret = (si == this->end() ? 0 : *si - *ri); if (ret == 0) { if (this->blen() > right.blen()) ret = 1; else if (this->blen() < right.blen()) ret = -1; } return ret; }
int pstring_t<F>::pcmp(const pstring_t &right) const { long l = std::min(blen(), right.blen()); if (l == 0) { if (blen() == 0 && right.blen() == 0) return 0; else if (right.blen() == 0) return 1; else return -1; } int ret = memcmp(m_ptr->str(), right.cstr(), l); if (ret == 0) ret = this->blen() - right.blen(); if (ret < 0) return -1; else if (ret > 0) return 1; else return 0; }
pstring_t<F> pstring_t<F>::replace(const pstring_t &search, const pstring_t &replace) const { pstring_t ret(""); const size_type slen = search.len(); auto last_s = begin(); auto s = find(search, last_s); while (s != end()) { ret += substr(last_s, s); ret += replace; last_s = s + slen; s = find(search, last_s); } ret += substr(last_s, end()); return ret; }
int pstring_t<F>::find_first_not_of(const pstring_t &no) const { char *t = m_ptr->str(); unsigned nolen = no.len(); unsigned tlen = len(); for (unsigned i=0; i < tlen; i++) { char *n = no.m_ptr->str(); bool f = true; for (unsigned j=0; j < nolen; j++) { if (F::code(t) == F::code(n)) f = false; n += F::codelen(t); } if (f) return i; t += F::codelen(t); } return -1; }