All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
stringUtils.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_BASE_TF_STRING_UTILS_H
8#define PXR_BASE_TF_STRING_UTILS_H
9
13
14#include "pxr/pxr.h"
15
17#include "pxr/base/arch/hints.h"
19#include "pxr/base/tf/api.h"
20#include "pxr/base/tf/enum.h"
21
22#include <cstdarg>
23#include <cstring>
24#include <list>
25#include <locale>
26#include <set>
27#include <sstream>
28#include <string>
29#include <type_traits>
30#include <vector>
31
32PXR_NAMESPACE_OPEN_SCOPE
33
34class TfToken;
35
38
56TF_API
57std::string TfStringPrintf(const char *fmt, ...)
58#ifndef doxygen
60#endif /* doxygen */
61 ;
62
72TF_API
73std::string TfVStringPrintf(const std::string& fmt, va_list ap);
74
76
77TF_API
78std::string TfVStringPrintf(const char *fmt, va_list ap)
79#ifndef doxygen
81#endif /* doxygen */
82 ;
83
87inline std::string TfSafeString(const char* ptr) {
88 return ptr ? std::string(ptr) : std::string();
89}
90
92inline std::string TfIntToString(int i) {
93 return TfStringPrintf("%d", i);
94}
95
115TF_API double TfStringToDouble(const std::string& txt);
116
118TF_API double TfStringToDouble(const char *text);
119
121TF_API double TfStringToDouble(const char *text, int len);
122
134TF_API
135long TfStringToLong(const std::string &txt, bool *outOfRange=NULL);
136
138
139TF_API
140long TfStringToLong(const char *txt, bool *outOfRange=NULL);
141
152TF_API
153unsigned long TfStringToULong(const std::string &txt, bool *outOfRange=NULL);
154
156
157TF_API
158unsigned long TfStringToULong(const char *txt, bool *outOfRange=NULL);
159
171TF_API
172int64_t TfStringToInt64(const std::string &txt, bool *outOfRange=NULL);
173
175TF_API
176int64_t TfStringToInt64(const char *txt, bool *outOfRange=NULL);
177
188TF_API
189uint64_t TfStringToUInt64(const std::string &txt, bool *outOfRange=NULL);
190
192TF_API
193uint64_t TfStringToUInt64(const char *txt, bool *outOfRange=NULL);
194
195inline bool
196Tf_StringStartsWithImpl(char const *s, size_t slen,
197 char const *prefix, size_t prelen)
198{
199 return slen >= prelen && strncmp(s, prefix, prelen) == 0;
200}
201
203inline bool
204TfStringStartsWith(const std::string& s, const char *prefix)
205{
206 return Tf_StringStartsWithImpl(
207 s.c_str(), s.length(), prefix, strlen(prefix));
208}
209
211inline bool
212TfStringStartsWith(const std::string& s, const std::string& prefix) {
213 return TfStringStartsWith(s, prefix.c_str());
214}
215
216inline bool
217Tf_StringEndsWithImpl(char const *s, size_t slen,
218 char const *suffix, size_t suflen)
219{
220 return slen >= suflen && strcmp(s + (slen - suflen), suffix) == 0;
221}
222
224inline bool TfStringEndsWith(const std::string& s, const char *suffix)
225{
226 return Tf_StringEndsWithImpl(s.c_str(), s.length(),
227 suffix, strlen(suffix));
228}
229
231inline bool
232TfStringEndsWith(const std::string& s, const std::string& suffix)
233{
234 return TfStringEndsWith(s, suffix.c_str());
235}
236
238// \ingroup group_tf_String
239TF_API
240bool TfStringContains(const std::string& s, const char *substring);
241
243inline bool
244TfStringContains(const std::string &s, const std::string &substring) {
245 return TfStringContains(s, substring.c_str());
246}
247
249TF_API
250bool TfStringContains(const std::string &s, const TfToken& substring);
251
253TF_API
254std::string TfStringToLower(const std::string& source);
255
257TF_API
258std::string TfStringToUpper(const std::string& source);
259
262TF_API
263std::string TfStringCapitalize(const std::string& source);
264
276TF_API
277std::string TfStringToLowerAscii(const std::string& source);
278
283TF_API
284std::string TfStringTrimLeft(const std::string& s,
285 const char* trimChars = " \n\t\r");
286
291TF_API
292std::string TfStringTrimRight(const std::string& s,
293 const char* trimChars = " \n\t\r");
294
300TF_API
301std::string TfStringTrim(const std::string& s,
302 const char* trimChars = " \n\t\r");
303
309TF_API
310std::string TfStringGetCommonPrefix(std::string a, std::string b);
311
317TF_API
318std::string TfStringGetSuffix(const std::string& name, char delimiter = '.');
319
326TF_API
327std::string TfStringGetBeforeSuffix(const std::string& name, char delimiter = '.');
328
330TF_API
331std::string TfGetBaseName(const std::string& fileName);
332
340TF_API
341std::string TfGetPathName(const std::string& fileName);
342
348TF_API
349std::string TfStringReplace(const std::string& source, const std::string& from,
350 const std::string& to);
351
357template <class ForwardIterator>
358std::string TfStringJoin(
359 ForwardIterator begin, ForwardIterator end,
360 const char* separator = " ")
361{
362 if (begin == end)
363 return std::string();
364
365 size_t distance = std::distance(begin, end);
366 if (distance == 1)
367 return *begin;
368
369 std::string retVal;
370
371 size_t sum = 0;
372 ForwardIterator i = begin;
373 for (i = begin; i != end; ++i)
374 sum += i->size();
375 retVal.reserve(sum + strlen(separator) * (distance - 1));
376
377 i = begin;
378 retVal.append(*i);
379 while (++i != end) {
380 retVal.append(separator);
381 retVal.append(*i);
382 }
383
384 return retVal;
385}
386
391TF_API
392std::string TfStringJoin(const std::vector<std::string>& strings,
393 const char* separator = " ");
394
399TF_API
400std::string TfStringJoin(const std::set<std::string>& strings,
401 const char* separator = " ");
402
408TF_API
409std::vector<std::string> TfStringSplit(std::string const &src,
410 std::string const &separator);
411
421TF_API
422std::vector<std::string> TfStringTokenize(const std::string& source,
423 const char* delimiters = " \t\n");
424
428TF_API
429std::set<std::string> TfStringTokenizeToSet(const std::string& source,
430 const char* delimiters = " \t\n");
431
441TF_API
442std::vector<std::string>
443TfQuotedStringTokenize(const std::string& source,
444 const char* delimiters = " \t\n",
445 std::string *errors = NULL);
446
457TF_API
458std::vector<std::string>
459TfMatchedStringTokenize(const std::string& source,
460 char openDelimiter,
461 char closeDelimiter,
462 char escapeCharacter = '\0',
463 std::string *errors = NULL);
464
471inline
472std::vector<std::string>
473TfMatchedStringTokenize(const std::string& source,
474 char openDelimiter,
475 char closeDelimiter,
476 std::string *errors)
477{
478 return TfMatchedStringTokenize(source, openDelimiter,
479 closeDelimiter, '\0', errors);
480}
481
525 inline bool operator()(const std::string &lhs,
526 const std::string &rhs) const {
527 // Check first chars first. By far, it is most common that these
528 // characters are ASCII letters that differ. It is very rare that we
529 // have to account for different cases, or numerical comparisons, or
530 // UTF-8 characters so we special-case this first.
531 const unsigned char l = lhs.c_str()[0], r = rhs.c_str()[0];
532 const bool bothAscii = l < 0x80 && r < 0x80;
533 const bool differsIgnoringCase = (l & ~0x20) != (r & ~0x20);
534 const bool inLetterZone = (l >= 0x40) && (r >= 0x40);
535 if (ARCH_LIKELY(bothAscii && differsIgnoringCase && inLetterZone)) {
536 // This bit about add 5 mod 32 makes it so that '_' sorts less than
537 // all letters, which preserves existing behavior.
538 return ((l + 5) & 31) < ((r + 5) & 31);
539 }
540 else {
541 return _LessImpl(lhs, rhs);
542 }
543 }
544private:
545 TF_API bool _LessImpl(const std::string &lhs,
546 const std::string &rhs) const;
547};
548
554template <typename T>
555std::string
556TfStringify(const T& v)
557{
558 if constexpr (std::is_enum<T>::value) {
559 return TfEnum::GetName(v);
560 }
561 else {
562 std::ostringstream stream;
563 stream.imbue(std::locale::classic());
564 stream << v;
565 return stream.str();
566 }
567}
568
570TF_API std::string TfStringify(bool v);
572TF_API std::string TfStringify(std::string const&);
574TF_API std::string TfStringify(float);
576TF_API std::string TfStringify(double);
577
585 double d, char* buffer, int len, bool emitTrailingZero);
586
592 explicit TfStreamFloat(float f) : value(f) {}
593 float value;
594};
595
596TF_API std::ostream& operator<<(std::ostream& o, TfStreamFloat t);
597
603 explicit TfStreamDouble(double d) : value(d) {}
604 double value;
605};
606
607TF_API std::ostream& operator<<(std::ostream& o, TfStreamDouble t);
608
614template <typename T>
615T
616TfUnstringify(const std::string &instring, bool* status = NULL)
617{
618 T v = T();
619 std::istringstream stream(instring);
620 stream >> v;
621 if (status && !stream)
622 *status = false;
623 return v;
624}
625
627template <>
628TF_API
629bool TfUnstringify(const std::string &instring, bool* status);
631template <>
632TF_API
633std::string TfUnstringify(const std::string &instring, bool* status);
634
640TF_API
641std::string TfStringGlobToRegex(const std::string& s);
642
669//
674TF_API std::string TfEscapeString(const std::string &in);
675TF_API void TfEscapeStringReplaceChar(const char** in, char** out);
676
687TF_API
688std::string TfStringCatPaths( const std::string &prefix,
689 const std::string &suffix );
690
696inline bool
697TfIsValidIdentifier(std::string const &identifier)
698{
699 char const *p = identifier.c_str();
700 auto letter = [](unsigned c) { return ((c-'A') < 26) || ((c-'a') < 26); };
701 auto number = [](unsigned c) { return (c-'0') < 10; };
702 auto under = [](unsigned c) { return c == '_'; };
703 unsigned x = *p;
704 if (!x || number(x)) {
705 return false;
706 }
707 while (letter(x) || number(x) || under(x)) {
708 x = *p++;
709 };
710 return x == 0;
711}
712
715TF_API
716std::string
717TfMakeValidIdentifier(const std::string &in);
718
723TF_API
724std::string TfGetXmlEscapedString(const std::string &in);
725
727
728PXR_NAMESPACE_CLOSE_SCOPE
729
730#endif // PXR_BASE_TF_STRING_UTILS_H
Define function attributes.
#define ARCH_PRINTF_FUNCTION(_fmt, _firstArg)
Macro used to indicate a function takes a printf-like specification.
Definition: attributes.h:34
static TF_API std::string GetName(TfEnum val)
Returns the name associated with an enumerated value.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
TF_API int64_t TfStringToInt64(const std::string &txt, bool *outOfRange=NULL)
Convert a sequence of digits in txt to an int64_t value.
TF_API bool TfStringContains(const std::string &s, const char *substring)
Returns true if s contains substring.
TF_API std::string TfStringTrimLeft(const std::string &s, const char *trimChars=" \n\t\r")
Trims characters (by default, whitespace) from the left.
bool TfStringStartsWith(const std::string &s, const char *prefix)
Returns true if s starts with prefix.
Definition: stringUtils.h:204
T TfUnstringify(const std::string &instring, bool *status=NULL)
Convert a string to an arbitrary type.
Definition: stringUtils.h:616
TF_API std::string TfStringToLower(const std::string &source)
Makes all characters in source lowercase, and returns the result.
std::string TfSafeString(const char *ptr)
Safely create a std::string from a (possibly NULL) char*.
Definition: stringUtils.h:87
TF_API std::string TfStringToLowerAscii(const std::string &source)
Locale-independent case folding of [A-Z] for ASCII or UTF-8 encoded source strings.
std::string TfIntToString(int i)
Returns the given integer as a string.
Definition: stringUtils.h:92
TF_API bool TfDoubleToString(double d, char *buffer, int len, bool emitTrailingZero)
Writes the string representation of d to buffer of length len.
TF_API std::string TfMakeValidIdentifier(const std::string &in)
Produce a valid identifier (see TfIsValidIdentifier) from in by replacing invalid characters with '_'...
std::string TfStringify(const T &v)
Convert an arbitrary type into a string.
Definition: stringUtils.h:556
TF_API std::string TfVStringPrintf(const std::string &fmt, va_list ap)
Returns a string formed by a printf()-like specification.
TF_API std::string TfStringGlobToRegex(const std::string &s)
Returns a string with glob characters converted to their regular expression equivalents.
TF_API std::set< std::string > TfStringTokenizeToSet(const std::string &source, const char *delimiters=" \t\n")
Breaks the given string apart, returning a set of strings.
bool TfIsValidIdentifier(std::string const &identifier)
Test whether identifier is valid.
Definition: stringUtils.h:697
TF_API std::vector< std::string > TfQuotedStringTokenize(const std::string &source, const char *delimiters=" \t\n", std::string *errors=NULL)
Breaks the given quoted string apart, returning a vector of strings.
TF_API std::string TfStringCatPaths(const std::string &prefix, const std::string &suffix)
Concatenate two strings containing '/' and '..' tokens like a file path or scope name.
TF_API std::vector< std::string > TfMatchedStringTokenize(const std::string &source, char openDelimiter, char closeDelimiter, char escapeCharacter='\0', std::string *errors=NULL)
Breaks the given string apart by matching delimiters.
TF_API std::string TfEscapeString(const std::string &in)
Process escape sequences in ANSI C string constants.
TF_API std::vector< std::string > TfStringTokenize(const std::string &source, const char *delimiters=" \t\n")
Breaks the given string apart, returning a vector of strings.
TF_API std::string TfStringReplace(const std::string &source, const std::string &from, const std::string &to)
Replaces all occurrences of string from with to in source.
TF_API long TfStringToLong(const std::string &txt, bool *outOfRange=NULL)
Convert a sequence of digits in txt to a long int value.
TF_API std::string TfStringGetBeforeSuffix(const std::string &name, char delimiter='.')
Returns everything up to the suffix of a string.
bool TfStringEndsWith(const std::string &s, const char *suffix)
Returns true if s ends with suffix.
Definition: stringUtils.h:224
TF_API uint64_t TfStringToUInt64(const std::string &txt, bool *outOfRange=NULL)
Convert a sequence of digits in txt to a uint64_t value.
std::string TfStringJoin(ForwardIterator begin, ForwardIterator end, const char *separator=" ")
Concatenates the strings (begin, end), with default separator.
Definition: stringUtils.h:358
TF_API std::string TfStringCapitalize(const std::string &source)
Returns a copy of the source string with only its first character capitalized.
TF_API std::string TfStringGetCommonPrefix(std::string a, std::string b)
Returns the common prefix of the input strings, if any.
TF_API unsigned long TfStringToULong(const std::string &txt, bool *outOfRange=NULL)
Convert a sequence of digits in txt to an unsigned long value.
TF_API std::string TfGetBaseName(const std::string &fileName)
Returns the base name of a file (final component of the path).
TF_API std::string TfStringToUpper(const std::string &source)
Makes all characters in source uppercase, and returns the result.
TF_API std::string TfStringPrintf(const char *fmt,...)
Returns a string formed by a printf()-like specification.
TF_API std::string TfStringTrimRight(const std::string &s, const char *trimChars=" \n\t\r")
Trims characters (by default, whitespace) from the right.
TF_API double TfStringToDouble(const std::string &txt)
Converts text string to double.
TF_API std::string TfGetXmlEscapedString(const std::string &in)
Escapes characters in in so that they are valid XML.
TF_API std::string TfGetPathName(const std::string &fileName)
Returns the path component of a file (complement of TfGetBaseName()).
TF_API std::string TfStringGetSuffix(const std::string &name, char delimiter='.')
Returns the suffix of a string.
TF_API std::string TfStringTrim(const std::string &s, const char *trimChars=" \n\t\r")
Trims characters (by default, whitespace) from the beginning and end of string.
TF_API std::vector< std::string > TfStringSplit(std::string const &src, std::string const &separator)
Breaks the given string apart, returning a vector of strings.
Compiler hints.
Define integral types.
Provides dictionary ordering binary predicate function on strings.
Definition: stringUtils.h:512
bool operator()(const std::string &lhs, const std::string &rhs) const
Return true if lhs is less than rhs in dictionary order.
Definition: stringUtils.h:525
A type which offers streaming for doubles in a canonical format that can safely roundtrip with the mi...
Definition: stringUtils.h:602
A type which offers streaming for floats in a canonical format that can safely roundtrip with the min...
Definition: stringUtils.h:591