diff --git a/osu.Game/Utils/NamingUtils.cs b/osu.Game/Utils/NamingUtils.cs index 2439d4ba22..0f55c79e84 100644 --- a/osu.Game/Utils/NamingUtils.cs +++ b/osu.Game/Utils/NamingUtils.cs @@ -31,6 +31,30 @@ namespace osu.Game.Utils { string pattern = $@"^(?i){Regex.Escape(desiredName)}(?-i)( \((?[1-9][0-9]*)\))?$"; var regex = new Regex(pattern, RegexOptions.Compiled); + + int bestNumber = findBestNumber(existingNames, regex); + + return bestNumber == 0 + ? desiredName + : $"{desiredName} ({bestNumber.ToString()})"; + } + + /// + /// 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}"; + } + + private static int findBestNumber(IEnumerable existingNames, Regex regex) + { var takenNumbers = new HashSet(); foreach (string name in existingNames) @@ -54,23 +78,7 @@ namespace osu.Game.Utils while (takenNumbers.Contains(bestNumber)) bestNumber += 1; - return bestNumber == 0 - ? 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}"; + return bestNumber; } } }