diff --git a/doomsday/libs/core/include/de/data/string.h b/doomsday/libs/core/include/de/data/string.h index 7d54602edd..5fdf8b5b79 100644 --- a/doomsday/libs/core/include/de/data/string.h +++ b/doomsday/libs/core/include/de/data/string.h @@ -58,21 +58,21 @@ struct DE_PUBLIC Sensitivity }; enum PositionType { ByteOffset, CharacterOffset }; - + template struct StronglyTypedPosition { dsize index; using Pos = StronglyTypedPosition; - + static constexpr PositionType type = PosType; static constexpr dsize npos = std::numeric_limits::max(); explicit constexpr StronglyTypedPosition(dsize i = npos) : index(i) {} - + explicit operator bool() const { return index != npos; } inline bool operator==(dsize i) const { return index == i; } @@ -101,14 +101,14 @@ struct StronglyTypedPosition Pos &operator++() { if (index != npos) index++; return *this; } Pos &operator--() { if (index != npos) index--; return *this; } }; - + using BytePos = StronglyTypedPosition; /** * Character index. A single character may be composed of multiple bytes. */ using CharPos = StronglyTypedPosition; - + /** * Multibyte character iterator. */ @@ -144,7 +144,7 @@ struct DE_PUBLIC mb_iterator BytePos pos(const char *reference) const { return BytePos(i - reference); } BytePos pos(const String &reference) const; - Char decode() const; + Char decode(bool *ok = nullptr) const; }; /** diff --git a/doomsday/libs/core/src/data/string.cpp b/doomsday/libs/core/src/data/string.cpp index e76b011477..d91e97dfc8 100644 --- a/doomsday/libs/core/src/data/string.cpp +++ b/doomsday/libs/core/src/data/string.cpp @@ -1102,7 +1102,7 @@ String String::fromPercentEncoding(const Block &percentEncoded) // static } //------------------------------------------------------------------------------------------------ - + String::const_reverse_iterator::const_reverse_iterator(const Range &range) : iter(range.end, range.start) { @@ -1133,7 +1133,7 @@ Char mb_iterator::operator*() const return decode(); } -Char mb_iterator::decode() const +Char mb_iterator::decode(bool *ok) const { wchar_t ch = 0; curCharLen = std::mbrtowc(&ch, i, MB_CUR_MAX, &mb); @@ -1143,6 +1143,11 @@ Char mb_iterator::decode() const // doesn't hang due to never reaching the end of the string. ch = '?'; curCharLen = 1; + if (ok) *ok = false; + } + else + { + if (ok) *ok = true; } return ch; } @@ -1173,7 +1178,9 @@ mb_iterator &mb_iterator::operator--() for (int j = 1; j < MB_CUR_MAX; ++j) { mb_iterator prec = i - j; - if (prec.decode() != 0 && prec.curCharLen == j) + bool ok; + prec.decode(&ok); + if (ok) // && prec.curCharLen == j) { prec.start = start; *this = prec;