lib/icu/Collate: pass std::string_view

This commit is contained in:
Max Kellermann 2020-04-03 15:41:09 +02:00
parent e620677d7c
commit 91c75a133f
4 changed files with 17 additions and 13 deletions

View File

@ -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

View File

@ -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

View File

@ -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
}

View File

@ -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