From 760e39479e9159dbb586d016dcbf21408df9839d Mon Sep 17 00:00:00 2001 From: Ferdinand Majerech Date: Sat, 2 Aug 2014 23:29:55 +0200 Subject: [PATCH] yaml_gen now gens strings with user-specified alphabet, with Unicode support. --- examples/yaml_gen/config.yaml | 7 ++++--- examples/yaml_gen/yaml_gen.d | 14 +++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/examples/yaml_gen/config.yaml b/examples/yaml_gen/config.yaml index 910b637..5484686 100644 --- a/examples/yaml_gen/config.yaml +++ b/examples/yaml_gen/config.yaml @@ -1,8 +1,8 @@ -root-type: seq +root-type: map documents: 2 complex-keys: false collection-keys: false -min-nodes-per-document: 65536 +min-nodes-per-document: 4096 encoding: utf-8 indent: 4 text-width: 40 @@ -11,7 +11,8 @@ text-width: 40 #we end up with extremely deeply nested structures string: - probability: 10 + probability: 20 + alphabet: " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_0123456789ábćčďéěǵǧȟíǐǰḱǩĺľḿńňóǒôäṕŕřśšť" range: {min: 1, max: 40, dist: cubic} int: probability: 10 diff --git a/examples/yaml_gen/yaml_gen.d b/examples/yaml_gen/yaml_gen.d index 68eb21c..96be25a 100644 --- a/examples/yaml_gen/yaml_gen.d +++ b/examples/yaml_gen/yaml_gen.d @@ -1,6 +1,7 @@ ///Random YAML generator. Used to generate benchmarking inputs. +import std.algorithm; import std.conv; import std.datetime; import std.math; @@ -10,9 +11,6 @@ import std.string; import dyaml.all; -immutable alphabet = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_"; -immutable digits = "0123456789"; - Node config; Node function(bool)[string] generators; auto typesScalar = ["string", "int", "float", "bool", "timestamp", "binary"]; @@ -65,7 +63,7 @@ real randomReal(const real min, const real max, const string distribution = "lin return min + (max - min) * randomNormalized(distribution); } -char randomChar(const string chars) +dchar randomChar(const dstring chars) { return chars[randomLong(0, chars.length - 1)]; } @@ -84,17 +82,19 @@ Node genString(bool root = false) { auto range = config["string"]["range"]; + auto alphabet = config["string"]["alphabet"].as!dstring; + const chars = randomLong(range["min"].as!uint, range["max"].as!uint, range["dist"].as!string); - char[] result = new char[chars]; + dchar[] result = new dchar[chars]; result[0] = randomChar(alphabet); foreach(i; 1 .. chars) { - result[i] = randomChar(alphabet ~ digits); + result[i] = randomChar(alphabet); } - return Node(cast(string)result); + return Node(result.to!string); } Node genInt(bool root = false)