From 8412a441796b5d8bbea63b9a45b5a727084ab725 Mon Sep 17 00:00:00 2001 From: Piggey Date: Wed, 30 Nov 2022 18:32:14 +0100 Subject: [PATCH] create `NamingUtils.GetNextBestFilename()` --- osu.Game/Utils/NamingUtils.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/osu.Game/Utils/NamingUtils.cs b/osu.Game/Utils/NamingUtils.cs index 482e3d0954..ca4667b82a 100644 --- a/osu.Game/Utils/NamingUtils.cs +++ b/osu.Game/Utils/NamingUtils.cs @@ -1,7 +1,9 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using System; using System.Collections.Generic; +using System.Linq; using System.Text.RegularExpressions; namespace osu.Game.Utils @@ -57,5 +59,19 @@ namespace osu.Game.Utils ? desiredName : $"{desiredName} ({bestNumber})"; } + + /// + /// Given a set of and a desired target + /// finds a filename closest to that is not in + /// + /// SHOULD NOT CONTAIN the file extension. + /// + /// + public static string GetNextBestFilename(IEnumerable existingFilenames, string desiredName, string fileExtension) + { + var stripped = existingFilenames.Select(filename => filename.Substring(0, filename.Length - fileExtension.Length)); + + return $"{GetNextBestName(stripped, desiredName)}{fileExtension}"; + } } }