24 #ifndef PXR_BASE_TF_STRING_UTILS_H 25 #define PXR_BASE_TF_STRING_UTILS_H 35 #include "pxr/base/tf/api.h" 44 #include <type_traits> 47 PXR_NAMESPACE_OPEN_SCOPE
103 return ptr ? std::string(ptr) : std::string();
150 long TfStringToLong(
const std::string &txt,
bool *outOfRange=NULL);
168 unsigned long TfStringToULong(
const std::string &txt,
bool *outOfRange=NULL);
187 int64_t
TfStringToInt64(
const std::string &txt,
bool *outOfRange=NULL);
211 Tf_StringStartsWithImpl(
char const *s,
size_t slen,
212 char const *prefix,
size_t prelen)
214 return slen >= prelen && strncmp(s, prefix, prelen) == 0;
221 return Tf_StringStartsWithImpl(
222 s.c_str(), s.length(), prefix, strlen(prefix));
232 Tf_StringEndsWithImpl(
char const *s,
size_t slen,
233 char const *suffix,
size_t suflen)
235 return slen >= suflen && strcmp(s + (slen - suflen), suffix) == 0;
241 return Tf_StringEndsWithImpl(s.c_str(), s.length(),
242 suffix, strlen(suffix));
286 const char* trimChars =
" \n\t\r");
294 const char* trimChars =
" \n\t\r");
303 const char* trimChars =
" \n\t\r");
350 std::string
TfStringReplace(
const std::string& source,
const std::string& from,
351 const std::string& to);
358 template <
class ForwardIterator>
360 ForwardIterator begin, ForwardIterator end,
361 const char* separator =
" ")
364 return std::string();
366 size_t distance = std::distance(begin, end);
373 ForwardIterator i = begin;
374 for (i = begin; i != end; ++i)
376 retVal.reserve(sum + strlen(separator) * (distance - 1));
381 retVal.append(separator);
393 std::string
TfStringJoin(
const std::vector<std::string>& strings,
394 const char* separator =
" ");
401 std::string
TfStringJoin(
const std::set<std::string>& strings,
402 const char* separator =
" ");
410 std::vector<std::string>
TfStringSplit(std::string
const &src,
411 std::string
const &separator);
424 const char* delimiters =
" \t\n");
431 const char* delimiters =
" \t\n");
443 std::vector<std::string>
445 const char* delimiters =
" \t\n",
446 std::string *errors = NULL);
459 std::vector<std::string>
463 char escapeCharacter =
'\0',
464 std::string *errors = NULL);
473 std::vector<std::string>
480 closeDelimiter,
'\0', errors);
513 const std::string &rhs)
const {
519 char l = lhs.c_str()[0], r = rhs.c_str()[0];
520 if (((l & ~0x20) != (r & ~0x20)) &
bool(l & r & ~0x3f)) {
523 return ((l + 5) & 31) < ((r + 5) & 31);
526 return _LessImpl(lhs, rhs);
530 TF_API
bool _LessImpl(
const std::string &lhs,
531 const std::string &rhs)
const;
539 template <
typename T>
540 typename std::enable_if<!std::is_enum<T>::value, std::string>::type
543 std::ostringstream stream;
549 template <
typename T>
550 typename std::enable_if<std::is_enum<T>::value, std::string>::type
559 TF_API std::string
TfStringify(std::string
const&);
572 double d,
char* buffer,
int len,
bool emitTrailingZero);
601 template <
typename T>
606 std::istringstream stream(instring);
608 if (status && !stream)
616 bool TfUnstringify(
const std::string &instring,
bool* status);
620 std::string
TfUnstringify(
const std::string &instring,
bool* status);
662 TF_API
void TfEscapeStringReplaceChar(
const char** in,
char** out);
676 const std::string &suffix );
686 char const *p = identifier.c_str();
687 auto letter = [](
unsigned c) {
return ((c-
'A') < 26) || ((c-
'a') < 26); };
688 auto number = [](
unsigned c) {
return (c-
'0') < 10; };
689 auto under = [](
unsigned c) {
return c ==
'_'; };
691 if (!x || number(x)) {
694 while (letter(x) || number(x) || under(x)) {
715 PXR_NAMESPACE_CLOSE_SCOPE
717 #endif // PXR_BASE_TF_STRING_UTILS_H TF_API std::string TfGetXmlEscapedString(const std::string &in)
Escapes characters in in so that they are valid XML.
TF_API std::string TfStringPrintf(const char *fmt,...)
Returns a string formed by a printf()-like specification.
TF_API unsigned long TfStringToULong(const std::string &txt, bool *outOfRange=NULL)
Convert a sequence of digits in txt to an unsigned long value.
std::string TfSafeString(const char *ptr)
Safely create a std::string from a (possibly NULL) char*.
bool TfStringEndsWith(const std::string &s, const char *suffix)
Returns true if s ends with suffix.
Provides dictionary ordering binary predicate function on strings.
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.
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.
TF_API std::string TfStringTrimRight(const std::string &s, const char *trimChars=" \n\t\r")
Trims characters (by default, whitespace) from the right.
Define function attributes.
bool TfStringStartsWith(const std::string &s, const char *prefix)
Returns true if s starts with prefix.
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.
TF_API double TfStringToDouble(const std::string &txt)
Converts text string to double.
TF_API std::string TfStringGlobToRegex(const std::string &s)
Returns a string with glob characters converted to their regular expression equivalents.
TF_API std::string TfMakeValidIdentifier(const std::string &in)
Produce a valid identifier (see TfIsValidIdentifier) from in by replacing invalid characters with '_'...
TF_API std::string TfStringToLower(const std::string &source)
Makes all characters in source lowercase, and returns the result.
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 > 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 bool TfDoubleToString(double d, char *buffer, int len, bool emitTrailingZero)
Writes the string representation of d to buffer of length len.
std::string TfIntToString(int i)
Returns the given integer as a string.
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.
A type which offers streaming for floats in a canonical format that can safely roundtrip with the min...
static TF_API std::string GetName(TfEnum val)
Returns the name associated with an enumerated value.
bool operator()(const std::string &lhs, const std::string &rhs) const
Return true if lhs is less than rhs in dictionary order.
Token for efficient comparison, assignment, and hashing of known strings.
TF_API std::string TfVStringPrintf(const std::string &fmt, va_list ap)
Returns a string formed by a printf()-like specification.
TF_API uint64_t TfStringToUInt64(const std::string &txt, bool *outOfRange=NULL)
Convert a sequence of digits in txt to a uint64_t value.
#define ARCH_PRINTF_FUNCTION(_fmt, _firstArg)
Macro used to indicate a function takes a printf-like specification.
TF_API bool TfStringContains(const std::string &s, const char *substring)
Returns true if s contains substring.
std::string TfStringJoin(ForwardIterator begin, ForwardIterator end, const char *separator=" ")
Concatenates the strings (begin, end), with default separator.
TF_API std::string TfStringGetSuffix(const std::string &name, char delimiter='.')
Returns the suffix of a string.
TF_API std::string TfStringCapitalize(const std::string &source)
Returns a copy of the source string with only its first character capitalized.
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
TF_API std::string TfStringToUpper(const std::string &source)
Makes all characters in source uppercase, and returns the result.
TF_API std::string TfStringGetCommonPrefix(std::string a, std::string b)
Returns the common prefix of the input strings, if any.
A type which offers streaming for doubles in a canonical format that can safely roundtrip with the mi...
T TfUnstringify(const std::string &instring, bool *status=NULL)
Convert a string to an arbitrary type.
TF_API std::string TfGetBaseName(const std::string &fileName)
Returns the base name of a file (final component of the path).
bool TfIsValidIdentifier(std::string const &identifier)
Test whether identifier is valid.
std::enable_if<!std::is_enum< T >::value, std::string >::type TfStringify(const T &v)
Convert an arbitrary type into a string.
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 int64_t TfStringToInt64(const std::string &txt, bool *outOfRange=NULL)
Convert a sequence of digits in txt to an int64_t value.
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 std::string TfStringTrimLeft(const std::string &s, const char *trimChars=" \n\t\r")
Trims characters (by default, whitespace) from the left.
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::string TfEscapeString(const std::string &in)
Process escape sequences in ANSI C string constants.
TF_API std::string TfGetPathName(const std::string &fileName)
Returns the path component of a file (complement of TfGetBaseName()).