Skip to content

Commit

Permalink
libcore|CString: Iterating using mb_iterator; added more methods
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent 578f86e commit 0007490
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
14 changes: 9 additions & 5 deletions doomsday/libs/core/include/de/data/cstring.h
Expand Up @@ -35,7 +35,7 @@ class DE_PUBLIC CString
public:
CString() : _range{nullptr, nullptr} {}
CString(const char *cStr) : _range{cStr, nullptr} {} // lazy init
CString(const char *start, const char *end) : _range{start, end} {}
CString(const char *start, const char *end) : _range{start, end} { DE_ASSERT(end >= start); }
CString(const std::string &str) : _range{str.data(), str.data() + str.size()} {}
CString(const String &str) : _range{str.data(), str.data() + str.size()} {}
CString(const Rangecc &cc) : _range{cc} {}
Expand Down Expand Up @@ -65,21 +65,25 @@ class DE_PUBLIC CString
}
inline bool isEmpty() const { return size() == 0; }
inline bool empty() const { return size() == 0; }
inline void setEnd(const char *p) { _range.end = p; }
const char *ptr(BytePos pos = BytePos(0)) const { return _range.start + pos; }
dsize length() const;
bool contains(char ch) const;
bool endsWith(const CString &suffix, Sensitivity cs = CaseSensitive) const;
dsize indexOf(char ch, size_t from = 0) const;
dsize indexOf(const char *cStr, size_t from = 0) const;
CString substr(size_t start, size_t count = npos) const;
inline const char *begin() const { return _range.start; }
inline const char *end() const { updateEnd(); return _range.end; }
CString left(BytePos pos) const { return {_range.start, _range.start + pos}; }
inline mb_iterator begin() const { return _range.start; }
inline mb_iterator end() const { updateEnd(); return {_range.end, _range.start}; }
int compare(const CString &other, Sensitivity cs = CaseSensitive) const;
int compare(const char *cStr, Sensitivity cs = CaseSensitive) const;
inline bool operator==(const char *cStr) const { return compare(cStr) == 0; }
inline bool operator==(const CString &other) const { return compare(other) == 0; }
Char first() const { return *mb_iterator(begin()); }
Char first() const { return *begin(); }
String lower() const;
String upper() const;
std::string toStdString() const { return {begin(), end()}; }
std::string toStdString() const { updateEnd(); return {_range.start, _range.end}; }

static size_t npos;

Expand Down
2 changes: 2 additions & 0 deletions doomsday/libs/core/include/de/data/string.h
Expand Up @@ -114,6 +114,8 @@ struct DE_PUBLIC mb_iterator
explicit operator bool() const { return i != nullptr; }

BytePos pos() const { return BytePos(i - start); }
BytePos pos(const char *reference) const { return BytePos(i - reference); }
BytePos pos(const String &reference) const;

Char decode() const;
};
Expand Down
9 changes: 8 additions & 1 deletion doomsday/libs/core/src/data/cstring.cpp
Expand Up @@ -23,6 +23,13 @@ namespace de {

dsize CString::npos = dsize(-1);

dsize CString::length() const
{
dsize len = 0;
for (auto i = begin(), j = end(); i != j; ++i, ++len) {}
return len;
}

bool CString::contains(char ch) const
{
updateEnd();
Expand All @@ -36,7 +43,7 @@ bool CString::contains(char ch) const
bool CString::endsWith(const CString &suffix, Sensitivity cs) const
{
if (suffix.size() > size()) return false;
return CString(end() - suffix.size(), end()).compare(suffix, cs) == 0;
return CString(_range.end - suffix.size(), end()).compare(suffix, cs) == 0;
}

dsize CString::indexOf(char ch, size_t from) const
Expand Down
8 changes: 7 additions & 1 deletion doomsday/libs/core/src/data/string.cpp
Expand Up @@ -1077,7 +1077,8 @@ String::const_reverse_iterator::const_reverse_iterator(const String &str)
//------------------------------------------------------------------------------------------------

mb_iterator::mb_iterator(const String &str)
: i(str.data()), start(str.data())
: i(str.data())
, start(str.data())
{}

Char mb_iterator::operator*() const
Expand Down Expand Up @@ -1167,6 +1168,11 @@ mb_iterator &mb_iterator::operator-=(int offset)
return (*this) += -offset;
}

BytePos mb_iterator::pos(const String &reference) const
{
return BytePos(i - reference.c_str());
}

//------------------------------------------------------------------------------------------------

std::string stringf(const char *format, ...)
Expand Down

0 comments on commit 0007490

Please sign in to comment.