lib/icu/CaseFold: rename to IcuCanonicalize() and add flag parameter

Prepare for adding more transformations.
This commit is contained in:
Max Kellermann 2022-11-15 17:09:02 +01:00
parent 852df2239e
commit 8b72cb64b2
4 changed files with 30 additions and 24 deletions

View File

@ -17,10 +17,10 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "CaseFold.hxx"
#include "Canonicalize.hxx"
#include "config.h"
#ifdef HAVE_ICU_CASE_FOLD
#ifdef HAVE_ICU_CANONICALIZE
#include "util/AllocatedString.hxx"
@ -32,18 +32,19 @@
#endif
AllocatedString
IcuCaseFold(std::string_view src) noexcept
IcuCanonicalize(std::string_view src, bool fold_case) noexcept
try {
#ifdef HAVE_ICU
auto u = UCharFromUTF8(src);
if (u.data() == nullptr)
return {src};
auto folded = IcuFoldCase(ToStringView(std::span{u}));
if (folded == nullptr)
return {src};
if (fold_case)
if (auto folded = IcuFoldCase(ToStringView(std::span{u}));
folded != nullptr)
u = std::move(folded);
return UCharToUTF8(ToStringView(std::span{folded}));
return UCharToUTF8(ToStringView(std::span{u}));
#else
#error not implemented
#endif
@ -51,4 +52,4 @@ try {
return {src};
}
#endif /* HAVE_ICU_CASE_FOLD */
#endif /* HAVE_ICU_CANONICALIZE */

View File

@ -17,21 +17,25 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_ICU_CASE_FOLD_HXX
#define MPD_ICU_CASE_FOLD_HXX
#pragma once
#include "config.h"
#ifdef HAVE_ICU
#define HAVE_ICU_CASE_FOLD
#define HAVE_ICU_CANONICALIZE
#include <string_view>
class AllocatedString;
/**
* Transform the given string to "canonical" form to allow fuzzy
* string comparisons. The full set of features (if ICU is being
* used):
*
* - case folding (optional)
*/
AllocatedString
IcuCaseFold(std::string_view src) noexcept;
#endif
IcuCanonicalize(std::string_view src, bool fold_case) noexcept;
#endif

View File

@ -18,7 +18,7 @@
*/
#include "Compare.hxx"
#include "CaseFold.hxx"
#include "Canonicalize.hxx"
#include "util/StringAPI.hxx"
#include "util/StringCompare.hxx"
#include "config.h"
@ -28,10 +28,10 @@
#include <windows.h>
#endif
#ifdef HAVE_ICU_CASE_FOLD
#ifdef HAVE_ICU_CANONICALIZE
IcuCompare::IcuCompare(std::string_view _needle) noexcept
:needle(IcuCaseFold(_needle)) {}
:needle(IcuCanonicalize(_needle, true)) {}
#elif defined(_WIN32)
@ -54,8 +54,8 @@ IcuCompare::IcuCompare(std::string_view _needle) noexcept
bool
IcuCompare::operator==(const char *haystack) const noexcept
{
#ifdef HAVE_ICU_CASE_FOLD
return StringIsEqual(IcuCaseFold(haystack).c_str(), needle.c_str());
#ifdef HAVE_ICU_CANONICALIZE
return StringIsEqual(IcuCanonicalize(haystack, true).c_str(), needle.c_str());
#elif defined(_WIN32)
if (needle == nullptr)
/* the MultiByteToWideChar() call in the constructor
@ -80,8 +80,8 @@ IcuCompare::operator==(const char *haystack) const noexcept
bool
IcuCompare::IsIn(const char *haystack) const noexcept
{
#ifdef HAVE_ICU_CASE_FOLD
return StringFind(IcuCaseFold(haystack).c_str(),
#ifdef HAVE_ICU_CANONICALIZE
return StringFind(IcuCanonicalize(haystack, true).c_str(),
needle.c_str()) != nullptr;
#elif defined(_WIN32)
if (needle == nullptr)
@ -117,8 +117,9 @@ IcuCompare::IsIn(const char *haystack) const noexcept
bool
IcuCompare::StartsWith(const char *haystack) const noexcept
{
#ifdef HAVE_ICU_CASE_FOLD
return StringStartsWith(IcuCaseFold(haystack).c_str(), needle);
#ifdef HAVE_ICU_CANONICALIZE
return StringStartsWith(IcuCanonicalize(haystack, true).c_str(),
needle);
#elif defined(_WIN32)
if (needle == nullptr)
/* the MultiByteToWideChar() call in the constructor

View File

@ -2,7 +2,7 @@ icu_dep = dependency('icu-i18n', version: '>= 50', required: get_option('icu'))
conf.set('HAVE_ICU', icu_dep.found())
icu_sources = [
'CaseFold.cxx',
'Canonicalize.cxx',
'Compare.cxx',
'Collate.cxx',
'Converter.cxx',