apply conventions to asset part enumerator
This commit is contained in:
parent
70cde89480
commit
eed1deb3c7
|
@ -1,66 +1,70 @@
|
||||||
using System;
|
using System;
|
||||||
using ToolkitPathUtilities = StardewModdingAPI.Toolkit.Utilities.PathUtilities;
|
using ToolkitPathUtilities = StardewModdingAPI.Toolkit.Utilities.PathUtilities;
|
||||||
|
|
||||||
namespace StardewModdingAPI.Utilities.AssetPathUtilities;
|
namespace StardewModdingAPI.Utilities.AssetPathUtilities
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// A helper class that yields out each bit of an asset path
|
|
||||||
/// </summary>
|
|
||||||
internal ref struct AssetNamePartEnumerator
|
|
||||||
{
|
{
|
||||||
private ReadOnlySpan<char> RemainderImpl;
|
/// <summary>Handles enumerating the normalized segments in an asset name.</summary>
|
||||||
|
internal ref struct AssetNamePartEnumerator
|
||||||
/// <summary>
|
|
||||||
/// Construct an instance.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="assetName">The asset name.</param>
|
|
||||||
internal AssetNamePartEnumerator(ReadOnlySpan<char> assetName)
|
|
||||||
{
|
{
|
||||||
this.RemainderImpl = AssetNamePartEnumerator.TrimLeadingPathSeparators(assetName);
|
/*********
|
||||||
}
|
** Fields
|
||||||
|
*********/
|
||||||
|
/// <summary>The backing field for <see cref="Remainder"/>.</summary>
|
||||||
|
private ReadOnlySpan<char> RemainderImpl;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The remainder of the assetName (that hasn't been yielded out yet.)
|
|
||||||
/// </summary>
|
|
||||||
internal ReadOnlySpan<char> Remainder => this.RemainderImpl;
|
|
||||||
|
|
||||||
/// <summary>
|
/*********
|
||||||
/// The current segment.
|
** Properties
|
||||||
/// </summary>
|
*********/
|
||||||
public ReadOnlySpan<char> Current { get; private set; } = default;
|
/// <summary>The remainder of the asset name being enumerated, ignoring segments which have already been yielded.</summary>
|
||||||
|
public ReadOnlySpan<char> Remainder => this.RemainderImpl;
|
||||||
|
|
||||||
// this is just so it can be used in a foreach loop.
|
/// <summary>Get the current segment.</summary>
|
||||||
public AssetNamePartEnumerator GetEnumerator() => this;
|
public ReadOnlySpan<char> Current { get; private set; } = default;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Moves the enumerator to the next element.
|
/*********
|
||||||
/// </summary>
|
** Public methods
|
||||||
/// <returns>True if there is a new</returns>
|
*********/
|
||||||
public bool MoveNext()
|
/// <summary>Construct an instance.</summary>
|
||||||
{
|
/// <param name="assetName">The asset name to enumerate.</param>
|
||||||
if (this.RemainderImpl.Length == 0)
|
public AssetNamePartEnumerator(ReadOnlySpan<char> assetName)
|
||||||
{
|
{
|
||||||
return false;
|
this.RemainderImpl = AssetNamePartEnumerator.TrimLeadingPathSeparators(assetName);
|
||||||
}
|
}
|
||||||
|
|
||||||
int index = this.RemainderImpl.IndexOfAny(ToolkitPathUtilities.PossiblePathSeparators);
|
/// <summary>Move the enumerator to the next segment.</summary>
|
||||||
|
/// <returns>Returns true if a new value was found (accessible via <see cref="Current"/>).</returns>
|
||||||
// no more separator characters found, I'm done.
|
public bool MoveNext()
|
||||||
if (index < 0)
|
|
||||||
{
|
{
|
||||||
this.Current = this.RemainderImpl;
|
if (this.RemainderImpl.Length == 0)
|
||||||
this.RemainderImpl = ReadOnlySpan<char>.Empty;
|
return false;
|
||||||
|
|
||||||
|
int index = this.RemainderImpl.IndexOfAny(ToolkitPathUtilities.PossiblePathSeparators);
|
||||||
|
|
||||||
|
// no more separator characters found, I'm done.
|
||||||
|
if (index < 0)
|
||||||
|
{
|
||||||
|
this.Current = this.RemainderImpl;
|
||||||
|
this.RemainderImpl = ReadOnlySpan<char>.Empty;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Yield the next separate character bit
|
||||||
|
this.Current = this.RemainderImpl[..index];
|
||||||
|
this.RemainderImpl = AssetNamePartEnumerator.TrimLeadingPathSeparators(this.RemainderImpl[(index + 1)..]);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Yield the next separate character bit
|
|
||||||
this.Current = this.RemainderImpl[..index];
|
|
||||||
this.RemainderImpl = AssetNamePartEnumerator.TrimLeadingPathSeparators(this.RemainderImpl[(index + 1)..]);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static ReadOnlySpan<char> TrimLeadingPathSeparators(ReadOnlySpan<char> span)
|
/*********
|
||||||
{
|
** Private methods
|
||||||
return span.TrimStart(new ReadOnlySpan<char>(ToolkitPathUtilities.PossiblePathSeparators));
|
*********/
|
||||||
|
/// <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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue