From 691cda5b223133396a53246c14f613ff04542628 Mon Sep 17 00:00:00 2001 From: Kevin Backhouse Date: Wed, 5 Feb 2020 11:28:46 +0000 Subject: [PATCH] Make initialization of static variable thread safe. --- spellcheck/spellcheck_utils.cpp | 34 +++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/spellcheck/spellcheck_utils.cpp b/spellcheck/spellcheck_utils.cpp index 81efbd1..5749b3b 100644 --- a/spellcheck/spellcheck_utils.cpp +++ b/spellcheck/spellcheck_utils.cpp @@ -222,9 +222,11 @@ QChar::Script WordScript(const QStringRef &word) { : firstLetter->script(); } -bool IsWordSkippable(const QStringRef &word) { - static auto systemScripts = std::vector(); - if (!systemScripts.size()) { +class SystemScripts { + std::vector systemScripts; +public: + + SystemScripts() { std::vector languages; Platform::Spellchecker::KnownLanguages(&languages); systemScripts = ( @@ -238,16 +240,24 @@ bool IsWordSkippable(const QStringRef &word) { systemScripts = { QChar::Script_Common }; } } - const auto wordScript = WordScript(word); - if (ranges::find(systemScripts, wordScript) == end(systemScripts)) { - return true; + + bool IsWordSkippable(const QStringRef &word) const { + const auto wordScript = WordScript(word); + if (ranges::find(systemScripts, wordScript) == end(systemScripts)) { + return true; + } + return ranges::find_if(word, [&](QChar c) { + return (c.script() != wordScript) + && !IsAcuteAccentChar(c) + && (c.unicode() != '\'') // Patched Qt to make it a non-separator. + && (c.unicode() != '_'); // This is not a word separator. + }) != word.end(); } - return ranges::find_if(word, [&](QChar c) { - return (c.script() != wordScript) - && !IsAcuteAccentChar(c) - && (c.unicode() != '\'') // Patched Qt to make it a non-separator. - && (c.unicode() != '_'); // This is not a word separator. - }) != word.end(); +}; + +bool IsWordSkippable(const QStringRef &word) { + static SystemScripts systemScripts; + return systemScripts.IsWordSkippable(word); } MisspelledWords RangesFromText(