From 5fb72951a0cccc27fb324e555735dc48b3b2beca Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Sat, 12 Jun 2021 00:56:09 +0200 Subject: [PATCH] Add test for ReadOnlySpan.Count extension --- .../Data/SqliteItemRepository.cs | 2 +- .../Extensions/StringExtensions.cs | 3 +-- .../Extensions/StringExtensionsTests.cs | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 tests/Jellyfin.Controller.Tests/Extensions/StringExtensionsTests.cs diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs index 3ae28fadf..9b147b5d7 100644 --- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs @@ -1048,7 +1048,7 @@ namespace Emby.Server.Implementations.Data // TODO The following is an ugly performance optimization, but it's extremely unlikely that the data in the database would be malformed var valueSpan = value.AsSpan(); - var count = valueSpan.CountOccurrences('|') + 1; + var count = valueSpan.Count('|') + 1; var position = 0; var result = new ItemImageInfo[count]; diff --git a/MediaBrowser.Controller/Extensions/StringExtensions.cs b/MediaBrowser.Controller/Extensions/StringExtensions.cs index 291485e75..f1af01345 100644 --- a/MediaBrowser.Controller/Extensions/StringExtensions.cs +++ b/MediaBrowser.Controller/Extensions/StringExtensions.cs @@ -1,7 +1,6 @@ #pragma warning disable CS1591 using System; -using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; @@ -28,7 +27,7 @@ namespace MediaBrowser.Controller.Extensions /// The haystack to search in. /// The character to search for. /// The number of occurrences of the [needle] character. - public static int CountOccurrences(this ReadOnlySpan value, char needle) + public static int Count(this ReadOnlySpan value, char needle) { var count = 0; var length = value.Length; diff --git a/tests/Jellyfin.Controller.Tests/Extensions/StringExtensionsTests.cs b/tests/Jellyfin.Controller.Tests/Extensions/StringExtensionsTests.cs new file mode 100644 index 000000000..576c0a49b --- /dev/null +++ b/tests/Jellyfin.Controller.Tests/Extensions/StringExtensionsTests.cs @@ -0,0 +1,19 @@ +using System; +using MediaBrowser.Controller.Extensions; +using Xunit; + +namespace Jellyfin.Controller.Extensions.Tests +{ + public class StringExtensionsTests + { + [Theory] + [InlineData("", '_', 0)] + [InlineData("___", '_', 3)] + [InlineData("test\x00", '\x00', 1)] + [InlineData("Imdb=tt0119567|Tmdb=330|TmdbCollection=328", '|', 2)] + public void ReadOnlySpan_Count_Success(string str, char needle, int count) + { + Assert.Equal(count, str.AsSpan().Count(needle)); + } + } +}