Skip to content

Commit

Permalink
fix warnings proposed from code analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
smdn committed Dec 23, 2022
1 parent 12f3fc8 commit 5f087da
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
37 changes: 17 additions & 20 deletions src/Smdn.Text.Ondulish/Smdn.Text.Ondulish/KanaUtils.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
// SPDX-FileCopyrightText: 2012 smdn <smdn@smdn.jp>
// SPDX-License-Identifier: MIT

using System;
using System.Text;

namespace Smdn.Text.Ondulish;

public static class KanaUtils {
private const char wideHiraganaStart = '\u3041';
private const char wideHiraganaEnd = '\u3096';
private const char WideHiraganaStart = '\u3041';
private const char WideHiraganaEnd = '\u3096';

private const char wideKatakanaStart = '\u30a1';
private const char wideKatakanaEnd = '\u30f6';
private const char WideKatakanaStart = '\u30a1';
private const char WideKatakanaEnd = '\u30f6';

private const int offsetFromHiraganaToKatakana = ((int)wideKatakanaStart - (int)wideHiraganaStart);
private const int OffsetFromHiraganaToKatakana = WideKatakanaStart - WideHiraganaStart;

private const char wideKatakanaExEnd = '\u30fa';
private const char WideKatakanaExEnd = '\u30fa';

private static readonly string[] wideToNarrowKatakanaMap = new[] {
private static readonly string[] WideToNarrowKatakanaMap = new[] {
"", "", "", "", "", "", "", "", "", "", "", "ガ", "", "ギ", "", // 30A1 - 30AF
"グ", "", "ゲ", "", "ゴ", "", "ザ", "", "ジ", "", "ズ", "", "ゼ", "ソ", "ゾ", "", // 30B0 - 30BF
"ダ", "", "ヂ", "", "", "ヅ", "", "デ", "", "ド", "", "", "", "", "", "", // 30C0 - 30CF
Expand All @@ -33,7 +32,7 @@ public static string ConvertWideHiraganaToKatakana(string input)
var outputChars = new char[inputChars.Length];

for (var index = 0; index < inputChars.Length; index++) {
if (wideHiraganaStart <= inputChars[index] && inputChars[index] <= wideHiraganaEnd)
if (WideHiraganaStart <= inputChars[index] && inputChars[index] <= WideHiraganaEnd)
outputChars[index] = (char)((int)inputChars[index] + offsetFromHiraganaToKatakana);
else
outputChars[index] = inputChars[index];
Expand All @@ -43,10 +42,9 @@ public static string ConvertWideHiraganaToKatakana(string input)
#else
return string.Create(input.Length, input, (chars, s) => {
for (var index = 0; index < chars.Length; index++) {
if (wideHiraganaStart <= s[index] && s[index] <= wideHiraganaEnd)
chars[index] = (char)(s[index] + offsetFromHiraganaToKatakana);
else
chars[index] = s[index];
chars[index] = s[index] is >= WideHiraganaStart and <= WideHiraganaEnd
? (char)(s[index] + OffsetFromHiraganaToKatakana)
: s[index];
}
});
#endif
Expand All @@ -59,7 +57,7 @@ public static string ConvertWideKatakanaToHiragana(string input)
var outputChars = new char[inputChars.Length];

for (var index = 0; index < inputChars.Length; index++) {
if (wideKatakanaStart <= inputChars[index] && inputChars[index] <= wideKatakanaEnd)
if (WideKatakanaStart <= inputChars[index] && inputChars[index] <= WideKatakanaEnd)
outputChars[index] = (char)((int)inputChars[index] - offsetFromHiraganaToKatakana);
else
outputChars[index] = inputChars[index];
Expand All @@ -69,10 +67,9 @@ public static string ConvertWideKatakanaToHiragana(string input)
#else
return string.Create(input.Length, input, (chars, s) => {
for (var index = 0; index < chars.Length; index++) {
if (wideKatakanaStart <= s[index] && s[index] <= wideKatakanaEnd)
chars[index] = (char)(s[index] - offsetFromHiraganaToKatakana);
else
chars[index] = s[index];
chars[index] = s[index] is >= WideKatakanaStart and <= WideKatakanaEnd
? (char)(s[index] - OffsetFromHiraganaToKatakana)
: s[index];
}
});
#endif
Expand All @@ -84,8 +81,8 @@ public static string ConvertWideKatakanaToNarrowKatakana(string input)
var output = new StringBuilder();

for (var index = 0; index < inputChars.Length; index++) {
if (wideKatakanaStart <= inputChars[index] && inputChars[index] <= wideKatakanaExEnd)
output.Append(wideToNarrowKatakanaMap[inputChars[index] - wideKatakanaStart]);
if (inputChars[index] is >= WideKatakanaStart and <= WideKatakanaExEnd)
output.Append(WideToNarrowKatakanaMap[inputChars[index] - WideKatakanaStart]);
else if (inputChars[index] == 'ー')
output.Append('ー');
else if (inputChars[index] == '゛')
Expand Down
26 changes: 14 additions & 12 deletions src/Smdn.Text.Ondulish/Smdn.Text.Ondulish/Translator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
// SPDX-License-Identifier: MIT

using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using MeCab;

using Smdn.Formats.Csv;

using MeCab;
using MeCabConsts = MeCab.MeCab;

namespace Smdn.Text.Ondulish;
Expand All @@ -29,7 +30,7 @@ public Translator(string taggerArgs, string dictionaryDirectory)
WordDictionary = LoadDictionary(System.IO.Path.Combine(dictionaryDirectory, "dictionary-words.csv"));
}

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

private static SortedList<string, string> LoadDictionary(string dictionaryPath)
{
Expand Down Expand Up @@ -115,13 +116,14 @@ public void Translate(string input, bool convertKatakanaToNarrow, TextWriter out
)
);

if (convertKatakanaToNarrow)
fragments = fragments.Select(f =>
if (convertKatakanaToNarrow) {
fragments = fragments.Select(static f =>
new TextFragment(
f.SourceText,
KanaUtils.ConvertWideKatakanaToNarrowKatakana(f.ConvertedText)
)
);
}

output.WriteLine(
string.Concat(
Expand All @@ -133,7 +135,7 @@ public void Translate(string input, bool convertKatakanaToNarrow, TextWriter out
output.Flush();
}

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

private string ConvertToKatakana(string input)
{
Expand Down Expand Up @@ -169,14 +171,14 @@ private string ConvertToKatakana(string input)
return ret.ToString();
}

readonly struct TextFragment {
private readonly struct TextFragment {
public readonly string SourceText;
public readonly string ConvertedText;

public TextFragment(string sourceText, string convertedText)
{
this.SourceText = sourceText;
this.ConvertedText = convertedText;
SourceText = sourceText;
ConvertedText = convertedText;
}
}

Expand Down Expand Up @@ -230,10 +232,10 @@ private class ReadOnlyOrderedDictionary<TKey, TValue> : IReadOnlyDictionary<TKey
public IEnumerable<TValue> Values => throw new NotImplementedException();
public int Count => dictionary.Count;

public ReadOnlyOrderedDictionary(IEnumerable<(TKey key, TValue value)> dictionary)
public ReadOnlyOrderedDictionary(IEnumerable<(TKey Key, TValue Value)> dictionary)
: this(
(dictionary ?? throw new ArgumentNullException(nameof(dictionary)))
.Select(pair => new KeyValuePair<TKey, TValue>(pair.key, pair.value))
.Select(pair => new KeyValuePair<TKey, TValue>(pair.Key, pair.Value))
.ToList()
)
{ }
Expand Down

0 comments on commit 5f087da

Please sign in to comment.