From dc133de4ccf14b2b896272b64d8249d61cbd0d49 Mon Sep 17 00:00:00 2001 From: Lukhnos Liu Date: Sun, 14 Jan 2024 16:22:49 -0800 Subject: [PATCH] C++ coding style fixes --- src/Engine/McBopomofoLM.cpp | 4 +- src/Engine/McBopomofoLM.h | 19 +-- src/InputMacro.cpp | 238 +++++++++++++++++++++++------------- src/InputMacro.h | 37 ++++-- src/LanguageModelLoader.cpp | 4 +- 5 files changed, 196 insertions(+), 106 deletions(-) diff --git a/src/Engine/McBopomofoLM.cpp b/src/Engine/McBopomofoLM.cpp index ff52fce..1f974c1 100644 --- a/src/Engine/McBopomofoLM.cpp +++ b/src/Engine/McBopomofoLM.cpp @@ -172,12 +172,12 @@ bool McBopomofoLM::externalConverterEnabled() const return m_externalConverterEnabled; } -void McBopomofoLM::setExternalConverter(std::function externalConverter) +void McBopomofoLM::setExternalConverter(std::function externalConverter) { m_externalConverter = externalConverter; } -void McBopomofoLM::setMacroConverter(std::function macroConverter) +void McBopomofoLM::setMacroConverter(std::function macroConverter) { m_macroConverter = macroConverter; } diff --git a/src/Engine/McBopomofoLM.h b/src/Engine/McBopomofoLM.h index feb6589..09956ec 100644 --- a/src/Engine/McBopomofoLM.h +++ b/src/Engine/McBopomofoLM.h @@ -24,17 +24,18 @@ #ifndef SRC_ENGINE_MCBOPOMOFOLM_H_ #define SRC_ENGINE_MCBOPOMOFOLM_H_ -#include "AssociatedPhrases.h" -#include "ParselessLM.h" -#include "PhraseReplacementMap.h" -#include "UserPhrasesLM.h" -#include "gramambular2/language_model.h" #include #include #include #include #include +#include "AssociatedPhrases.h" +#include "ParselessLM.h" +#include "PhraseReplacementMap.h" +#include "UserPhrasesLM.h" +#include "gramambular2/language_model.h" + namespace McBopomofo { /// McBopomofoLM is a facade for managing a set of models including @@ -102,10 +103,10 @@ class McBopomofoLM : public Formosa::Gramambular2::LanguageModel { /// If the external converted is enabled or not. bool externalConverterEnabled() const; /// Sets a lambda to let the values of unigrams could be converted by it. - void setExternalConverter(std::function externalConverter); + void setExternalConverter(std::function externalConverter); /// Sets a lambda to convert the macro to a string. - void setMacroConverter(std::function macroConverter); + void setMacroConverter(std::function macroConverter); const std::vector associatedPhrasesForKey(const std::string& key); bool hasAssociatedPhrasesForKey(const std::string& key); @@ -130,10 +131,10 @@ class McBopomofoLM : public Formosa::Gramambular2::LanguageModel { UserPhrasesLM m_excludedPhrases; PhraseReplacementMap m_phraseReplacement; AssociatedPhrases m_associatedPhrases; - std::function m_macroConverter; + std::function m_macroConverter; bool m_phraseReplacementEnabled; bool m_externalConverterEnabled; - std::function m_externalConverter; + std::function m_externalConverter; }; } // namespace McBopomofo diff --git a/src/InputMacro.cpp b/src/InputMacro.cpp index 845012b..3d9f681 100644 --- a/src/InputMacro.cpp +++ b/src/InputMacro.cpp @@ -1,143 +1,211 @@ +// Copyright (c) 2023 and onwards The McBopomofo Authors. +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. + #include "InputMacro.h" #include #include #include +#include #include namespace McBopomofo { class InputMacroDateTodayShort : public InputMacro { public: - std::string name() const { return "MACRO@DATE_TODAY_SHORT"; }; - std::string replacement() const; + [[nodiscard]] std::string name() const override { + return "MACRO@DATE_TODAY_SHORT"; + }; + [[nodiscard]] std::string replacement() const override; }; class InputMacroDateTodayMedium : public InputMacro { public: - std::string name() const { return "MACRO@DATE_TODAY_MEDIUM"; }; - std::string replacement() const; + [[nodiscard]] std::string name() const override { + return "MACRO@DATE_TODAY_MEDIUM"; + }; + [[nodiscard]] std::string replacement() const override; }; class InputMacroDateTodayMediumRoc : public InputMacro { public: - std::string name() const { return "MACRO@DATE_TODAY_MEDIUM_ROC"; }; - std::string replacement() const; + [[nodiscard]] std::string name() const override { + return "MACRO@DATE_TODAY_MEDIUM_ROC"; + }; + [[nodiscard]] std::string replacement() const override; }; class InputMacroDateTodayMediumChinese : public InputMacro { public: - std::string name() const { return "MACRO@DATE_TODAY_MEDIUM_CHINESE"; }; - std::string replacement() const; + [[nodiscard]] std::string name() const override { + return "MACRO@DATE_TODAY_MEDIUM_CHINESE"; + }; + [[nodiscard]] std::string replacement() const override; }; class InputMacroDateYesterdayShort : public InputMacro { public: - std::string name() const { return "MACRO@DATE_YESTERDAY_SHORT"; }; - std::string replacement() const; + [[nodiscard]] std::string name() const override { + return "MACRO@DATE_YESTERDAY_SHORT"; + }; + [[nodiscard]] std::string replacement() const override; }; class InputMacroDateYesterdayMedium : public InputMacro { public: - std::string name() const { return "MACRO@DATE_YESTERDAY_MEDIUM"; }; - std::string replacement() const; + [[nodiscard]] std::string name() const override { + return "MACRO@DATE_YESTERDAY_MEDIUM"; + }; + [[nodiscard]] std::string replacement() const override; }; class InputMacroDateYesterdayMediumRoc : public InputMacro { public: - std::string name() const { return "MACRO@DATE_YESTERDAY_MEDIUM_ROC"; }; - std::string replacement() const; + [[nodiscard]] std::string name() const override { + return "MACRO@DATE_YESTERDAY_MEDIUM_ROC"; + }; + [[nodiscard]] std::string replacement() const override; }; class InputMacroDateYesterdayMediumChinese : public InputMacro { public: - std::string name() const { return "MACRO@DATE_YESTERDAY_MEDIUM_CHINESE"; }; - std::string replacement() const; + [[nodiscard]] std::string name() const override { + return "MACRO@DATE_YESTERDAY_MEDIUM_CHINESE"; + }; + [[nodiscard]] std::string replacement() const override; }; class InputMacroDateTomorrowShort : public InputMacro { public: - std::string name() const { return "MACRO@DATE_TOMORROW_SHORT"; }; - std::string replacement() const; + [[nodiscard]] std::string name() const override { + return "MACRO@DATE_TOMORROW_SHORT"; + }; + [[nodiscard]] std::string replacement() const override; }; class InputMacroDateTomorrowMedium : public InputMacro { public: - std::string name() const { return "MACRO@DATE_TOMORROW_MEDIUM"; }; - std::string replacement() const; + [[nodiscard]] std::string name() const override { + return "MACRO@DATE_TOMORROW_MEDIUM"; + }; + [[nodiscard]] std::string replacement() const override; }; class InputMacroDateTomorrowMediumRoc : public InputMacro { public: - std::string name() const { return "MACRO@DATE_TOMORROW_MEDIUM_ROC"; }; - std::string replacement() const; + [[nodiscard]] std::string name() const override { + return "MACRO@DATE_TOMORROW_MEDIUM_ROC"; + }; + [[nodiscard]] std::string replacement() const override; }; class InputMacroDateTomorrowMediumChinese : public InputMacro { public: - std::string name() const { return "MACRO@DATE_TOMORROW_MEDIUM_CHINESE"; }; - std::string replacement() const; + [[nodiscard]] std::string name() const override { + return "MACRO@DATE_TOMORROW_MEDIUM_CHINESE"; + }; + [[nodiscard]] std::string replacement() const override; }; class InputMacroDateTimeNowShort : public InputMacro { public: - std::string name() const { return "MACRO@TIME_NOW_SHORT"; }; - std::string replacement() const; + [[nodiscard]] std::string name() const override { + return "MACRO@TIME_NOW_SHORT"; + }; + [[nodiscard]] std::string replacement() const override; }; class InputMacroDateTimeNowMedium : public InputMacro { public: - std::string name() const { return "MACRO@TIME_NOW_MEDIUM"; }; - std::string replacement() const; + [[nodiscard]] std::string name() const override { + return "MACRO@TIME_NOW_MEDIUM"; + }; + [[nodiscard]] std::string replacement() const override; }; class InputMacroTimeZoneStandard : public InputMacro { public: - std::string name() const { return "MACRO@TIMEZONE_STANDARD"; }; - std::string replacement() const; + [[nodiscard]] std::string name() const override { + return "MACRO@TIMEZONE_STANDARD"; + }; + [[nodiscard]] std::string replacement() const override; }; class InputMacroTimeZoneShortGeneric : public InputMacro { public: - std::string name() const { return "MACRO@TIMEZONE_GENERIC_SHORT"; }; - std::string replacement() const; + [[nodiscard]] std::string name() const override { + return "MACRO@TIMEZONE_GENERIC_SHORT"; + }; + [[nodiscard]] std::string replacement() const override; }; class InputMacroThisYearGanZhi : public InputMacro { public: - std::string name() const { return "MACRO@THIS_YEAR_GANZHI"; }; - std::string replacement() const; + [[nodiscard]] std::string name() const override { + return "MACRO@THIS_YEAR_GANZHI"; + }; + [[nodiscard]] std::string replacement() const override; }; class InputMacroLastYearGanZhi : public InputMacro { public: - std::string name() const { return "MACRO@LAST_YEAR_GANZHI"; }; - std::string replacement() const; + [[nodiscard]] std::string name() const override { + return "MACRO@LAST_YEAR_GANZHI"; + }; + [[nodiscard]] std::string replacement() const override; }; class InputMacroNextYearGanZhi : public InputMacro { public: - std::string name() const { return "MACRO@NEXT_YEAR_GANZHI"; }; - std::string replacement() const; + [[nodiscard]] std::string name() const override { + return "MACRO@NEXT_YEAR_GANZHI"; + }; + [[nodiscard]] std::string replacement() const override; }; class InputMacroThisYearChineseZodiac : public InputMacro { public: - std::string name() const { return "MACRO@THIS_YEAR_CHINESE_ZODIAC"; }; - std::string replacement() const; + [[nodiscard]] std::string name() const override { + return "MACRO@THIS_YEAR_CHINESE_ZODIAC"; + }; + [[nodiscard]] std::string replacement() const override; }; class InputMacroLastYearChineseZodiac : public InputMacro { public: - std::string name() const { return "MACRO@LAST_YEAR_CHINESE_ZODIAC"; }; - std::string replacement() const; + [[nodiscard]] std::string name() const override { + return "MACRO@LAST_YEAR_CHINESE_ZODIAC"; + }; + [[nodiscard]] std::string replacement() const override; }; class InputMacroNextYearChineseZodiac : public InputMacro { public: - std::string name() const { return "MACRO@NEXT_YEAR_CHINESE_ZODIAC"; }; - std::string replacement() const; + [[nodiscard]] std::string name() const override { + return "MACRO@NEXT_YEAR_CHINESE_ZODIAC"; + }; + [[nodiscard]] std::string replacement() const override; }; static void AddMacro( @@ -171,9 +239,7 @@ InputMacroController::InputMacroController() { AddMacro(macros_, std::make_unique()); } -InputMacroController::~InputMacroController() {} - -std::string InputMacroController::handle(std::string input) { +std::string InputMacroController::handle(const std::string& input) const { const auto& it = macros_.find(input); if (it != macros_.cend()) { return it->second->replacement(); @@ -181,12 +247,12 @@ std::string InputMacroController::handle(std::string input) { return input; } -std::string formatDate(std::string calendarName, int DayOffset, - icu::DateFormat::EStyle dateStyle) { +static std::string FormatDate(const std::string& calendarName, int DayOffset, + icu::DateFormat::EStyle dateStyle) { UErrorCode status = U_ZERO_ERROR; icu::TimeZone* timezone = icu::TimeZone::createDefault(); std::string calendarNameBase = "zh_Hant_TW"; - if (calendarName.empty() == false) { + if (!calendarName.empty()) { calendarNameBase += "@calendar=" + calendarName; } @@ -209,54 +275,54 @@ std::string formatDate(std::string calendarName, int DayOffset, } std::string InputMacroDateTodayShort::replacement() const { - return formatDate("", 0, icu::DateFormat::EStyle::kShort); + return FormatDate("", 0, icu::DateFormat::EStyle::kShort); } std::string InputMacroDateTodayMedium::replacement() const { - return formatDate("", 0, icu::DateFormat::EStyle::kMedium); + return FormatDate("", 0, icu::DateFormat::EStyle::kMedium); } std::string InputMacroDateTodayMediumRoc::replacement() const { - return formatDate("roc", 0, icu::DateFormat::EStyle::kMedium); + return FormatDate("roc", 0, icu::DateFormat::EStyle::kMedium); } std::string InputMacroDateTodayMediumChinese::replacement() const { - return formatDate("chinese", 0, icu::DateFormat::EStyle::kMedium); + return FormatDate("chinese", 0, icu::DateFormat::EStyle::kMedium); } std::string InputMacroDateYesterdayShort::replacement() const { - return formatDate("", -1, icu::DateFormat::EStyle::kShort); + return FormatDate("", -1, icu::DateFormat::EStyle::kShort); } std::string InputMacroDateYesterdayMedium::replacement() const { - return formatDate("", -1, icu::DateFormat::EStyle::kMedium); + return FormatDate("", -1, icu::DateFormat::EStyle::kMedium); } std::string InputMacroDateYesterdayMediumRoc::replacement() const { - return formatDate("roc", -1, icu::DateFormat::EStyle::kMedium); + return FormatDate("roc", -1, icu::DateFormat::EStyle::kMedium); } std::string InputMacroDateYesterdayMediumChinese::replacement() const { - return formatDate("chinese", -1, icu::DateFormat::EStyle::kMedium); + return FormatDate("chinese", -1, icu::DateFormat::EStyle::kMedium); } std::string InputMacroDateTomorrowShort::replacement() const { - return formatDate("", 1, icu::DateFormat::EStyle::kShort); + return FormatDate("", 1, icu::DateFormat::EStyle::kShort); } std::string InputMacroDateTomorrowMedium::replacement() const { - return formatDate("", 1, icu::DateFormat::EStyle::kMedium); + return FormatDate("", 1, icu::DateFormat::EStyle::kMedium); } std::string InputMacroDateTomorrowMediumRoc::replacement() const { - return formatDate("roc", 1, icu::DateFormat::EStyle::kMedium); + return FormatDate("roc", 1, icu::DateFormat::EStyle::kMedium); } std::string InputMacroDateTomorrowMediumChinese::replacement() const { - return formatDate("chinese", 1, icu::DateFormat::EStyle::kMedium); + return FormatDate("chinese", 1, icu::DateFormat::EStyle::kMedium); } -std::string formatTime(icu::DateFormat::EStyle timeStyle) { +static std::string FormatTime(icu::DateFormat::EStyle timeStyle) { UErrorCode status = U_ZERO_ERROR; icu::TimeZone* timezone = icu::TimeZone::createDefault(); std::string calendarNameBase = "zh_Hant_TW"; @@ -278,14 +344,14 @@ std::string formatTime(icu::DateFormat::EStyle timeStyle) { } std::string InputMacroDateTimeNowShort::replacement() const { - return formatTime(icu::DateFormat::EStyle::kShort); + return FormatTime(icu::DateFormat::EStyle::kShort); } std::string InputMacroDateTimeNowMedium::replacement() const { - return formatTime(icu::DateFormat::EStyle::kMedium); + return FormatTime(icu::DateFormat::EStyle::kMedium); } -std::string formatTimeZone(icu::TimeZone::EDisplayType type) { +static std::string FormatTimeZone(icu::TimeZone::EDisplayType type) { icu::TimeZone* timezone = icu::TimeZone::createDefault(); const icu::Locale locale = icu::Locale::createCanonical("zh_Hant_TW"); icu::UnicodeString formatted; @@ -297,14 +363,14 @@ std::string formatTimeZone(icu::TimeZone::EDisplayType type) { } std::string InputMacroTimeZoneStandard::replacement() const { - return formatTimeZone(icu::TimeZone::EDisplayType::LONG_GENERIC); + return FormatTimeZone(icu::TimeZone::EDisplayType::LONG_GENERIC); } std::string InputMacroTimeZoneShortGeneric::replacement() const { - return formatTimeZone(icu::TimeZone::EDisplayType::SHORT_GENERIC); + return FormatTimeZone(icu::TimeZone::EDisplayType::SHORT_GENERIC); } -int currentYear() { +static int GetCurrentYear() { UErrorCode status = U_ZERO_ERROR; icu::GregorianCalendar calendar(icu::TimeZone::createDefault(), status); calendar.setTime(icu::Calendar::getNow(), status); @@ -312,7 +378,7 @@ int currentYear() { return year; } -int getYearBase(int year) { +static int GetYearBase(int year) { if (year < 4) { year = year * -1; return 60 - (year + 2) % 60; @@ -320,56 +386,56 @@ int getYearBase(int year) { return (year - 3) % 60; } -std::string ganzhi(int year) { +static std::string GetGanzhi(int year) { const std::vector gan( {"癸", "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬"}); const std::vector zhi( {"亥", "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌"}); - int base = getYearBase(year); + int base = GetYearBase(year); int ganIndex = base % 10; int zhiIndex = base % 12; return gan[ganIndex] + zhi[zhiIndex] + "年"; } -std::string chineseZodiac(int year) { +static std::string GetChineseZodiac(int year) { const std::vector gan( {"水", "木", "木", "火", "火", "土", "土", "金", "金", "水"}); const std::vector zhi( {"豬", "鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊", "猴", "雞", "狗"}); - int base = getYearBase(year); + int base = GetYearBase(year); int ganIndex = base % 10; int zhiIndex = base % 12; return gan[ganIndex] + zhi[zhiIndex] + "年"; } std::string InputMacroThisYearGanZhi::replacement() const { - int year = currentYear(); - return ganzhi(year); + int year = GetCurrentYear(); + return GetGanzhi(year); } std::string InputMacroLastYearGanZhi::replacement() const { - int year = currentYear(); - return ganzhi(year - 1); + int year = GetCurrentYear(); + return GetGanzhi(year - 1); } std::string InputMacroNextYearGanZhi::replacement() const { - int year = currentYear(); - return ganzhi(year + 1); + int year = GetCurrentYear(); + return GetGanzhi(year + 1); } std::string InputMacroThisYearChineseZodiac::replacement() const { - int year = currentYear(); - return chineseZodiac(year); + int year = GetCurrentYear(); + return GetChineseZodiac(year); } std::string InputMacroLastYearChineseZodiac::replacement() const { - int year = currentYear(); - return chineseZodiac(year - 1); + int year = GetCurrentYear(); + return GetChineseZodiac(year - 1); } std::string InputMacroNextYearChineseZodiac::replacement() const { - int year = currentYear(); - return chineseZodiac(year + 1); + int year = GetCurrentYear(); + return GetChineseZodiac(year + 1); } } // namespace McBopomofo diff --git a/src/InputMacro.h b/src/InputMacro.h index 7215f85..9be8370 100644 --- a/src/InputMacro.h +++ b/src/InputMacro.h @@ -1,5 +1,28 @@ -#ifndef INPUTMACRO -#define INPUTMACRO +// Copyright (c) 2023 and onwards The McBopomofo Authors. +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. + +#ifndef SRC_INPUTMACRO_H_ +#define SRC_INPUTMACRO_H_ #include #include @@ -8,15 +31,15 @@ namespace McBopomofo { class InputMacro { public: - virtual std::string name() const = 0; - virtual std::string replacement() const = 0; + virtual ~InputMacro() = default; + [[nodiscard]] virtual std::string name() const = 0; + [[nodiscard]] virtual std::string replacement() const = 0; }; class InputMacroController { public: InputMacroController(); - ~InputMacroController(); - std::string handle(std::string input); + std::string handle(const std::string& input) const; private: std::unordered_map> macros_; @@ -24,4 +47,4 @@ class InputMacroController { } // namespace McBopomofo -#endif /* INPUTMACRO */ +#endif // SRC_INPUTMACRO_H_ diff --git a/src/LanguageModelLoader.cpp b/src/LanguageModelLoader.cpp index e217e13..698c43e 100644 --- a/src/LanguageModelLoader.cpp +++ b/src/LanguageModelLoader.cpp @@ -52,7 +52,7 @@ LanguageModelLoader::LanguageModelLoader( } FCITX_MCBOPOMOFO_INFO() << "Set macro converter"; - auto converter = [this](std::string input) { + auto converter = [this](const std::string& input) { return this->inputMacroController_.handle(input); }; lm_->setMacroConverter(converter); @@ -60,7 +60,7 @@ LanguageModelLoader::LanguageModelLoader( std::string userDataPath = fcitx::StandardPath::global().userDirectory( fcitx::StandardPath::Type::PkgData); - // fcitx5 is configured to not to provide userDataPath, bail. + // fcitx5 is configured not to provide userDataPath, bail. if (userDataPath.empty()) { return; }