From 8b72cb64b2d556cc27c2e6bc91d3884bd328dbba Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 15 Nov 2022 17:09:02 +0100 Subject: [PATCH] lib/icu/CaseFold: rename to IcuCanonicalize() and add flag parameter Prepare for adding more transformations. --- .../icu/{CaseFold.cxx => Canonicalize.cxx} | 17 +++++++++-------- .../icu/{CaseFold.hxx => Canonicalize.hxx} | 16 ++++++++++------ src/lib/icu/Compare.cxx | 19 ++++++++++--------- src/lib/icu/meson.build | 2 +- 4 files changed, 30 insertions(+), 24 deletions(-) rename src/lib/icu/{CaseFold.cxx => Canonicalize.cxx} (78%) rename src/lib/icu/{CaseFold.hxx => Canonicalize.hxx} (76%) diff --git a/src/lib/icu/CaseFold.cxx b/src/lib/icu/Canonicalize.cxx similarity index 78% rename from src/lib/icu/CaseFold.cxx rename to src/lib/icu/Canonicalize.cxx index fd435cb06..8f97cb9f5 100644 --- a/src/lib/icu/CaseFold.cxx +++ b/src/lib/icu/Canonicalize.cxx @@ -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 */ diff --git a/src/lib/icu/CaseFold.hxx b/src/lib/icu/Canonicalize.hxx similarity index 76% rename from src/lib/icu/CaseFold.hxx rename to src/lib/icu/Canonicalize.hxx index 5cfe77717..d2d33c004 100644 --- a/src/lib/icu/CaseFold.hxx +++ b/src/lib/icu/Canonicalize.hxx @@ -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 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 diff --git a/src/lib/icu/Compare.cxx b/src/lib/icu/Compare.cxx index 109b167fc..d8b147c45 100644 --- a/src/lib/icu/Compare.cxx +++ b/src/lib/icu/Compare.cxx @@ -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 #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 diff --git a/src/lib/icu/meson.build b/src/lib/icu/meson.build index 65d90b199..2eb5ce446 100644 --- a/src/lib/icu/meson.build +++ b/src/lib/icu/meson.build @@ -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',