Mercury library 1.0 Translation of CFG grammars into an object model (Bachelor's thesis).

- Main Page
- Packages
- Classes
- Files

- File List

All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
Extensions.cs
Go to the documentation of this file.
1 using Mercury.Nucleus.Collections;
2 using Mercury.Scanning;
3 using System;
4 using System.Collections.Generic;
5 using System.Text;
6
7 namespace Mercury
8 {
12 public static class Extensions
13 {
14 //--[ Private stuff ]------------------------------------------------
15
16 private static Tokenizer defaultTokenizer;
17 private static Random rgen;
18 private static string rchars;
19
20 #region [ UniqueCharSeqGenerator ]
21
22 private static class UniqueCharSeqGenerator
23 {
24 private static ulong nextid;
25 private static char[] chrarr;
26
27 static UniqueCharSeqGenerator()
28 {
29 nextid = 0L;
30 List<char> chars = new List<char>();
31
32 for (char c = '0'; c <= '9'; ++c) chars.Add(c);
33 for (char c = 'a'; c <= 'z'; ++c) chars.Add(c);
34 for (char c = 'A'; c <= 'Z'; ++c) chars.Add(c);
35
36 chrarr = chars.ToArray();
37 }
38
39 public static string GetNextSeq()
40 {
41 var id = nextid++;
42 var cn = (ulong) chrarr.Length;
43 var sb = new StringBuilder();
44
45 bool init = true;
46 ulong rm;
47 do
48 {
49 if (init) init = false;
50 else if (id != 0) --id;
51
52 rm = id % cn;
53 id /= cn;
54 sb.Insert(0, chrarr[rm]);
55 } while (id != 0L);
56
57 return sb.ToString();
58 }
59 }
60
61 #endregion
62
63 //--[ Static constructor ]-------------------------------------------
64
65 static Extensions()
66 {
67 defaultTokenizer = new Tokenizer();
68 rgen = new Random();
69 StringBuilder sb = new StringBuilder();
70
71 for (char c = 'a'; c < 'z'; ++c)
72 sb.Append(c).Append(char.ToUpperInvariant(c));
73
74 rchars = sb.ToString();
75 }
76
77 //--[ Global extensions ]--------------------------------------------
78
82 public static bool Power2(int x)
83 { return (x != 0) && (x & (x - 1)) == 0; }
84
90 public static int IntegralPow(int a, uint exp)
91 {
92 int ret = 1;
93
94 while (exp != 0)
95 {
96 if ((exp & 1) == 1) ret *= a;
97 a *= a;
98 exp >>= 1;
99 }
100
101 return ret;
102 }
103
108 public static string UniqueCharSeq()
109 { return UniqueCharSeqGenerator.GetNextSeq(); }
110
115 public static string RandomString(int length, string init = "")
116 {
117 StringBuilder sb = new StringBuilder(init, init.Length + length);
118
119 for (int ix = 0; ix < length; ++ix)
120 sb.Append(rchars[rgen.Next(rchars.Length)]);
121
122 return sb.ToString();
123 }
124
135 public static string RandomString(string pattern)
136 {
137 StringBuilder sb = new StringBuilder(pattern.Length);
138
139 for (int ix = 0; ix < pattern.Length; ++ix)
140 switch (pattern[ix])
141 {
142 case '.': sb.Append(rchars[rgen.Next(rchars.Length)]); break;
143 case '%': sb.Append(rgen.Next(0, 9)); break;
144 case '*': sb.Append(UniqueCharSeqGenerator.GetNextSeq()); break;
145 default: sb.Append(pattern[ix]); break;
146 }
147
148 return sb.ToString();
149 }
150
151 //--[ Extensions ]---------------------------------------------------
152
153 #region [ String ]
154
161 public static IList<Token> Tokenize(this string src)
162 { return defaultTokenizer.Tokenize(src); }
163
169 public static string Unescape(this string src)
170 {
171 int start = src.IndexOf('\\');
172 if (start == -1)
173 return src;
174
175 StringBuilder sb = new StringBuilder(src, 0, start, src.Length - 1);
176 for(int ix = start; ix < src.Length; ++ix)
177 {
178 if (src[ix] != '\\')
179 sb.Append(src[ix]);
180 }
181
182 return sb.ToString();
183 }
184
185 #endregion
186
187 #region [ IEnumerable ]
188
197 public static IMultiDictionary<TKey, TValue> ToMultiDictionary<TSource, TKey, TValue>(
198 this IEnumerable<TSource> source,
199 Func<TSource, TKey> keySelector,
200 Func<TSource, TValue> valueSelector)
201 {
202 var temp = new ListMultiDictionary<TKey, TValue>();
203
204 foreach (TSource value in source)
205 temp.Add(keySelector(value), valueSelector(value));
206
207 return temp;
208 }
209
216 public static IMultiDictionary<TKey, TSource> ToMultiDictionary<TSource, TKey>(
217 this IEnumerable<TSource> source,
218 Func<TSource, TKey> keySelector)
219 { return ToMultiDictionary(source, keySelector, x => x); }
220
221 #endregion
222
223 #region [ IReadOnlyList ]
224
233 public static T ElementAtOrLast<T>(this IReadOnlyList<T> source, int index)
234 { return index >= source.Count ? source[source.Count - 1] : source[index]; }
235
236 #endregion
237
238 }
239 }
Mercury.Extensions.RandomString
static string RandomString(string pattern)
Creates a random string according to the pattern. The pattern can contain any characters that will re...
Definition: Extensions.cs:135
Mercury.Extensions.IntegralPow
static int IntegralPow(int a, uint exp)
Computes an integral power of given number
Definition: Extensions.cs:90
Mercury.Extensions.Power2
static bool Power2(int x)
Checks whether the given number is a power of two
Definition: Extensions.cs:82
Mercury.Extensions.ToMultiDictionary< TSource, TKey >
static IMultiDictionary< TKey, TSource > ToMultiDictionary< TSource, TKey >(this IEnumerable< TSource > source, Func< TSource, TKey > keySelector)
Creates a MultiDictionary from the sequence of values
Definition: Extensions.cs:216
Mercury.Extensions.RandomString
static string RandomString(int length, string init="")
Creates a random [A-Za-z] string.
Definition: Extensions.cs:115
Mercury.Scanning.Tokenizer
A default implementation of ITokenizer interface using Microsoft Regular Expressions. Splits tokens in the following fashion:
Definition: Tokenizer.cs:71
Mercury.Extensions.ElementAtOrLast< T >
static T ElementAtOrLast< T >(this IReadOnlyList< T > source, int index)
Returns the element specified by the index or the last element if the index is greater than number of...
Definition: Extensions.cs:233
Mercury.Extensions.Tokenize
static IList< Token > Tokenize(this string src)
Uses defaultTokenizer (Mercury.Nucleus.Tokens.Tokenizer) to tokenize string
Definition: Extensions.cs:161
Mercury.Nucleus.Collections.IMultiDictionary< TKey, TValue >
Represents a multi-valued dictionary, in other words a map between a key and a collection of values...
Definition: MultiDictionary.cs:64
Mercury.Extensions.UniqueCharSeq
static string UniqueCharSeq()
Creates a unique sequence of [A-Za-z] characters.
Definition: Extensions.cs:108
Mercury.Extensions.Unescape
static string Unescape(this string src)
Removes '\' from the string more efficiently than src.Where(x => x != '\')
Definition: Extensions.cs:169
Mercury.Extensions
Provides extension methods for the Mercury library.
Definition: Extensions.cs:12
Mercury.Nucleus.Collections.ListMultiDictionary< TKey, TValue >
An implementation of IMultiDictionary{TKey,TValue} using T:System.Collections.Generic.List{T} as an underlying collection of values.
Definition: MultiDictionary.cs:223
Mercury.Extensions.ToMultiDictionary< TSource, TKey, TValue >
static IMultiDictionary< TKey, TValue > ToMultiDictionary< TSource, TKey, TValue >(this IEnumerable< TSource > source, Func< TSource, TKey > keySelector, Func< TSource, TValue > valueSelector)
Creates a multidictionary from the sequence of values
Definition: Extensions.cs:197