diff options
| author | Maxime Coste <mawww@kakoune.org> | 2022-12-03 08:24:38 +1100 |
|---|---|---|
| committer | Maxime Coste <mawww@kakoune.org> | 2022-12-03 08:24:38 +1100 |
| commit | 78c44e94dc5b346a9963556bc226f4bdcb2c6d80 (patch) | |
| tree | 834e8efedd2a87218d3ef583eea7c49a0769599c /src/string.hh | |
| parent | cd73f2aa1783cfce1cefd056ac459d0b20db5913 (diff) | |
Rework StringOps::substr implementation
Avoid iterating over the whole string when the length is not provided
just use the end iterator directly.
Diffstat (limited to 'src/string.hh')
| -rw-r--r-- | src/string.hh | 18 |
1 files changed, 6 insertions, 12 deletions
diff --git a/src/string.hh b/src/string.hh index c3e7401f..4af99c80 100644 --- a/src/string.hh +++ b/src/string.hh @@ -82,9 +82,9 @@ public: ColumnCount column_count_to(ByteCount count) const { return utf8::column_distance(begin(), begin() + (int)count); } - StringView substr(ByteCount from, ByteCount length = INT_MAX) const; - StringView substr(CharCount from, CharCount length = INT_MAX) const; - StringView substr(ColumnCount from, ColumnCount length = INT_MAX) const; + StringView substr(ByteCount from, ByteCount length = -1) const; + StringView substr(CharCount from, CharCount length = -1) const; + StringView substr(ColumnCount from, ColumnCount length = -1) const; private: [[gnu::always_inline]] @@ -284,29 +284,23 @@ inline String String::no_copy(StringView str) { return {NoCopy{}, str}; } template<typename Type, typename CharType> inline StringView StringOps<Type, CharType>::substr(ByteCount from, ByteCount length) const { - if (length < 0) - length = INT_MAX; const auto str_len = type().length(); kak_assert(from >= 0 and from <= str_len); - return StringView{ type().data() + (int)from, std::min(str_len - from, length) }; + return StringView{type().data() + (int)from, std::min(str_len - from, length >= 0 ? length : str_len)}; } template<typename Type, typename CharType> inline StringView StringOps<Type, CharType>::substr(CharCount from, CharCount length) const { - if (length < 0) - length = INT_MAX; auto beg = utf8::advance(begin(), end(), from); - return StringView{ beg, utf8::advance(beg, end(), length) }; + return StringView{beg, length >= 0 ? utf8::advance(beg, end(), length) : end()}; } template<typename Type, typename CharType> inline StringView StringOps<Type, CharType>::substr(ColumnCount from, ColumnCount length) const { - if (length < 0) - length = INT_MAX; auto beg = utf8::advance(begin(), end(), from); - return StringView{ beg, utf8::advance(beg, end(), length) }; + return StringView{beg, (length >= 0) ? utf8::advance(beg, end(), length) : end()}; } template<typename Type, typename CharType> |
