private static string IntegerRandom(string input) { // replace <int_random> to both inclusive rand(int.MinValue, int.MaxValue). return(Regex.Replace(input, $"(<{VarNameRegex}int64_random>)", m => { Group group = m.Groups[1]; string originalInput = @group.Value; string replace = Convert.ToInt32(DoubleCompiler.RandomDouble(int.MinValue, int.MaxValue)) .ToString(); CreateLuaVariable(m.Groups["var_name"], Log, replace); Log.Info($"Replacing {originalInput} with {replace}"); return string.Format("{0}{1}{2}", m.Value.Substring(0, @group.Index - m.Index), replace, m.Value.Substring(@group.Index - m.Index + @group.Length)); })); }
private static string DoubleMatrix(string input, CultureInfo cultureInfo) { return(Regex.Replace(input, $@"<double_matrix(\(({DoubleIntervalRegex})?{DoublePrecisionRegex}?\))?: ?((?<width>\d+)|(?<width_var_name>\$[A-Za-z]\w*)) ?x ?((?<height>\d+)|(?<height_var_name>\$[A-Za-z]\w*))>", m => { Group group = m.Groups[1]; int width = 0; if (m.Groups["width"].Success) { width = int.Parse(m.Groups["width"].Value); } else if (m.Groups["width_var_name"].Success) { width = int.Parse(ShowLuaVarCompiler.GetLuaVarValue(m.Groups["width_var_name"].Value)); } int height = 0; if (m.Groups["height"].Success) { height = int.Parse(m.Groups["height"].Value); } else if (m.Groups["height_var_name"].Success) { height = int.Parse(ShowLuaVarCompiler.GetLuaVarValue(m.Groups["height_var_name"].Value)); } double from = 0; if (m.Groups["from"].Success) { from = double.Parse(m.Groups["from"].Value, NumberStyles.Any, CultureInfo.InvariantCulture); } double to = 1; if (m.Groups["to"].Success) { to = double.Parse(m.Groups["to"].Value, NumberStyles.Any, CultureInfo.InvariantCulture); } if (from > to) { Log.Error($"FROM > TO in {input}"); return input; } Func <double> randFunc; if (m.Groups["from"].Success && m.Groups["to"].Success) { randFunc = () => DoubleCompiler.RandomDouble(from, to); } else { randFunc = () => RandomProvider.Rand.NextDouble(); } string format = m.Groups["format"].Value; string replace = ""; string[] doublesRow = new string[width]; for (int i = 0; i < height; i++) { doublesRow = doublesRow.Select(x => (format.Length > 0) ? randFunc().ToString(format, cultureInfo) : randFunc().ToString(cultureInfo) ).ToArray(); replace += string.Join(Program._matrixColDelimiter, doublesRow) + Program._matrixRowDelimiter; } return replace.Trim(' ', '\n', '\r'); })); }