Skip to content

Commit

Permalink
fix or suppress code analyzer warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
smdn committed Feb 12, 2024
1 parent e8d547f commit 86270f8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ SPDX-License-Identifier: MIT
<ItemGroup>
<PackageReference
Include="Smdn.MSBuild.DefineConstants.NETSdkApi"
Version="1.3.6"
Version="1.4.4"
PrivateAssets="all"
IncludeAssets="build"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public int Compare(string? x, string? y)
}
}

private static readonly char[] dictionaryPunctuationChars = new[] { '!', '?', '!', '?', '、', '。' };
private static readonly char[] DictionaryPunctuationChars = new[] { '!', '?', '!', '?', '、', '。' };

private static SortedList<string, string> LoadDictionary(Stream stream)
{
Expand All @@ -46,15 +46,15 @@ private static SortedList<string, string> LoadDictionary(Stream stream)
if (entry.StartsWith('#'))
continue; // comment line

var key = entries[1].Trim().RemoveChars(dictionaryPunctuationChars);
var key = entries[1].Trim().RemoveChars(DictionaryPunctuationChars);

dictionary[KanaUtils.ConvertWideHiraganaToKatakana(key).ToLowerInvariant()] = entries[2].Trim().ToLowerInvariant();
}

return dictionary;
}

private static readonly IReadOnlyDictionary<string, string> phonemeDictionary =
private static readonly IReadOnlyDictionary<string, string> PhonemeDictionary =
new ReadOnlyOrderedDictionary<string, string>(
new[] {
// 最優先
Expand Down
25 changes: 18 additions & 7 deletions src/Smdn.Text.Ondulish/Smdn.Text.Ondulish/Translator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,24 @@ bool shouldDisposeTagger

PhraseDictionary = LoadDictionary(stream);
}
#pragma warning disable CA1031
catch {
// ignore exceptions
// ignore any exceptions
PhraseDictionary = CreateEmptyDictionary();
}
#pragma warning restore CA1031

try {
using var stream = OndulishDictionaries.OpenWordDictionaryStream();

WordDictionary = LoadDictionary(stream);
}
#pragma warning disable CA1031
catch {
// ignore exceptions
// ignore any exceptions
WordDictionary = CreateEmptyDictionary();
}
#pragma warning restore CA1031

static IReadOnlyDictionary<string, string> CreateEmptyDictionary()
=> Enumerable.Empty<(string Key, string Value)>().ToDictionary(static pair => pair.Key, static pair => pair.Value);
Expand Down Expand Up @@ -114,10 +118,11 @@ public string Translate(
return string.Empty;

var sb = new StringBuilder(input.Length * 2);
using var writer = new StringWriter(sb);

Translate(
input: new StringReader(input),
output: new StringWriter(sb),
output: writer,
convertKatakanaToNarrow: convertKatakanaToNarrow
);

Expand Down Expand Up @@ -173,7 +178,7 @@ f.ConvertedText is null
)
.SelectMany(f =>
f.ConvertedText is null
? ConvertWithDictionary(f.SourceText, phonemeDictionary)
? ConvertWithDictionary(f.SourceText, PhonemeDictionary)
: Enumerable.Repeat(f, 1)
)
.Select(static f =>
Expand Down Expand Up @@ -202,19 +207,25 @@ f.ConvertedText is null
output.Flush();
}

private static readonly char[] featureSplitter = new[] { ',' };
private static readonly char[] FeatureSplitter = { ',' };

private string ConvertToKatakana(string input)
{
input = input.Replace(",", ""); // XXX: feature splitter
// XXX: feature splitter
input = input
#if SYSTEM_STRING_REPLACE_STRING_STRING_STRINGCOMPARISON
.Replace(",", "", StringComparison.Ordinal);
#else
.Replace(",", "");
#endif

var ret = new StringBuilder(input.Length * 2);

for (var node = tagger!.parseToNode(input); node != null; node = node.next) {
if (node.stat == MeCabConsts.MECAB_BOS_NODE || node.stat == MeCabConsts.MECAB_EOS_NODE)
continue;

var featureEntries = node.feature.Split(featureSplitter);
var featureEntries = node.feature.Split(FeatureSplitter);

if (8 <= featureEntries.Length) {
switch (featureEntries[6]) {
Expand Down

0 comments on commit 86270f8

Please sign in to comment.