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 {
auto len = size();
if (position > len)
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));
StringView ret;
auto it_end = end();
auto it = begin();
std::advance(it, position);
for (size_t i = 0; i < n; ++i) {
if (it == end())
break;
ret.append(*it);
++it;
for (size_t i = 0; i != position; ++i) {
if (++it == it_end)
throw OutOfRangeException(strf("out of range in StringView::substr({}, {})", position, n));
}
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,

View File

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