From 615472ff4b8d17f3745d023a1b20914e2853d160 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 19 Oct 2019 13:18:03 +0100 Subject: Template and inline case_less strncasecmp wrapper --- slicer/slicer/modelParts.cpp | 7 ------- slicer/slicer/modelParts.h | 9 +++++++-- slicer/test/serializers.cpp | 21 +++++++++++---------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/slicer/slicer/modelParts.cpp b/slicer/slicer/modelParts.cpp index 6f68e9c..61cbd25 100644 --- a/slicer/slicer/modelParts.cpp +++ b/slicer/slicer/modelParts.cpp @@ -89,12 +89,5 @@ namespace Slicer { { ch(this->name, modelPart, this); } - - bool - case_less::operator()(std::string_view && a, std::string_view && b) const - { - const auto cmp = strncasecmp(a.data(), b.data(), std::min(a.length(), b.length())); - return (cmp < 0) || (!cmp && a.length() < b.length()); - } } diff --git a/slicer/slicer/modelParts.h b/slicer/slicer/modelParts.h index 9d6384e..3ec4201 100644 --- a/slicer/slicer/modelParts.h +++ b/slicer/slicer/modelParts.h @@ -116,8 +116,13 @@ namespace Slicer { const std::string name; }; - struct DLL_PUBLIC case_less { - bool operator()(std::string_view && lhs, std::string_view && rhs) const; + 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()); + } }; class DLL_PUBLIC ModelPart : public std::enable_shared_from_this { diff --git a/slicer/test/serializers.cpp b/slicer/test/serializers.cpp index 9b0ef95..bf50b22 100644 --- a/slicer/test/serializers.cpp +++ b/slicer/test/serializers.cpp @@ -665,17 +665,18 @@ BOOST_FIXTURE_TEST_SUITE(l, Slicer::case_less); BOOST_AUTO_TEST_CASE(case_less_test) { + using namespace std::literals; const auto & lc { *this }; - BOOST_CHECK(!lc("", "")); - BOOST_CHECK(lc("a", "b")); - BOOST_CHECK(lc("A", "b")); - BOOST_CHECK(lc("Aa", "b")); - BOOST_CHECK(lc("AA", "b")); - BOOST_CHECK(lc("aA", "b")); - BOOST_CHECK(lc("A", "B")); - BOOST_CHECK(lc("Aa", "Bb")); - BOOST_CHECK(lc("AA", "bB")); - BOOST_CHECK(lc("aA", "BB")); + 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