Skip to content

Commit f5fd6b5

Browse files
committed
Use ReadOnlySpan instead
1 parent 4b7db3c commit f5fd6b5

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

Flow.Launcher.Infrastructure/PinyinAlphabet.cs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Concurrent;
1+
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.Collections.ObjectModel;
45
using System.Text;
@@ -148,41 +149,53 @@ public bool ShouldTranslate(string stringToTranslate)
148149
private static string ToDoublePin(string fullPinyin)
149150
{
150151
// Assuming s is valid
152+
var fullPinyinSpan = fullPinyin.AsSpan();
151153
var doublePin = new StringBuilder();
152154

153-
if (fullPinyin.Length <= 3 && (fullPinyin[0] == 'a' || fullPinyin[0] == 'e' || fullPinyin[0] == 'o'))
155+
// Handle special cases (a, o, e)
156+
if (fullPinyin.Length <= 3 && (fullPinyinSpan[0] == 'a' || fullPinyinSpan[0] == 'e' || fullPinyinSpan[0] == 'o'))
154157
{
155158
if (special.TryGetValue(fullPinyin, out var value))
156159
{
157160
return value;
158161
}
159162
}
160163

161-
// zh, ch, sh
162-
if (fullPinyin.Length >= 2 && first.ContainsKey(fullPinyin[..2]))
164+
// Check for initials that are two characters long (zh, ch, sh)
165+
if (fullPinyin.Length >= 2)
163166
{
164-
doublePin.Append(first[fullPinyin[..2]]);
165-
166-
if (second.TryGetValue(fullPinyin[2..], out var tmp))
167-
{
168-
doublePin.Append(tmp);
169-
}
170-
else
167+
var firstTwo = fullPinyinSpan[..2];
168+
var firstTwoString = firstTwo.ToString();
169+
if (first.ContainsKey(firstTwoString))
171170
{
172-
doublePin.Append(fullPinyin[2..]);
171+
doublePin.Append(firstTwoString);
172+
173+
var lastTwo = fullPinyinSpan[2..];
174+
var lastTwoString = lastTwo.ToString();
175+
if (second.TryGetValue(lastTwoString, out var tmp))
176+
{
177+
doublePin.Append(tmp);
178+
}
179+
else
180+
{
181+
doublePin.Append(lastTwo);
182+
}
173183
}
174184
}
185+
// Handle single-character initials
175186
else
176187
{
177-
doublePin.Append(fullPinyin[0]);
188+
doublePin.Append(fullPinyinSpan[0]);
178189

179-
if (second.TryGetValue(fullPinyin[1..], out var tmp))
190+
var lastOne = fullPinyinSpan[1..];
191+
var lastOneString = lastOne.ToString();
192+
if (second.TryGetValue(lastOneString, out var tmp))
180193
{
181194
doublePin.Append(tmp);
182195
}
183196
else
184197
{
185-
doublePin.Append(fullPinyin[1..]);
198+
doublePin.Append(lastOne);
186199
}
187200
}
188201

0 commit comments

Comments
 (0)