121 #ifndef OPENIMAGEIO_USTRING_H
122 #define OPENIMAGEIO_USTRING_H
124 #if defined(_MSC_VER)
130 # pragma warning (disable : 4251 4996)
150 typedef char value_type;
151 typedef value_type * pointer;
152 typedef value_type & reference;
153 typedef const value_type & const_reference;
154 typedef size_t size_type;
155 static const size_type npos =
static_cast<size_type
>(-1);
156 typedef std::string::const_iterator const_iterator;
157 typedef std::string::const_reverse_iterator const_reverse_iterator;
161 ustring (
void) : m_chars(NULL) { }
165 explicit ustring (
const char *str) {
166 m_chars = str ? make_unique(str) : NULL;
171 ustring (
const char *str, size_type pos, size_type n)
172 : m_chars (make_unique(std::string(str,pos,n).c_str())) { }
176 ustring (
const char *str, size_type n)
177 : m_chars (make_unique(std::string(str,n).c_str())) { }
182 : m_chars (make_unique(std::string(n,c).c_str())) { }
186 explicit ustring (
const std::string &str) { *
this =
ustring(str.c_str()); }
190 ustring (
const std::string &str, size_type pos, size_type n=npos)
191 : m_chars (make_unique(std::string(str, pos, n).c_str())) { }
200 : m_chars (make_unique(std::string(str.c_str(),pos,n).c_str())) { }
209 m_chars = str.m_chars;
215 const ustring & assign (
const ustring &str, size_type pos, size_type n=npos)
216 { *
this =
ustring(str,pos,n);
return *
this; }
220 const ustring & assign (
const std::string &str) {
221 assign (str.c_str());
227 const ustring & assign (
const std::string &str, size_type pos,
229 { *
this =
ustring(str,pos,n);
return *
this; }
233 const ustring & assign (
const char *str) {
234 m_chars = str ? make_unique(str) : NULL;
240 const ustring & assign (
const char *str, size_type n)
241 { *
this =
ustring(str,n);
return *
this; }
245 const ustring & assign (size_type n,
char c)
246 { *
this =
ustring(n,c);
return *
this; }
250 const ustring & operator= (
const ustring &str) {
return assign(str); }
254 const ustring & operator= (
const char *str) {
return assign(str); }
258 const ustring & operator= (
const std::string &str) {
return assign(str); }
262 const ustring & operator= (
char c) {
271 const char *c_str ()
const {
277 const char *data ()
const {
return c_str(); }
281 const std::string & string ()
const {
283 const TableRep *rep = (
const TableRep *)m_chars - 1;
286 else return empty_std_string;
297 size_t length (
void)
const {
300 const TableRep *rep = ((
const TableRep *)m_chars) - 1;
306 size_t hash (
void)
const {
309 const TableRep *rep = ((
const TableRep *)m_chars) - 1;
315 size_t size (
void)
const {
return length(); }
319 bool empty (
void)
const {
return (size() == 0); }
324 operator int (
void)
const {
return !empty(); }
328 const_iterator begin ()
const {
return string().begin(); }
332 const_iterator end ()
const {
return string().end(); }
336 const_reverse_iterator rbegin ()
const {
return string().rbegin(); }
340 const_reverse_iterator rend ()
const {
return string().rend(); }
345 const_reference operator[] (size_type pos)
const {
return c_str()[pos]; }
349 size_type copy (
char* s, size_type n, size_type pos = 0)
const {
350 if (m_chars == NULL) {
354 char *c = strncpy (s, c_str()+pos, n);
355 return (size_type)(c-s);
360 ustring substr (size_type pos = 0, size_type n = npos)
const {
361 return ustring (*
this, pos, n);
366 size_type find(
const ustring &str, size_type pos = 0)
const {
367 return string().find(str.string(), pos);
370 size_type find(
const std::string &str, size_type pos = 0)
const {
371 return string().find(str, pos);
374 size_type find(
const char *s, size_type pos, size_type n)
const {
375 return string().find(s, pos, n);
378 size_type find(
const char *s, size_type pos = 0)
const {
379 return string().find(s, pos);
382 size_type find(
char c, size_type pos = 0)
const {
383 return string().find(c, pos);
386 size_type rfind(
const ustring &str, size_type pos = npos)
const {
387 return string().rfind(str.string(), pos);
390 size_type rfind(
const std::string &str, size_type pos = npos)
const {
391 return string().rfind(str, pos);
394 size_type rfind(
const char *s, size_type pos, size_type n)
const {
395 return string().rfind(s, pos, n);
398 size_type rfind(
const char *s, size_type pos = npos)
const {
399 return string().rfind(s, pos);
402 size_type rfind(
char c, size_type pos = npos)
const {
403 return string().rfind(c, pos);
406 size_type find_first_of(
const ustring &str, size_type pos = 0)
const {
407 return string().find_first_of(str.string(), pos);
410 size_type find_first_of(
const std::string &str, size_type pos = 0)
const {
411 return string().find_first_of(str, pos);
414 size_type find_first_of(
const char *s, size_type pos, size_type n)
const {
415 return string().find_first_of(s, pos, n);
418 size_type find_first_of(
const char *s, size_type pos = 0)
const {
419 return string().find_first_of(s, pos);
422 size_type find_first_of(
char c, size_type pos = 0)
const {
423 return string().find_first_of(c, pos);
426 size_type find_last_of(
const ustring &str, size_type pos = npos)
const {
427 return string().find_last_of(str.string(), pos);
430 size_type find_last_of(
const std::string &str, size_type pos = npos)
const {
431 return string().find_last_of(str, pos);
434 size_type find_last_of(
const char *s, size_type pos, size_type n)
const {
435 return string().find_last_of(s, pos, n);
438 size_type find_last_of(
const char *s, size_type pos = npos)
const {
439 return string().find_last_of(s, pos);
442 size_type find_last_of(
char c, size_type pos = npos)
const {
443 return string().find_last_of(c, pos);
446 size_type find_first_not_of(
const ustring &str, size_type pos = 0)
const {
447 return string().find_first_not_of(str.string(), pos);
450 size_type find_first_not_of(
const std::string &str, size_type pos = 0)
const {
451 return string().find_first_not_of(str, pos);
454 size_type find_first_not_of(
const char *s, size_type pos, size_type n)
const {
455 return string().find_first_not_of(s, pos, n);
458 size_type find_first_not_of(
const char *s, size_type pos = 0)
const {
459 return string().find_first_not_of(s, pos);
462 size_type find_first_not_of(
char c, size_type pos = 0)
const {
463 return string().find_first_not_of(c, pos);
466 size_type find_last_not_of(
const ustring &str, size_type pos = npos)
const {
467 return string().find_last_not_of(str.string(), pos);
470 size_type find_last_not_of(
const std::string &str, size_type pos = npos)
const {
471 return string().find_last_not_of(str, pos);
474 size_type find_last_not_of(
const char *s, size_type pos, size_type n)
const {
475 return string().find_last_not_of(s, pos, n);
478 size_type find_last_not_of(
const char *s, size_type pos = npos)
const {
479 return string().find_last_not_of(s, pos);
482 size_type find_last_not_of(
char c, size_type pos = npos)
const {
483 return string().find_last_not_of(c, pos);
490 int compare (
const ustring& str)
const {
491 return (c_str() == str.c_str()) ? 0
492 : strcmp (c_str() ? c_str() :
"", str.c_str() ? str.c_str() :
"");
498 int compare (
const std::string& str)
const {
499 return strcmp (c_str() ? c_str() :
"", str.c_str());
505 friend int compare (
const std::string& a,
const ustring &b) {
506 return strcmp (a.c_str(), b.c_str() ? b.c_str() :
"");
513 bool operator== (
const ustring &str)
const {
514 return c_str() == str.c_str();
521 bool operator!= (
const ustring &str)
const {
522 return c_str() != str.c_str();
527 bool operator== (
const std::string &x)
const {
return compare(x) == 0; }
531 friend bool operator== (
const std::string &a,
const ustring &b) {
532 return b.compare(a) == 0;
537 bool operator!= (
const std::string &x)
const {
return compare(x) != 0; }
541 friend bool operator!= (
const std::string &a,
const ustring &b) {
542 return b.compare(a) != 0;
547 bool operator< (
const ustring &x)
const {
return compare(x) < 0; }
557 TINYFORMAT_WRAP_FORMAT (
static ustring, format, ,
558 std::ostringstream msg;, msg,
return ustring(msg.str());)
562 friend std::ostream &
operator<< (std::ostream &out,
const ustring &str) {
570 static std::string getstats (
bool verbose =
true);
574 static size_t memory ();
582 static const char * make_unique (
const char *str);
586 static bool is_unique (
const char *str) {
587 return str == NULL || make_unique(str) == str;
593 static ustring from_unique (
const char *unique) {
616 size_t dummy_capacity;
618 TableRep (
const char *s,
size_t len);
620 const char *c_str ()
const {
return (
const char *)(
this + 1); }
624 static std::string empty_std_string;
634 size_t operator() (
const ustring &s)
const {
return s.hash(); }
642 return a==b || Strutil::iequals(a.string(), b.string());
644 inline bool iequals (
ustring a,
const std::string &b) {
645 return Strutil::iequals(a.string(), b);
647 inline bool iequals (
const std::string &a,
ustring b) {
648 return Strutil::iequals(a, b.string());
655 #endif // OPENIMAGEIO_USTRING_H