From 4e3b2810e6951b72bdf5c5cbdd23a079d53a4c96 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 16 Oct 2022 14:41:45 -0400 Subject: [PATCH] fix index-out-of-range error when StartsWith prefix is empty --- src/SMAPI/Framework/Content/AssetName.cs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/SMAPI/Framework/Content/AssetName.cs b/src/SMAPI/Framework/Content/AssetName.cs index d7ee6dba..c0572105 100644 --- a/src/SMAPI/Framework/Content/AssetName.cs +++ b/src/SMAPI/Framework/Content/AssetName.cs @@ -139,21 +139,19 @@ namespace StardewModdingAPI.Framework.Content if (prefix is null) return false; + // get initial values ReadOnlySpan trimmed = prefix.AsSpan().Trim(); + if (trimmed.Length == 0) + return true; + ReadOnlySpan pathSeparators = new(ToolkitPathUtilities.PossiblePathSeparators); // just to simplify calling other span APIs - // just because most ReadOnlySpan/Span APIs expect a ReadOnlySpan/Span, easier to read. - ReadOnlySpan pathSeparators = new(ToolkitPathUtilities.PossiblePathSeparators); - - // asset keys can't have a leading slash, but AssetPathYielder won't yield that. + // asset keys can't have a leading slash, but AssetPathYielder will trim them if (pathSeparators.Contains(trimmed[0])) return false; - if (trimmed.Length == 0) - return true; - + // compare segments AssetNamePartEnumerator curParts = new(this.Name); AssetNamePartEnumerator prefixParts = new(trimmed); - while (true) { bool prefixHasMore = prefixParts.MoveNext();