1 using Mercury.Nucleus.Collections;
2 using Mercury.Scanning;
4 using System.Collections.Generic;
16 private static Tokenizer defaultTokenizer;
17 private static Random rgen;
18 private static string rchars;
20 #region [ UniqueCharSeqGenerator ]
22 private static class UniqueCharSeqGenerator
24 private static ulong nextid;
25 private static char[] chrarr;
27 static UniqueCharSeqGenerator()
30 List<char> chars =
new List<char>();
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);
36 chrarr = chars.ToArray();
39 public static string GetNextSeq()
42 var cn = (ulong) chrarr.Length;
43 var sb =
new StringBuilder();
49 if (init) init =
false;
50 else if (
id != 0) --id;
54 sb.Insert(0, chrarr[rm]);
69 StringBuilder sb =
new StringBuilder();
71 for (
char c =
'a'; c <
'z'; ++c)
72 sb.Append(c).Append(char.ToUpperInvariant(c));
74 rchars = sb.ToString();
83 {
return (x != 0) && (x & (x - 1)) == 0; }
96 if ((exp & 1) == 1) ret *= a;
109 {
return UniqueCharSeqGenerator.GetNextSeq(); }
117 StringBuilder sb =
new StringBuilder(init, init.Length + length);
119 for (
int ix = 0; ix < length; ++ix)
120 sb.Append(rchars[rgen.Next(rchars.Length)]);
122 return sb.ToString();
137 StringBuilder sb =
new StringBuilder(pattern.Length);
139 for (
int ix = 0; ix < pattern.Length; ++ix)
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;
148 return sb.ToString();
161 public static IList<Token>
Tokenize(
this string src)
162 {
return defaultTokenizer.Tokenize(src); }
171 int start = src.IndexOf(
'\\');
175 StringBuilder sb =
new StringBuilder(src, 0, start, src.Length - 1);
176 for(
int ix = start; ix < src.Length; ++ix)
182 return sb.ToString();
187 #region [ IEnumerable ]
198 this IEnumerable<TSource> source,
199 Func<TSource, TKey> keySelector,
200 Func<TSource, TValue> valueSelector)
204 foreach (TSource value
in source)
205 temp.Add(keySelector(value), valueSelector(value));
217 this IEnumerable<TSource> source,
218 Func<TSource, TKey> keySelector)
219 {
return ToMultiDictionary(source, keySelector, x => x); }
223 #region [ IReadOnlyList ]
234 {
return index >= source.Count ? source[source.Count - 1] : source[index]; }
static string RandomString(string pattern)
Creates a random string according to the pattern. The pattern can contain any characters that will re...
static int IntegralPow(int a, uint exp)
Computes an integral power of given number
static bool Power2(int x)
Checks whether the given number is a power of two
static IMultiDictionary< TKey, TSource > ToMultiDictionary< TSource, TKey >(this IEnumerable< TSource > source, Func< TSource, TKey > keySelector)
Creates a MultiDictionary from the sequence of values
static string RandomString(int length, string init="")
Creates a random [A-Za-z] string.
A default implementation of ITokenizer interface using Microsoft Regular Expressions. Splits tokens in the following fashion:
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...
static IList< Token > Tokenize(this string src)
Uses defaultTokenizer (Mercury.Nucleus.Tokens.Tokenizer) to tokenize string
Represents a multi-valued dictionary, in other words a map between a key and a collection of values...
static string UniqueCharSeq()
Creates a unique sequence of [A-Za-z] characters.
static string Unescape(this string src)
Removes '\' from the string more efficiently than src.Where(x => x != '\')
Provides extension methods for the Mercury library.
An implementation of IMultiDictionary{TKey,TValue} using T:System.Collections.Generic.List{T} as an underlying collection of values.
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