1 /*********************************************************************************************** 2 * Copyright: © 2017-2021 UI Manufaktur UG 3 * License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. 4 * Authors: UI Manufaktur Team 5 * Documentation [DE]: https://ui-manufaktur.com/docu/uim-core/dataytypes/string 6 ************************************************************************************************/ 7 module uim.core.datatypes.string_; 8 9 @safe: 10 import std.stdio; 11 import std..string; 12 import uim.core; 13 14 /// create a string with defined length and content 15 string fill(size_t length = 0, string txt = "0") { 16 string result; 17 if (txt) { 18 while (result.length < length) result ~= txt; 19 result.length = length; // cut result to length 20 } 21 return result; 22 } 23 unittest { 24 assert(fill(10, "0") == "0000000000"); 25 assert(fill(10, "TXT") == "TXTTXTTXTT"); 26 } 27 28 string bind(string source, string[string] values, string limiter = "%") { 29 import std..string; 30 string result = source; 31 foreach(k, v; values) { result = result.replace(limiter~k~limiter, v); } 32 return result; 33 } 34 unittest { 35 /// TODO 36 } 37 38 bool endsWith(string str, string txt) { 39 if (str.length == 0) return false; 40 if (txt.length == 0) return false; 41 return (lastIndexOf(str, txt) == str.length-1); 42 } 43 unittest { 44 assert("ABC".endsWith("C")); 45 assert(!"".endsWith("C")); 46 assert(!"ABC".endsWith("")); 47 } 48 49 bool has(string base, string[] values...) { return has(base, values); } 50 bool has(string base, string[] values) { 51 foreach(value; values) if ((base.indexOf(value) >= 0) && (base.indexOf(value) < base.length)) return true; 52 return false; 53 } 54 unittest { 55 assert("One Two Three".has("One")); 56 assert("One Two Three".has("Five", "Four", "Three")); 57 assert(!"One Two Three".has("Five", "Four")); 58 } 59 60 bool has(string[] bases, string[] values...) { return has(bases, values); } 61 bool has(string[] bases, string[] values) { 62 foreach(base; bases) if (base.has(values)) return true; 63 return false; 64 } 65 unittest { 66 assert(["One Two Three"].has("One")); 67 assert(["One Two Three", "Eight Seven Six"].has("Five", "Four", "Six")); 68 assert(!["One Two Three"].has("Five", "Four")); 69 } 70 71 /// remove all string values from a array of strings 72 string[] remove(string[] values, string[] removeValues...) { 73 string[] results = values; 74 foreach(removeValue; removeValues) { 75 auto existingValues = results; 76 results = null; 77 foreach(value; existingValues) { if (value != removeValue) results ~= value; } 78 } 79 return results; 80 } 81 unittest{ 82 assert(remove(["a", "b", "c"], "b") == ["a", "c"]); 83 assert(remove(["a", "b", "c", "b"], "b") == ["a", "c"]); 84 85 assert(remove(["a", "b", "c"], "a", "b") == ["c"]); 86 assert(remove(["a", "b", "c", "b"], "a", "b") == ["c"]); 87 } 88 89 /// Unique - Reduce duplicates in array 90 string[] unique(string[] values) { 91 string[] results; results.length = values.length; size_t counter = 0; 92 foreach(value; values) { if (!has(results, value)) { results[counter] = value; counter++; }} 93 results.length = counter; 94 return results; 95 } 96 unittest{ 97 assert(["a", "b", "c"].unique == ["a", "b", "c"]); 98 assert(["a", "b", "c", "c"].unique == ["a", "b", "c"]); 99 } 100 101 size_t[string] countValues(string[] values) { 102 size_t[string] results; 103 foreach(v; values) { 104 if (v in results) results[v] += 1; 105 else results[v] = 1; 106 } 107 return results; 108 } 109 unittest { 110 /// TODO 111 } 112 113 114 bool startsWith(string str, string txt) { 115 if (str.length == 0) return false; 116 if (txt.length == 0) return false; 117 return (indexOf(str, txt) == 0); 118 } 119 unittest { 120 assert("ABC".startsWith("A")); 121 assert(!"".startsWith("A")); 122 assert(!"ABC".startsWith("")); 123 } 124 125 string toString(string[] values) { 126 import std..string; 127 return "%s".format(values); 128 } 129 130 string quotes(string text, string leftAndRight) { 131 return leftAndRight~text~leftAndRight; 132 } 133 string quotes(string text, string left, string right) { 134 return left~text~right; 135 } 136 137 string[] toStrings(T...)(T tt){ 138 string[] results; 139 foreach(t; tt) results ~= "%s".format(t); 140 return results; 141 } 142 unittest { 143 /// TODO 144 } 145 146 string indent(in string txt, int indent = 2) { 147 string result = txt; 148 for(auto i = 0; i < indent; i++) result = " "~result; 149 return result; 150 } 151 unittest { 152 assert(indent("Hallo") == " Hallo"); 153 assert(indent("Hallo", 3) == " Hallo"); 154 } 155 156 size_t[] indexOfAll(string text, string searchTxt) { 157 if (text.indexOf(searchTxt) == -1) return []; 158 159 size_t[] results; 160 size_t currentPos = 0; 161 while((currentPos < text.length) && (currentPos >= 0)) { 162 currentPos = text.indexOf(searchTxt, currentPos); 163 if ((currentPos < text.length) && (currentPos >= 0)) { 164 results ~= currentPos; 165 currentPos++; 166 } 167 } 168 169 return results; 170 } 171 unittest { 172 }