Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
RegexCache.cs
Go to the documentation of this file.
6
8
9internal sealed class RegexCache
10{
11 internal readonly struct Key : IEquatable<Key>
12 {
13 private readonly string _pattern;
14
15 private readonly string _culture;
16
17 private readonly RegexOptions _options;
18
19 private readonly TimeSpan _matchTimeout;
20
28
29 public override bool Equals([NotNullWhen(true)] object obj)
30 {
31 if (obj is Key other)
32 {
33 return Equals(other);
34 }
35 return false;
36 }
37
38 public bool Equals(Key other)
39 {
40 if (_pattern.Equals(other._pattern) && _culture.Equals(other._culture) && _options == other._options)
41 {
42 return _matchTimeout == other._matchTimeout;
43 }
44 return false;
45 }
46
47 public override int GetHashCode()
48 {
49 return _pattern.GetHashCode() ^ (int)_options;
50 }
51 }
52
53 private sealed class Node
54 {
55 public readonly Key Key;
56
57 public readonly Regex Regex;
58
59 public long LastAccessStamp;
60
62 {
63 Key = key;
64 Regex = regex;
65 }
66 }
67
68 private static volatile Node s_lastAccessed;
69
71
72 private static readonly List<Node> s_cacheList = new List<Node>(15);
73
74 private static readonly Random s_random = new Random();
75
76 private static int s_maxCacheSize = 15;
77
78 private static object SyncObj => s_cacheDictionary;
79
80 public static int MaxCacheSize
81 {
82 get
83 {
84 return s_maxCacheSize;
85 }
86 set
87 {
89 {
91 if (value == 0)
92 {
93 s_cacheDictionary.Clear();
94 s_cacheList.Clear();
95 s_lastAccessed = null;
96 }
97 else if (value < s_cacheList.Count)
98 {
99 s_cacheList.Sort((Node n1, Node n2) => Volatile.Read(ref n2.LastAccessStamp).CompareTo(Volatile.Read(ref n1.LastAccessStamp)));
101 for (int i = value; i < s_cacheList.Count; i++)
102 {
104 }
105 s_cacheList.RemoveRange(value, s_cacheList.Count - value);
106 }
107 }
108 }
109 }
110
111 public static Regex GetOrAdd(string pattern)
112 {
116 Regex regex = Get(key);
117 if (regex == null)
118 {
120 Add(key, regex);
121 }
122 return regex;
123 }
124
126 {
130 CultureInfo cultureInfo = (((options & RegexOptions.CultureInvariant) != 0) ? CultureInfo.InvariantCulture : CultureInfo.CurrentCulture);
131 Key key = new Key(pattern, cultureInfo.ToString(), options, matchTimeout);
132 Regex regex = Get(key);
133 if (regex == null)
134 {
136 Add(key, regex);
137 }
138 return regex;
139 }
140
141 private static Regex Get(Key key)
142 {
143 long num = 0L;
145 if (node != null)
146 {
147 if (key.Equals(node.Key))
148 {
149 return node.Regex;
150 }
151 num = Volatile.Read(ref node.LastAccessStamp);
152 }
153 if (s_maxCacheSize != 0 && s_cacheDictionary.TryGetValue(key, out var value))
154 {
155 Volatile.Write(ref value.LastAccessStamp, num + 1);
157 return value.Regex;
158 }
159 return null;
160 }
161
162 private static void Add(Key key, Regex regex)
163 {
164 lock (SyncObj)
165 {
166 if (s_maxCacheSize == 0 || s_cacheDictionary.TryGetValue(key, out var value))
167 {
168 return;
169 }
170 if (s_cacheList.Count == s_maxCacheSize)
171 {
172 int num;
173 bool flag;
174 if (s_maxCacheSize <= 30)
175 {
176 num = s_cacheList.Count;
177 flag = false;
178 }
179 else
180 {
181 num = 30;
182 flag = true;
183 }
184 int index = (flag ? s_random.Next(s_cacheList.Count) : 0);
185 long num2 = Volatile.Read(ref s_cacheList[index].LastAccessStamp);
186 for (int i = 1; i < num; i++)
187 {
188 int num3 = (flag ? s_random.Next(s_cacheList.Count) : i);
189 long num4 = Volatile.Read(ref s_cacheList[num3].LastAccessStamp);
190 if (num4 < num2)
191 {
192 index = num3;
193 num2 = num4;
194 }
195 }
197 s_cacheList.RemoveAt(index);
198 }
199 Node node = (s_lastAccessed = new Node(key, regex));
200 s_cacheList.Add(node);
201 s_cacheDictionary.TryAdd(key, node);
202 }
203 }
204}
static CultureInfo CurrentCulture
static Regex GetOrAdd(string pattern)
static readonly List< Node > s_cacheList
Definition RegexCache.cs:72
static void Add(Key key, Regex regex)
static Regex GetOrAdd(string pattern, RegexOptions options, TimeSpan matchTimeout)
static readonly ConcurrentDictionary< Key, Node > s_cacheDictionary
Definition RegexCache.cs:70
static void ValidateOptions(RegexOptions options)
Definition Regex.cs:161
static void ValidatePattern(string pattern)
Definition Regex.cs:153
static readonly TimeSpan s_defaultMatchTimeout
Definition Regex.cs:39
static void ValidateMatchTimeout(TimeSpan matchTimeout)
Definition Regex.cs:169
static bool Read(ref bool location)
Definition Volatile.cs:67
static void Write(ref bool location, bool value)
Definition Volatile.cs:74
override bool Equals([NotNullWhen(true)] object obj)
Definition RegexCache.cs:29
Key(string pattern, string culture, RegexOptions options, TimeSpan matchTimeout)
Definition RegexCache.cs:21