2022-10-02 22:57:22 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2022, mat
|
2024-06-18 12:24:06 -03:00
|
|
|
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
|
2022-10-02 22:57:22 -03:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2024-06-18 12:24:06 -03:00
|
|
|
#include <AK/StringBuilder.h>
|
2024-06-23 10:14:27 -03:00
|
|
|
#include <LibUnicode/ICU.h>
|
2022-10-02 22:57:22 -03:00
|
|
|
#include <LibUnicode/Normalize.h>
|
2022-12-13 12:07:23 -03:00
|
|
|
|
2024-06-18 12:24:06 -03:00
|
|
|
#include <unicode/normalizer2.h>
|
2022-10-02 22:57:22 -03:00
|
|
|
|
|
|
|
|
namespace Unicode {
|
|
|
|
|
|
2022-10-06 11:45:59 -03:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-21 14:03:06 -03:00
|
|
|
NormalizationForm normalization_form_from_string(Utf16View form)
|
|
|
|
|
{
|
|
|
|
|
if (form == u"NFD"sv)
|
|
|
|
|
return NormalizationForm::NFD;
|
|
|
|
|
if (form == u"NFC"sv)
|
|
|
|
|
return NormalizationForm::NFC;
|
|
|
|
|
if (form == u"NFKD"sv)
|
|
|
|
|
return NormalizationForm::NFKD;
|
|
|
|
|
if (form == u"NFKC"sv)
|
|
|
|
|
return NormalizationForm::NFKC;
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static icu::Normalizer2 const* normalizer_for_form(NormalizationForm form, UErrorCode& status)
|
2022-10-02 22:57:22 -03:00
|
|
|
{
|
|
|
|
|
switch (form) {
|
|
|
|
|
case NormalizationForm::NFD:
|
2026-06-21 14:03:06 -03:00
|
|
|
return icu::Normalizer2::getNFDInstance(status);
|
2022-10-02 22:57:22 -03:00
|
|
|
case NormalizationForm::NFC:
|
2026-06-21 14:03:06 -03:00
|
|
|
return icu::Normalizer2::getNFCInstance(status);
|
2022-10-02 22:57:22 -03:00
|
|
|
case NormalizationForm::NFKD:
|
2026-06-21 14:03:06 -03:00
|
|
|
return icu::Normalizer2::getNFKDInstance(status);
|
2022-10-02 22:57:22 -03:00
|
|
|
case NormalizationForm::NFKC:
|
2026-06-21 14:03:06 -03:00
|
|
|
return icu::Normalizer2::getNFKCInstance(status);
|
2022-10-02 22:57:22 -03:00
|
|
|
}
|
2026-06-21 14:03:06 -03:00
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String normalize(StringView string, NormalizationForm form)
|
|
|
|
|
{
|
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
|
auto const* normalizer = normalizer_for_form(form, status);
|
2022-10-02 22:57:22 -03:00
|
|
|
|
2024-06-23 10:14:27 -03:00
|
|
|
if (icu_failure(status))
|
2024-06-18 12:24:06 -03:00
|
|
|
return MUST(String::from_utf8(string));
|
|
|
|
|
|
|
|
|
|
VERIFY(normalizer);
|
|
|
|
|
|
|
|
|
|
StringBuilder builder { string.length() };
|
|
|
|
|
icu::StringByteSink sink { &builder };
|
2022-10-02 22:57:22 -03:00
|
|
|
|
2024-06-23 10:14:27 -03:00
|
|
|
normalizer->normalizeUTF8(0, icu_string_piece(string), sink, nullptr, status);
|
|
|
|
|
if (icu_failure(status))
|
2024-06-18 12:24:06 -03:00
|
|
|
return MUST(String::from_utf8(string));
|
2022-10-02 22:57:22 -03:00
|
|
|
|
2023-09-06 08:25:56 -03:00
|
|
|
return MUST(builder.to_string());
|
2022-10-02 22:57:22 -03:00
|
|
|
}
|
|
|
|
|
|
2026-06-21 14:03:06 -03:00
|
|
|
Utf16String normalize(Utf16View string, NormalizationForm form)
|
|
|
|
|
{
|
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
|
auto const* normalizer = normalizer_for_form(form, status);
|
|
|
|
|
|
|
|
|
|
if (icu_failure(status))
|
|
|
|
|
return Utf16String::from_utf16(string);
|
|
|
|
|
|
|
|
|
|
VERIFY(normalizer);
|
|
|
|
|
|
|
|
|
|
auto icu_input = icu_string(string);
|
|
|
|
|
UErrorCode normalize_status = U_ZERO_ERROR;
|
|
|
|
|
auto icu_output = normalizer->normalize(icu_input, normalize_status);
|
|
|
|
|
if (icu_failure(normalize_status))
|
|
|
|
|
return Utf16String::from_utf16(string);
|
|
|
|
|
|
|
|
|
|
return icu_string_to_utf16_string(icu_output);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-02 22:57:22 -03:00
|
|
|
}
|