COMMON: Move all find and replace functions into BaseString

This commit is contained in:
Cameron Cawley 2023-12-30 22:03:29 +00:00 committed by Eugene Sandulenko
parent 35125f5509
commit f7c2d14587
4 changed files with 180 additions and 182 deletions

View file

@ -609,6 +609,110 @@ TEMPLATE size_t BASESTRING::find(const value_type *strP, uint32 pos) const {
return npos;
}
TEMPLATE size_t BASESTRING::rfind(const value_type *s) const {
int sLen = cStrLen(s);
for (int idx = (int)_size - sLen; idx >= 0; --idx) {
if (!memcmp(_str + idx, s, sLen * sizeof(value_type)))
return idx;
}
return npos;
}
TEMPLATE size_t BASESTRING::rfind(value_type c, size_t pos) const {
if (pos == npos || pos > _size)
pos = _size;
else
++pos;
while (pos > 0) {
--pos;
if ((*this)[pos] == c)
return pos;
}
return npos;
}
TEMPLATE size_t BASESTRING::findFirstOf(value_type c, size_t pos) const {
const value_type *strP = (pos >= _size) ? nullptr : cMemChr(_str + pos, c, _size - pos);
return strP ? strP - _str : npos;
}
TEMPLATE size_t BASESTRING::findFirstOf(const value_type *chars, size_t pos) const {
uint32 charsLen = cStrLen(chars);
for (uint idx = pos; idx < _size; ++idx) {
if (cMemChr(chars, (*this)[idx], charsLen))
return idx;
}
return npos;
}
TEMPLATE size_t BASESTRING::findLastOf(value_type c, size_t pos) const {
int start = (pos == npos) ? (int)_size - 1 : MIN((int)_size - 1, (int)pos);
for (int idx = start; idx >= 0; --idx) {
if ((*this)[idx] == c)
return idx;
}
return npos;
}
TEMPLATE size_t BASESTRING::findLastOf(const value_type *chars, size_t pos) const {
uint32 charsLen = cStrLen(chars);
int start = (pos == npos) ? (int)_size - 1 : MIN((int)_size - 1, (int)pos);
for (int idx = start; idx >= 0; --idx) {
if (cMemChr(chars, (*this)[idx], charsLen))
return idx;
}
return npos;
}
TEMPLATE size_t BASESTRING::findFirstNotOf(value_type c, size_t pos) const {
for (uint idx = pos; idx < _size; ++idx) {
if ((*this)[idx] != c)
return idx;
}
return npos;
}
TEMPLATE size_t BASESTRING::findFirstNotOf(const value_type *chars, size_t pos) const {
uint32 charsLen = cStrLen(chars);
for (uint idx = pos; idx < _size; ++idx) {
if (!cMemChr(chars, (*this)[idx], charsLen))
return idx;
}
return npos;
}
TEMPLATE size_t BASESTRING::findLastNotOf(value_type c) const {
for (int idx = (int)_size - 1; idx >= 0; --idx) {
if ((*this)[idx] != c)
return idx;
}
return npos;
}
TEMPLATE size_t BASESTRING::findLastNotOf(const value_type *chars) const {
uint32 charsLen = cStrLen(chars);
for (int idx = (int)_size - 1; idx >= 0; --idx) {
if (!cMemChr(chars, (*this)[idx], charsLen))
return idx;
}
return npos;
}
TEMPLATE void BASESTRING::replace(uint32 pos, uint32 count, const BaseString &str) {
replace(pos, count, str, 0, str._size);
}
@ -666,6 +770,30 @@ TEMPLATE void BASESTRING::replace(uint32 posOri, uint32 countOri, const value_ty
memmove(_str + posOri, str + posDest, countDest * sizeof(value_type));
}
TEMPLATE void BASESTRING::replace(value_type from, value_type to) {
// Don't allow removing trailing \x00
if (from == '\x00') {
return;
}
value_type *next = cMemChr(_str, from, _size);
if (!next) {
// Nothing to do
return;
}
size_t off = next - _str;
makeUnique();
value_type *end = _str + _size;
next = _str + off;
while(next) {
*next = to;
next++;
next = cMemChr(next + 1, from, end - next);
}
}
TEMPLATE uint64 BASESTRING::asUint64() const {
uint64 result = 0;
for (uint32 i = 0; i < _size; ++i) {

View file

@ -201,6 +201,51 @@ public:
size_t find(const value_type *s, uint32 pos = 0) const;
uint32 find(const BaseString &str, uint32 pos = 0) const;
/** Does a reverse find for the passed string */
size_t rfind(const value_type *s) const;
size_t rfind(const BaseString &s) const {
return rfind(s.c_str());
}
/** Does a reverse find for a passed character */
size_t rfind(value_type c, size_t pos = npos) const;
/** Find first character in the string matching the passed character */
size_t findFirstOf(value_type c, size_t pos = 0) const;
/** Find first character in the string that's any character of the passed string */
size_t findFirstOf(const value_type *chars, size_t pos = 0) const;
size_t findFirstOf(const BaseString &chars, size_t pos = 0) const {
return findFirstOf(chars.c_str(), pos);
}
/** Find the last character in the string that's the specified character */
size_t findLastOf(value_type c, size_t pos = npos) const;
/** Find the last character in the string that's in any of the passed characters */
size_t findLastOf(const value_type *chars, size_t pos = npos) const;
size_t findLastOf(const BaseString &chars, size_t pos = npos) const {
return findLastOf(chars.c_str(), pos);
}
/** Find first character in the string that's not the specified character */
size_t findFirstNotOf(value_type c, size_t pos = 0) const;
/** Find first character in the string that's not any character of the passed string */
size_t findFirstNotOf(const value_type *chars, size_t pos = 0) const;
size_t findFirstNotOf(const BaseString &chars, size_t pos = 0) const {
return findFirstNotOf(chars.c_str(), pos);
}
/** Find the last character in the string that's not the specified character */
size_t findLastNotOf(value_type c) const;
/** Find the last character in the string that's not in any of the passed characters */
size_t findLastNotOf(const value_type *chars) const;
size_t findLastNotOf(const BaseString &chars) const {
return findLastNotOf(chars.c_str());
}
/**@{
* Functions to replace some amount of chars with chars from some other string.
*
@ -233,6 +278,13 @@ public:
uint32 posDest, uint32 countDest);
/**@}*/
/**
* Replace all from characters in object by to character
* @param from the character to look for
* @param to The replacement character
*/
void replace(value_type from, value_type to);
/** Appends a string containing the characters between beginP (including) and endP (excluding). */
void append(const value_type *begin, const value_type *end);

View file

@ -175,30 +175,6 @@ bool String::matchString(const String &pat, bool ignoreCase, const char *wildcar
#endif
void String::replace(char from, char to) {
// Don't allow removing trailing \x00
if (from == '\x00') {
return;
}
char *next = (char *)cMemChr(_str, from, _size);
if (!next) {
// Nothing to do
return;
}
size_t off = next - _str;
makeUnique();
char *end = _str + _size;
next = _str + off;
while(next) {
*next = to;
next++;
next = (char *)cMemChr(next + 1, from, end - next);
}
}
// static
String String::format(const char *fmt, ...) {
String output;
@ -263,110 +239,6 @@ String String::vformat(const char *fmt, va_list args) {
return output;
}
size_t String::rfind(const char *s) const {
int sLen = cStrLen(s);
for (int idx = (int)_size - sLen; idx >= 0; --idx) {
if (!memcmp(_str + idx, s, sLen * sizeof(value_type)))
return idx;
}
return npos;
}
size_t String::rfind(char c, size_t pos) const {
if (pos == npos || pos > _size)
pos = _size;
else
++pos;
while (pos > 0) {
--pos;
if ((*this)[pos] == c)
return pos;
}
return npos;
}
size_t String::findFirstOf(char c, size_t pos) const {
const char *strP = (pos >= _size) ? nullptr : cMemChr(_str + pos, c, _size - pos);
return strP ? strP - _str : npos;
}
size_t String::findFirstOf(const char *chars, size_t pos) const {
uint32 charsLen = cStrLen(chars);
for (uint idx = pos; idx < _size; ++idx) {
if (cMemChr(chars, (*this)[idx], charsLen))
return idx;
}
return npos;
}
size_t String::findLastOf(char c, size_t pos) const {
int start = (pos == npos) ? (int)_size - 1 : MIN((int)_size - 1, (int)pos);
for (int idx = start; idx >= 0; --idx) {
if ((*this)[idx] == c)
return idx;
}
return npos;
}
size_t String::findLastOf(const char *chars, size_t pos) const {
uint32 charsLen = cStrLen(chars);
int start = (pos == npos) ? (int)_size - 1 : MIN((int)_size - 1, (int)pos);
for (int idx = start; idx >= 0; --idx) {
if (cMemChr(chars, (*this)[idx], charsLen))
return idx;
}
return npos;
}
size_t String::findFirstNotOf(char c, size_t pos) const {
for (uint idx = pos; idx < _size; ++idx) {
if ((*this)[idx] != c)
return idx;
}
return npos;
}
size_t String::findFirstNotOf(const char *chars, size_t pos) const {
uint32 charsLen = cStrLen(chars);
for (uint idx = pos; idx < _size; ++idx) {
if (!cMemChr(chars, (*this)[idx], charsLen))
return idx;
}
return npos;
}
size_t String::findLastNotOf(char c) const {
for (int idx = (int)_size - 1; idx >= 0; --idx) {
if ((*this)[idx] != c)
return idx;
}
return npos;
}
size_t String::findLastNotOf(const char *chars) const {
uint32 charsLen = cStrLen(chars);
for (int idx = (int)_size - 1; idx >= 0; --idx) {
if (!cMemChr(chars, (*this)[idx], charsLen))
return idx;
}
return npos;
}
String String::substr(size_t pos, size_t len) const {
if (pos >= _size)
return String();

View file

@ -147,15 +147,6 @@ public:
bool matchString(const char *pat, bool ignoreCase = false, const char *wildcardExclusions = NULL) const;
bool matchString(const String &pat, bool ignoreCase = false, const char *wildcardExclusions = NULL) const;
using BaseString<value_type>::replace;
/**
* Replace all from characters in object by to character
* @param from the character to look for
* @param to The replacement character
*/
void replace(char from, char to);
/**
* Print formatted data into a String object. Similar to sprintf,
* except that it stores the result in (variably sized) String
@ -170,51 +161,6 @@ public:
*/
static String vformat(const char *fmt, va_list args);
/** Does a reverse find for the passed string */
size_t rfind(const char *s) const;
size_t rfind(const String &s) const {
return rfind(s.c_str());
}
/** Does a reverse find for a passed character */
size_t rfind(char c, size_t pos = npos) const;
/** Find first character in the string matching the passed character */
size_t findFirstOf(char c, size_t pos = 0) const;
/** Find first character in the string that's any character of the passed string */
size_t findFirstOf(const char *chars, size_t pos = 0) const;
size_t findFirstOf(const String &chars, size_t pos = 0) const {
return findFirstOf(chars.c_str(), pos);
}
/** Find the last character in the string that's the specified character */
size_t findLastOf(char c, size_t pos = npos) const;
/** Find the last character in the string that's in any of the passed characters */
size_t findLastOf(const char *chars, size_t pos = npos) const;
size_t findLastOf(const String &chars, size_t pos = npos) const {
return findLastOf(chars.c_str(), pos);
}
/** Find first character in the string that's not the specified character */
size_t findFirstNotOf(char c, size_t pos = 0) const;
/** Find first character in the string that's not any character of the passed string */
size_t findFirstNotOf(const char *chars, size_t pos = 0) const;
size_t findFirstNotOf(const String &chars, size_t pos = 0) const {
return findFirstNotOf(chars.c_str(), pos);
}
/** Find the last character in the string that's not the specified character */
size_t findLastNotOf(char c) const;
/** Find the last character in the string that's not in any of the passed characters */
size_t findLastNotOf(const char *chars) const;
size_t findLastNotOf(const String &chars) const {
return findLastNotOf(chars.c_str());
}
/** Return a substring of this string */
String substr(size_t pos = 0, size_t len = npos) const;