This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClearnameResolver.cs
76 lines (61 loc) · 2.15 KB
/
ClearnameResolver.cs
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System.Collections.Generic;
namespace JPPSVN {
public class ClearnameResolver {
private readonly Dictionary<string, List<string>> nameToId, idToName;
public static readonly ClearnameResolver EMPTY = new ClearnameResolver(new Dictionary<string, List<string>>(), new Dictionary<string, List<string>>());
public ClearnameResolver(Dictionary<string, List<string>> nameToId, Dictionary<string, List<string>> idToName) {
this.nameToId = nameToId;
this.idToName = idToName;
}
public bool ResolveID(string name, out List<string> res) {
return nameToId.TryGetValue(name, out res);
}
public bool ResolveName(string id, out List<string> res) {
return idToName.TryGetValue(id, out res);
}
private static void InsertOrAppend<TKey, TValue>(Dictionary<TKey, List<TValue>> dictionary, TKey key, TValue value) {
if (dictionary.TryGetValue(key, out var list)) {
list.Add(value);
} else {
list = new List<TValue> { value };
dictionary.Add(key, list);
}
}
public ICollection<string> Names => nameToId.Keys;
public ICollection<string> Ids => idToName.Keys;
public static ClearnameResolver FromExternals(string property) {
Dictionary<string, List<string>> nameToId = new Dictionary<string, List<string>>(),
idToName = new Dictionary<string, List<string>>();
for (int i = 0; i < property.Length; ++i) {
while (property[i] != '"') {
++i;
if (!(i < property.Length))
goto end;
}
int i1 = i;
++i;
if (!(i < property.Length))
goto end;
while (property[i] != '"') {
++i;
if (!(i < property.Length))
goto end;
}
int i2 = i;
int j;
for (j = i2; j > i1; --j) {
if (property[j] == '-')
break;
}
if (j != i1) {
string fullName = property.Substring(i1 + 1, j - 1 - (i1 + 1));
string sn = property.Substring(j + 2, i2 - 1 - (j + 1));
InsertOrAppend(nameToId, fullName, sn);
InsertOrAppend(idToName, sn, fullName);
}
}
end:
return new ClearnameResolver(nameToId, idToName);
}
}
}