Skip to content

Commit

Permalink
change namespaces to Smdn.Text.Ondulish
Browse files Browse the repository at this point in the history
  • Loading branch information
smdn committed Dec 22, 2022
1 parent 99af089 commit c8d18fd
Show file tree
Hide file tree
Showing 4 changed files with 478 additions and 480 deletions.
175 changes: 87 additions & 88 deletions src/Smdn.Text.Ondulish/Smdn.Text.Ondulish/KanaUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,111 +25,110 @@
using System;
using System.Text;

namespace Smdn.Applications.OndulishTranslator {
public static class KanaUtils {
private const char wideHiraganaStart = '\u3041';
private const char wideHiraganaEnd = '\u3096';
namespace Smdn.Text.Ondulish;

private const char wideKatakanaStart = '\u30a1';
private const char wideKatakanaEnd = '\u30f6';
public static class KanaUtils {
private const char wideHiraganaStart = '\u3041';
private const char wideHiraganaEnd = '\u3096';

private const int offsetFromHiraganaToKatakana = ((int)wideKatakanaStart - (int)wideHiraganaStart);
private const char wideKatakanaStart = '\u30a1';
private const char wideKatakanaEnd = '\u30f6';

private const char wideKatakanaExEnd = '\u30fa';
private const int offsetFromHiraganaToKatakana = ((int)wideKatakanaStart - (int)wideHiraganaStart);

private static readonly string[] wideToNarrowKatakanaMap = new[] {
"", "", "", "", "", "", "", "", "", "", "", "ガ", "", "ギ", "", // 30A1 - 30AF
"グ", "", "ゲ", "", "ゴ", "", "ザ", "", "ジ", "", "ズ", "", "ゼ", "ソ", "ゾ", "", // 30B0 - 30BF
"ダ", "", "ヂ", "", "", "ヅ", "", "デ", "", "ド", "", "", "", "", "", "", // 30C0 - 30CF
"バ", "パ", "", "ビ", "ピ", "", "ブ", "プ", "", "ベ", "ペ", "", "ボ", "ポ", "", "", // 30D0 - 30DF
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", // 30E0 - 30EF
"", "", "", "", "ヴ", "", "", "ヷ", "", "", "ヺ", // 30F0 - 30FA
};
private const char wideKatakanaExEnd = '\u30fa';

public static string ConvertWideHiraganaToKatakana(string input)
{
private static readonly string[] wideToNarrowKatakanaMap = new[] {
"", "", "", "", "", "", "", "", "", "", "", "ガ", "", "ギ", "", // 30A1 - 30AF
"グ", "", "ゲ", "", "ゴ", "", "ザ", "", "ジ", "", "ズ", "", "ゼ", "ソ", "ゾ", "", // 30B0 - 30BF
"ダ", "", "ヂ", "", "", "ヅ", "", "デ", "", "ド", "", "", "", "", "", "", // 30C0 - 30CF
"バ", "パ", "", "ビ", "ピ", "", "ブ", "プ", "", "ベ", "ペ", "", "ボ", "ポ", "", "", // 30D0 - 30DF
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", // 30E0 - 30EF
"", "", "", "", "ヴ", "", "", "ヷ", "", "", "ヺ", // 30F0 - 30FA
};

public static string ConvertWideHiraganaToKatakana(string input)
{
#if NETFRAMEWORK
var inputChars = input.ToCharArray();
var outputChars = new char[inputChars.Length];
var inputChars = input.ToCharArray();
var outputChars = new char[inputChars.Length];

for (var index = 0; index < inputChars.Length; index++) {
if (wideHiraganaStart <= inputChars[index] && inputChars[index] <= wideHiraganaEnd)
outputChars[index] = (char)((int)inputChars[index] + offsetFromHiraganaToKatakana);
else
outputChars[index] = inputChars[index];
}

for (var index = 0; index < inputChars.Length; index++) {
if (wideHiraganaStart <= inputChars[index] && inputChars[index] <= wideHiraganaEnd)
outputChars[index] = (char)((int)inputChars[index] + offsetFromHiraganaToKatakana);
return new string(outputChars);
#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
outputChars[index] = inputChars[index];
chars[index] = s[index];
}

return new string(outputChars);
#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];
}
});
});
#endif
}
}

public static string ConvertWideKatakanaToHiragana(string input)
{
public static string ConvertWideKatakanaToHiragana(string input)
{
#if NETFRAMEWORK
var inputChars = input.ToCharArray();
var outputChars = new char[inputChars.Length];

for (var index = 0; index < inputChars.Length; index++) {
if (wideKatakanaStart <= inputChars[index] && inputChars[index] <= wideKatakanaEnd)
outputChars[index] = (char)((int)inputChars[index] - offsetFromHiraganaToKatakana);
else
outputChars[index] = inputChars[index];
}

return new string(outputChars);
#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];
}
});
#endif
var inputChars = input.ToCharArray();
var outputChars = new char[inputChars.Length];

for (var index = 0; index < inputChars.Length; index++) {
if (wideKatakanaStart <= inputChars[index] && inputChars[index] <= wideKatakanaEnd)
outputChars[index] = (char)((int)inputChars[index] - offsetFromHiraganaToKatakana);
else
outputChars[index] = inputChars[index];
}

public static string ConvertWideKatakanaToNarrowKatakana(string input)
{
var inputChars = input.ToCharArray();
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]);
else if (inputChars[index] == 'ー')
output.Append('ー');
else if (inputChars[index] == '゛')
output.Append('゙');
else if (inputChars[index] == '゜')
output.Append('゚');
else if (inputChars[index] == '?')
output.Append('?');
else if (inputChars[index] == '!')
output.Append('!');
else if (inputChars[index] == '、')
output.Append('、');
else if (inputChars[index] == '。')
output.Append('。');
else if (inputChars[index] == ',')
output.Append(',');
else if (inputChars[index] == '.')
output.Append('.');
return new string(outputChars);
#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
output.Append(inputChars[index]);
chars[index] = s[index];
}
});
#endif
}

return output.ToString();
public static string ConvertWideKatakanaToNarrowKatakana(string input)
{
var inputChars = input.ToCharArray();
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]);
else if (inputChars[index] == 'ー')
output.Append('ー');
else if (inputChars[index] == '゛')
output.Append('゙');
else if (inputChars[index] == '゜')
output.Append('゚');
else if (inputChars[index] == '?')
output.Append('?');
else if (inputChars[index] == '!')
output.Append('!');
else if (inputChars[index] == '、')
output.Append('、');
else if (inputChars[index] == '。')
output.Append('。');
else if (inputChars[index] == ',')
output.Append(',');
else if (inputChars[index] == '.')
output.Append('.');
else
output.Append(inputChars[index]);
}

return output.ToString();
}
}

Loading

0 comments on commit c8d18fd

Please sign in to comment.