From fc6502f4950192d9726ba7d0b06b1270393af842 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 17 Nov 2020 14:28:24 +0000 Subject: Add case_less from Slicer --- libadhocutil/case_less.h | 19 +++++++++++++++++++ libadhocutil/unittests/Jamfile.jam | 8 ++++++++ libadhocutil/unittests/testCaseLess.cpp | 24 ++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 libadhocutil/case_less.h create mode 100644 libadhocutil/unittests/testCaseLess.cpp 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 +#include + +namespace AdHoc { + struct case_less { + template + 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 @@ -89,6 +89,14 @@ run testCurl ; +run + testCaseLess.cpp + : : : + BOOST_TEST_DYN_LINK + ..//adhocutil + boost_utf + ; + run testBuffer.cpp : : : 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 + +#include + +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() -- cgit v1.2.3