Skip to content

Commit 64d67c1

Browse files
taoocerostoolgood
andcommitted
Update PinyinAlphabet.cs
optimize Chinese character check logic Co-Authored-By: ToolGood <[email protected]>
1 parent d64b4b5 commit 64d67c1

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

Flow.Launcher.Infrastructure/PinyinAlphabet.cs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,37 +39,32 @@ public string Translate(string content)
3939
{
4040
var resultList = WordsHelper.GetPinyinList(content);
4141

42-
List<int> chineseIndexs = new List<int>();
43-
44-
for (int i = 0; i < content.Length; i++)
45-
{
46-
if (resultList[i].Length != 1 || !(resultList[i][0] == content[i]))
47-
chineseIndexs.Add(i);
48-
}
4942
StringBuilder resultBuilder = new StringBuilder();
43+
5044

51-
foreach (var chineseIndex in chineseIndexs)
45+
for (int i = 0; i < resultList.Length; i++)
5246
{
53-
resultBuilder.Append(resultList[chineseIndex].First());
47+
if (content[i] >= 0x3400 && content[i] <= 0x9FD5)
48+
resultBuilder.Append(resultList[i].First());
5449
}
50+
5551
resultBuilder.Append(' ');
5652

57-
int currentChineseIndex = 0;
58-
int lastChineseIndex = -1;
53+
bool pre = false;
54+
5955
for (int i = 0; i < resultList.Length; i++)
6056
{
61-
if (currentChineseIndex < chineseIndexs.Count && chineseIndexs[currentChineseIndex] == i)
57+
if (content[i] >= 0x3400 && content[i] <= 0x9FD5)
6258
{
6359
resultBuilder.Append(' ');
64-
6560
resultBuilder.Append(resultList[i]);
66-
currentChineseIndex++;
67-
lastChineseIndex = i;
61+
pre = true;
6862
}
6963
else
7064
{
71-
if (i == lastChineseIndex + 1)
65+
if (pre)
7266
{
67+
pre = false;
7368
resultBuilder.Append(' ');
7469
}
7570
resultBuilder.Append(resultList[i]);

0 commit comments

Comments
 (0)