diff options
author | Max Kellermann <max@musicpd.org> | 2020-04-03 15:41:09 +0200 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2020-04-03 16:13:15 +0200 |
commit | 91c75a133f25d2b447391351c59b0e07e69c9ed1 (patch) | |
tree | c78ca0cc53db5d73bc384b6f303919eb30c30e3a /src | |
parent | e620677d7ce133214809dccd29c7cec13a34716b (diff) |
lib/icu/Collate: pass std::string_view
Diffstat (limited to 'src')
-rw-r--r-- | src/db/plugins/simple/Directory.cxx | 2 | ||||
-rw-r--r-- | src/db/plugins/simple/SongSort.cxx | 2 | ||||
-rw-r--r-- | src/lib/icu/Collate.cxx | 20 | ||||
-rw-r--r-- | src/lib/icu/Collate.hxx | 6 |
4 files changed, 17 insertions, 13 deletions
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 <unicode/ustring.h> #else #include <algorithm> + +#ifndef _WIN32 +#include <string> +#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<wchar_t> 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 <string_view> + /** * 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 |