refactor: Replace cached hyphenator function with static member variable

This commit is contained in:
Arthur Tazhitdinov 2026-01-14 18:11:19 +05:00
parent 85a737fd82
commit 268d215cbb
2 changed files with 11 additions and 8 deletions

View File

@ -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<CodepointInfo>& 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::BreakInfo> 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::BreakInfo> 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);
}

View File

@ -4,6 +4,8 @@
#include <string>
#include <vector>
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_;
};