2using System.Text.RegularExpressions;
13 private readonly Dictionary<string, MappingExpression> mappings =
new Dictionary<string, MappingExpression>();
14 private MappingStep mappingsStep0 =
null;
15 private bool hasMaps =
false;
48 if (!this.mappings.TryGetValue(RegexPattern, out MappingExpression Exp))
50 Exp =
new MappingExpression()
52 Pattern = RegexPattern,
53 Expression =
new Regex(RegexPattern, RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant),
56 this.mappings[RegexPattern] = Exp;
59 Dictionary<string, bool> Names =
new Dictionary<string, bool>();
61 foreach (
string Name
in Exp.Expression.GetGroupNames())
70 j = MapTo.IndexOf(
'}', i + 1);
74 string Name = MapTo.Substring(i + 1, j - i - 1);
76 if (Names.ContainsKey(Name))
78 Parameters.
Add(
new KeyValuePair<int, string>(i, Name));
79 MapTo = MapTo.Remove(i, j - i + 1);
89 Exp.Parameters = Parameters.
ToArray();
92 this.mappingsStep0 =
null;
105 if (this.mappings.Remove(RegexPattern))
107 this.hasMaps = this.mappings.Count > 0;
108 this.mappingsStep0 =
null;
131 foreach (KeyValuePair<string, MappingExpression> P
in this.mappings)
133 if (!(P.Value.Tag is
null) && P.Value.Tag.Equals(Tag))
135 if (ToRemove is
null)
142 if (!(ToRemove is
null))
146 while (!(Loop is
null))
148 for (
int i = Loop.
Start, c = Loop.
Pos; i < c; i++)
150 if (this.mappings.Remove(Loop[i]))
167 public bool TryMap(
string InputString, out
string Harmonized)
173 MappingStep
Step = this.mappingsStep0;
179 MappingExpression[] Expressions;
183 Expressions =
new MappingExpression[this.mappings.Count];
184 this.mappings.Values.CopyTo(Expressions, 0);
187 Step = this.mappingsStep0 = MappingStep.CalcStep((
char)0, Expressions);
190 foreach (
char ch
in InputString)
192 if (!(
Step.Expressions is
null))
194 foreach (Mapping Map
in Step.Expressions)
196 Match M = Map.Expression.Match(InputString);
197 if (M.Success && M.Index == 0 && M.Length == InputString.Length)
199 Harmonized = Map.MapSeed;
200 foreach (KeyValuePair<int, string> P
in Map.Parameters)
201 Harmonized = Harmonized.Insert(P.Key, M.Groups[P.Value]?.Value ??
string.Empty);
208 if (
Step.Next is
null)
213 foreach (MappingStep Step2
in Step.Next)
215 ch2 = Step2.Character;
231 if (!(
Step.Next is
null))
234 if (!(
Step.Expressions is
null))
236 foreach (Mapping Map
in Step.Expressions)
238 Match M = Map.Expression.Match(InputString);
239 if (M.Success && M.Index == 0 && M.Length == InputString.Length)
241 Harmonized = Map.MapSeed;
242 foreach (KeyValuePair<int, string> P
in Map.Parameters)
243 Harmonized = Harmonized.Insert(P.Key, M.Groups[P.Value]?.Value ??
string.Empty);
254 private class Mapping
256 public Regex Expression;
257 public string MapSeed;
258 public KeyValuePair<int, string>[] Parameters;
261 private class MappingExpression
263 public string Pattern;
264 public Regex Expression;
265 public string MapSeed;
266 public KeyValuePair<int, string>[] Parameters;
270 private class MappingStep
272 public char Character;
273 public MappingStep[] Next;
274 public Mapping[] Expressions;
276 internal static MappingStep CalcStep(
char Character, MappingExpression[] Expressions)
278 SortedDictionary<char, ChunkedList<MappingExpression>> Next =
new SortedDictionary<char, ChunkedList<MappingExpression>>();
282 foreach (MappingExpression Exp
in Expressions)
284 if (
string.IsNullOrEmpty(Exp.Pattern))
285 AddMap(ref Maps, Exp);
288 char ch = Exp.Pattern[0];
290 if (
"\\^$.|?*+()[{".IndexOf(ch) >= 0)
291 AddMap(ref Maps, Exp);
300 List.Add(
new MappingExpression()
302 Pattern = Exp.Pattern.Substring(1),
303 Expression = Exp.Expression,
304 MapSeed = Exp.MapSeed,
305 Parameters = Exp.Parameters,
317 Steps.
Add(CalcStep(P.Key, P.Value.ToArray()));
320 return new MappingStep()
322 Character = Character,
324 Expressions = Maps?.ToArray()
333 Maps.Add(
new Mapping()
335 Expression = Exp.Expression,
336 MapSeed = Exp.MapSeed,
337 Parameters = Exp.Parameters
343 return new string(this.Character, 1);
Node referencing a chunk in a ChunkedList<T>
ChunkNode< T > Next
Next chunk
int Pos
Index after the last element in chunk.
int Start
Index of first element in chunk.
A chunked list is a linked list of chunks of objects of type T .
void Reverse()
Reverses the order of the elements in the collection.
ChunkNode< T > FirstChunk
First chunk
int IndexOf(T Item)
Determines the index of a specific item
void Add(T Item)
Adds an item to the collection.
T[] ToArray()
Returns an array containing all elements of the collection.
Maps strings of text to a harmonized set of strings using collections of regular expressions and para...
int UnregisterMappings(object Tag)
Unregisters mappings tagged with a specific object.
void RegisterMapping(string RegexPattern, string MapTo)
Registers a mapping.
bool TryMap(string InputString, out string Harmonized)
Tries to map a string using registered mappings.
void RegisterMapping(string RegexPattern, string MapTo, object Tag)
Registers a mapping.
HarmonizedTextMap()
Maps strings of text to a harmonized set of strings using collections of regular expressions and para...
bool UnregisterMapping(string RegexPattern)
Unregisters a mapping.
Represents a sub-sequence of symbols.
delegate string ToString(IElement Element)
Delegate for callback methods that convert an element value to a string.