40 #ifndef OPENIMAGEIO_STRUTIL_H
41 #define OPENIMAGEIO_STRUTIL_H
53 #include "tinyformat.h"
55 #ifndef OPENIMAGEIO_PRINTF_ARGS
57 # define __attribute__(x)
70 # define OPENIMAGEIO_PRINTF_ARGS(fmtarg_pos, vararg_pos) \
71 __attribute__ ((format (printf, fmtarg_pos, vararg_pos) ))
90 TINYFORMAT_WRAP_FORMAT (std::string, format, ,
91 std::ostringstream msg;, msg,
return msg.str();)
98 std::string OIIO_API format_raw (
const char *fmt, ...)
99 OPENIMAGEIO_PRINTF_ARGS(1,2);
104 std::
string OIIO_API vformat (const
char *fmt, va_list ap)
105 OPENIMAGEIO_PRINTF_ARGS(1,0);
112 std::
string OIIO_API memformat (
long long bytes,
int digits=1);
116 std::
string OIIO_API timeintervalformat (
double secs,
int digits=1);
127 bool OIIO_API get_rest_arguments (const std::
string &str, std::
string &base,
128 std::map<std::
string, std::
string> &result);
133 std::
string OIIO_API escape_chars (const std::
string &unescaped);
137 std::
string OIIO_API unescape_chars (const std::
string &escaped);
145 std::
string OIIO_API wordwrap (std::
string src,
int columns=80,
int prefix=0);
151 strhash (const
char *s)
156 h += (
unsigned char)(*s);
171 bool OIIO_API iequals (
const std::string &a,
const std::string &b);
172 bool OIIO_API iequals (
const char *a,
const char *b);
176 bool OIIO_API istarts_with (
const std::string &a,
const std::string &b);
177 bool OIIO_API istarts_with (
const char *a,
const char *b);
181 bool OIIO_API iends_with (
const std::string &a,
const std::string &b);
182 bool OIIO_API iends_with (
const char *a,
const char *b);
186 bool OIIO_API iends_with (
const std::string &a,
const std::string &b);
187 bool OIIO_API iends_with (
const char *a,
const char *b);
190 bool OIIO_API contains (
const std::string &a,
const std::string &b);
191 bool OIIO_API contains (
const char *a,
const char *b);
195 bool OIIO_API icontains (
const std::string &a,
const std::string &b);
196 bool OIIO_API icontains (
const char *a,
const char *b);
200 void OIIO_API to_lower (std::string &a);
204 void OIIO_API to_upper (std::string &a);
209 std::string OIIO_API strip (
const std::string &str,
210 const std::string &chars=std::string());
215 void OIIO_API split (
const std::string &str, std::vector<std::string> &result,
216 const std::string &sep =
"",
int maxsplit = -1);
220 std::string OIIO_API join (
const std::vector<std::string> &seq,
221 const std::string &sep=
"");
227 inline T from_string (
const std::string &s) {
231 template<>
inline int from_string<int> (
const std::string &s) {
232 return strtol (s.c_str(), NULL, 10);
235 template<>
inline float from_string<float> (
const std::string &s) {
236 return (
float)strtod (s.c_str(), NULL);
255 void extract_from_list_string (std::vector<T> &vals,
256 const std::string &list,
257 const std::string &sep =
",")
259 size_t nvals = vals.size();
260 std::vector<std::string> valuestrings;
261 Strutil::split (list, valuestrings, sep);
262 for (
size_t i = 0, e = valuestrings.size(); i < e; ++i) {
263 if (valuestrings[i].size())
264 vals[i] = from_string<T> (valuestrings[i]);
266 if (valuestrings.size() == 1) {
268 vals.resize (nvals, vals[0]);
284 size_t operator() (
const char *s)
const {
285 return (
size_t)Strutil::strhash(s);
287 size_t operator() (
const std::string &s)
const {
288 return (
size_t)Strutil::strhash(s.c_str());
298 bool operator() (
const char *a,
const char *b)
const {
299 return strcmp (a, b) == 0;
337 std::wstring OIIO_API utf8_to_utf16(
const std::string& utf8str);
341 std::string OIIO_API utf16_to_utf8(
const std::wstring& utf16str);
350 #endif // OPENIMAGEIO_STRUTIL_H