From 6d69ca3637fc74d98cd693e9fdf667ec7da8e07a Mon Sep 17 00:00:00 2001 From: Lukhnos Liu Date: Thu, 14 Jul 2022 18:55:57 -0700 Subject: [PATCH] Sync engine source with fcitx5-mcbopomofo --- Source/Engine/AssociatedPhrases.cpp | 23 +++++++++++------------ Source/Engine/AssociatedPhrases.h | 23 +++++++++++++---------- Source/Engine/CMakeLists.txt | 2 ++ Source/Engine/Mandarin/.clang-format | 1 + Source/Engine/Mandarin/Mandarin.cpp | 9 +++------ Source/Engine/Mandarin/Mandarin.h | 21 ++++++++++----------- 6 files changed, 40 insertions(+), 39 deletions(-) create mode 120000 Source/Engine/Mandarin/.clang-format diff --git a/Source/Engine/AssociatedPhrases.cpp b/Source/Engine/AssociatedPhrases.cpp index 7231fad5b..5b4082320 100644 --- a/Source/Engine/AssociatedPhrases.cpp +++ b/Source/Engine/AssociatedPhrases.cpp @@ -27,10 +27,10 @@ #include "AssociatedPhrases.h" -#include -#include #include #include +#include +#include #include #include "KeyValueBlobReader.h" @@ -38,9 +38,9 @@ namespace McBopomofo { AssociatedPhrases::AssociatedPhrases() -: fd(-1) -, data(0) -, length(0) + : fd(-1) + , data(0) + , length(0) { } @@ -51,7 +51,7 @@ AssociatedPhrases::~AssociatedPhrases() } } -const bool AssociatedPhrases::isLoaded() +bool AssociatedPhrases::isLoaded() { if (data) { return true; @@ -59,7 +59,7 @@ const bool AssociatedPhrases::isLoaded() return false; } -bool AssociatedPhrases::open(const char *path) +bool AssociatedPhrases::open(const char* path) { if (data) { return false; @@ -87,8 +87,7 @@ bool AssociatedPhrases::open(const char *path) KeyValueBlobReader reader(static_cast(data), length); KeyValueBlobReader::KeyValue keyValue; - KeyValueBlobReader::State state; - while ((state = reader.Next(&keyValue)) == KeyValueBlobReader::State::HAS_PAIR) { + while (reader.Next(&keyValue) == KeyValueBlobReader::State::HAS_PAIR) { keyRowMap[keyValue.key].emplace_back(keyValue.key, keyValue.value); } return true; @@ -113,15 +112,15 @@ const std::vector AssociatedPhrases::valuesForKey(const std::string const std::vector& rows = iter->second; for (const auto& row : rows) { std::string_view value = row.value; - v.push_back({value.data(), value.size()}); + v.push_back({ value.data(), value.size() }); } } return v; } -const bool AssociatedPhrases::hasValuesForKey(const std::string& key) +bool AssociatedPhrases::hasValuesForKey(const std::string& key) { return keyRowMap.find(key) != keyRowMap.end(); } -}; // namespace McBopomofo +} // namespace McBopomofo diff --git a/Source/Engine/AssociatedPhrases.h b/Source/Engine/AssociatedPhrases.h index 17827b716..49a5c702d 100644 --- a/Source/Engine/AssociatedPhrases.h +++ b/Source/Engine/AssociatedPhrases.h @@ -28,28 +28,31 @@ #ifndef ASSOCIATEDPHRASES_H #define ASSOCIATEDPHRASES_H -#include -#include #include +#include +#include #include namespace McBopomofo { -class AssociatedPhrases -{ +class AssociatedPhrases { public: AssociatedPhrases(); ~AssociatedPhrases(); - const bool isLoaded(); - bool open(const char *path); + bool isLoaded(); + bool open(const char* path); void close(); const std::vector valuesForKey(const std::string& key); - const bool hasValuesForKey(const std::string& key); + bool hasValuesForKey(const std::string& key); protected: struct Row { - Row(std::string_view& k, std::string_view& v) : key(k), value(v) {} + Row(std::string_view& k, std::string_view& v) + : key(k) + , value(v) + { + } std::string_view key; std::string_view value; }; @@ -57,10 +60,10 @@ class AssociatedPhrases std::map> keyRowMap; int fd; - void *data; + void* data; size_t length; }; } -#endif /* AssociatedPhrases_hpp */ +#endif // ASSOCIATEDPHRASES_H diff --git a/Source/Engine/CMakeLists.txt b/Source/Engine/CMakeLists.txt index d32d6cb82..7e756665d 100644 --- a/Source/Engine/CMakeLists.txt +++ b/Source/Engine/CMakeLists.txt @@ -7,6 +7,8 @@ add_subdirectory(gramambular2) include_directories("Gramambular") add_library(McBopomofoLMLib + AssociatedPhrases.h + AssociatedPhrases.cpp KeyValueBlobReader.cpp KeyValueBlobReader.h ParselessPhraseDB.cpp diff --git a/Source/Engine/Mandarin/.clang-format b/Source/Engine/Mandarin/.clang-format new file mode 120000 index 000000000..3260daf75 --- /dev/null +++ b/Source/Engine/Mandarin/.clang-format @@ -0,0 +1 @@ +../../../.clang-format \ No newline at end of file diff --git a/Source/Engine/Mandarin/Mandarin.cpp b/Source/Engine/Mandarin/Mandarin.cpp index c5f760395..b1a97ee57 100644 --- a/Source/Engine/Mandarin/Mandarin.cpp +++ b/Source/Engine/Mandarin/Mandarin.cpp @@ -31,8 +31,7 @@ namespace Mandarin { class PinyinParseHelper { public: - static const bool ConsumePrefix(std::string& target, - const std::string& prefix) { + static bool ConsumePrefix(std::string& target, const std::string& prefix) { if (target.length() < prefix.length()) { return false; } @@ -601,10 +600,10 @@ const BPMF BPMF::FromComposedString(const std::string& str) { const std::string BPMF::composedString() const { std::string result; #define APPEND(c) \ - if (syllable_ & c) \ + if (syllable_ & c) \ result += \ (*BopomofoCharacterMap::SharedInstance().componentToCharacter.find( \ - syllable_ & c)) \ + syllable_ & c)) \ .second APPEND(ConsonantMask); APPEND(MiddleVowelMask); @@ -614,8 +613,6 @@ const std::string BPMF::composedString() const { return result; } - - const BopomofoCharacterMap& BopomofoCharacterMap::SharedInstance() { static BopomofoCharacterMap* map = new BopomofoCharacterMap(); return *map; diff --git a/Source/Engine/Mandarin/Mandarin.h b/Source/Engine/Mandarin/Mandarin.h index 2d267ac28..14a320f8a 100644 --- a/Source/Engine/Mandarin/Mandarin.h +++ b/Source/Engine/Mandarin/Mandarin.h @@ -67,9 +67,7 @@ class BopomofoSyllable { Component consonantComponent() const { return syllable_ & ConsonantMask; } - Component middleVowelComponent() const { - return syllable_ & MiddleVowelMask; - } + Component middleVowelComponent() const { return syllable_ & MiddleVowelMask; } Component vowelComponent() const { return syllable_ & VowelMask; } @@ -113,8 +111,8 @@ class BopomofoSyllable { const BopomofoSyllable operator+(const BopomofoSyllable& another) const { Component newSyllable = syllable_; -#define OP_SOVER(mask) \ - if (another.syllable_ & mask) { \ +#define OP_SOVER(mask) \ + if (another.syllable_ & mask) { \ newSyllable = (newSyllable & ~mask) | (another.syllable_ & mask); \ } OP_SOVER(ConsonantMask); @@ -126,8 +124,8 @@ class BopomofoSyllable { } BopomofoSyllable& operator+=(const BopomofoSyllable& another) { -#define OPE_SOVER(mask) \ - if (another.syllable_ & mask) { \ +#define OPE_SOVER(mask) \ + if (another.syllable_ & mask) { \ syllable_ = (syllable_ & ~mask) | (another.syllable_ & mask); \ } OPE_SOVER(ConsonantMask); @@ -183,7 +181,7 @@ class BopomofoKeyboardLayout { BopomofoKeyboardLayout(const BopomofoKeyToComponentMap& ktcm, const std::string& name) - : m_keyToComponent(ktcm), m_name(name) { + : m_name(name), m_keyToComponent(ktcm) { for (BopomofoKeyToComponentMap::const_iterator miter = m_keyToComponent.begin(); miter != m_keyToComponent.end(); ++miter) @@ -382,6 +380,8 @@ class BopomofoReadingBuffer { } } + const BopomofoKeyboardLayout* keyboardLayout() const { return layout_; } + bool isValidKey(char k) const { if (!pinyin_mode_) { return layout_ ? (layout_->keyToComponents(k)).size() > 0 : false; @@ -468,11 +468,10 @@ class BopomofoReadingBuffer { bool hasToneMarkerOnly() const { return syllable_.hasToneMarker() && - !(syllable_.hasConsonant() || syllable_.hasMiddleVowel() || - syllable_.hasVowel()); + !(syllable_.hasConsonant() || syllable_.hasMiddleVowel() || + syllable_.hasVowel()); } - protected: const BopomofoKeyboardLayout* layout_; BPMF syllable_;