mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-31 13:19:05 +00:00
Everywhere: Hoist the Libraries folder to the top-level
This commit is contained in:
parent
950e819ee7
commit
93712b24bf
Notes:
github-actions[bot]
2024-11-10 11:51:52 +00:00
Author: https://github.com/trflynn89
Commit: 93712b24bf
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2256
Reviewed-by: https://github.com/sideshowbarker
4547 changed files with 104 additions and 113 deletions
79
Libraries/LibUnicode/Normalize.cpp
Normal file
79
Libraries/LibUnicode/Normalize.cpp
Normal file
|
@ -0,0 +1,79 @@
|
|||
/*
|
||||
* Copyright (c) 2022, mat
|
||||
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#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());
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue