Skip to content

Commit

Permalink
Remove extra ToString() when stringifying M3U8 metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrubN committed Mar 20, 2024
1 parent 74f69f8 commit 69f5695
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Text;
using TwitchDownloaderCore.Extensions;

namespace TwitchDownloaderCore.Tests.ExtensionTests
{
public class StringBuilderExtensionTests
{
[Theory]
[InlineData("Foo", "o", "F")]
[InlineData("Foo\r\n", "\r\n", "Foo")]
[InlineData("oo", "o", "")]
[InlineData("Foo", "L", "Foo")]
[InlineData("Foo", "oL", "F")]
public void CorrectlyTrimsCharacters(string baseString, string trimChars, string expectedResult)
{
var sb = new StringBuilder(baseString);

sb.TrimEnd(trimChars);

Assert.Equal(expectedResult, sb.ToString());
}
}
}
19 changes: 19 additions & 0 deletions TwitchDownloaderCore/Extensions/StringBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Text;

namespace TwitchDownloaderCore.Extensions
{
public static class StringBuilderExtensions
{
public static StringBuilder TrimEnd(this StringBuilder sb, ReadOnlySpan<char> trimChars)
{
var trimLength = 0;
while (sb.Length - trimLength > 0 && trimChars.Contains(sb[^(trimLength + 1)]))
{
trimLength++;
}

return sb.Remove(sb.Length - trimLength, trimLength);
}
}
}
2 changes: 1 addition & 1 deletion TwitchDownloaderCore/Tools/M3U8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public override string ToString()
return "";
}

return sb.ToString().AsSpan().TrimEnd(itemSeparator).ToString();
return sb.TrimEnd(itemSeparator).ToString();
}

public sealed class Builder
Expand Down

0 comments on commit 69f5695

Please sign in to comment.