1 module viva.io.format; 2 3 /++ 4 + Formats a given string 5 + Params: 6 + str = The string to be formatted 7 + Returns: The formatted string 8 +/ 9 string formatStr(string str) pure nothrow @safe 10 { 11 return str; 12 } 13 14 /++ 15 + Appends text to both sides of a given string 16 + Params: 17 + side = The text to be appended on both sides 18 + str = The string it should be appended to 19 + Returns: The new string 20 +/ 21 string envelop(string side, string str) pure nothrow @safe 22 { 23 return side ~ str ~ side; 24 } 25 26 /++ 27 + Appends a prefix and suffix to a given string 28 + Params: 29 + prefix = The text to be appended on the right of the string 30 + str = The string it should be appended to 31 + suffix = The text to be appended on the left of the string 32 + Returns: The new string 33 +/ 34 string envelop(string prefix, string str, string suffix) pure nothrow @safe 35 { 36 return prefix ~ str ~ suffix; 37 } 38 39 /++ 40 + Sanitizes a given string 41 + Params: 42 + str = The string that should be sanitized 43 + target = The text that should be removed 44 + Returns: The new, sanitized string 45 +/ 46 string sanitize(string str, string target) pure nothrow @safe 47 { 48 import std.array : replace; 49 50 return str.replace(target, ""); 51 } 52 53 /++ 54 + Prettifies a given string using regex 55 + Params: 56 + str = The string to be prettified 57 + regex = The regex that should be used to prettify (Default = `(?<!^)(?=[A-Z])`) 58 + Returns: The new, prettified string 59 +/ 60 string prettify(string str, string regex = r"(?<!^)(?=[A-Z])") pure nothrow @safe 61 { 62 import std.array : replace; 63 64 return str.replace(regex, " "); 65 } 66 67 /++ 68 + Checks if a string is empty, and returns a null 69 + Params: 70 + str = The target string 71 + Returns: `null` if the string is empty, else it returns the string 72 +/ 73 string asNullIfEmpty(string str) pure nothrow @safe 74 { 75 if (str == "") return null; 76 else return str; 77 } 78 79 /++ 80 + Checks if a string is empty, and returns an alternative 81 + Params: 82 + str = The target string 83 + alt = The alternate string 84 + Returns: `alt` if the string is empty, else it returns the string 85 +/ 86 string withAlternative(string str, string alt) pure nothrow @safe 87 { 88 if (asNullIfEmpty(str) == null) return alt; 89 else return str; 90 }