-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeneratorParser.cs
57 lines (54 loc) · 2.67 KB
/
GeneratorParser.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
namespace Experiment {
class GeneratorParser {
/// <summary>
/// Generate numbers, 1 per set. Pass a Random as source of RNG.
/// </summary>
/// <param name="set_"> set of range and restrictions to parse from
/// <para> "0:99%(2)==1", generates 0-99 number, which %2 == 1 </para>
/// <para> "0:99%({0})", generates 0-99 number, which % of the previous number (0 number in the set) == 0 </para>
/// <para> "0:99<=({1}&&5||4)==3", generates 0-99 number, which % of the previous number (1 number in the set) and 5 or only %4 == 3 </para>
/// </param>
/// <param name="random"> RNG source </param>
public static List<double> Parse(in string[] set_, Random random) {
List<double> generatedSet = new List<double>{};
for(int i = 0; i < set_.Length; i++) {
string builtSet = BuildSet(set_[i], generatedSet);
generatedSet.Add(Generator.Generate(builtSet, random));
}
return generatedSet;
}
/// <summary>
/// Replace tokens in string, from passed set of numbers. Optional: ignore strings starting with `{}`
/// </summary>
/// <param name="set"> string set
/// <para> ex. "This is number 0 {0}, This is number 1 {1}
/// </param>
public static string BuildSet(string set, in List<double> numberList, bool ignoreCurly = false) {
while(true) {
int index = 0;
int indexOpen = set.IndexOf('{');
int indexClose = set.IndexOf('}');
if((ignoreCurly == false && indexOpen <= 0) || indexClose == -1) {
return set;
}
for(int i = 1; i <= indexClose - indexOpen -1; i++) {
if(set[indexOpen + i] == '-') {
index = -1 * (set[indexOpen + i + 1] - '0');
i++;
} else {
index = index * 10 + (set[indexOpen + i] - '0');
}
}
if(index >= numberList.Count) {
Console.WriteLine("There are only {0} numbers generated. Request for {1} number index failed", numberList.Count);
throw new Exception("Invalid character");
}
if(index >= 0) {
set = set.Substring(0, indexOpen) + numberList[index] + set.Substring(indexClose + 1);
} else {
set = set.Substring(0, indexOpen) + numberList[numberList.Count + index] + set.Substring(indexClose + 1);
}
}
}
}
}