summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2020-11-17 14:28:24 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2020-11-17 14:28:24 +0000
commitfc6502f4950192d9726ba7d0b06b1270393af842 (patch)
treead804441618036f30f395b4a251d7bee31beb590
parentPass a cppcheck (diff)
downloadlibadhocutil-fc6502f4950192d9726ba7d0b06b1270393af842.tar.bz2
libadhocutil-fc6502f4950192d9726ba7d0b06b1270393af842.tar.xz
libadhocutil-fc6502f4950192d9726ba7d0b06b1270393af842.zip
Add case_less from Slicer
-rw-r--r--libadhocutil/case_less.h19
-rw-r--r--libadhocutil/unittests/Jamfile.jam8
-rw-r--r--libadhocutil/unittests/testCaseLess.cpp24
3 files changed, 51 insertions, 0 deletions
diff --git a/libadhocutil/case_less.h b/libadhocutil/case_less.h
new file mode 100644
index 0000000..fbe3d3b
--- /dev/null
+++ b/libadhocutil/case_less.h
@@ -0,0 +1,19 @@
+#ifndef ADHOCUTIL_CASELESS_H
+#define ADHOCUTIL_CASELESS_H
+
+#include <algorithm>
+#include <cstring>
+
+namespace AdHoc {
+ struct case_less {
+ template<typename A, typename B>
+ inline bool
+ operator()(const A & a, const B & b) const
+ {
+ const auto cmp = strncasecmp(a.data(), b.data(), std::min(a.length(), b.length()));
+ return (cmp < 0) || (!cmp && a.length() < b.length());
+ }
+ };
+}
+
+#endif
diff --git a/libadhocutil/unittests/Jamfile.jam b/libadhocutil/unittests/Jamfile.jam
index 9d1f304..f91579a 100644
--- a/libadhocutil/unittests/Jamfile.jam
+++ b/libadhocutil/unittests/Jamfile.jam
@@ -90,6 +90,14 @@ run
;
run
+ testCaseLess.cpp
+ : : :
+ <define>BOOST_TEST_DYN_LINK
+ <library>..//adhocutil
+ <library>boost_utf
+ ;
+
+run
testBuffer.cpp
: : :
<define>BOOST_TEST_DYN_LINK
diff --git a/libadhocutil/unittests/testCaseLess.cpp b/libadhocutil/unittests/testCaseLess.cpp
new file mode 100644
index 0000000..9c4dd0b
--- /dev/null
+++ b/libadhocutil/unittests/testCaseLess.cpp
@@ -0,0 +1,24 @@
+#define BOOST_TEST_MODULE CaseLess
+#include <boost/test/unit_test.hpp>
+
+#include <case_less.h>
+
+BOOST_FIXTURE_TEST_SUITE(l, AdHoc::case_less);
+
+BOOST_AUTO_TEST_CASE(case_less_test)
+{
+ using namespace std::literals;
+ const auto & lc {*this};
+ BOOST_CHECK(!lc(""sv, ""sv));
+ BOOST_CHECK(lc("a"sv, "b"sv));
+ BOOST_CHECK(lc("A"sv, "b"sv));
+ BOOST_CHECK(lc("Aa"sv, "b"sv));
+ BOOST_CHECK(lc("AA"sv, "b"sv));
+ BOOST_CHECK(lc("aA"sv, "b"s));
+ BOOST_CHECK(lc("A"s, "B"sv));
+ BOOST_CHECK(lc("Aa"sv, "Bb"s));
+ BOOST_CHECK(lc("AA"s, "bB"s));
+ BOOST_CHECK(lc("aA"s, "BB"s));
+}
+
+BOOST_AUTO_TEST_SUITE_END()