Skip to content

Instantly share code, notes, and snippets.

@codelynx
Created March 3, 2021 02:59
Show Gist options
  • Save codelynx/df0c31ef35de14a9d6ac6d41d565fa02 to your computer and use it in GitHub Desktop.
Save codelynx/df0c31ef35de14a9d6ac6d41d565fa02 to your computer and use it in GitHub Desktop.
Generating a unique name by adding or incrementing suffix number of base name.
// String.makeUniqueName
//
// The MIT License (MIT)
//
// Copyright (c) 2021 Electricwoods LLC, Kaz Yoshikawa.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import Foundation
// function: makeUniqueName()
// In some case, you may want to find a name to duplicate a file by adding or by increminting suffix number. This function adds or incremnet its
// suffix number, you may have to provide a closure to check if given name is uniqe to the context.
extension String {
public static func makeUniqueName(name: String, validating: ((String)->Bool)) -> String {
var candidate = name
let numberRange: Range<String.Index>? = name.range(of: "[0-9]+$", options: [.regularExpression])
var number: Int = numberRange.flatMap { Int(name[$0]) } ?? 2
while !validating(candidate) {
defer { number += 1 }
candidate = numberRange.map { name.replacingCharacters(in: $0, with: String(number + 1)) } ?? (name + " " + String(number))
}
return candidate
}
}
let names = ["Hello", "Hello 2", "Hello 3", "Hello 12", "World", "World2", "World3", "World-21"]
print(String.makeUniqueName(name: "Hello") { (name) -> Bool in !names.contains(name) }) // "Hello 4"
print(String.makeUniqueName(name: "Hello2") { (name) -> Bool in !names.contains(name) }) // "Hello2"
print(String.makeUniqueName(name: "Hello 12") { (name) -> Bool in !names.contains(name) }) // "Hello 13"
print(String.makeUniqueName(name: "World") { (name) -> Bool in !names.contains(name) }) // "World 2"
print(String.makeUniqueName(name: "World2") { (name) -> Bool in !names.contains(name) }) // "World4"
print(String.makeUniqueName(name: "World-21") { (name) -> Bool in !names.contains(name) }) // "World-22"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment