7#ifndef PXR_BASE_TF_STRING_UTILS_H
8#define PXR_BASE_TF_STRING_UTILS_H
19#include "pxr/base/tf/api.h"
31PXR_NAMESPACE_OPEN_SCOPE
87 return ptr ? std::string(ptr) : std::string();
195Tf_StringStartsWithImpl(
char const *s,
size_t slen,
196 char const *prefix,
size_t prelen)
198 return slen >= prelen && strncmp(s, prefix, prelen) == 0;
205 return Tf_StringStartsWithImpl(
206 s.c_str(), s.length(), prefix, strlen(prefix));
216Tf_StringEndsWithImpl(
char const *s,
size_t slen,
217 char const *suffix,
size_t suflen)
219 return slen >= suflen && strcmp(s + (slen - suflen), suffix) == 0;
225 return Tf_StringEndsWithImpl(s.c_str(), s.length(),
226 suffix, strlen(suffix));
284 const char* trimChars =
" \n\t\r");
292 const char* trimChars =
" \n\t\r");
301 const char* trimChars =
" \n\t\r");
349 const std::string& to);
356template <
class ForwardIterator>
358 ForwardIterator begin, ForwardIterator end,
359 const char* separator =
" ")
362 return std::string();
364 size_t distance = std::distance(begin, end);
371 ForwardIterator i = begin;
372 for (i = begin; i != end; ++i)
374 retVal.reserve(sum + strlen(separator) * (distance - 1));
379 retVal.append(separator);
392 const char* separator =
" ");
400 const char* separator =
" ");
409 std::string
const &separator);
422 const char* delimiters =
" \t\n");
429 const char* delimiters =
" \t\n");
441std::vector<std::string>
443 const char* delimiters =
" \t\n",
444 std::string *errors = NULL);
457std::vector<std::string>
461 char escapeCharacter =
'\0',
462 std::string *errors = NULL);
471std::vector<std::string>
478 closeDelimiter,
'\0', errors);
525 const std::string &rhs)
const {
530 const unsigned char l = lhs.c_str()[0], r = rhs.c_str()[0];
531 const bool bothAscii = l < 0x80 && r < 0x80;
532 const bool differsIgnoringCase = (l & ~0x20) != (r & ~0x20);
533 const bool inLetterZone = (l >= 0x40) && (r >= 0x40);
534 if (ARCH_LIKELY(bothAscii && differsIgnoringCase && inLetterZone)) {
537 return ((l + 5) & 31) < ((r + 5) & 31);
540 return _LessImpl(lhs, rhs);
544 TF_API
bool _LessImpl(
const std::string &lhs,
545 const std::string &rhs)
const;
557 if constexpr (std::is_enum<T>::value) {
561 std::ostringstream stream;
583 double d,
char* buffer,
int len,
bool emitTrailingZero);
617 std::istringstream stream(instring);
619 if (status && !stream)
673TF_API
void TfEscapeStringReplaceChar(
const char** in,
char** out);
687 const std::string &suffix );
697 char const *p = identifier.c_str();
698 auto letter = [](
unsigned c) {
return ((c-
'A') < 26) || ((c-
'a') < 26); };
699 auto number = [](
unsigned c) {
return (c-
'0') < 10; };
700 auto under = [](
unsigned c) {
return c ==
'_'; };
702 if (!x || number(x)) {
705 while (letter(x) || number(x) || under(x)) {
726PXR_NAMESPACE_CLOSE_SCOPE
Define function attributes.
#define ARCH_PRINTF_FUNCTION(_fmt, _firstArg)
Macro used to indicate a function takes a printf-like specification.
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.
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.
T TfUnstringify(const std::string &instring, bool *status=NULL)
Convert a string to an arbitrary type.
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*.
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.
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.
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.
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.
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.
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.
Provides dictionary ordering binary predicate function on strings.
bool operator()(const std::string &lhs, const std::string &rhs) const
Return true if lhs is less than rhs in dictionary order.
A type which offers streaming for doubles in a canonical format that can safely roundtrip with the mi...
A type which offers streaming for floats in a canonical format that can safely roundtrip with the min...