From 91c75a133f25d2b447391351c59b0e07e69c9ed1 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 3 Apr 2020 15:41:09 +0200 Subject: lib/icu/Collate: pass std::string_view --- src/db/plugins/simple/Directory.cxx | 2 +- src/db/plugins/simple/SongSort.cxx | 2 +- src/lib/icu/Collate.cxx | 20 +++++++++++--------- src/lib/icu/Collate.hxx | 6 ++++-- 4 files changed, 17 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/db/plugins/simple/Directory.cxx b/src/db/plugins/simple/Directory.cxx index 56eaa3877..4c4522622 100644 --- a/src/db/plugins/simple/Directory.cxx +++ b/src/db/plugins/simple/Directory.cxx @@ -199,7 +199,7 @@ gcc_pure static bool directory_cmp(const Directory &a, const Directory &b) noexcept { - return IcuCollate(a.path.c_str(), b.path.c_str()) < 0; + return IcuCollate(a.path, b.path) < 0; } void diff --git a/src/db/plugins/simple/SongSort.cxx b/src/db/plugins/simple/SongSort.cxx index 0aa41b882..4f38384f4 100644 --- a/src/db/plugins/simple/SongSort.cxx +++ b/src/db/plugins/simple/SongSort.cxx @@ -96,7 +96,7 @@ song_cmp(const Song &a, const Song &b) noexcept return ret < 0; /* still no difference? compare file name */ - return IcuCollate(a.filename.c_str(), b.filename.c_str()) < 0; + return IcuCollate(a.filename, b.filename) < 0; } void diff --git a/src/lib/icu/Collate.cxx b/src/lib/icu/Collate.cxx index ee1bb8f88..a91e4e122 100644 --- a/src/lib/icu/Collate.cxx +++ b/src/lib/icu/Collate.cxx @@ -29,6 +29,11 @@ #include #else #include + +#ifndef _WIN32 +#include +#endif + #endif #ifdef _WIN32 @@ -73,19 +78,14 @@ IcuCollateFinish() noexcept gcc_pure int -IcuCollate(const char *a, const char *b) noexcept +IcuCollate(std::string_view a, std::string_view b) noexcept { -#if !CLANG_CHECK_VERSION(3,6) - /* disabled on clang due to -Wtautological-pointer-compare */ - assert(a != nullptr); - assert(b != nullptr); -#endif - #ifdef HAVE_ICU assert(collator != nullptr); UErrorCode code = U_ZERO_ERROR; - return (int)ucol_strcollUTF8(collator, a, -1, b, -1, &code); + return (int)ucol_strcollUTF8(collator, a.data(), a.size(), + b.data(), b.size(), &code); #elif defined(_WIN32) AllocatedString wa = nullptr, wb = nullptr; @@ -120,6 +120,8 @@ IcuCollate(const char *a, const char *b) noexcept return result; #else - return strcoll(a, b); + /* need to duplicate for the fallback because std::string_view + is not null-terminated */ + return strcoll(std::string(a).c_str(), std::string(b).c_str()); #endif } diff --git a/src/lib/icu/Collate.hxx b/src/lib/icu/Collate.hxx index d796e5eba..82a9cdee7 100644 --- a/src/lib/icu/Collate.hxx +++ b/src/lib/icu/Collate.hxx @@ -22,6 +22,8 @@ #include "util/Compiler.h" +#include + /** * Throws #std::runtime_error on error. */ @@ -31,8 +33,8 @@ IcuCollateInit(); void IcuCollateFinish() noexcept; -gcc_pure gcc_nonnull_all +gcc_pure int -IcuCollate(const char *a, const char *b) noexcept; +IcuCollate(std::string_view a, std::string_view b) noexcept; #endif -- cgit v1.2.3