String to replace tokens in.
Default lower bound when no range given.
Default upper bound when no range given.
A new string with occurrences of $random and $random(i..n) replaced, or the original string if there were no changes made.
1 import lu.string : advancePast, splitInto; 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.advancePast(' ').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.advancePast(' ').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.advancePast(' ').to!int; 20 assert(((number >= -20) && (number < -10)), number.to!string); 21 } 22 { 23 // syntax error, no bounds given 24 enum line = "$random() bottles of beer on the wall"; 25 immutable replaced = line.replaceRandom(); 26 assert((replaced == line), replaced); 27 } 28 { 29 // syntax error, no closing paren 30 enum line = "$random( bottles of beer on the wall"; 31 immutable replaced = line.replaceRandom(); 32 assert((replaced == line), replaced); 33 } 34 { 35 // syntax error, no upper bound given 36 enum line = "$random(0..) bottles of beer on the wall"; 37 immutable replaced = line.replaceRandom(); 38 assert((replaced == line), replaced); 39 } 40 { 41 // syntax error, no bounds given 42 enum line = "$random(..) bottles of beer on the wall"; 43 immutable replaced = line.replaceRandom(); 44 assert((replaced == line), replaced); 45 } 46 { 47 // syntax error, invalid bounds 48 enum line = "$random(X.....Y) bottles of beer on the wall"; 49 immutable replaced = line.replaceRandom(); 50 assert((replaced == line), replaced); 51 } 52 { 53 // syntax error, missing closing paren 54 enum line = "$random(0..100 bottles of beer on the wall"; 55 immutable replaced = line.replaceRandom(); 56 assert((replaced == line), replaced); 57 } 58 { 59 // syntax error, parens include text 60 enum line = "$random(0..100 bottles of beer on the wall)"; 61 immutable replaced = line.replaceRandom(); 62 assert((replaced == line), replaced); 63 } 64 { 65 // syntax error, i == n 66 enum line = "$random(0..0) bottles of beer on the wall"; 67 immutable replaced = line.replaceRandom(); 68 assert((replaced == line), replaced); 69 } 70 { 71 // syntax error, i > n 72 enum line = "$random(2..1) bottles of beer on the wall"; 73 immutable replaced = line.replaceRandom(); 74 assert((replaced == line), replaced); 75 } 76 { 77 // partly syntax error 78 enum line = "blerp $random(50..55) $random(2..1) $random blarp"; 79 immutable replaced = line.replaceRandom(); 80 string slice = replaced; // mutable 81 string blerp, n1s, syntaxError, n2s, blarp; 82 slice.splitInto(blerp, n1s, syntaxError, n2s, blarp); 83 immutable n1 = n1s.to!int; 84 immutable n2 = n2s.to!int; 85 assert((blerp == "blerp"), blerp); 86 assert((n1 >= 50 && n1 < 55), n1.to!string); 87 assert((syntaxError == "$random(2..1)"), syntaxError); 88 assert((n2 >= 0 && n2 < 100), n2.to!string); 89 assert((blarp == "blarp"), blarp); 90 } 91 { 92 // empty string 93 enum line = string.init; 94 string replaced = line.replaceRandom(); 95 assert(!replaced.length, replaced); 96 } 97 { 98 // no $random token 99 enum line = "99 bottles of beer on the wall"; 100 immutable replaced = line.replaceRandom(); 101 assert((replaced == line), replaced); 102 } 103 { 104 // multiple tokens 105 enum line = "$random(1..100) $random(101..200) $random(201..300)"; 106 immutable replaced = line.replaceRandom(); 107 string slice = replaced; // mutable 108 string n1s, n2s, n3s; 109 slice.splitInto(n1s, n2s, n3s); 110 immutable n1 = n1s.to!int; 111 immutable n2 = n2s.to!int; 112 immutable n3 = n3s.to!int; 113 assert((n1 >= 1 && n1 < 100), n1.to!string); 114 assert((n2 >= 101 && n2 < 200), n2.to!string); 115 assert((n3 >= 201 && n3 < 300), n3.to!string); 116 } 117 { 118 // multiple tokens with other text 119 enum line = "$random $randomz $random gau gau"; 120 immutable replaced = line.replaceRandom(); 121 string slice = replaced; // mutable 122 string n1s, n2z, n3s; 123 slice.splitInto(n1s, n2z, n3s); 124 immutable n1 = n1s.to!int; 125 immutable n2 = n2z[0..$-1].to!int; 126 immutable n3 = n3s.to!int; 127 immutable z = n2z[$-1..$]; 128 assert((n1 >= 0 && n1 < 100), n1.to!string); 129 assert((n2 >= 0 && n2 < 100), n1.to!string); 130 assert((n3 >= 0 && n3 < 100), n3.to!string); 131 assert((z == "z"), z); 132 assert((slice == "gau gau"), slice); 133 } 134 { 135 // multiple tokens with other text again 136 enum line = "$random, $random! $random?"; 137 immutable replaced = line.replaceRandom(); 138 string slice = replaced; // mutable 139 string n1comma, n2excl, n3question; 140 slice.splitInto(n1comma, n2excl, n3question); 141 immutable n1 = n1comma[0..$-1].to!int; 142 immutable comma = n1comma[$-1..$]; 143 immutable n2 = n2excl[0..$-1].to!int; 144 immutable excl = n2excl[$-1..$]; 145 immutable n3 = n3question[0..$-1].to!int; 146 immutable question = n3question[$-1..$]; 147 assert((n1 >= 0 && n1 < 100), n1.to!string); 148 assert((n2 >= 0 && n2 < 100), n1.to!string); 149 assert((n3 >= 0 && n3 < 100), n3.to!string); 150 assert((comma == ","), comma); 151 assert((excl == "!"), excl); 152 assert((question == "?"), question); 153 }
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.