1 /// Converts all elements in range into a string separated by separator. 2 module ddash.algorithm.stringify; 3 4 /// 5 unittest { 6 assert([1, 2, 3].stringifySeperatedBy(',') == "1,2,3"); 7 assert([1, 2, 3].stringify == "123"); 8 assert([1, 2, 3].stringifySeperatedBy("-") == "1-2-3"); 9 } 10 11 import ddash.common; 12 13 /** 14 Converts all elements in range into a string separated by separator. 15 16 Params: 17 range = an input range 18 sep = string/char to be used as seperator, default is empty. 19 20 Returns: 21 New string 22 23 Since: 24 0.0.1 25 */ 26 string stringifySeperatedBy(Range, S)(Range range, S sep) if (from!"std.traits".isSomeString!S) { 27 import std.algorithm: joiner, map; 28 import std.conv: to; 29 import std.array; 30 return range 31 .map!(to!string) 32 .joiner(sep) 33 .to!string; 34 } 35 36 /// ditto 37 string stringifySeperatedBy(Range, S)(Range range, S sep) if (from!"std.traits".isSomeChar!S) { 38 import std.conv: to; 39 return range.stringifySeperatedBy(sep.to!string); 40 } 41 42 /** 43 Converts a list of values in to a string 44 45 Params: 46 values = combinable sequence of values 47 48 Returns: 49 New string 50 51 Since: 52 0.0.1 53 */ 54 string stringify(Values...)(Values values) { 55 import ddash.algorithm.concat; 56 return concat(values).stringifySeperatedBy(""); 57 }