From c4039fa56fb646be30e09789a9db5e93111c79a4 Mon Sep 17 00:00:00 2001 From: q66 Date: Fri, 2 Sep 2016 00:06:13 +0100 Subject: [PATCH] add case insensitive string compare --- ostd/string.hh | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/ostd/string.hh b/ostd/string.hh index 5fe4f46..4af8761 100644 --- a/ostd/string.hh +++ b/ostd/string.hh @@ -8,6 +8,7 @@ #include #include +#include #include "ostd/utility.hh" #include "ostd/range.hh" @@ -184,6 +185,21 @@ diffsize: return (s1 < s2) ? -1 : ((s1 > s2) ? 1 : 0); } + int case_compare(CharRangeBase s) const { + ostd::Size s1 = size(), s2 = s.size(); + if (!s1 || !s2) { + goto diffsize; + } + for (ostd::Size i = 0, ms = ostd::min(s1, s2); i < ms; ++i) { + int d = toupper(p_beg[i]) - toupper(s[i]); + if (d) { + return d; + } + } +diffsize: + return (s1 < s2) ? -1 : ((s1 > s2) ? 1 : 0); + } + template EnableIf, Size> copy(R &&orange, Size n = -1) { return orange.put_n(data(), ostd::min(n, size())); @@ -613,6 +629,10 @@ public: return iter().compare(r); } + int case_compare(ConstRange r) const { + return iter().case_compare(r); + } + Range iter() { return Range(p_buf.first(), size()); }