Flow.Launcher/Flow.Launcher.Infrastructure/TranslationMapping.cs
VictoriousRaptor d9e89ad610 Refactor TranslationMapping class
- Always add index to mapping rather than only Chinese characters
- Simplify mapping algorithm
- Add unit test for TranslationMapping
2025-07-13 16:07:14 +08:00

37 lines
1.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace Flow.Launcher.Infrastructure
{
public class TranslationMapping
{
private bool constructed;
// Asssuming one original item maps to multi translated items
// list[i] is the last translated index + 1 of original index i
private readonly List<int> originalToTranslated = [];
public void AddNewIndex(int translatedIndex, int length)
{
if (constructed)
throw new InvalidOperationException("Mapping shouldn't be changed after constructed");
originalToTranslated.Add(translatedIndex + length);
}
public int MapToOriginalIndex(int translatedIndex)
{
int loc = originalToTranslated.BinarySearch(translatedIndex);
return loc > 0 ? loc : ~loc;
}
public void endConstruct()
{
if (constructed)
throw new InvalidOperationException("Mapping has already been constructed");
constructed = true;
}
}
}