From 78c44e94dc5b346a9963556bc226f4bdcb2c6d80 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 3 Dec 2022 08:24:38 +1100 Subject: [PATCH] Rework StringOps::substr implementation Avoid iterating over the whole string when the length is not provided just use the end iterator directly. --- src/string.hh | 18 ++++++------------ 1 file 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 inline StringView StringOps::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 inline StringView StringOps::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 inline StringView StringOps::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