apply conventions to asset part enumerator

This commit is contained in:
Jesse Plamondon-Willard 2022-10-16 14:41:45 -04:00
parent 70cde89480
commit eed1deb3c7
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
1 changed files with 51 additions and 47 deletions

View File

@ -1,47 +1,44 @@
using System;
using ToolkitPathUtilities = StardewModdingAPI.Toolkit.Utilities.PathUtilities;
namespace StardewModdingAPI.Utilities.AssetPathUtilities;
/// <summary>
/// A helper class that yields out each bit of an asset path
/// </summary>
namespace StardewModdingAPI.Utilities.AssetPathUtilities
{
/// <summary>Handles enumerating the normalized segments in an asset name.</summary>
internal ref struct AssetNamePartEnumerator
{
/*********
** Fields
*********/
/// <summary>The backing field for <see cref="Remainder"/>.</summary>
private ReadOnlySpan<char> RemainderImpl;
/// <summary>
/// Construct an instance.
/// </summary>
/// <param name="assetName">The asset name.</param>
internal AssetNamePartEnumerator(ReadOnlySpan<char> assetName)
/*********
** Properties
*********/
/// <summary>The remainder of the asset name being enumerated, ignoring segments which have already been yielded.</summary>
public ReadOnlySpan<char> Remainder => this.RemainderImpl;
/// <summary>Get the current segment.</summary>
public ReadOnlySpan<char> Current { get; private set; } = default;
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="assetName">The asset name to enumerate.</param>
public AssetNamePartEnumerator(ReadOnlySpan<char> assetName)
{
this.RemainderImpl = AssetNamePartEnumerator.TrimLeadingPathSeparators(assetName);
}
/// <summary>
/// The remainder of the assetName (that hasn't been yielded out yet.)
/// </summary>
internal ReadOnlySpan<char> Remainder => this.RemainderImpl;
/// <summary>
/// The current segment.
/// </summary>
public ReadOnlySpan<char> Current { get; private set; } = default;
// this is just so it can be used in a foreach loop.
public AssetNamePartEnumerator GetEnumerator() => this;
/// <summary>
/// Moves the enumerator to the next element.
/// </summary>
/// <returns>True if there is a new</returns>
/// <summary>Move the enumerator to the next segment.</summary>
/// <returns>Returns true if a new value was found (accessible via <see cref="Current"/>).</returns>
public bool MoveNext()
{
if (this.RemainderImpl.Length == 0)
{
return false;
}
int index = this.RemainderImpl.IndexOfAny(ToolkitPathUtilities.PossiblePathSeparators);
@ -59,8 +56,15 @@ internal ref struct AssetNamePartEnumerator
return true;
}
/*********
** Private methods
*********/
/// <summary>Trim path separators at the start of the given path or segment.</summary>
/// <param name="span">The path or segment to trim.</param>
private static ReadOnlySpan<char> TrimLeadingPathSeparators(ReadOnlySpan<char> span)
{
return span.TrimStart(new ReadOnlySpan<char>(ToolkitPathUtilities.PossiblePathSeparators));
}
}
}