2
* This file is part of bino, a 3D video player.
4
* Copyright (C) 2009-2011
5
* Martin Lambers <marlam@marlam.de>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 3 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24
* Tiny tools for strings.
38
# define STR_AFP(a, b) __attribute__ ((format (printf, a, b)))
40
# define STR_AFP(a, b) /* empty */
45
/* Sanitize a string (replace control characters with '?') */
46
std::string sanitize(const std::string &s);
48
/* Trim a string (remove whitespace from both ends) */
49
std::string trim(const std::string &s);
51
/* Create std::strings from all basic data types */
52
std::string from(bool x);
53
std::string from(signed char x);
54
std::string from(unsigned char x);
55
std::string from(short x);
56
std::string from(unsigned short x);
57
std::string from(int x);
58
std::string from(unsigned int x);
59
std::string from(long x);
60
std::string from(unsigned long x);
61
std::string from(long long x);
62
std::string from(unsigned long long x);
63
std::string from(float x);
64
std::string from(double x);
65
std::string from(long double x);
67
/* Convert a string to one of the basic data types */
68
template<typename T> T to(const std::string &s);
69
template<> bool to<bool>(const std::string &s);
70
template<> signed char to<signed char>(const std::string &s);
71
template<> unsigned char to<unsigned char>(const std::string &s);
72
template<> short to<short>(const std::string &s);
73
template<> unsigned short to<unsigned short>(const std::string &s);
74
template<> int to<int>(const std::string &s);
75
template<> unsigned int to<unsigned int>(const std::string &s);
76
template<> long to<long>(const std::string &s);
77
template<> unsigned long to<unsigned long>(const std::string &s);
78
template<> long long to<long long>(const std::string &s);
79
template<> unsigned long long to<unsigned long long>(const std::string &s);
80
template<> float to<float>(const std::string &s);
81
template<> double to<double>(const std::string &s);
82
template<> long double to<long double>(const std::string &s);
84
/* Create std::strings printf-like */
85
std::string vasprintf(const char *format, va_list args) STR_AFP(1, 0);
86
std::string asprintf(const char *format, ...) STR_AFP(1, 2);
88
/* Replace all instances of s with r in str */
89
std::string &replace(std::string &str, const std::string &s, const std::string &r);
91
/* Create a hex string from binary data */
92
std::string hex(const std::string &s, bool uppercase = false);
93
std::string hex(const void *buf, size_t n, bool uppercase = false);
95
/* Convert various values to human readable strings */
96
std::string human_readable_memsize(const uintmax_t size);
97
std::string human_readable_length(const double length);
98
std::string human_readable_time(int64_t microseconds);
100
/* Convert a string from one character set to another */
101
std::string convert(const std::string &src, const std::string &from_charset, const std::string &to_charset);