MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
strings.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Simon Fuhrmann
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 */
9
10#ifndef UTIL_STRING_HEADER
11#define UTIL_STRING_HEADER
12
13#include <sstream>
14#include <string>
15#include <iomanip>
16#include <stdexcept>
17#include <algorithm>
18#include <cstdint>
19
20#include "util/defines.h"
21
24
26template <typename T>
27std::string get (T const& value);
28
30template <typename T>
31std::string get_fixed (T const& value, int digits);
32
34template <typename T>
35std::string get_digits (T const& value, int digits);
36
38template <typename T>
39std::string get_filled (T const& value, int width, char fill = '0');
40
42template <typename T>
43T convert (std::string const& str, bool strict_conversion = true);
44
46template <typename T>
47char const* for_type (void);
48
53int size_for_type_string (std::string const& typestring);
54
56void punctate (std::string* input, char delim = ',', std::size_t spacing = 3);
57
59std::string punctated (std::string const& input,
60 char delim = ',', std::size_t spacing = 3);
61
63void clip_whitespaces (std::string* str);
64
66std::string clipped_whitespaces (std::string const& str);
67
69void clip_newlines (std::string* str);
70
72std::string clipped_newlines (std::string const& str);
73
75std::string wordwrap (char const* str, int width);
76
81std::string ellipsize (std::string const& in, std::size_t chars, int type = 0);
82
84void normalize (std::string* str);
85
87std::string normalized (std::string const& str);
88
90std::string left (std::string const& str, std::size_t chars);
91
93std::string right (std::string const& str, std::size_t chars);
94
96std::string lowercase (std::string const& str);
97
99std::string uppercase (std::string const& str);
100
102std::string get_size_string (std::size_t size);
103
104/* ---------------------------------------------------------------- */
105
106template <typename T>
107inline std::string
108get (T const& value)
109{
110 std::stringstream ss;
111 ss << value;
112 return ss.str();
113}
114
115template <>
116inline std::string
117get (std::string const& value)
118{
119 return value;
120}
121
122template <typename T>
123inline std::string
124get_fixed (T const& value, int digits)
125{
126 std::stringstream ss;
127 ss << std::fixed << std::setprecision(digits) << value;
128 return ss.str();
129}
130
131template <typename T>
132inline std::string
133get_digits (T const& value, int digits)
134{
135 std::stringstream ss;
136 ss << std::setprecision(digits) << value;
137 return ss.str();
138}
139
140template <typename T>
141inline std::string
142get_filled (T const& value, int width, char fill)
143{
144 std::stringstream ss;
145 ss << std::setw(width) << std::setfill(fill) << value;
146 return ss.str();
147}
148
149template <typename T>
150inline T
151convert (std::string const& str, bool strict_conversion)
152{
153 std::stringstream ss(str);
154 T ret = T();
155 ss >> ret;
156 if (strict_conversion && (!ss.eof() || ss.fail()))
157 throw std::invalid_argument("Invalid string conversion: " + str);
158 return ret;
159}
160
161template <typename T>
162inline char const*
164{
165 return "unknown";
166}
167
168template <>
169inline char const*
170for_type<int8_t> (void)
171{
172 return "sint8";
173}
174
175#ifdef __GNUC__
176/* Note: 'char' is neither recognized as 'int8_t' nor 'uint8_t'. We assume
177 * that 'char' is singed, which is not always true:
178 * http://www.network-theory.co.uk/docs/gccintro/gccintro_71.html
179 */
180template <>
181inline char const*
182for_type<char> (void)
183{
184 return "sint8";
185}
186#endif
187
188template <>
189inline char const*
190for_type<int16_t> (void)
191{
192 return "sint16";
193}
194
195template <>
196inline char const*
197for_type<int32_t> (void)
198{
199 return "sint32";
200}
201
202template <>
203inline char const*
204for_type<int64_t> (void)
205{
206 return "sint64";
207}
208
209template <>
210inline char const*
211for_type<uint8_t> (void)
212{
213 return "uint8";
214}
215
216template <>
217inline char const*
218for_type<uint16_t> (void)
219{
220 return "uint16";
221}
222
223template <>
224inline char const*
225for_type<uint32_t> (void)
226{
227 return "uint32";
228}
229
230template <>
231inline char const*
232for_type<uint64_t> (void)
233{
234 return "uint64";
235}
236
237template <>
238inline char const*
239for_type<float> (void)
240{
241 return "float";
242}
243
244template <>
245inline char const*
246for_type<double> (void)
247{
248 return "double";
249}
250
251inline int
252size_for_type_string (std::string const& typestring)
253{
254 if (typestring == "sint8" || typestring == "uint8")
255 return 1;
256 else if (typestring == "sint16" || typestring == "uint16")
257 return 2;
258 else if (typestring == "sint32" || typestring == "uint32")
259 return 4;
260 else if (typestring == "sint64" || typestring == "uint64")
261 return 8;
262 else if (typestring == "float")
263 return sizeof(float);
264 else if (typestring == "double")
265 return sizeof(double);
266 else
267 return 0;
268}
269
270inline void
271punctate (std::string* str, char delim, std::size_t spacing)
272{
273 if (str->size() <= spacing || spacing == 0)
274 return;
275
276 std::size_t pos = str->size() - 1;
277 std::size_t cnt = 0;
278 while (pos > 0)
279 {
280 cnt += 1;
281 if (cnt == spacing)
282 {
283 str->insert(str->begin() + pos, delim);
284 cnt = 0;
285 }
286 pos -= 1;
287 }
288}
289
290inline std::string
291punctated (std::string const& input, char delim, std::size_t spacing)
292{
293 std::string ret(input);
294 punctate(&ret, delim, spacing);
295 return ret;
296}
297
298inline void
299clip_whitespaces (std::string* str)
300{
301 // TODO: Use str->back() and str->front() once C++11 is standard.
302 while (!str->empty() && (*str->rbegin() == ' ' || *str->rbegin() == '\t'))
303 str->resize(str->size() - 1);
304 while (!str->empty() && (*str->begin() == ' ' || *str->begin() == '\t'))
305 str->erase(str->begin());
306}
307
308inline std::string
309clipped_whitespaces (std::string const& str)
310{
311 std::string ret(str);
312 clip_whitespaces(&ret);
313 return ret;
314}
315
316inline void
317clip_newlines (std::string* str)
318{
319 while (!str->empty() && (*str->rbegin() == '\r' || *str->rbegin() == '\n'))
320 str->resize(str->size() - 1);
321}
322
323
324inline std::string
325clipped_newlines (std::string const& str)
326{
327 std::string ret(str);
328 clip_newlines(&ret);
329 return ret;
330}
331
332inline std::string
333wordwrap (char const* str, int width)
334{
335 if (str == nullptr)
336 return std::string();
337 if (width <= 0)
338 return str;
339
340 int spaceleft = width;
341 bool firstword = true;
342 std::string out;
343 for (int i = 0, word = 0; true; ++i)
344 {
345 char c(str[i]);
346 bool softbreak = (c == ' ' || c == '\t' || c == '\0' || c == '\n');
347
348 if (softbreak)
349 {
350 if (word > spaceleft)
351 {
352 if (!firstword)
353 out.append(1, '\n');
354 out.append(str + i - word, word);
355 spaceleft = width - word - 1;
356 }
357 else
358 {
359 if (!firstword)
360 out.append(1, ' ');
361 out.append(str + i - word, word);
362 spaceleft -= word + 1;
363 }
364 firstword = false;
365
366 word = 0;
367 if (c == '\n')
368 {
369 out.append(1, '\n');
370 firstword = true;
371 word = 0;
372 spaceleft = width;
373 }
374 }
375 else
376 {
377 word += 1;
378 }
379
380 if (str[i] == '\0')
381 break;
382 }
383
384 return out;
385}
386
387inline std::string
388ellipsize (std::string const& str, std::size_t chars, int type)
389{
390 if (str.size() <= chars)
391 return str;
392
393 switch (type)
394 {
395 case 0:
396 return str.substr(0, chars - 3) + "...";
397 case 1:
398 return str.substr(0, chars / 2 - 1) + "..."
399 + str.substr(str.size() - chars / 2 + 1);
400 case 2:
401 return "..." + str.substr(str.size() - chars + 3);
402 default:
403 break;
404 }
405 return str;
406}
407
408inline void
409normalize (std::string* str)
410{
411 std::size_t iter = 0;
412 bool was_whitespace = false;
413 while (iter < str->size())
414 {
415 if (str->at(iter) == '\t')
416 str->at(iter) = ' ';
417
418 if (str->at(iter) == ' ')
419 {
420 if (was_whitespace)
421 {
422 str->erase(str->begin() + iter);
423 was_whitespace = true;
424 continue;
425 }
426 was_whitespace = true;
427 }
428 else
429 {
430 was_whitespace = false;
431 }
432
433 iter += 1;
434 }
435}
436
437inline std::string
438normalized (std::string const& str)
439{
440 std::string ret(str);
441 normalize(&ret);
442 return ret;
443}
444
445inline std::string
446left (std::string const& str, std::size_t chars)
447{
448 return str.substr(0, std::min(str.size(), chars));
449}
450
451inline std::string
452right (std::string const& str, std::size_t chars)
453{
454 return str.substr(str.size() - std::min(str.size(), chars));
455}
456
457inline std::string
458lowercase (std::string const& str)
459{
460 std::string ret(str);
461 for (std::size_t i = 0; i < str.size(); ++i)
462 if (ret[i] >= 0x41 && ret[i] <= 0x5a)
463 ret[i] += 0x20;
464 return ret;
465}
466
467inline std::string
468uppercase (std::string const& str)
469{
470 std::string ret(str);
471 for (std::size_t i = 0; i < str.size(); ++i)
472 if (ret[i] >= 0x61 && ret[i] <= 0x7a)
473 ret[i] -= 0x20;
474 return ret;
475}
476
477inline std::string
478get_size_string (std::size_t size)
479{
480 std::string size_str;
481 double size_flt;
482
483 if (size < 1000) /* 1000 * 1024^0 */
484 {
485 size_flt = (double)size;
486 size_str = " B";
487 }
488 else if (size < 1024000) /* 1000 * 1024^1 */
489 {
490 size_flt = size / 1024.0;
491 size_str = " KB";
492 }
493 else if (size < 1048576000) /* 1000 * 1024^2 */
494 {
495 size_flt = size / 1048576.0;
496 size_str = " MB";
497 }
498 else
499 {
500 size_flt = size / 1073741824.0;
501 size_str = " GB";
502 }
503
504 int digits = 1;
505 if (size_flt >= 10.0)
506 digits = 0;
507
508 return util::string::get_fixed(size_flt, digits) + size_str;
509}
510
513
514#endif /* UTIL_STRING_HEADER */
int size_for_type_string(std::string const &typestring)
Returns the byte size of given type string (e.g.
Definition strings.h:252
std::string clipped_newlines(std::string const &str)
Clips newlines from the end of the string.
Definition strings.h:325
std::string wordwrap(char const *str, int width)
Inserts line breaks on word boundaries to limit lines to 'width' chars.
Definition strings.h:333
std::string left(std::string const &str, std::size_t chars)
Returns the leftmost 'chars' characters of 'str'.
Definition strings.h:446
std::string ellipsize(std::string const &in, std::size_t chars, int type=0)
Reduces string size by inserting "..." at the end (type = 0), in the middle (type = 1) or at the begi...
Definition strings.h:388
std::string punctated(std::string const &input, char delim=',', std::size_t spacing=3)
Inserts 'delim' every 'spacing' characters from the right.
Definition strings.h:291
void punctate(std::string *input, char delim=',', std::size_t spacing=3)
Inserts 'delim' every 'spacing' characters from the right, in-place.
Definition strings.h:271
void clip_newlines(std::string *str)
Clips newlines from the end of the string, in-place.
Definition strings.h:317
void clip_whitespaces(std::string *str)
Clips whitespaces from the front and end of the string, in-place.
Definition strings.h:299
std::string clipped_whitespaces(std::string const &str)
Clips whitespaces from the front and end of the string.
Definition strings.h:309
std::string normalized(std::string const &str)
Replaces several whitespaces with a single blank.
Definition strings.h:438
std::string get_filled(T const &value, int width, char fill='0')
Returns a string filled to the left to a length of 'width' chars.
Definition strings.h:142
std::string get_fixed(T const &value, int digits)
Returns string with 'digits' of fixed precision (fills with zeros).
Definition strings.h:124
std::string get(T const &value)
From arbitrary types to string conversion.
Definition strings.h:108
std::string get_digits(T const &value, int digits)
Returns string with 'digits' of precision.
Definition strings.h:133
void normalize(std::string *str)
Replaces several whitespaces with a single blank, in-place.
Definition strings.h:409
std::string uppercase(std::string const &str)
Returns an upper-case version of the string.
Definition strings.h:468
std::string lowercase(std::string const &str)
Returns a lower-case version of the string.
Definition strings.h:458
T convert(std::string const &str, bool strict_conversion=true)
From string to other types conversions.
Definition strings.h:151
std::string right(std::string const &str, std::size_t chars)
Returns the rightmost 'chars' characters of 'str'.
Definition strings.h:452
char const * for_type(void)
String representation for types.
Definition strings.h:163
std::string get_size_string(std::size_t size)
Returns a string with a human readable byte size, e.g.
Definition strings.h:478
#define UTIL_STRING_NAMESPACE_END
Definition defines.h:20
#define UTIL_NAMESPACE_BEGIN
Definition defines.h:13
#define UTIL_STRING_NAMESPACE_BEGIN
Definition defines.h:19
#define UTIL_NAMESPACE_END
Definition defines.h:14