From e5ac664fe397d5874a244bbbc8a7b266223cb88b Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 26 Jan 2023 19:36:27 +0400 Subject: [PATCH] Provide LanguageId instead of QLocale. --- CMakeLists.txt | 62 ++++++++++++-------- spellcheck/platform/linux/language_linux.cpp | 4 +- spellcheck/platform/mac/language_mac.mm | 6 +- spellcheck/platform/platform_language.h | 8 +-- spellcheck/platform/win/language_win.cpp | 6 +- spellcheck/spellcheck_types.h | 44 ++++++++++++++ spellcheck/third_party/language_cld3.cpp | 7 +-- 7 files changed, 95 insertions(+), 42 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6a69cf8..90ad239 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,7 +35,28 @@ endif() target_precompile_headers(lib_spellcheck PRIVATE ${src_loc}/spellcheck/spellcheck_pch.h) nice_target_sources(lib_spellcheck ${src_loc} PRIVATE + spellcheck/platform/linux/language_linux.cpp + spellcheck/platform/linux/language_linux.h + spellcheck/platform/linux/linux_enchant.cpp + spellcheck/platform/linux/linux_enchant.h + spellcheck/platform/linux/spellcheck_linux.cpp + spellcheck/platform/linux/spellcheck_linux.h + spellcheck/platform/mac/language_mac.h + spellcheck/platform/mac/language_mac.mm + spellcheck/platform/mac/spellcheck_mac.h + spellcheck/platform/mac/spellcheck_mac.mm + spellcheck/platform/win/language_win.cpp + spellcheck/platform/win/language_win.h + spellcheck/platform/win/spellcheck_win.cpp + spellcheck/platform/win/spellcheck_win.h + spellcheck/platform/platform_language.h spellcheck/platform/platform_spellcheck.h + spellcheck/third_party/language_cld3.cpp + spellcheck/third_party/language_cld3.h + spellcheck/third_party/hunspell_controller.cpp + spellcheck/third_party/hunspell_controller.h + spellcheck/third_party/spellcheck_hunspell.cpp + spellcheck/third_party/spellcheck_hunspell.h spellcheck/spellcheck_utils.cpp spellcheck/spellcheck_utils.h spellcheck/spellcheck_types.h @@ -50,8 +71,12 @@ PRIVATE ) if (system_spellchecker) - nice_target_sources(lib_spellcheck ${src_loc} - PRIVATE + remove_target_sources(lib_spellcheck ${src_loc} + spellcheck/third_party/spellcheck_hunspell.cpp + spellcheck/third_party/spellcheck_hunspell.h + ) +else() + remove_target_sources(lib_spellcheck ${src_loc} spellcheck/platform/linux/linux_enchant.cpp spellcheck/platform/linux/linux_enchant.h spellcheck/platform/linux/spellcheck_linux.cpp @@ -61,23 +86,11 @@ if (system_spellchecker) spellcheck/platform/win/spellcheck_win.cpp spellcheck/platform/win/spellcheck_win.h ) -else() - nice_target_sources(lib_spellcheck ${src_loc} - PRIVATE - spellcheck/third_party/spellcheck_hunspell.cpp - spellcheck/third_party/spellcheck_hunspell.h - ) endif() if (use_cld3) - nice_target_sources(lib_spellcheck ${src_loc} - PRIVATE - spellcheck/third_party/language_cld3.cpp - spellcheck/third_party/language_cld3.h - ) -else() - nice_target_sources(lib_spellcheck ${src_loc} - PRIVATE + target_link_libraries(lib_spellcheck PRIVATE desktop-app::external_cld3) + remove_target_sources(lib_spellcheck ${src_loc} spellcheck/platform/linux/language_linux.cpp spellcheck/platform/linux/language_linux.h spellcheck/platform/mac/language_mac.h @@ -85,20 +98,21 @@ else() spellcheck/platform/win/language_win.cpp spellcheck/platform/win/language_win.h ) +else() + remove_target_sources(lib_spellcheck ${src_loc} + spellcheck/third_party/language_cld3.cpp + spellcheck/third_party/language_cld3.h + ) endif() # We should support both types of spellchecker for Windows. -if (NOT system_spellchecker OR WIN32) - nice_target_sources(lib_spellcheck ${src_loc} - PRIVATE +if (WIN32 OR NOT system_spellchecker) + target_link_libraries(lib_spellcheck PRIVATE desktop-app::external_hunspell) +else() + remove_target_sources(lib_spellcheck ${src_loc} spellcheck/third_party/hunspell_controller.cpp spellcheck/third_party/hunspell_controller.h ) - target_link_libraries(lib_spellcheck PRIVATE desktop-app::external_hunspell) -endif() - -if (use_cld3) - target_link_libraries(lib_spellcheck PRIVATE desktop-app::external_cld3) endif() target_include_directories(lib_spellcheck diff --git a/spellcheck/platform/linux/language_linux.cpp b/spellcheck/platform/linux/language_linux.cpp index d12bbc3..a691889 100644 --- a/spellcheck/platform/linux/language_linux.cpp +++ b/spellcheck/platform/linux/language_linux.cpp @@ -8,8 +8,8 @@ namespace Platform::Language { -RecognitionResult Recognize(QStringView text) { - return { .unknown = true }; +Id Recognize(QStringView text) { + return {}; } } // namespace Platform::Language diff --git a/spellcheck/platform/mac/language_mac.mm b/spellcheck/platform/mac/language_mac.mm index 1393261..dc00fae 100644 --- a/spellcheck/platform/mac/language_mac.mm +++ b/spellcheck/platform/mac/language_mac.mm @@ -15,7 +15,7 @@ namespace Platform::Language { -RecognitionResult Recognize(QStringView text) { +Id Recognize(QStringView text) { if (@available(macOS 10.14, *)) { constexpr auto kMaxHypotheses = 3; static auto r = [[NLLanguageRecognizer alloc] init]; @@ -33,11 +33,11 @@ RecognitionResult Recognize(QStringView text) { } } if (language) { - return { QLocale(NS2QString(language)) }; + return { QLocale(NS2QString(language)).language() }; } } - return { .unknown = true }; + return {}; } } // namespace Platform::Language diff --git a/spellcheck/platform/platform_language.h b/spellcheck/platform/platform_language.h index 1bc5998..5f42ac0 100644 --- a/spellcheck/platform/platform_language.h +++ b/spellcheck/platform/platform_language.h @@ -6,15 +6,11 @@ // #pragma once -#include +#include "spellcheck/spellcheck_types.h" namespace Platform::Language { -struct RecognitionResult final { - QLocale locale; - bool unknown = false; -}; -[[nodiscard]] RecognitionResult Recognize(QStringView text); +[[nodiscard]] LanguageId Recognize(QStringView text); } // namespace Platform::Language diff --git a/spellcheck/platform/win/language_win.cpp b/spellcheck/platform/win/language_win.cpp index 4877473..847aaa2 100644 --- a/spellcheck/platform/win/language_win.cpp +++ b/spellcheck/platform/win/language_win.cpp @@ -125,7 +125,7 @@ void RecognizeTextLanguages( } } -RecognitionResult Recognize(QStringView text) { +Id Recognize(QStringView text) { if (Supported()) { auto locales = std::vector(); RecognizeTextLanguages( @@ -135,9 +135,9 @@ RecognitionResult Recognize(QStringView text) { // Cut complex result, e.g. "sr-Cyrl". locales.emplace_back(QString::fromWCharArray(r, 2)); }); - return { .locale = locales[0] }; + return { locales[0].language() }; } - return { .unknown = true }; + return {}; } } // namespace Platform::Language diff --git a/spellcheck/spellcheck_types.h b/spellcheck/spellcheck_types.h index 7fd67a7..a669a9a 100644 --- a/spellcheck/spellcheck_types.h +++ b/spellcheck/spellcheck_types.h @@ -6,5 +6,49 @@ // #pragma once +#include + using MisspelledWord = std::pair; using MisspelledWords = std::vector; + +struct LanguageId { + QLocale::Language value = QLocale::AnyLanguage; + + [[nodiscard]] static LanguageId FromName(const QString &name) { + auto exact = QLocale(name); + return { + ((exact.language() == QLocale::C) + ? QLocale(name.mid(0, 2)) + : exact).language() + }; + } + + [[nodiscard]] QLocale locale() const { + if (value == QLocale::C) { + return QLocale(QLocale::English); + } + auto result = QLocale(value); + return (result.language() == QLocale::C) + ? QLocale(QLocale::English) + : result; + } + + [[nodiscard]] bool known() const noexcept { + return (value != QLocale::AnyLanguage); + } + explicit operator bool() const noexcept { + return known(); + } + + friend inline constexpr auto operator<=>( + LanguageId a, + LanguageId b) noexcept { + return (a.value == QLocale::C ? QLocale::English : a.value) + <=> (b.value == QLocale::C ? QLocale::English : b.value); + } + friend inline constexpr bool operator==( + LanguageId a, + LanguageId b) noexcept { + return (a <=> b) == 0; + } +}; diff --git a/spellcheck/third_party/language_cld3.cpp b/spellcheck/third_party/language_cld3.cpp index d0aea2f..b30cea5 100644 --- a/spellcheck/third_party/language_cld3.cpp +++ b/spellcheck/third_party/language_cld3.cpp @@ -10,7 +10,7 @@ namespace Platform::Language { -RecognitionResult Recognize(QStringView text) { +LanguageId Recognize(QStringView text) { using chrome_lang_id::NNetLanguageIdentifier; constexpr auto kMinNumBytes = 0; @@ -31,10 +31,9 @@ RecognitionResult Recognize(QStringView text) { } } if (final.language == NNetLanguageIdentifier::kUnknown) { - return { .unknown = true }; - } else { - return { .locale = QLocale(QString::fromStdString(final.language)) }; + return {}; } + return { QLocale(QString::fromStdString(final.language)).language() }; } } // namespace Platform::Language