1 module tagion.betterC.utils.StringHelper; 2 3 import std.traits; 4 import tagion.betterC.utils.Memory; 5 6 string int_to_str(T)(T data) if (isIntegral!T) { 7 Unqual!T mut_data = data; 8 int data_size = decimal_place(mut_data); 9 char[] res; 10 res.create(data_size); 11 auto pos = res.length - 1; 12 while (mut_data > 0) { 13 res[pos] = cast(char)(data % 10 + '0'); 14 mut_data /= 10; 15 pos++; 16 } 17 return cast(string)(res); 18 } 19 20 int decimal_place(T)(T data) { 21 auto tmp_data = data; 22 int count = 0; 23 do { 24 tmp_data /= 10; 25 count++; 26 } 27 while (tmp_data > 0); 28 return count; 29 } 30 31 int count_pieces(const(char)[] data, char splitter) { 32 int res = 1; 33 size_t pos = 0; 34 while (pos < data.length) { 35 if (data[pos] == splitter) { 36 res++; 37 while (data[pos] == splitter) { 38 pos++; 39 } 40 } 41 pos++; 42 } 43 return res; 44 } 45 46 size_t find_next_char(const(char)[] data, char symbol, size_t start_pos) { 47 for (size_t i = start_pos + 1; i < data.length; i++) { 48 if (data[i] == symbol) { 49 return i; 50 } 51 } 52 return data.length; 53 } 54 55 const(char[])[] split_by_char(const(char)[] data, char splitter) { 56 const(char)[][] res; 57 auto res_size = count_pieces(data, splitter); 58 res.create(res_size); 59 if (res_size != 1) { 60 size_t start_pos = 0; 61 size_t split_pos = find_next_char(data, splitter, start_pos); 62 size_t splits_num = 0; 63 do { 64 if (start_pos < split_pos) { 65 res[splits_num] = data[start_pos .. split_pos]; 66 splits_num++; 67 } 68 start_pos = split_pos + 1; 69 split_pos = find_next_char(data, splitter, split_pos); 70 } 71 while (start_pos < data.length); 72 } 73 else { 74 res[0] = data[0 .. $]; 75 } 76 return res; 77 } 78 79 void append(T)(ref T[] arr, T value) { 80 auto arr_length = arr.length; 81 // arr.length += 1; 82 83 T[] tmp_arr; 84 tmp_arr.create(arr_length + 1); 85 tmp_arr[0 .. $ - 1] = arr[0 .. $]; 86 arr.dispose; 87 arr = tmp_arr; 88 // tmp_arr.dispose; 89 // arr_length += 1; 90 // arr.resize(arr_length); 91 arr[$ - 1] = value; 92 } 93 94 T pop_back(T)(ref T[] arr) 95 in { 96 assert(arr.length > 0); 97 } 98 do { 99 auto result = arr[$ - 1]; 100 auto arr_length = arr.length; 101 arr.resize(arr_length - 1); 102 return result; 103 } 104 105 unittest { 106 // no need to split 107 // { 108 // string test = "123"; 109 // auto res = split_by_char(test, ','); 110 // string[] exp_res; 111 // exp_res ~= "123"; 112 // // exp_res ~= "321"; 113 // assert(res == exp_res); 114 // } 115 // //find next char pos 116 // { 117 // string test = "012,4,6"; 118 // size_t pos = find_next_char(test, ',', 0); 119 // auto count = count_pieces(test, ','); 120 121 // assert(count == 3); 122 123 // assert(pos == 3); 124 // pos = find_next_char(test, ',', pos); 125 // assert(pos == 5); 126 127 // pos = find_next_char(test, ',', pos); 128 // assert(pos == test.length); 129 // } 130 // // one spliter 131 // { 132 // string test = "123,321"; 133 // auto res = split_by_char(test, ','); 134 // string[] exp_res; 135 // exp_res ~= "123"; 136 // exp_res ~= "321"; 137 // assert(res == exp_res); 138 // } 139 // // one spliter many times 140 // { 141 // string test = "123,,,,,,321"; 142 // auto res = split_by_char(test, ','); 143 // string[] exp_res; 144 // exp_res ~= "123"; 145 // exp_res ~= "321"; 146 // assert(res == exp_res); 147 // } 148 // // more spliters 149 // { 150 // string test = "12,3,3,,,2,,1"; 151 // auto res = split_by_char(test, ','); 152 // string[] exp_res; 153 // exp_res ~= "12"; 154 // exp_res ~= "3"; 155 // exp_res ~= "3"; 156 // exp_res ~= "2"; 157 // exp_res ~= "1"; 158 // assert(res == exp_res); 159 // } 160 } 161 162 // this(Document doc) { 163 // Document doc - [ [Pubkey, bool], [] ,[] ] 164 // forech(elem; doc) { 165 // const tmp = elem.get!Document; 166 // tmp[0].get!Buffer; 167 // tmp[1].get!bool; 168 // } 169 170 // }