Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
rampaa committed Feb 3, 2024
1 parent 68a5ba3 commit 74cac66
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions JL.Core/Utilities/JapaneseUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public static class JapaneseUtils
'\n'
};

private static readonly Dictionary<char, char> s_bracketsDict = new(18)
private static readonly Dictionary<char, char> s_leftToRightBracketDict = new(18)
{
{ '「', '」' },
{ '『', '』' },
Expand All @@ -140,17 +140,9 @@ public static class JapaneseUtils
{ '{', '}' }
};

private static readonly HashSet<char> s_brackets = new(36)
{
#pragma warning disable format
'「', '」' , '『', '』' , '【', '】',
'《', '》', '〔', '〕', '(', ')',
'[', ']', '〈', '〉', '{', '}',
'<', '>', '〝', '〟', '"', '"',
''', ''', '「', '」', '⟨', '⟩',
'(', ')', '[', ']', '{', '}'
#pragma warning restore format
};
private static readonly Dictionary<char, char> s_rightToLeftBracketDict = s_leftToRightBracketDict.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);

private static readonly HashSet<char> s_brackets = s_leftToRightBracketDict.Keys.Union(s_leftToRightBracketDict.Values).ToHashSet();

public static string KatakanaToHiragana(string text)
{
Expand Down Expand Up @@ -300,22 +292,22 @@ internal static string FindSentence(string text, int position)
}

string sentence = startPosition < endPosition
? text[startPosition..(endPosition + 1)].Trim('\n', '\t', '\r', ' ', ' ')
? text[startPosition..(endPosition + 1)].Trim()
: "";

if (sentence.Length > 1)
{
if (s_bracketsDict.ContainsValue(sentence[0]))
if (s_rightToLeftBracketDict.ContainsKey(sentence[0]))
{
sentence = sentence[1..];
}

if (s_bracketsDict.ContainsKey(sentence.LastOrDefault()))
if (s_leftToRightBracketDict.ContainsKey(sentence.LastOrDefault()))
{
sentence = sentence[..^1];
}

if (s_bracketsDict.TryGetValue(sentence.FirstOrDefault(), out char rightBracket))
if (s_leftToRightBracketDict.TryGetValue(sentence.FirstOrDefault(), out char rightBracket))
{
if (sentence[^1] == rightBracket)
{
Expand All @@ -337,10 +329,8 @@ internal static string FindSentence(string text, int position)
}
}

else if (s_bracketsDict.ContainsValue(sentence.LastOrDefault()))
else if (s_rightToLeftBracketDict.TryGetValue(sentence.LastOrDefault(), out char leftBracket))
{
char leftBracket = s_bracketsDict.First(p => p.Value == sentence[^1]).Key;

if (!sentence.Contains(leftBracket, StringComparison.Ordinal))
{
sentence = sentence[..^1];
Expand Down

0 comments on commit 74cac66

Please sign in to comment.