Skip to content

Commit

Permalink
Make the utility easy to understand.
Browse files Browse the repository at this point in the history
  • Loading branch information
andy840119 committed Dec 6, 2023
1 parent 646bfab commit e7a4b76
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions LrcParser.Tests/Utils/TextIndexUtilsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions LrcParser/Utils/TextIndexUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ internal static int ToCharIndex(TextIndex index)
internal static int ToGapIndex(TextIndex index)
=> GetValueByState(index, index.Index, index.Index + 1);

/// <summary>
/// Get the value by state.
/// If the state is start, then return start value.
/// If the state is end, then return end value.
/// </summary>
/// <param name="index"></param>
/// <param name="startValue"></param>
/// <param name="endValue"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
internal static T GetValueByState<T>(TextIndex index, T startValue, T endValue) =>
index.State switch
{
Expand Down

0 comments on commit e7a4b76

Please sign in to comment.