-
-
Notifications
You must be signed in to change notification settings - Fork 746
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6de1dbb
commit 0ba7394
Showing
3 changed files
with
61 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
namespace Refit.Generator; | ||
|
||
public class UniqueNameBuilder() | ||
Check warning on line 3 in InterfaceStubGenerator.Shared/UniqueNameBuilder.cs GitHub Actions / build / build
|
||
{ | ||
private readonly HashSet<string> _usedNames = new(StringComparer.Ordinal); | ||
private readonly UniqueNameBuilder? _parentScope; | ||
|
||
private UniqueNameBuilder(UniqueNameBuilder parentScope) | ||
: this() | ||
{ | ||
_parentScope = parentScope; | ||
} | ||
|
||
public void Reserve(string name) => _usedNames.Add(name); | ||
|
||
public UniqueNameBuilder NewScope() => new(this); | ||
|
||
public string New(string name) | ||
{ | ||
var i = 0; | ||
var uniqueName = name; | ||
while (Contains(uniqueName)) | ||
{ | ||
uniqueName = name + i; | ||
i++; | ||
} | ||
|
||
_usedNames.Add(uniqueName); | ||
|
||
return uniqueName; | ||
} | ||
|
||
public void Reserve(IEnumerable<string> names) | ||
{ | ||
foreach (var name in names) | ||
Check warning on line 35 in InterfaceStubGenerator.Shared/UniqueNameBuilder.cs GitHub Actions / build / build
|
||
{ | ||
_usedNames.Add(name); | ||
} | ||
} | ||
|
||
private bool Contains(string name) | ||
{ | ||
if (_usedNames.Contains(name)) | ||
return true; | ||
|
||
if (_parentScope != null) | ||
return _parentScope.Contains(name); | ||
|
||
return false; | ||
} | ||
} |