ladybird/Userland/Libraries/LibUnicode/Normalize.cpp
Timothy Flynn ebdb92eef6 LibUnicode+Everywhere: Merge LibLocale back into LibUnicode
LibLocale was split off from LibUnicode a couple years ago to reduce the
number of applications on SerenityOS that depend on CLDR data. Now that
we use ICU, both LibUnicode and LibLocale are actually linking in this
data. And since vcpkg gives us static libraries, both libraries are over
30MB in size.

This patch reverts the separation and merges LibLocale into LibUnicode
again. We now have just one library that includes the ICU data.

Further, this will let LibUnicode share the locale cache that previously
would only exist in LibLocale.
2024-06-23 19:52:45 +02:00

82 lines
2.0 KiB
C++

/*
* Copyright (c) 2022, mat
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#define AK_DONT_REPLACE_STD
#include <AK/StringBuilder.h>
#include <LibUnicode/ICU.h>
#include <LibUnicode/Normalize.h>
#include <unicode/normalizer2.h>
namespace Unicode {
NormalizationForm normalization_form_from_string(StringView form)
{
if (form == "NFD"sv)
return NormalizationForm::NFD;
if (form == "NFC"sv)
return NormalizationForm::NFC;
if (form == "NFKD"sv)
return NormalizationForm::NFKD;
if (form == "NFKC"sv)
return NormalizationForm::NFKC;
VERIFY_NOT_REACHED();
}
StringView normalization_form_to_string(NormalizationForm form)
{
switch (form) {
case NormalizationForm::NFD:
return "NFD"sv;
case NormalizationForm::NFC:
return "NFC"sv;
case NormalizationForm::NFKD:
return "NFKD"sv;
case NormalizationForm::NFKC:
return "NFKC"sv;
}
VERIFY_NOT_REACHED();
}
String normalize(StringView string, NormalizationForm form)
{
UErrorCode status = U_ZERO_ERROR;
icu::Normalizer2 const* normalizer = nullptr;
switch (form) {
case NormalizationForm::NFD:
normalizer = icu::Normalizer2::getNFDInstance(status);
break;
case NormalizationForm::NFC:
normalizer = icu::Normalizer2::getNFCInstance(status);
break;
case NormalizationForm::NFKD:
normalizer = icu::Normalizer2::getNFKDInstance(status);
break;
case NormalizationForm::NFKC:
normalizer = icu::Normalizer2::getNFKCInstance(status);
break;
}
if (icu_failure(status))
return MUST(String::from_utf8(string));
VERIFY(normalizer);
StringBuilder builder { string.length() };
icu::StringByteSink sink { &builder };
normalizer->normalizeUTF8(0, icu_string_piece(string), sink, nullptr, status);
if (icu_failure(status))
return MUST(String::from_utf8(string));
return MUST(builder.to_string());
}
}