expand code comments for clarity

This commit is contained in:
Jesse Plamondon-Willard 2022-11-11 21:22:52 -05:00
parent 2bccdd9737
commit ad2dcc2879
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
2 changed files with 10 additions and 7 deletions

View File

@ -265,15 +265,14 @@ namespace SMAPI.Tests.Core
return name.StartsWith(otherAssetName, allowPartialWord: true, allowSubfolder: allowSubfolder);
}
// the enumerator strips the trailing path seperator
// so each of these cases has to be handled on each branch.
// The enumerator strips the trailing path separator, so each of these cases has to be handled on each branch.
[TestCase("Mods/SomeMod", "Mods/", false, ExpectedResult = true)]
[TestCase("Mods/SomeMod", "Mods", false, ExpectedResult = false)]
[TestCase("Mods/Jasper/Data", "Mods/Jas/", false, ExpectedResult = false)]
[TestCase("Mods/Jasper/Data", "Mods/Jas", false, ExpectedResult = false)]
[TestCase("Mods/Jas", "Mods/Jas/", false, ExpectedResult = false)]
[TestCase("Mods/Jas", "Mods/Jas", false, ExpectedResult = true)]
public bool StartsWith_PrefixHasSeperator(string mainAssetName, string otherAssetName, bool allowSubfolder)
public bool StartsWith_PrefixHasSeparator(string mainAssetName, string otherAssetName, bool allowSubfolder)
{
// arrange
mainAssetName = PathUtilities.NormalizeAssetName(mainAssetName);

View File

@ -162,10 +162,14 @@ namespace StardewModdingAPI.Framework.Content
if (prefixHasMore)
return false;
// match if subfolder paths are fine (e.g. prefix 'Data/Events' with target 'Data/Events/Beach')
// special case if the original prefix ended with a '/' - subfolder checking pushes forward one, to check the Remainder instead of Current
// which is necessarily nonzero in this block.
return allowSubfolder || (pathSeparators.Contains(trimmedPrefix[^1]) && curParts.Remainder.Length == 0);
// match: every segment in the prefix matched and subfolders are allowed (e.g. prefix 'Data/Events' with target 'Data/Events/Beach')
if (allowSubfolder)
return true;
// Special case: the prefix ends with a path separator, but subfolders aren't allowed. This case
// matches if there's no further path separator in the asset name *after* the current separator.
// For example, the prefix 'A/B/' matches 'A/B/C' but not 'A/B/C/D'.
return pathSeparators.Contains(trimmedPrefix[^1]) && curParts.Remainder.Length == 0;
}
// previous segments matched exactly and both reached the end