lib/icu/Transliterator: wrapper for UTransliterator

This commit is contained in:
Max Kellermann 2022-11-15 00:55:39 +01:00
parent 7be39e07e1
commit 9f3faaf3c4
3 changed files with 129 additions and 0 deletions

View File

@ -0,0 +1,64 @@
/*
* 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 "Transliterator.hxx"
#include "util/AllocatedArray.hxx"
#include <algorithm> // for std::copy()
#include <stdexcept>
static UTransliterator *
OpenTransliterator(const UChar *id, const UChar *rules)
{
UErrorCode error_code = U_ZERO_ERROR;
UTransliterator *t = utrans_openU(id, -1, UTRANS_FORWARD,
rules, -1,
nullptr, &error_code);
if (t == nullptr)
throw std::runtime_error(u_errorName(error_code));
return t;
}
IcuTransliterator::IcuTransliterator(const UChar *id, const UChar *rules)
:transliterator(OpenTransliterator(id, rules))
{
}
AllocatedArray<UChar>
IcuTransliterator::Transliterate(std::basic_string_view<UChar> src) noexcept
{
AllocatedArray<UChar> dest(src.size() * 2U);
std::copy(src.begin(), src.end(), dest.begin());
int32_t length = src.size();
int32_t limit = length;
UErrorCode status = U_ZERO_ERROR;
utrans_transUChars(transliterator,
dest.data(), &length, dest.size(),
0, &limit,
&status);
if (U_FAILURE(status))
return nullptr;
dest.SetSize(length);
return dest;
}

View File

@ -0,0 +1,64 @@
/*
* 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/utrans.h>
#include <utility> // for std::exchange()
template<class T> class AllocatedArray;
/**
* Wrapper for an ICU #UTransliterator instance.
*/
class IcuTransliterator {
UTransliterator *transliterator = nullptr;
IcuTransliterator(UTransliterator *_transliterator) noexcept
:transliterator(_transliterator) {}
public:
IcuTransliterator() noexcept = default;
/**
* Throws on error.
*/
IcuTransliterator(const UChar *id, const UChar *rules);
~IcuTransliterator() noexcept {
if (transliterator != nullptr)
utrans_close(transliterator);
}
IcuTransliterator(IcuTransliterator &&src) noexcept
:transliterator(std::exchange(src.transliterator, nullptr)) {}
IcuTransliterator &operator=(IcuTransliterator &&src) noexcept {
using std::swap;
swap(transliterator, src.transliterator);
return *this;
}
/**
* @return the transliterated string (or nullptr on error)
*/
[[gnu::pure]]
AllocatedArray<UChar> Transliterate(std::basic_string_view<UChar> src) noexcept;
};

View File

@ -19,6 +19,7 @@ if icu_dep.found()
'Util.cxx',
'FoldCase.cxx',
'Normalize.cxx',
'Transliterator.cxx',
]
else
if meson.version().version_compare('>= 0.60')