Project
Loading...
Searching...
No Matches
StringUtils.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#include <cctype>
13#include <cstdint>
14#include <cmath>
15#include <cstdlib>
16#include <string>
17#include <random>
18#include <vector>
19#include <list>
20#include <memory>
21#include <fstream>
22#include <regex>
23#include <map>
24#include <functional>
25#include <utility>
26#include <sstream>
27#include <istream>
28#include <iomanip>
29#include <sstream>
30#include <iterator>
32
33using namespace std;
34
35random_device dev;
36default_random_engine gen(dev());
37uniform_int_distribution<long long> uniform_dist(32, 126);
38geometric_distribution<long long> geom_dist(0.1);
39
40constexpr char ALPHABET[39]{
41 'E', 'T', 'A', 'O', 'I', '_', 'N', 'S', 'H', 'R',
42 '/', 'D', 'L', '1', '2', 'C', 'U', 'M', 'W', '3',
43 '4', '5', '6', '7', '8', '9', '0', 'F', 'G', 'Y',
44 'P', 'B', 'V', 'K', 'J', 'X', 'Q', 'Z', '?'};
45
51constexpr uint64_t PRIMES[52]{
52 2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
53 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
54 73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
55 127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
56 179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
57 233, 239};
58
63constexpr uint64_t MODULO(13835058055282163729ULL);
64
71uint8_t lookup(const char input) noexcept
72{
73 switch (input) {
74 case 'e':
75 case 'E':
76 return 0;
77 case 't':
78 case 'T':
79 return 1;
80 case 'a':
81 case 'A':
82 return 2;
83 case 'o':
84 case 'O':
85 return 3;
86 case 'i':
87 case 'I':
88 return 4;
89 case '_':
90 return 5;
91 case 'n':
92 case 'N':
93 return 6;
94 case 's':
95 case 'S':
96 return 7;
97 case 'h':
98 case 'H':
99 return 8;
100 case 'r':
101 case 'R':
102 return 9;
103 case '/':
104 return 10;
105 case 'd':
106 case 'D':
107 return 11;
108 case 'l':
109 case 'L':
110 return 12;
111 case '1':
112 return 13;
113 case '2':
114 return 14;
115 case 'c':
116 case 'C':
117 return 15;
118 case 'u':
119 case 'U':
120 return 16;
121 case 'm':
122 case 'M':
123 return 17;
124 case 'w':
125 case 'W':
126 return 18;
127 case '3':
128 return 19;
129 case '4':
130 return 20;
131 case '5':
132 return 21;
133 case '6':
134 return 22;
135 case '7':
136 return 23;
137 case '8':
138 return 24;
139 case '9':
140 return 25;
141 case '0':
142 return 26;
143 case 'f':
144 case 'F':
145 return 27;
146 case 'g':
147 case 'G':
148 return 28;
149 case 'y':
150 case 'Y':
151 return 29;
152 case 'p':
153 case 'P':
154 return 30;
155 case 'b':
156 case 'B':
157 return 31;
158 case 'v':
159 case 'V':
160 return 32;
161 case 'k':
162 case 'K':
163 return 33;
164 case 'j':
165 case 'J':
166 return 34;
167 case 'x':
168 case 'X':
169 return 35;
170 case 'q':
171 case 'Q':
172 return 36;
173 case 'z':
174 case 'Z':
175 return 37;
176 default:
177 return 38;
178 }
179}
180
192uint64_t exp(uint64_t base, uint8_t exp) noexcept
193{
194 uint64_t result(1);
195 while (exp) {
196 if (exp & 1) {
197 result *= base;
198 }
199 exp >>= 1;
200 base *= base;
201 }
202 return result;
203}
204
205string o2::dcs::random_string(const size_t length) noexcept
206{
207 string s;
208 s.reserve(length);
209 for (size_t i = 0; i < length; ++i) {
210 s.push_back(uniform_dist(gen));
211 }
212 return s;
213}
214
215string o2::dcs::random_string2(const size_t length) noexcept
216{
217 string s;
218 s.reserve(length);
219 for (size_t i = 0; i < length; ++i) {
220 s.push_back(ALPHABET[geom_dist(gen) % 39]);
221 }
222 return s;
223}
224
225uint64_t o2::dcs::hash_code(const std::string& input) noexcept
226{
227 size_t result(0);
228 const size_t length(min(input.length(), (string::size_type)52));
229 for (size_t i = 0; i < length; ++i) {
230 // result += exp(PRIMES[i], (uint8_t) input[i]);
231 result += exp(PRIMES[i], lookup(input[i]));
232 // result += exp(PRIMES[i], ((uint8_t) input[i]) & 0x1F);
233 }
234 return result;
235}
236
237unique_ptr<vector<string>> o2::dcs::convert_strings(const int argc,
238 char** argv) noexcept
239{
240 vector<string>* arguments = new vector<string>();
241
242 for (int i = 0; i < argc; ++i) {
243 arguments->push_back(std::move(string(argv[i])));
244 }
245
246 return unique_ptr<vector<string>>(arguments);
247}
248
249unique_ptr<vector<string>> o2::dcs::split(const string& source,
250 const char separator) noexcept
251{
252 stringstream ss(source);
253 string item;
254 vector<string>* substrings = new vector<string>();
255 while (getline(ss, item, separator)) {
256 substrings->push_back(item);
257 }
258 return unique_ptr<vector<string>>(substrings);
259}
260
261unique_ptr<vector<string>> o2::dcs::split_by_whitespace(const string& source) noexcept
262{
263 istringstream buffer(source);
264 vector<string>* ret = new vector<string>();
265
266 std::copy(istream_iterator<string>(buffer),
267 istream_iterator<string>(),
268 back_inserter(*ret));
269 return unique_ptr<vector<string>>(ret);
270}
271
272inline char to_alpha_numeric(const uint8_t c) noexcept
273{
274 switch (c) {
275 case 0x00:
276 return '0';
277 case 0x01:
278 case 0x10:
279 return '1';
280 case 0x02:
281 case 0x20:
282 return '2';
283 case 0x03:
284 case 0x30:
285 return '3';
286 case 0x04:
287 case 0x40:
288 return '4';
289 case 0x05:
290 case 0x50:
291 return '5';
292 case 0x06:
293 case 0x60:
294 return '6';
295 case 0x07:
296 case 0x70:
297 return '7';
298 case 0x08:
299 case 0x80:
300 return '8';
301 case 0x09:
302 case 0x90:
303 return '9';
304 case 0x0A:
305 case 0xA0:
306 return 'A';
307 case 0x0B:
308 case 0xB0:
309 return 'B';
310 case 0x0C:
311 case 0xC0:
312 return 'C';
313 case 0x0D:
314 case 0xD0:
315 return 'D';
316 case 0x0E:
317 case 0xE0:
318 return 'E';
319 case 0x0F:
320 case 0xF0:
321 return 'F';
322 default:
323 return '?';
324 }
325}
326
327string o2::dcs::to_hex_big_endian(const char* const start, const size_t length) noexcept
328{
329 string s;
330 s.reserve(length * 3);
331 // My machine is little endian:
332 size_t i = length - 1;
333 do {
334 const char next = start[i];
335 s.push_back(to_alpha_numeric(next & 0xF0));
336 s.push_back(to_alpha_numeric(next & 0x0F));
337 s.push_back(' ');
338 --i;
339 } while (i < length);
340 s.pop_back();
341 return s;
342}
343
344string o2::dcs::to_hex_little_endian(const char* const start, const size_t length) noexcept
345{
346 string s;
347 s.reserve(length * 3);
348 // My machine is little endian:
349 size_t i = 0;
350 do {
351 const char next = start[i];
352 s.push_back(to_alpha_numeric(next & 0xF0));
353 s.push_back(to_alpha_numeric(next & 0x0F));
354 s.push_back(' ');
355 ++i;
356 } while (i < length);
357 s.pop_back();
358 return s;
359}
360
362 const string& list_name,
363 const list<pair<string, string>>& parameters) noexcept
364{
365 stringstream ss;
366 ss << list_name << endl
367 << string(list_name.length() + 26, '-') << endl
368 << endl;
369 uint32_t key_length(0);
370 for (auto k_v : parameters) {
371 if (k_v.first.length() > key_length) {
372 key_length = k_v.first.length();
373 }
374 }
375 key_length += 4 - (key_length % 4);
376 for (auto k_v : parameters) {
377 ss << " " << k_v.first << string(key_length - k_v.first.length(), ' ') << k_v.second << endl;
378 }
379 ss << endl
380 << "=========================================================="
381 "======================"
382 << endl
383 << endl;
384}
char to_alpha_numeric(const uint8_t c) noexcept
uniform_int_distribution< long long > uniform_dist(32, 126)
geometric_distribution< long long > geom_dist(0.1)
constexpr char ALPHABET[39]
constexpr uint64_t PRIMES[52]
default_random_engine gen(dev())
random_device dev
uint64_t exp(uint64_t base, uint8_t exp) noexcept
constexpr uint64_t MODULO(13835058055282163729ULL)
uint8_t lookup(const char input) noexcept
int32_t i
uint32_t c
Definition RawData.h:2
GLsizei const GLchar *const * string
Definition glcorearb.h:809
GLuint64EXT * result
Definition glcorearb.h:5662
GLuint buffer
Definition glcorearb.h:655
GLsizei GLsizei GLchar * source
Definition glcorearb.h:798
GLuint GLsizei GLsizei * length
Definition glcorearb.h:790
GLuint start
Definition glcorearb.h:469
bpo::variables_map arguments
std::string random_string2(const size_t length) noexcept
std::string to_hex_little_endian(const char *const start, size_t length) noexcept
std::unique_ptr< std::vector< std::string > > split(const std::string &source, const char separator) noexcept
uint64_t hash_code(const std::string &input) noexcept
std::unique_ptr< std::vector< std::string > > convert_strings(const int argc, char **argv) noexcept
std::string to_hex_big_endian(const char *const start, size_t length) noexcept
void print_k_v_list(const std::string &list_name, const std::list< std::pair< std::string, std::string > > &parameters) noexcept
std::string random_string(const size_t length) noexcept
std::unique_ptr< std::vector< std::string > > split_by_whitespace(const std::string &source) noexcept
Defining DataPointCompositeObject explicitly as copiable.
Definition list.h:40
constexpr size_t min