replaceRandom

Replaces $random and $random(i..n) tokens in a string with corresponding random values.

If given only $random, a value between the passed defaultLowerBound inclusive to defaultUpperBound exclusive is substituted, whereas if a range of $random(i..n) is given, a value between i inclusive and n exclusive is substituted.

On syntax errors, or if n is not greater than i, the original line is silently returned.

@safe
replaceRandom
(
const string line
,
const long defaultLowerBound = 0
,
const long defaultUpperBound = 100
)

Parameters

line string

String to replace tokens in.

defaultLowerBound long

Default lower bound when no range given.

defaultUpperBound long

Default upper bound when no range given.

Return Value

Type: auto

A new string with occurences of $random and $random(i..n) replaced, or the original string if there were no changes made.

Examples

1 import lu.string : nom;
2 import std.conv : to;
3 
4 {
5     enum line = "$random bottles of beer on the wall";
6     string replaced = line.replaceRandom();  // mutable
7     immutable number = replaced.nom(' ').to!int;
8     assert(((number >= 0) && (number < 100)), number.to!string);
9 }
10 {
11     enum line = "$random(100..200) bottles of beer on the wall";
12     string replaced = line.replaceRandom();  // mutable
13     immutable number = replaced.nom(' ').to!int;
14     assert(((number >= 100) && (number < 200)), number.to!string);
15 }
16 {
17     enum line = "$random(-20..-10) bottles of beer on the wall";
18     string replaced = line.replaceRandom();  // mutable
19     immutable number = replaced.nom(' ').to!int;
20     assert(((number >= -20) && (number < -10)), number.to!string);
21 }
22 /*{
23     static if (__VERSION__ > 2089L)
24     {
25         // Fails pre-2.090 with Error: signed integer overflow
26         enum line = "$random(-9223372036854775808..9223372036854775807) bottles of beer on the wall";
27         string replaced = line.replaceRandom();  // mutable
28         immutable number = replaced.nom(' ').to!long;
29         //assert(((number >= cast(long)-9223372036854775808) && (number < 9223372036854775807)), number.to!string);
30     }
31 }*/
32 {
33     // syntax error, no bounds given
34     enum line = "$random() bottles of beer on the wall";
35     immutable replaced = line.replaceRandom();
36     assert((replaced == line), replaced);
37 }
38 {
39     // syntax error, no closing paren
40     enum line = "$random( bottles of beer on the wall";
41     immutable replaced = line.replaceRandom();
42     assert((replaced == line), replaced);
43 }
44 {
45     // syntax error, no upper bound given
46     enum line = "$random(0..) bottles of beer on the wall";
47     immutable replaced = line.replaceRandom();
48     assert((replaced == line), replaced);
49 }
50 {
51     // syntax error, no boudns given
52     enum line = "$random(..) bottles of beer on the wall";
53     immutable replaced = line.replaceRandom();
54     assert((replaced == line), replaced);
55 }
56 {
57     // syntax error, missing closing paren
58     enum line = "$random(0..100 bottles of beer on the wall";
59     immutable replaced = line.replaceRandom();
60     assert((replaced == line), replaced);
61 }
62 {
63     // syntax error, parens include text
64     enum line = "$random(0..100 bottles of beer on the wall)";
65     immutable replaced = line.replaceRandom();
66     assert((replaced == line), replaced);
67 }
68 {
69     // syntax error, i == n
70     enum line = "$random(0..0) bottles of beer on the wall";
71     immutable replaced = line.replaceRandom();
72     assert((replaced == line), replaced);
73 }
74 {
75     // syntax error, i > n
76     enum line = "$random(2..1) bottles of beer on the wall";
77     immutable replaced = line.replaceRandom();
78     assert((replaced == line), replaced);
79 }
80 {
81     // empty string
82     enum line = string.init;
83     string replaced = line.replaceRandom();
84     assert(!replaced.length, replaced);
85 }
86 {
87     // no $random token
88     enum line = "99 bottles of beer on the wall";
89     immutable replaced = line.replaceRandom();
90     assert((replaced == line), replaced);
91 }

Meta