parent
3142c9b990
commit
7f4a229cd2
@ -0,0 +1,37 @@
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
|
||||
namespace MediaBrowser.Common.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Extensions methods to simplify string operations.
|
||||
/// </summary>
|
||||
public static class StringExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the part left of the <c>needle</c>.
|
||||
/// </summary>
|
||||
/// <param name="str">The string to seek.</param>
|
||||
/// <param name="needle">The needle to find.</param>
|
||||
/// <returns>The part left of the <c>needle</c>.</returns>
|
||||
public static ReadOnlySpan<char> LeftPart(this ReadOnlySpan<char> str, char needle)
|
||||
{
|
||||
var pos = str.IndexOf(needle);
|
||||
return pos == -1 ? str : str[..pos];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the part left of the <c>needle</c>.
|
||||
/// </summary>
|
||||
/// <param name="str">The string to seek.</param>
|
||||
/// <param name="needle">The needle to find.</param>
|
||||
/// <param name="stringComparison">One of the enumeration values that specifies the rules for the search.</param>
|
||||
/// <returns>The part left of the <c>needle</c>.</returns>
|
||||
public static ReadOnlySpan<char> LeftPart(this ReadOnlySpan<char> str, ReadOnlySpan<char> needle, StringComparison stringComparison = default)
|
||||
{
|
||||
var pos = str.IndexOf(needle, stringComparison);
|
||||
return pos == -1 ? str : str[..pos];
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using MediaBrowser.Common.Extensions;
|
||||
using Xunit;
|
||||
|
||||
namespace Jellyfin.Common.Tests.Extensions
|
||||
{
|
||||
public class StringExtensionsTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("Banana split", ' ', "Banana")]
|
||||
[InlineData("Banana split", 'q', "Banana split")]
|
||||
public void LeftPartCharTest(string str, char needle, string result)
|
||||
{
|
||||
Assert.Equal(result, str.AsSpan().LeftPart(needle).ToString());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Banana split", " ", "Banana")]
|
||||
[InlineData("Banana split test", " split", "Banana")]
|
||||
public void LeftPartWithoutStringComparisonTest(string str, string needle, string result)
|
||||
{
|
||||
Assert.Equal(result, str.AsSpan().LeftPart(needle).ToString());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Banana split", " ", StringComparison.Ordinal, "Banana")]
|
||||
[InlineData("Banana split test", " split", StringComparison.Ordinal, "Banana")]
|
||||
[InlineData("Banana split test", " Split", StringComparison.Ordinal, "Banana split test")]
|
||||
[InlineData("Banana split test", " Splït", StringComparison.InvariantCultureIgnoreCase, "Banana split test")]
|
||||
public void LeftPartTest(string str, string needle, StringComparison stringComparison, string result)
|
||||
{
|
||||
Assert.Equal(result, str.AsSpan().LeftPart(needle, stringComparison).ToString());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
using Emby.Server.Implementations.HttpServer;
|
||||
using Xunit;
|
||||
|
||||
namespace Jellyfin.Server.Implementations.Tests.HttpServer
|
||||
{
|
||||
public class HttpServerTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("This is a clean string.", "This is a clean string.")]
|
||||
[InlineData("This isn't \n\ra clean string.", "This isn't a clean string.")]
|
||||
public void RemoveControlCharactersTest(string input, string result)
|
||||
{
|
||||
Assert.Equal(result, ResponseFilter.RemoveControlCharacters(input));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
using Emby.Server.Implementations.Library;
|
||||
using Xunit;
|
||||
|
||||
namespace Jellyfin.Server.Implementations.Tests.Library
|
||||
{
|
||||
public class PathExtensionsTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("Superman: Red Son [imdbid=tt10985510]", "imdbid", "tt10985510")]
|
||||
[InlineData("Superman: Red Son - tt10985510", "imdbid", "tt10985510")]
|
||||
[InlineData("Superman: Red Son", "imdbid", null)]
|
||||
public void GetAttributeValueTest(string input, string attribute, string? result)
|
||||
{
|
||||
Assert.Equal(result, PathExtensions.GetAttributeValue(input, attribute));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue