Skip to content

Commit

Permalink
Add test for ReadOnlySpan.Count extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Bond-009 committed Jun 11, 2021
1 parent cfad97f commit 5fb7295
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Emby.Server.Implementations/Data/SqliteItemRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ internal ItemImageInfo[] DeserializeImages(string value)

// 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];
Expand Down
3 changes: 1 addition & 2 deletions MediaBrowser.Controller/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma warning disable CS1591

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
Expand All @@ -28,7 +27,7 @@ public static string RemoveDiacritics(this string text)
/// <param name="value">The haystack to search in.</param>
/// <param name="needle">The character to search for.</param>
/// <returns>The number of occurrences of the [needle] character.</returns>
public static int CountOccurrences(this ReadOnlySpan<char> value, char needle)
public static int Count(this ReadOnlySpan<char> value, char needle)
{
var count = 0;
var length = value.Length;
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
}

0 comments on commit 5fb7295

Please sign in to comment.