diff --git a/src/lib/icu/Normalize.cxx b/src/lib/icu/Normalize.cxx new file mode 100644 index 000000000..430707205 --- /dev/null +++ b/src/lib/icu/Normalize.cxx @@ -0,0 +1,61 @@ +/* + * 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 "Normalize.hxx" +#include "util/AllocatedArray.hxx" + +#include + +static AllocatedArray +IcuNormalize(const UNormalizer2 *norm2, std::basic_string_view src) noexcept +{ + AllocatedArray dest{src.size() * 2U}; + + UErrorCode error_code = U_ZERO_ERROR; + auto dest_length = unorm2_normalize(norm2, src.data(), src.size(), + dest.data(), dest.size(), + &error_code); + if (U_FAILURE(error_code)) + return nullptr; + + dest.SetSize(dest_length); + return dest; +} + +AllocatedArray +IcuNormalize(std::basic_string_view src) noexcept +{ + UErrorCode error_code = U_ZERO_ERROR; + auto *norm2 = unorm2_getNFKCInstance(&error_code); + if (U_FAILURE(error_code)) + return nullptr; + + return IcuNormalize(norm2, src); +} + +AllocatedArray +IcuNormalizeCaseFold(std::basic_string_view src) noexcept +{ + UErrorCode error_code = U_ZERO_ERROR; + auto *norm2 = unorm2_getNFKCCasefoldInstance(&error_code); + if (U_FAILURE(error_code)) + return nullptr; + + return IcuNormalize(norm2, src); +} diff --git a/src/lib/icu/Normalize.hxx b/src/lib/icu/Normalize.hxx new file mode 100644 index 000000000..12c7e5ad1 --- /dev/null +++ b/src/lib/icu/Normalize.hxx @@ -0,0 +1,40 @@ +/* + * 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 +IcuNormalize(std::basic_string_view src) noexcept; + +/** + * @return the normalized string (or nullptr on error) + */ +[[gnu::pure]] +AllocatedArray +IcuNormalizeCaseFold(std::basic_string_view src) noexcept; diff --git a/src/lib/icu/meson.build b/src/lib/icu/meson.build index ff0995f8b..57b816946 100644 --- a/src/lib/icu/meson.build +++ b/src/lib/icu/meson.build @@ -18,6 +18,7 @@ if icu_dep.found() 'Init.cxx', 'Util.cxx', 'FoldCase.cxx', + 'Normalize.cxx', ] else if meson.version().version_compare('>= 0.60')