From 154fb4317fd6ad4bd70833c914929dd91cd35745 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 15 Nov 2022 00:23:04 +0100 Subject: [PATCH] lib/icu/CaseFold: move low-level wrapper to FoldCase.cxx --- src/lib/icu/CaseFold.cxx | 24 ++++------------------- src/lib/icu/FoldCase.cxx | 41 ++++++++++++++++++++++++++++++++++++++++ src/lib/icu/FoldCase.hxx | 33 ++++++++++++++++++++++++++++++++ src/lib/icu/meson.build | 3 ++- 4 files changed, 80 insertions(+), 21 deletions(-) create mode 100644 src/lib/icu/FoldCase.cxx create mode 100644 src/lib/icu/FoldCase.hxx diff --git a/src/lib/icu/CaseFold.cxx b/src/lib/icu/CaseFold.cxx index 2d9f6a24e..fd435cb06 100644 --- a/src/lib/icu/CaseFold.cxx +++ b/src/lib/icu/CaseFold.cxx @@ -25,41 +25,25 @@ #include "util/AllocatedString.hxx" #ifdef HAVE_ICU +#include "FoldCase.hxx" #include "Util.hxx" #include "util/AllocatedArray.hxx" #include "util/SpanCast.hxx" - -#include -#include -#else -#include #endif -#include - -#include - AllocatedString IcuCaseFold(std::string_view src) noexcept try { #ifdef HAVE_ICU - const auto u = UCharFromUTF8(src); + auto u = UCharFromUTF8(src); if (u.data() == nullptr) return {src}; - AllocatedArray folded(u.size() * 2U); - - UErrorCode error_code = U_ZERO_ERROR; - size_t folded_length = u_strFoldCase(folded.data(), folded.size(), - u.data(), u.size(), - U_FOLD_CASE_DEFAULT, - &error_code); - if (folded_length == 0 || error_code != U_ZERO_ERROR) + auto folded = IcuFoldCase(ToStringView(std::span{u})); + if (folded == nullptr) return {src}; - folded.SetSize(folded_length); return UCharToUTF8(ToStringView(std::span{folded})); - #else #error not implemented #endif diff --git a/src/lib/icu/FoldCase.cxx b/src/lib/icu/FoldCase.cxx new file mode 100644 index 000000000..6147ea1a3 --- /dev/null +++ b/src/lib/icu/FoldCase.cxx @@ -0,0 +1,41 @@ +/* + * Copyright 2003-2022 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "FoldCase.hxx" +#include "util/AllocatedArray.hxx" + +#include +#include + +AllocatedArray +IcuFoldCase(std::basic_string_view src) noexcept +{ + AllocatedArray dest(src.size() * 2U); + + UErrorCode error_code = U_ZERO_ERROR; + auto length = u_strFoldCase(dest.data(), dest.size(), + src.data(), src.size(), + U_FOLD_CASE_DEFAULT, + &error_code); + if (U_FAILURE(error_code)) + return nullptr; + + dest.SetSize(length); + return dest; +} diff --git a/src/lib/icu/FoldCase.hxx b/src/lib/icu/FoldCase.hxx new file mode 100644 index 000000000..cb5c6bc84 --- /dev/null +++ b/src/lib/icu/FoldCase.hxx @@ -0,0 +1,33 @@ +/* + * Copyright 2003-2022 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#pragma once + +#include + +#include + +template class AllocatedArray; + +/** + * @return the normalized string (or nullptr on error) + */ +[[gnu::pure]] +AllocatedArray +IcuFoldCase(std::basic_string_view src) noexcept; diff --git a/src/lib/icu/meson.build b/src/lib/icu/meson.build index 92f9e6b1f..ff0995f8b 100644 --- a/src/lib/icu/meson.build +++ b/src/lib/icu/meson.build @@ -15,8 +15,9 @@ endif iconv_dep = [] if icu_dep.found() icu_sources += [ - 'Util.cxx', 'Init.cxx', + 'Util.cxx', + 'FoldCase.cxx', ] else if meson.version().version_compare('>= 0.60')