From 3d55ad783eda0b9b2624b833d87f259bf401c9a6 Mon Sep 17 00:00:00 2001 From: SysC0mp Date: Tue, 3 Dec 2019 14:55:22 +0100 Subject: [PATCH] Only pass needed setting to Alphabet As Alphabet class is static, its methods could be used without ever calling Alphabet.initialize(_settings) beforehand which would end in an exception. Therefor only _shouldUsePinyin settings needed will be transferred with a given default value. --- Wox.Infrastructure/Alphabet.cs | 74 +++++++++++++++++++--------------- Wox/App.xaml.cs | 4 +- 2 files changed, 44 insertions(+), 34 deletions(-) diff --git a/Wox.Infrastructure/Alphabet.cs b/Wox.Infrastructure/Alphabet.cs index d7fae882cb..a2f64241a4 100644 --- a/Wox.Infrastructure/Alphabet.cs +++ b/Wox.Infrastructure/Alphabet.cs @@ -15,17 +15,26 @@ namespace Wox.Infrastructure private static readonly HanyuPinyinOutputFormat Format = new HanyuPinyinOutputFormat(); private static ConcurrentDictionary PinyinCache; private static BinaryStorage> _pinyinStorage; - private static Settings _settings; - - public static void Initialize(Settings settings) + private static bool _shouldUsePinyin = true; + + public static void Initialize(bool shouldUsePinyin = true) + { + _shouldUsePinyin = shouldUsePinyin; + if (_shouldUsePinyin) + { + InitializePinyinHelpers(); + } + } + + private static void InitializePinyinHelpers() { - _settings = settings; Format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); Stopwatch.Normal("|Wox.Infrastructure.Alphabet.Initialize|Preload pinyin cache", () => { _pinyinStorage = new BinaryStorage>("Pinyin"); PinyinCache = _pinyinStorage.TryLoad(new ConcurrentDictionary()); + // force pinyin library static constructor initialize PinyinHelper.toHanyuPinyinStringArray('T', Format); }); @@ -34,6 +43,10 @@ namespace Wox.Infrastructure public static void Save() { + if (!_shouldUsePinyin) + { + return; + } _pinyinStorage.Save(PinyinCache); } @@ -46,7 +59,7 @@ namespace Wox.Infrastructure /// public static string[] Pinyin(string word) { - if (!_settings.ShouldUsePinyin) + if (!_shouldUsePinyin) { return EmptyStringArray; } @@ -68,39 +81,36 @@ namespace Wox.Infrastructure /// public static string[][] PinyinComination(string characters) { - if (_settings.ShouldUsePinyin && !string.IsNullOrEmpty(characters)) + if (!_shouldUsePinyin || string.IsNullOrEmpty(characters)) { - if (!PinyinCache.ContainsKey(characters)) - { + return Empty2DStringArray; + } - var allPinyins = new List(); - foreach (var c in characters) + if (!PinyinCache.ContainsKey(characters)) + { + var allPinyins = new List(); + foreach (var c in characters) + { + var pinyins = PinyinHelper.toHanyuPinyinStringArray(c, Format); + if (pinyins != null) { - var pinyins = PinyinHelper.toHanyuPinyinStringArray(c, Format); - if (pinyins != null) - { - var r = pinyins.Distinct().ToArray(); - allPinyins.Add(r); - } - else - { - var r = new[] { c.ToString() }; - allPinyins.Add(r); - } + var r = pinyins.Distinct().ToArray(); + allPinyins.Add(r); } + else + { + var r = new[] { c.ToString() }; + allPinyins.Add(r); + } + } - var combination = allPinyins.Aggregate(Combination).Select(c => c.Split(';')).ToArray(); - PinyinCache[characters] = combination; - return combination; - } - else - { - return PinyinCache[characters]; - } + var combination = allPinyins.Aggregate(Combination).Select(c => c.Split(';')).ToArray(); + PinyinCache[characters] = combination; + return combination; } else { - return Empty2DStringArray; + return PinyinCache[characters]; } } @@ -112,7 +122,7 @@ namespace Wox.Infrastructure public static bool ContainsChinese(string word) { - if (!_settings.ShouldUsePinyin) + if (!_shouldUsePinyin) { return false; } @@ -130,7 +140,7 @@ namespace Wox.Infrastructure private static string[] Combination(string[] array1, string[] array2) { - if (!_settings.ShouldUsePinyin) + if (!_shouldUsePinyin) { return EmptyStringArray; } diff --git a/Wox/App.xaml.cs b/Wox/App.xaml.cs index 0ec1614bf9..acfb88d266 100644 --- a/Wox/App.xaml.cs +++ b/Wox/App.xaml.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Diagnostics; using System.Threading.Tasks; using System.Timers; @@ -53,7 +53,7 @@ namespace Wox _settingsVM = new SettingWindowViewModel(); _settings = _settingsVM.Settings; - Alphabet.Initialize(_settings); + Alphabet.Initialize(_settings.ShouldUsePinyin); StringMatcher.UserSettingSearchPrecision = _settings.QuerySearchPrecision;