10#ifndef UTIL_STRING_HEADER
11#define UTIL_STRING_HEADER
27std::string
get (T
const& value);
31std::string
get_fixed (T
const& value,
int digits);
35std::string
get_digits (T
const& value,
int digits);
39std::string
get_filled (T
const& value,
int width,
char fill =
'0');
43T
convert (std::string
const& str,
bool strict_conversion =
true);
56void punctate (std::string* input,
char delim =
',', std::size_t spacing = 3);
59std::string
punctated (std::string
const& input,
60 char delim =
',', std::size_t spacing = 3);
75std::string
wordwrap (
char const* str,
int width);
81std::string
ellipsize (std::string
const& in, std::size_t chars,
int type = 0);
87std::string
normalized (std::string
const& str);
90std::string
left (std::string
const& str, std::size_t chars);
93std::string
right (std::string
const& str, std::size_t chars);
96std::string
lowercase (std::string
const& str);
99std::string
uppercase (std::string
const& str);
110 std::stringstream ss;
117get (std::string
const& value)
126 std::stringstream ss;
127 ss << std::fixed << std::setprecision(digits) << value;
135 std::stringstream ss;
136 ss << std::setprecision(digits) << value;
144 std::stringstream ss;
145 ss << std::setw(width) << std::setfill(fill) << value;
151convert (std::string
const& str,
bool strict_conversion)
153 std::stringstream ss(str);
156 if (strict_conversion && (!ss.eof() || ss.fail()))
157 throw std::invalid_argument(
"Invalid string conversion: " + str);
170for_type<int8_t> (
void)
190for_type<int16_t> (
void)
197for_type<int32_t> (
void)
204for_type<int64_t> (
void)
211for_type<uint8_t> (
void)
218for_type<uint16_t> (
void)
225for_type<uint32_t> (
void)
232for_type<uint64_t> (
void)
239for_type<float> (
void)
246for_type<double> (
void)
254 if (typestring ==
"sint8" || typestring ==
"uint8")
256 else if (typestring ==
"sint16" || typestring ==
"uint16")
258 else if (typestring ==
"sint32" || typestring ==
"uint32")
260 else if (typestring ==
"sint64" || typestring ==
"uint64")
262 else if (typestring ==
"float")
263 return sizeof(float);
264 else if (typestring ==
"double")
265 return sizeof(double);
271punctate (std::string* str,
char delim, std::size_t spacing)
273 if (str->size() <= spacing || spacing == 0)
276 std::size_t pos = str->size() - 1;
283 str->insert(str->begin() + pos, delim);
291punctated (std::string
const& input,
char delim, std::size_t spacing)
293 std::string ret(input);
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());
311 std::string ret(str);
319 while (!str->empty() && (*str->rbegin() ==
'\r' || *str->rbegin() ==
'\n'))
320 str->resize(str->size() - 1);
327 std::string ret(str);
336 return std::string();
340 int spaceleft = width;
341 bool firstword =
true;
343 for (
int i = 0, word = 0;
true; ++i)
346 bool softbreak = (c ==
' ' || c ==
'\t' || c ==
'\0' || c ==
'\n');
350 if (word > spaceleft)
354 out.append(str + i - word, word);
355 spaceleft = width - word - 1;
361 out.append(str + i - word, word);
362 spaceleft -= word + 1;
388ellipsize (std::string
const& str, std::size_t chars,
int type)
390 if (str.size() <= chars)
396 return str.substr(0, chars - 3) +
"...";
398 return str.substr(0, chars / 2 - 1) +
"..."
399 + str.substr(str.size() - chars / 2 + 1);
401 return "..." + str.substr(str.size() - chars + 3);
411 std::size_t iter = 0;
412 bool was_whitespace =
false;
413 while (iter < str->size())
415 if (str->at(iter) ==
'\t')
418 if (str->at(iter) ==
' ')
422 str->erase(str->begin() + iter);
423 was_whitespace =
true;
426 was_whitespace =
true;
430 was_whitespace =
false;
440 std::string ret(str);
446left (std::string
const& str, std::size_t chars)
448 return str.substr(0, std::min(str.size(), chars));
452right (std::string
const& str, std::size_t chars)
454 return str.substr(str.size() - std::min(str.size(), chars));
460 std::string ret(str);
461 for (std::size_t i = 0; i < str.size(); ++i)
462 if (ret[i] >= 0x41 && ret[i] <= 0x5a)
470 std::string ret(str);
471 for (std::size_t i = 0; i < str.size(); ++i)
472 if (ret[i] >= 0x61 && ret[i] <= 0x7a)
480 std::string size_str;
485 size_flt = (double)size;
488 else if (size < 1024000)
490 size_flt = size / 1024.0;
493 else if (size < 1048576000)
495 size_flt = size / 1048576.0;
500 size_flt = size / 1073741824.0;
505 if (size_flt >= 10.0)
int size_for_type_string(std::string const &typestring)
Returns the byte size of given type string (e.g.
std::string clipped_newlines(std::string const &str)
Clips newlines from the end of the string.
std::string wordwrap(char const *str, int width)
Inserts line breaks on word boundaries to limit lines to 'width' chars.
std::string left(std::string const &str, std::size_t chars)
Returns the leftmost 'chars' characters of 'str'.
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...
std::string punctated(std::string const &input, char delim=',', std::size_t spacing=3)
Inserts 'delim' every 'spacing' characters from the right.
void punctate(std::string *input, char delim=',', std::size_t spacing=3)
Inserts 'delim' every 'spacing' characters from the right, in-place.
void clip_newlines(std::string *str)
Clips newlines from the end of the string, in-place.
void clip_whitespaces(std::string *str)
Clips whitespaces from the front and end of the string, in-place.
std::string clipped_whitespaces(std::string const &str)
Clips whitespaces from the front and end of the string.
std::string normalized(std::string const &str)
Replaces several whitespaces with a single blank.
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.
std::string get_fixed(T const &value, int digits)
Returns string with 'digits' of fixed precision (fills with zeros).
std::string get(T const &value)
From arbitrary types to string conversion.
std::string get_digits(T const &value, int digits)
Returns string with 'digits' of precision.
void normalize(std::string *str)
Replaces several whitespaces with a single blank, in-place.
std::string uppercase(std::string const &str)
Returns an upper-case version of the string.
std::string lowercase(std::string const &str)
Returns a lower-case version of the string.
T convert(std::string const &str, bool strict_conversion=true)
From string to other types conversions.
std::string right(std::string const &str, std::size_t chars)
Returns the rightmost 'chars' characters of 'str'.
char const * for_type(void)
String representation for types.
std::string get_size_string(std::size_t size)
Returns a string with a human readable byte size, e.g.
#define UTIL_STRING_NAMESPACE_END
#define UTIL_NAMESPACE_BEGIN
#define UTIL_STRING_NAMESPACE_BEGIN
#define UTIL_NAMESPACE_END