Loading...
Searching...
No Matches
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
332TF_API
333std::string TfGetBaseName(const std::string& fileName);
334
345
346TF_API
347std::string TfGetPathName(const std::string& fileName);
348
354TF_API
355std::string TfStringReplace(const std::string& source, const std::string& from,
356 const std::string& to);
357
363template <class ForwardIterator>
364std::string TfStringJoin(
365 ForwardIterator begin, ForwardIterator end,
366 const char* separator = " ")
367{
368 if (begin == end)
369 return std::string();
370
371 size_t distance = std::distance(begin, end);
372 if (distance == 1)
373 return *begin;
374
375 std::string retVal;
376
377 size_t sum = 0;
378 ForwardIterator i = begin;
379 for (i = begin; i != end; ++i)
380 sum += i->size();
381 retVal.reserve(sum + strlen(separator) * (distance - 1));
382
383 i = begin;
384 retVal.append(*i);
385 while (++i != end) {
386 retVal.append(separator);
387 retVal.append(*i);
388 }
389
390 return retVal;
391}
392
397TF_API
398std::string TfStringJoin(const std::vector<std::string>& strings,
399 const char* separator = " ");
400
405TF_API
406std::string TfStringJoin(const std::set<std::string>& strings,
407 const char* separator = " ");
408
414TF_API
415std::vector<std::string> TfStringSplit(std::string const &src,
416 std::string const &separator);
417
427TF_API
428std::vector<std::string> TfStringTokenize(const std::string& source,
429 const char* delimiters = " \t\n");
430
434TF_API
435std::set<std::string> TfStringTokenizeToSet(const std::string& source,
436 const char* delimiters = " \t\n");
437
447TF_API
448std::vector<std::string>
449TfQuotedStringTokenize(const std::string& source,
450 const char* delimiters = " \t\n",
451 std::string *errors = NULL);
452
463TF_API
464std::vector<std::string>
465TfMatchedStringTokenize(const std::string& source,
466 char openDelimiter,
467 char closeDelimiter,
468 char escapeCharacter = '\0',
469 std::string *errors = NULL);
470
477inline
478std::vector<std::string>
479TfMatchedStringTokenize(const std::string& source,
480 char openDelimiter,
481 char closeDelimiter,
482 std::string *errors)
483{
484 return TfMatchedStringTokenize(source, openDelimiter,
485 closeDelimiter, '\0', errors);
486}
487
531 inline bool operator()(const std::string &lhs,
532 const std::string &rhs) const {
533 // Check first chars first. By far, it is most common that these
534 // characters are ASCII letters that differ. It is very rare that we
535 // have to account for different cases, or numerical comparisons, or
536 // UTF-8 characters so we special-case this first.
537 const unsigned char l = lhs.c_str()[0], r = rhs.c_str()[0];
538 const bool bothAscii = l < 0x80 && r < 0x80;
539 const bool differsIgnoringCase = (l & ~0x20) != (r & ~0x20);
540 const bool inLetterZone = (l >= 0x40) && (r >= 0x40);
541 if (ARCH_LIKELY(bothAscii && differsIgnoringCase && inLetterZone)) {
542 // This bit about add 5 mod 32 makes it so that '_' sorts less than
543 // all letters, which preserves existing behavior.
544 return ((l + 5) & 31) < ((r + 5) & 31);
545 }
546 else {
547 return _LessImpl(lhs, rhs);
548 }
549 }
550private:
551 TF_API bool _LessImpl(const std::string &lhs,
552 const std::string &rhs) const;
553};
554
560template <typename T>
561std::string
562TfStringify(const T& v)
563{
564 if constexpr (std::is_enum<T>::value) {
565 return TfEnum::GetName(v);
566 }
567 else {
568 std::ostringstream stream;
569 stream.imbue(std::locale::classic());
570 stream << v;
571 return stream.str();
572 }
573}
574
576TF_API std::string TfStringify(bool v);
578TF_API std::string TfStringify(std::string const&);
580TF_API std::string TfStringify(float);
582TF_API std::string TfStringify(double);
583
591 double d, char* buffer, int len, bool emitTrailingZero);
592
598 explicit TfStreamFloat(float f) : value(f) {}
599 float value;
600};
601
602TF_API std::ostream& operator<<(std::ostream& o, TfStreamFloat t);
603
609 explicit TfStreamDouble(double d) : value(d) {}
610 double value;
611};
612
613TF_API std::ostream& operator<<(std::ostream& o, TfStreamDouble t);
614
620template <typename T>
621T
622TfUnstringify(const std::string &instring, bool* status = NULL)
623{
624 T v = T();
625 std::istringstream stream(instring);
626 stream >> v;
627 if (status && !stream)
628 *status = false;
629 return v;
630}
631
633template <>
634TF_API
635bool TfUnstringify(const std::string &instring, bool* status);
637template <>
638TF_API
639std::string TfUnstringify(const std::string &instring, bool* status);
640
646TF_API
647std::string TfStringGlobToRegex(const std::string& s);
648
675//
680TF_API std::string TfEscapeString(const std::string &in);
681TF_API void TfEscapeStringReplaceChar(const char** in, char** out);
682
696TF_API
697std::string TfStringCatPaths( const std::string &prefix,
698 const std::string &suffix );
699
705inline bool
706TfIsValidIdentifier(std::string const &identifier)
707{
708 char const *p = identifier.c_str();
709 auto letter = [](unsigned c) { return ((c-'A') < 26) || ((c-'a') < 26); };
710 auto number = [](unsigned c) { return (c-'0') < 10; };
711 auto under = [](unsigned c) { return c == '_'; };
712 unsigned x = *p;
713 if (!x || number(x)) {
714 return false;
715 }
716 while (letter(x) || number(x) || under(x)) {
717 x = *p++;
718 };
719 return x == 0;
720}
721
724TF_API
725std::string
726TfMakeValidIdentifier(const std::string &in);
727
732TF_API
733std::string TfGetXmlEscapedString(const std::string &in);
734
736
737PXR_NAMESPACE_CLOSE_SCOPE
738
739#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:622
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:562
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:706
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:364
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, after stripping all trailing slashes - ...
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:518
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:531
A type which offers streaming for doubles in a canonical format that can safely roundtrip with the mi...
Definition: stringUtils.h:608
A type which offers streaming for floats in a canonical format that can safely roundtrip with the min...
Definition: stringUtils.h:597