lib/icu/CaseFold: move low-level wrapper to FoldCase.cxx

This commit is contained in:
Max Kellermann 2022-11-15 00:23:04 +01:00
parent aad5210820
commit 154fb4317f
4 changed files with 80 additions and 21 deletions

View File

@ -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 <unicode/ucol.h>
#include <unicode/ustring.h>
#else
#include <algorithm>
#endif
#include <memory>
#include <string.h>
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<UChar> 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

41
src/lib/icu/FoldCase.cxx Normal file
View File

@ -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 <unicode/ucol.h>
#include <unicode/ustring.h>
AllocatedArray<UChar>
IcuFoldCase(std::basic_string_view<UChar> src) noexcept
{
AllocatedArray<UChar> 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;
}

33
src/lib/icu/FoldCase.hxx Normal file
View File

@ -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 <unicode/umachine.h>
#include <string_view>
template<class T> class AllocatedArray;
/**
* @return the normalized string (or nullptr on error)
*/
[[gnu::pure]]
AllocatedArray<UChar>
IcuFoldCase(std::basic_string_view<UChar> src) noexcept;

View File

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