PKHeX/PKHeX.Core/Util/PathUtil.cs
Kurt 998b8f1ce0 Minor tweaks
Add unit test for VC kata/hiragana edge case
De-duplicate some slot change notifications
Span in more spots
2023-04-22 17:51:32 -07:00

32 lines
917 B
C#

using System;
using System.IO;
namespace PKHeX.Core;
public static partial class Util
{
/// <summary>
/// Cleans the local <see cref="fileName"/> by removing any invalid filename characters.
/// </summary>
/// <returns>New string without any invalid characters.</returns>
public static string CleanFileName(string fileName)
{
Span<char> result = stackalloc char[fileName.Length];
int ctr = GetCleanFileName(fileName, result);
if (ctr == fileName.Length)
return fileName;
return new string(result[..ctr]);
}
private static int GetCleanFileName(ReadOnlySpan<char> input, Span<char> output)
{
ReadOnlySpan<char> invalid = Path.GetInvalidFileNameChars();
int ctr = 0;
foreach (var c in input)
{
if (!invalid.Contains(c))
output[ctr++] = c;
}
return ctr;
}
}