Commit ed544d9c authored by Dominik Charousset's avatar Dominik Charousset

Fix string_view::compare, avoid redundant min()

parent 20338eab
...@@ -103,18 +103,14 @@ string_view string_view::substr(size_type pos, size_type n) const noexcept { ...@@ -103,18 +103,14 @@ string_view string_view::substr(size_type pos, size_type n) const noexcept {
int string_view::compare(string_view str) const noexcept { int string_view::compare(string_view str) const noexcept {
auto s0 = size(); auto s0 = size();
auto s1 = str.size(); auto s1 = str.size();
if (s0 == s1) { auto fallback = [](int x, int y) {
return strncmp(data(), str.data(), std::min(size(), str.size())); return x == 0 ? y : x;
} else if (s0 < s1) { };
auto res = strncmp(data(), str.data(), std::min(size(), str.size())); if (s0 == s1)
if (res == 0) return strncmp(data(), str.data(), s0);
return -1; else if (s0 < s1)
} else { return fallback(strncmp(data(), str.data(), s0), -1);
auto res = strncmp(data(), str.data(), std::min(size(), str.size())); return fallback(strncmp(data(), str.data(), s1), 1);
if (res == 0)
return 1;
}
return 0;
} }
int string_view::compare(size_type pos1, size_type n1, int string_view::compare(size_type pos1, size_type n1,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment