Fix StringView::substr

This commit is contained in:
Kae 2023-06-28 20:07:22 +10:00
parent 1fc295b979
commit 4b91eb7175
2 changed files with 17 additions and 18 deletions

View File

@ -338,27 +338,22 @@ bool StringView::equalsIgnoreCase(StringView s) const {
} }
StringView StringView::substr(size_t position, size_t n) const { StringView StringView::substr(size_t position, size_t n) const {
auto len = size(); StringView ret;
if (position > len) auto it_end = end();
throw OutOfRangeException(strf("out of range in StringView::substr({}, {})", position, n));
if (position == 0 && n >= len)
return *this;
String ret;
ret.reserve(std::min(n, len - position));
auto it = begin(); auto it = begin();
std::advance(it, position); for (size_t i = 0; i != position; ++i) {
if (++it == it_end)
for (size_t i = 0; i < n; ++i) { throw OutOfRangeException(strf("out of range in StringView::substr({}, {})", position, n));
if (it == end())
break;
ret.append(*it);
++it;
} }
return ret; const char* start = &*it.base();
for (size_t i = 0; i != n; ++i) {
if (it++ == it_end)
return StringView(start, &*it.base() - start - 1);
}
return StringView(start, &*it.base() - start);
} }
int StringView::compare(size_t selfOffset, size_t selfLen, StringView other, int StringView::compare(size_t selfOffset, size_t selfLen, StringView other,

View File

@ -49,6 +49,10 @@ public:
U8ToU32Iterator(BaseIterator b) : m_position(b), m_value(pending_read) {} U8ToU32Iterator(BaseIterator b) : m_position(b), m_value(pending_read) {}
BaseIterator const& base() const {
return m_position;
}
U32Type const& operator*() const { U32Type const& operator*() const {
if (m_value == pending_read) if (m_value == pending_read)
extract_current(); extract_current();