-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathUniqueNameManager.cs
More file actions
58 lines (44 loc) · 1.67 KB
/
UniqueNameManager.cs
File metadata and controls
58 lines (44 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System.Collections.Generic;
using System.IO;
namespace Microsoft.ClearScript.Util
{
internal interface IUniqueNameManager
{
string GetUniqueName(string inputName, string alternate);
}
internal sealed class UniqueNameManager : IUniqueNameManager
{
private readonly Dictionary<string, uint> map = new();
#region IUniqueNameManager implementation
public string GetUniqueName(string inputName, string alternate)
{
lock (map)
{
var nonBlankName = inputName.ToNonBlank(alternate);
map.TryGetValue(nonBlankName, out var count);
map[nonBlankName] = ++count;
return (count < 2) ? nonBlankName : string.Concat(nonBlankName, " [", count, "]");
}
}
#endregion
}
internal sealed class UniqueFileNameManager : IUniqueNameManager
{
private readonly Dictionary<string, uint> map = new();
#region IUniqueNameManager implementation
public string GetUniqueName(string inputName, string alternate)
{
lock (map)
{
var nonBlankName = Path.GetFileNameWithoutExtension(inputName).ToNonBlank(alternate);
var extension = Path.GetExtension(inputName);
map.TryGetValue(nonBlankName, out var count);
map[nonBlankName] = ++count;
return (count < 2) ? string.Concat(nonBlankName, extension) : string.Concat(nonBlankName, " [", count, "]", extension);
}
}
#endregion
}
}