aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCody Robibero <cody@robibe.ro>2021-12-09 08:06:06 -0700
committerCody Robibero <cody@robibe.ro>2021-12-09 08:06:06 -0700
commit593b2fd359de378a2588a8d54d11ebc0bb2cd3f1 (patch)
treed3f8b37293a89ba9d543d0dbce063cbacaf0ac5b
parent3513f5a84bf21c64b5f909352e260bda2e9ab057 (diff)
Add more speed and more tests
-rw-r--r--Emby.Server.Implementations/Library/PathExtensions.cs23
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs8
2 files changed, 22 insertions, 9 deletions
diff --git a/Emby.Server.Implementations/Library/PathExtensions.cs b/Emby.Server.Implementations/Library/PathExtensions.cs
index 73a658186..78850c149 100644
--- a/Emby.Server.Implementations/Library/PathExtensions.cs
+++ b/Emby.Server.Implementations/Library/PathExtensions.cs
@@ -28,21 +28,26 @@ namespace Emby.Server.Implementations.Library
throw new ArgumentException("String can't be empty.", nameof(attribute));
}
- var openBracketIndex = str.IndexOf('[');
var attributeIndex = str.IndexOf(attribute);
- var closingBracketIndex = str.IndexOf(']');
- while (openBracketIndex < attributeIndex && attributeIndex < closingBracketIndex)
+
+ // Must be at least 3 characters after the attribute =, ], any character.
+ var maxIndex = str.Length - attribute.Length - 3;
+ while (attributeIndex > -1 && attributeIndex < maxIndex)
{
- if (openBracketIndex + 1 == attributeIndex
- && str[attributeIndex + attribute.Length] == '=')
+ var attributeEnd = attributeIndex + attribute.Length;
+ if (attributeIndex > 0
+ && str[attributeIndex - 1] == '['
+ && str[attributeEnd] == '=')
{
- return str[(attributeIndex + attribute.Length + 1)..closingBracketIndex].Trim().ToString();
+ var closingIndex = str[attributeEnd..].IndexOf(']');
+ if (closingIndex != -1)
+ {
+ return str[(attributeEnd + 1)..(attributeEnd + closingIndex)].Trim().ToString();
+ }
}
- str = str[(closingBracketIndex + 1)..];
- openBracketIndex = str.IndexOf('[');
+ str = str[attributeEnd..];
attributeIndex = str.IndexOf(attribute);
- closingBracketIndex = str.IndexOf(']');
}
// for imdbid we also accept pattern matching
diff --git a/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs
index d6cbe9647..950f0e76d 100644
--- a/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs
+++ b/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs
@@ -14,6 +14,14 @@ namespace Jellyfin.Server.Implementations.Tests.Library
[InlineData("Superman: Red Son [imdbid1=tt11111111][imdbid=tt10985510]", "imdbid", "tt10985510")]
[InlineData("Superman: Red Son [tmdbid=618355][imdbid=tt10985510]", "imdbid", "tt10985510")]
[InlineData("Superman: Red Son [tmdbid=618355][imdbid=tt10985510]", "tmdbid", "618355")]
+ [InlineData("[tmdbid=618355]", "tmdbid", "618355")]
+ [InlineData("tmdbid=618355][tmdbid=618355]", "tmdbid", "618355")]
+ [InlineData("[tmdbid=618355]tmdbid=618355]", "tmdbid", "618355")]
+ [InlineData("tmdbid=618355]", "tmdbid", null)]
+ [InlineData("[tmdbid=618355", "tmdbid", null)]
+ [InlineData("tmdbid=618355", "tmdbid", null)]
+ [InlineData("tmdbid=", "tmdbid", null)]
+ [InlineData("tmdbid", "tmdbid", null)]
public void GetAttributeValue_ValidArgs_Correct(string input, string attribute, string? expectedResult)
{
Assert.Equal(expectedResult, PathExtensions.GetAttributeValue(input, attribute));