Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
rampaa committed Aug 23, 2024
1 parent 812c857 commit 92c80a8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
27 changes: 17 additions & 10 deletions JL.Core/Utilities/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,29 @@ public static T GetEnum<T>(this string description) where T : struct, Enum
// return default;
}

internal static List<string> ListUnicodeCharacters(this string s)
internal static List<string> ListUnicodeCharacters(this ReadOnlySpan<char> text)
{
List<string> textBlocks = new(s.Length);
for (int i = 0; i < s.Length; i++)
List<string> textBlocks = new(text.Length);
for (int i = 0; i < text.Length; i++)
{
if (char.IsHighSurrogate(s, i)
&& s.Length > i + 1
&& char.IsLowSurrogate(s, i + 1))
char highSurrogateCandidate = text[i];
if (char.IsHighSurrogate(highSurrogateCandidate)
&& text.Length > i + 1)
{
textBlocks.Add(char.ConvertFromUtf32(char.ConvertToUtf32(s, i)));

++i;
char lowSurragateCandidate = text[i + 1];
if (char.IsLowSurrogate(lowSurragateCandidate))
{
textBlocks.Add(char.ConvertFromUtf32(char.ConvertToUtf32(highSurrogateCandidate, lowSurragateCandidate)));
++i;
}
else
{
textBlocks.Add(highSurrogateCandidate.ToString());
}
}
else
{
textBlocks.Add(s[i].ToString());
textBlocks.Add(highSurrogateCandidate.ToString());
}
}

Expand Down
12 changes: 6 additions & 6 deletions JL.Core/Utilities/JapaneseUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public static partial class JapaneseUtils

private static readonly FrozenSet<char> s_expressionTerminatingCharacters = s_leftToRightBracketDict.Keys.Union(s_leftToRightBracketDict.Values).Union(s_sentenceTerminatingCharacters).ToFrozenSet();

private static int FirstKatakanaIndex(string text)
private static int FirstKatakanaIndex(ReadOnlySpan<char> text)
{
int textLength = text.Length;
for (int i = 0; i < textLength; i++)
Expand Down Expand Up @@ -219,7 +219,7 @@ public static string KatakanaToHiragana(string text)
return textInHiragana.ToString();
}

internal static List<string> LongVowelMarkToKana(string text)
internal static List<string> LongVowelMarkToKana(ReadOnlySpan<char> text)
{
List<string> unicodeTextList = text.ListUnicodeCharacters();

Expand Down Expand Up @@ -278,7 +278,7 @@ internal static List<string> LongVowelMarkToKana(string text)
return stringBuilders.ConvertAll(static sb => sb.ToString());
}

public static List<string> CreateCombinedForm(string text)
public static List<string> CreateCombinedForm(ReadOnlySpan<char> text)
{
List<string> combinedForm = new(text.Length);

Expand All @@ -300,12 +300,12 @@ public static List<string> CreateCombinedForm(string text)
return combinedForm;
}

internal static bool IsKatakana(string text)
internal static bool IsKatakana(ReadOnlySpan<char> text)
{
return s_katakanaToHiraganaDict.ContainsKey(text[0]);
}

public static int FindExpressionBoundary(string text, int position)
public static int FindExpressionBoundary(ReadOnlySpan<char> text, int position)
{
int endPosition = text.Length;
for (int i = position; i < text.Length; i++)
Expand Down Expand Up @@ -412,7 +412,7 @@ internal static string FindSentence(string text, int position)
return sentence;
}

private static int FirstPunctuationIndex(string text)
private static int FirstPunctuationIndex(ReadOnlySpan<char> text)
{
int charIndex = 0;
foreach (Rune rune in text.EnumerateRunes())
Expand Down
2 changes: 1 addition & 1 deletion JL.Core/Utilities/TextUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace JL.Core.Utilities;

public static class TextUtils
{
private static int FirstInvalidUnicodeSequenceIndex(string text)
private static int FirstInvalidUnicodeSequenceIndex(ReadOnlySpan<char> text)
{
for (int i = 0; i < text.Length; i++)
{
Expand Down

0 comments on commit 92c80a8

Please sign in to comment.