From 268d215cbbfee27f2522fb999073ba6540b596b6 Mon Sep 17 00:00:00 2001 From: Arthur Tazhitdinov Date: Wed, 14 Jan 2026 18:11:19 +0500 Subject: [PATCH] refactor: Replace cached hyphenator function with static member variable --- lib/Epub/Epub/hyphenation/Hyphenator.cpp | 14 ++++++-------- lib/Epub/Epub/hyphenation/Hyphenator.h | 5 +++++ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/Epub/Epub/hyphenation/Hyphenator.cpp b/lib/Epub/Epub/hyphenation/Hyphenator.cpp index d6436712..e53f6366 100644 --- a/lib/Epub/Epub/hyphenation/Hyphenator.cpp +++ b/lib/Epub/Epub/hyphenation/Hyphenator.cpp @@ -8,6 +8,8 @@ #include "HyphenationCommon.h" #include "LanguageRegistry.h" +const LanguageHyphenator* Hyphenator::cachedHyphenator_ = nullptr; + namespace { // Maps a BCP-47 language tag to a language-specific hyphenator. @@ -27,12 +29,6 @@ const LanguageHyphenator* hyphenatorForLanguage(const std::string& langTag) { return getLanguageHyphenatorForPrimaryTag(primary); } -// Cached hyphenator instance for the current preferred language. -const LanguageHyphenator*& cachedHyphenator() { - static const LanguageHyphenator* hyphenator = nullptr; - return hyphenator; -} - // Maps a codepoint index back to its byte offset inside the source word. size_t byteOffsetForIndex(const std::vector& cps, const size_t index) { return (index < cps.size()) ? cps[index].byteOffset : (cps.empty() ? 0 : cps.back().byteOffset); @@ -88,7 +84,7 @@ std::vector Hyphenator::breakOffsets(const std::string& w auto cps = collectCodepoints(word); trimSurroundingPunctuation(cps); trimTrailingFootnoteReference(cps); - const auto* hyphenator = cachedHyphenator(); + const auto* hyphenator = cachedHyphenator_; const size_t minPrefix = hyphenator ? hyphenator->minPrefix() : LiangWordConfig::kDefaultMinPrefix; const size_t minSuffix = hyphenator ? hyphenator->minSuffix() : LiangWordConfig::kDefaultMinSuffix; @@ -127,4 +123,6 @@ std::vector Hyphenator::breakOffsets(const std::string& w return breaks; } -void Hyphenator::setPreferredLanguage(const std::string& lang) { cachedHyphenator() = hyphenatorForLanguage(lang); } +void Hyphenator::setPreferredLanguage(const std::string& lang) { + cachedHyphenator_ = hyphenatorForLanguage(lang); +} diff --git a/lib/Epub/Epub/hyphenation/Hyphenator.h b/lib/Epub/Epub/hyphenation/Hyphenator.h index 992d1dd5..ffbe16fa 100644 --- a/lib/Epub/Epub/hyphenation/Hyphenator.h +++ b/lib/Epub/Epub/hyphenation/Hyphenator.h @@ -4,6 +4,8 @@ #include #include +class LanguageHyphenator; + class Hyphenator { public: struct BreakInfo { @@ -16,4 +18,7 @@ class Hyphenator { // Provide a publication-level language hint (e.g. "en", "en-US", "ru") used to select hyphenation rules. static void setPreferredLanguage(const std::string& lang); + + private: + static const LanguageHyphenator* cachedHyphenator_; }; \ No newline at end of file