From e7a4b766ecbfc8dbe993d5a89ec4da9a34ae7137 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Thu, 7 Dec 2023 00:11:12 +0800 Subject: [PATCH] Make the utility easy to understand. --- LrcParser.Tests/Utils/TextIndexUtilsTest.cs | 4 ++-- LrcParser/Utils/TextIndexUtils.cs | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/LrcParser.Tests/Utils/TextIndexUtilsTest.cs b/LrcParser.Tests/Utils/TextIndexUtilsTest.cs index 7020357..b5578ad 100644 --- a/LrcParser.Tests/Utils/TextIndexUtilsTest.cs +++ b/LrcParser.Tests/Utils/TextIndexUtilsTest.cs @@ -35,8 +35,8 @@ public void TestToGapIndex(int index, IndexState state, int expected) [TestCase(IndexState.Start, 1, -1, 1)] [TestCase(IndexState.End, 1, -1, -1)] - [TestCase(IndexState.Start, "1", "-1", "1")] - [TestCase(IndexState.End, "1", "-1", "-1")] + [TestCase(IndexState.Start, "start", "end", "start")] + [TestCase(IndexState.End, "start", "end", "end")] public void TestGetValueByState(IndexState state, object startValue, object endValue, object expected) { var textIndex = new TextIndex(0, state); diff --git a/LrcParser/Utils/TextIndexUtils.cs b/LrcParser/Utils/TextIndexUtils.cs index bc78424..57530d5 100644 --- a/LrcParser/Utils/TextIndexUtils.cs +++ b/LrcParser/Utils/TextIndexUtils.cs @@ -26,6 +26,17 @@ internal static int ToCharIndex(TextIndex index) internal static int ToGapIndex(TextIndex index) => GetValueByState(index, index.Index, index.Index + 1); + /// + /// Get the value by state. + /// If the state is start, then return start value. + /// If the state is end, then return end value. + /// + /// + /// + /// + /// + /// + /// internal static T GetValueByState(TextIndex index, T startValue, T endValue) => index.State switch {