HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stringUtils.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_BASE_TF_STRING_UTILS_H
25 #define PXR_BASE_TF_STRING_UTILS_H
26 
27 /// \file tf/stringUtils.h
28 /// \ingroup group_tf_String
29 /// Definitions of basic string utilities in tf.
30 
31 #include "pxr/pxr.h"
32 
34 #include "pxr/base/arch/inttypes.h"
35 #include "pxr/base/tf/api.h"
36 #include "pxr/base/tf/enum.h"
37 
38 #include <cstdarg>
39 #include <cstring>
40 #include <list>
41 #include <set>
42 #include <sstream>
43 #include <string>
44 #include <type_traits>
45 #include <vector>
46 
48 
49 class TfToken;
50 
51 /// \addtogroup group_tf_String
52 ///@{
53 
54 /// Returns a string formed by a printf()-like specification.
55 ///
56 /// \c TfStringPrintf() is a memory-safe way of forming a string using
57 /// printf()-like formatting. For example,
58 /// \code
59 /// string formatMsg(const string& caller, int i, double val[])
60 /// {
61 /// return TfStringfPrintf("%s: val[%d] = %g\n", caller.c_str(), i, val[i]);
62 /// }
63 /// \endcode
64 ///
65 /// The function is safe only to the extent that the arguments match the
66 /// formatting string. In particular, be careful to pass strings themselves
67 /// into \c TfStringPrintf() as in the above example (i.e. \c caller.c_str()
68 /// as opposed to just passing \c caller).
69 ///
70 /// \note \c TfStringPrintf() is just a wrapper for \c ArchStringPrintf().
71 TF_API
72 std::string TfStringPrintf(const char *fmt, ...)
73 #ifndef doxygen
74  ARCH_PRINTF_FUNCTION(1, 2)
75 #endif /* doxygen */
76  ;
77 
78 /// Returns a string formed by a printf()-like specification.
79 ///
80 /// \c TfVStringPrintf() is equivalent to \c TfStringPrintf() except that it
81 /// is called with a \c va_list instead of a variable number of arguments. \c
82 /// TfVStringPrintf() does not call the \c va_end macro. Consequently, the
83 /// value of \c ap is undefined after the call. A functions that calls \c
84 /// TfVStringPrintf() should call \c va_end(ap) itself afterwards.
85 ///
86 /// \note \c TfVStringPrintf() is just a wrapper for \c ArchVStringPrintf().
87 TF_API
88 std::string TfVStringPrintf(const std::string& fmt, va_list ap);
89 
90 /// Bloat-avoidance version of TfVStringPrintf()
91 
92 TF_API
93 std::string TfVStringPrintf(const char *fmt, va_list ap)
94 #ifndef doxygen
95  ARCH_PRINTF_FUNCTION(1, 0)
96 #endif /* doxygen */
97  ;
98 
99 /// Safely create a std::string from a (possibly NULL) char*.
100 ///
101 /// If \p ptr is NULL, the empty string is safely returned.
102 inline std::string TfSafeString(const char* ptr) {
103  return ptr ? std::string(ptr) : std::string();
104 }
105 
106 /// Returns the given integer as a string.
107 inline std::string TfIntToString(int i) {
108  return TfStringPrintf("%d", i);
109 }
110 
111 /// Converts text string to double
112 ///
113 /// This method converts strings to floating point numbers. It is similar to
114 /// libc's atof(), but performs the conversion much more quickly.
115 ///
116 /// It expects somewhat valid input: it will continue parsing the input until
117 /// it hits an unrecognized character, as described by the regexp below, and
118 /// at that point will return the results up to that point.
119 ///
120 /// (-?[0-9]+(\.[0-9]*)?|-?\.[0-9]+)([eE][-+]?[0-9]+)?
121 ///
122 /// It will not check to see if there is any input at all, or whitespace
123 /// after the digits. Ie:
124 /// TfStringToDouble("") == 0.0
125 /// TfStringToDouble("blah") == 0.0
126 /// TfStringToDouble("-") == -0.0
127 /// TfStringToDouble("1.2foo") == 1.2
128 ///
129 /// \note \c TfStringToDouble is a wrapper around the extern-c TfStringToDouble
130 TF_API double TfStringToDouble(const std::string& txt);
131 
132 /// \overload
133 TF_API double TfStringToDouble(const char *text);
134 
135 /// \overload
136 TF_API double TfStringToDouble(const char *text, int len);
137 
138 /// Convert a sequence of digits in \p txt to a long int value. Caller is
139 /// responsible for ensuring that \p txt has content matching:
140 ///
141 /// \code
142 /// -?[0-9]+
143 /// \endcode
144 ///
145 /// If the digit sequence's value is out of range, set \p *outOfRange to true
146 /// (if \p outOfRange is not NULL) and return either
147 /// std::numeric_limits<long>::min() or max(), whichever is closest to the
148 /// true value.
149 TF_API
150 long TfStringToLong(const std::string &txt, bool *outOfRange=NULL);
151 
152 /// \overload
153 
154 TF_API
155 long TfStringToLong(const char *txt, bool *outOfRange=NULL);
156 
157 /// Convert a sequence of digits in \p txt to an unsigned long value. Caller
158 /// is responsible for ensuring that \p txt has content matching:
159 ///
160 /// \code
161 /// [0-9]+
162 /// \endcode
163 ///
164 /// If the digit sequence's value is out of range, set \p *outOfRange to true
165 /// (if \p outOfRange is not NULL) and return std::numeric_limits<unsigned
166 /// long>::max().
167 TF_API
168 unsigned long TfStringToULong(const std::string &txt, bool *outOfRange=NULL);
169 
170 /// \overload
171 
172 TF_API
173 unsigned long TfStringToULong(const char *txt, bool *outOfRange=NULL);
174 
175 /// Convert a sequence of digits in \p txt to an int64_t value. Caller must
176 /// ensure that \p txt has content matching:
177 ///
178 /// \code
179 /// -?[0-9]+
180 /// \endcode
181 ///
182 /// If the digit sequence's value is out of range, set \p *outOfRange to true
183 /// (if \p outOfRange is not NULL) and return either
184 /// std::numeric_limits<int64_t>::min() or max(), whichever is closest to the
185 /// true value.
186 TF_API
187 int64_t TfStringToInt64(const std::string &txt, bool *outOfRange=NULL);
188 
189 /// \overload
190 TF_API
191 int64_t TfStringToInt64(const char *txt, bool *outOfRange=NULL);
192 
193 /// Convert a sequence of digits in \p txt to a uint64_t value. Caller is
194 /// responsible for ensuring that \p txt has content matching:
195 ///
196 /// \code
197 /// [0-9]+
198 /// \endcode
199 ///
200 /// If the digit sequence's value is out of range, set \p *outOfRange to true
201 /// (if \p outOfRange is not NULL) and return std::numeric_limits<unsigned
202 /// long>::max().
203 TF_API
204 uint64_t TfStringToUInt64(const std::string &txt, bool *outOfRange=NULL);
205 
206 /// \overload
207 TF_API
208 uint64_t TfStringToUInt64(const char *txt, bool *outOfRange=NULL);
209 
210 inline bool
211 Tf_StringStartsWithImpl(char const *s, size_t slen,
212  char const *prefix, size_t prelen)
213 {
214  return slen >= prelen && strncmp(s, prefix, prelen) == 0;
215 }
216 
217 /// Returns true if \p s starts with \p prefix.
218 inline bool
219 TfStringStartsWith(const std::string& s, const char *prefix)
220 {
222  s.c_str(), s.length(), prefix, strlen(prefix));
223 }
224 
225 /// \overload
226 inline bool
227 TfStringStartsWith(const std::string& s, const std::string& prefix) {
228  return TfStringStartsWith(s, prefix.c_str());
229 }
230 
231 inline bool
232 Tf_StringEndsWithImpl(char const *s, size_t slen,
233  char const *suffix, size_t suflen)
234 {
235  return slen >= suflen && strcmp(s + (slen - suflen), suffix) == 0;
236 }
237 
238 /// Returns true if \p s ends with \p suffix.
239 inline bool TfStringEndsWith(const std::string& s, const char *suffix)
240 {
241  return Tf_StringEndsWithImpl(s.c_str(), s.length(),
242  suffix, strlen(suffix));
243 }
244 
245 /// \overload
246 inline bool
248 {
249  return TfStringEndsWith(s, suffix.c_str());
250 }
251 
252 /// Returns true if \p s contains \p substring.
253 // \ingroup group_tf_String
254 TF_API
255 bool TfStringContains(const std::string& s, const char *substring);
256 
257 /// \overload
258 inline bool
259 TfStringContains(const std::string &s, const std::string &substring) {
260  return TfStringContains(s, substring.c_str());
261 }
262 
263 /// \overload
264 TF_API
265 bool TfStringContains(const std::string &s, const TfToken& substring);
266 
267 /// Makes all characters in \p source lowercase, and returns the result.
268 TF_API
270 
271 /// Makes all characters in \p source uppercase, and returns the result.
272 TF_API
274 
275 /// Returns a copy of the \p source string with only its first character
276 /// capitalized. This emulates the behavior of Python's \c str.capitalize().
277 TF_API
279 
280 /// Trims characters (by default, whitespace) from the left.
281 ///
282 /// Characters from the beginning of \p s are removed until a character not in
283 /// \p trimChars is found; the result is returned.
284 TF_API
286  const char* trimChars = " \n\t\r");
287 
288 /// Trims characters (by default, whitespace) from the right.
289 ///
290 /// Characters at the end of \p s are removed until a character not in \p
291 /// trimChars is found; the result is returned.
292 TF_API
294  const char* trimChars = " \n\t\r");
295 
296 /// Trims characters (by default, whitespace) from the beginning and end of
297 /// string.
298 ///
299 /// Characters at the beginning and end of \p s are removed until a character
300 /// not in \p trimChars is found; the result is returned.
301 TF_API
303  const char* trimChars = " \n\t\r");
304 
305 /// Returns the common prefix of the input strings, if any.
306 ///
307 /// Copies of the input strings are compared. Returns a new string which is
308 /// the longest prefix common to both input strings. If the strings have no
309 /// common prefix, an empty string is returned.
310 TF_API
312 
313 /// Returns the suffix of a string
314 ///
315 /// Returns characters after the final character \c delimiter (default ".") of
316 /// a string. Thus suffix of "abc.def" is "def" using "." as the delimiter.
317 /// If the delimiter does not occur, the empty string is returned.
318 TF_API
319 std::string TfStringGetSuffix(const std::string& name, char delimiter = '.');
320 
321 /// Returns everything up to the suffix of a string
322 ///
323 /// Returns characters before the final character \c delimiter (default ".")
324 /// of a string. Thus not-suffix of "abc.def" is "abc" using "." as the
325 /// delimiter. If the delimiter does not occur, the original string is
326 /// returned.
327 TF_API
328 std::string TfStringGetBeforeSuffix(const std::string& name, char delimiter = '.');
329 
330 /// Returns the base name of a file (final component of the path).
331 TF_API
332 std::string TfGetBaseName(const std::string& fileName);
333 
334 /// Returns the path component of a file (complement of TfGetBaseName()).
335 ///
336 /// The returned string ends in a '/' (or possibly a '\' on Windows), unless
337 /// none was found in \c fileName, in which case the empty string is returned.
338 /// In particular, \c TfGetPathName(s)+TfGetBaseName(s) == \c s for any string
339 /// \c s (as long as \c s doesn't end with multiple adjacent slashes, which is
340 /// illegal).
341 TF_API
342 std::string TfGetPathName(const std::string& fileName);
343 
344 /// Replaces all occurrences of string \p from with \p to in \p source
345 ///
346 /// Returns a new string which is created by copying \p string and replacing
347 /// every occurrence of \p from with \p to. Correctly handles the case in which
348 /// \p to contains \p from.
349 TF_API
351  const std::string& to);
352 
353 /// Concatenates the strings (\p begin, \p end), with default separator.
354 ///
355 /// Returns the concatenation of the strings in the range \p begin to \p end,
356 /// with \p separator (by default, a space) added between each successive pair
357 /// of strings.
358 template <class ForwardIterator>
360  ForwardIterator begin, ForwardIterator end,
361  const char* separator = " ")
362 {
363  if (begin == end)
364  return std::string();
365 
366  size_t distance = std::distance(begin, end);
367  if (distance == 1)
368  return *begin;
369 
370  std::string retVal;
371 
372  size_t sum = 0;
373  ForwardIterator i = begin;
374  for (i = begin; i != end; ++i)
375  sum += i->size();
376  retVal.reserve(sum + strlen(separator) * (distance - 1));
377 
378  i = begin;
379  retVal.append(*i);
380  while (++i != end) {
381  retVal.append(separator);
382  retVal.append(*i);
383  }
384 
385  return retVal;
386 }
387 
388 /// Concatenates \p strings, with default separator.
389 ///
390 /// Returns the concatenation of the strings in \p strings, with \p separator
391 /// (by default, a space) added between each successive pair of strings.
392 TF_API
393 std::string TfStringJoin(const std::vector<std::string>& strings,
394  const char* separator = " ");
395 
396 /// Concatenates \p strings, with default separator.
397 ///
398 /// Returns the concatenation of the strings in \p strings, with \p separator
399 /// (by default, a space) added between each successive pair of strings.
400 TF_API
401 std::string TfStringJoin(const std::set<std::string>& strings,
402  const char* separator = " ");
403 
404 /// Breaks the given string apart, returning a vector of strings.
405 ///
406 /// The string \p source is broken apart into individual words, where a word
407 /// is delimited by the string \p separator. This function behaves like
408 /// pythons string split method.
409 TF_API
410 std::vector<std::string> TfStringSplit(std::string const &src,
411  std::string const &separator);
412 
413 /// Breaks the given string apart, returning a vector of strings.
414 ///
415 /// The string \p source is broken apart into individual words, where a word
416 /// is delimited by the characters in \p delimiters. Delimiters default to
417 /// white space (space, tab, and newline).
418 ///
419 /// No empty strings are returned: delimiters at the start or end are ignored,
420 /// consecutive delimiters are treated as though they were one, and an empty
421 /// input will result in an empty return vector.
422 TF_API
423 std::vector<std::string> TfStringTokenize(const std::string& source,
424  const char* delimiters = " \t\n");
425 
426 /// Breaks the given string apart, returning a set of strings.
427 ///
428 /// Same as TfStringTokenize, except this one returns a set.
429 TF_API
430 std::set<std::string> TfStringTokenizeToSet(const std::string& source,
431  const char* delimiters = " \t\n");
432 
433 /// Breaks the given quoted string apart, returning a vector of strings.
434 ///
435 /// The string \p source is broken apart into individual words, where a word
436 /// is delimited by the characters in \p delimiters. This function is similar
437 /// to \c TfStringTokenize, except it considers a quoted string as a single
438 /// word. The function will preserve quotes that are nested within other
439 /// quotes or are preceded by a backslash character. \p errors, if provided,
440 /// contains any error messages. Delimiters default to white space (space,
441 /// tab, and newline).
442 TF_API
443 std::vector<std::string>
445  const char* delimiters = " \t\n",
446  std::string *errors = NULL);
447 
448 /// Breaks the given string apart by matching delimiters.
449 ///
450 /// The string \p source is broken apart into individual words, where a word
451 /// begins with \p openDelimiter and ends with a matching \p closeDelimiter.
452 /// Any delimiters within the matching delimiters become part of the word, and
453 /// anything outside matching delimiters gets dropped. For example, \c
454 /// TfMatchedStringTokenize("{a} string {to {be} split}", '{', '}') would
455 /// return a vector containing "a" and "to {be} split". If \p openDelimiter and
456 /// \p closeDelimiter cannot be the same. \p errors, if provided, contains any
457 /// error messages.
458 TF_API
459 std::vector<std::string>
461  char openDelimiter,
462  char closeDelimiter,
463  char escapeCharacter = '\0',
464  std::string *errors = NULL);
465 
466 /// This overloaded version of \c TfMatchedStringTokenize does not take an \c
467 /// escapeCharacter parameter but does take \param errors. It allows \c
468 /// TfMatchedStringTokenize to be called with or without an \c escapeCharacter
469 /// and with or without \c errors.
470 ///
471 /// \overload
472 inline
473 std::vector<std::string>
475  char openDelimiter,
476  char closeDelimiter,
477  std::string *errors)
478 {
479  return TfMatchedStringTokenize(source, openDelimiter,
480  closeDelimiter, '\0', errors);
481 }
482 
483 /// \class TfDictionaryLessThan
484 ///
485 /// Provides dictionary ordering binary predicate function on strings.
486 ///
487 /// The \c TfDictionaryLessThan class is a functor as defined by the STL
488 /// standard. It compares strings using "dictionary" order: for example, the
489 /// following strings are in dictionary order:
490 /// ["abacus", "Albert", "albert", "baby", "Bert", "file01", "file001", "file2",
491 /// "file10"]
492 ///
493 /// Note that capitalization matters only if the strings differ by
494 /// capitalization alone.
495 ///
496 /// Characters whose ASCII value are inbetween upper- and lowercase letters,
497 /// such as underscore, are sorted to come after all letters.
498 ///
500  /// Return true if \p lhs is less than \p rhs in dictionary order.
501  ///
502  /// Normally this functor is used to supply an ordering functor for STL
503  /// containers: for example,
504  /// \code
505  /// map<string, DataType, TfDictionaryLessThan> table;
506  /// \endcode
507  ///
508  /// If you simply need to compare two strings, you can do so as follows:
509  /// \code
510  /// bool aIsFirst = TfDictionaryLessThan()(aString, bString);
511  /// \endcode
512  inline bool operator()(const std::string &lhs,
513  const std::string &rhs) const {
514  // Check first chars first. By far, it is most common that these
515  // characters are letters of the same case that differ, or of different
516  // case that differ. It is very rare that we have to account for
517  // different cases, or numerical comparisons, so we special-case this
518  // first.
519  char l = lhs.c_str()[0], r = rhs.c_str()[0];
520  if (((l & ~0x20) != (r & ~0x20)) & bool(l & r & ~0x3f)) {
521  // This bit about add 5 mod 32 makes it so that '_' sorts less than
522  // all letters, which preserves existing behavior.
523  return ((l + 5) & 31) < ((r + 5) & 31);
524  }
525  else {
526  return _LessImpl(lhs, rhs);
527  }
528  }
529 private:
530  TF_API bool _LessImpl(const std::string &lhs,
531  const std::string &rhs) const;
532 };
533 
534 /// Convert an arbitrary type into a string
535 ///
536 /// Use the type's stream output operator to convert it into a string. You are
537 /// free to use the stream operators in ostreamMethods.h, but are not required
538 /// to do so.
539 template <typename T>
541 TfStringify(const T& v)
542 {
543  std::ostringstream stream;
544  stream << v;
545  return stream.str();
546 }
547 
548 /// \overload
549 template <typename T>
551 TfStringify(const T& v)
552 {
553  return TfEnum::GetName(v);
554 }
555 
556 /// \overload
558 /// \overload
560 /// \overload
562 /// \overload
564 
565 /// Writes the string representation of \c d to \c buffer of length \c len.
566 /// If \c emitTrailingZero is true, the string representation will end with .0
567 /// in the case where d is an integer otherwise it will be omitted.
568 /// The buffer length must be at least 25 in order to ensure that all doubles
569 /// values can be represented.
570 /// Returns whether the conversion was successful.
572  double d, char* buffer, int len, bool emitTrailingZero);
573 
574 /// \struct TfStreamFloat
575 ///
576 /// A type which offers streaming for floats in a canonical
577 /// format that can safely roundtrip with the minimal number of digits.
579  explicit TfStreamFloat(float f) : value(f) {}
580  float value;
581 };
582 
583 TF_API std::ostream& operator<<(std::ostream& o, TfStreamFloat t);
584 
585 /// \struct TfStreamDouble
586 ///
587 /// A type which offers streaming for doubles in a canonical
588 /// format that can safely roundtrip with the minimal number of digits.
590  explicit TfStreamDouble(double d) : value(d) {}
591  double value;
592 };
593 
594 TF_API std::ostream& operator<<(std::ostream& o, TfStreamDouble t);
595 
596 /// Convert a string to an arbitrary type
597 ///
598 /// Use the type's stream input operator to get it from a string. If \p status
599 /// is non-NULL and \p instring cannot be converted to a \c T, \p *status is
600 /// set to \c false; otherwise, \p *status is not modified.
601 template <typename T>
602 T
603 TfUnstringify(const std::string &instring, bool* status = NULL)
604 {
605  T v = T();
606  std::istringstream stream(instring);
607  stream >> v;
608  if (status && !stream)
609  *status = false;
610  return v;
611 }
612 
613 /// \overload
614 template <>
615 TF_API
616 bool TfUnstringify(const std::string &instring, bool* status);
617 /// \overload
618 template <>
619 TF_API
620 std::string TfUnstringify(const std::string &instring, bool* status);
621 
622 /// Returns a string with glob characters converted to their regular
623 /// expression equivalents.
624 ///
625 /// Currently, this transforms strings by replacing all instances of '.' with
626 /// '\.', '*' with '.*', and '?' with '.', in that order.
627 TF_API
629 
630 /// Process escape sequences in ANSI C string constants.
631 ///
632 /// The following escape sequences are accepted:
633 ///
634 /// \li \\\\: backslash
635 /// \li \\a: ring the bell
636 /// \li \\b: backspace
637 /// \li \\f: form feed
638 /// \li \\n: new line
639 /// \li \\r: carriage return
640 /// \li \\t: tab
641 /// \li \\v: vertical tab
642 /// \li \\xdd: hex constant
643 /// \li \\ddd: octal constant
644 ///
645 /// So, if the two-character sequence "\\n" appears in the string, it is
646 /// replaced by an actual newline. Each hex and octal constant translates into
647 /// one character in the output string. Hex constants can be up to 2 digits,
648 /// octal constants can be up to 3 digits. Both are terminated by a character
649 /// that is not a valid constant. Note that it is good practice to encode hex
650 /// and octal constants with maximum width (2 and 3 digits, respectively) using
651 /// leading zeroes if necessary. This avoids problems where characters after
652 /// the hex/octal constant that shouldn't be part of the constant get
653 /// interpreted as part of it. For example, the sequence "\x2defaced" will
654 /// produce the characters "-efaced" when what was probably intended was the
655 /// character 0x02 (STX) followed by "defaced".
656 //
657 /// Illegal escape sequences are replaced by the character following the
658 /// backslash, so the two character sequence "\\c" would become "c". Processing
659 /// continues until the input hits a NUL character in the input string -
660 /// anything appearing after the NUL will be ignored.
662 TF_API void TfEscapeStringReplaceChar(const char** in, char** out);
663 
664 /// Concatenate two strings containing '/' and '..' tokens like a file path or
665 /// scope name.
666 ///
667 /// Tokenize the input strings using a '/' delimiter. Look for '..' tokens in
668 /// the suffix and construct the appropriate result.
669 ///
670 /// Examples:
671 ///
672 /// \li TfStringCatPaths( "foo/bar", "jive" ) => "foo/bar/jive"
673 /// \li TfStringCatPaths( "foo/bar", "../jive" ) => "foo/jive"
674 TF_API
676  const std::string &suffix );
677 
678 /// Test whether \a identifier is valid.
679 ///
680 /// An identifier is valid if it follows the C/Python identifier convention;
681 /// that is, it must be at least one character long, must start with a letter
682 /// or underscore, and must contain only letters, underscores, and numerals.
683 inline bool
685 {
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 == '_'; };
690  unsigned x = *p;
691  if (!x || number(x)) {
692  return false;
693  }
694  while (letter(x) || number(x) || under(x)) {
695  x = *p++;
696  };
697  return x == 0;
698 }
699 
700 /// Produce a valid identifier (see TfIsValidIdentifier) from \p in by
701 /// replacing invalid characters with '_'. If \p in is empty, return "_".
702 TF_API
705 
706 /// Escapes characters in \a in so that they are valid XML.
707 ///
708 /// Returns the name with special characters (&, <, >, ", ') replaced with the
709 /// corresponding escape sequences.
710 TF_API
712 
713 ///@}
714 
716 
717 #endif // PXR_BASE_TF_STRING_UTILS_H
TF_API std::string TfGetXmlEscapedString(const std::string &in)
TF_API std::string TfStringPrintf(const char *fmt,...)
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
#define TF_API
Definition: api.h:40
TF_API unsigned long TfStringToULong(const std::string &txt, bool *outOfRange=NULL)
std::string TfSafeString(const char *ptr)
Definition: stringUtils.h:102
bool TfStringEndsWith(const std::string &s, const char *suffix)
Returns true if s ends with suffix.
Definition: stringUtils.h:239
TF_API long TfStringToLong(const std::string &txt, bool *outOfRange=NULL)
TF_API std::string TfStringGetSuffix(const std::string &name, char delimiter= '.')
TF_API std::vector< std::string > TfStringSplit(std::string const &src, std::string const &separator)
TF_API std::string TfStringTrimRight(const std::string &s, const char *trimChars=" \n\t\r")
const GLfloat * c
Definition: glew.h:16631
TF_API std::ostream & operator<<(std::ostream &o, TfStreamFloat t)
GLuint GLuint stream
Definition: glcorearb.h:1832
bool TfStringStartsWith(const std::string &s, const char *prefix)
Returns true if s starts with prefix.
Definition: stringUtils.h:219
TF_API std::set< std::string > TfStringTokenizeToSet(const std::string &source, const char *delimiters=" \t\n")
TF_API double TfStringToDouble(const std::string &txt)
TF_API std::string TfStringGlobToRegex(const std::string &s)
TF_API std::string TfMakeValidIdentifier(const std::string &in)
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")
GLuint const GLchar * name
Definition: glcorearb.h:786
bool operator()(const std::string &lhs, const std::string &rhs) const
Definition: stringUtils.h:512
GLenum src
Definition: glcorearb.h:1793
GLdouble GLdouble t
Definition: glew.h:1403
TfStreamFloat(float f)
Definition: stringUtils.h:579
TF_API std::vector< std::string > TfQuotedStringTokenize(const std::string &source, const char *delimiters=" \t\n", std::string *errors=NULL)
TF_API std::vector< std::string > TfMatchedStringTokenize(const std::string &source, char openDelimiter, char closeDelimiter, char escapeCharacter= '\0', std::string *errors=NULL)
GLuint in
Definition: glew.h:11552
TF_API bool TfDoubleToString(double d, char *buffer, int len, bool emitTrailingZero)
GLdouble l
Definition: glew.h:9164
std::string TfIntToString(int i)
Returns the given integer as a string.
Definition: stringUtils.h:107
GLint GLenum GLint x
Definition: glcorearb.h:409
Definition: token.h:87
Definition: core.h:760
TF_API std::string TfVStringPrintf(const std::string &fmt, va_list ap)
TF_API uint64_t TfStringToUInt64(const std::string &txt, bool *outOfRange=NULL)
GLsizei GLsizei GLfloat distance
Definition: glew.h:13923
GLenum GLsizei len
Definition: glew.h:7782
const GLdouble * v
Definition: glcorearb.h:837
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:803
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLuint GLuint end
Definition: glcorearb.h:475
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
TF_API bool TfStringContains(const std::string &s, const char *substring)
Returns true if s contains substring.
GLfloat GLfloat p
Definition: glew.h:16656
GLsizei const GLchar *const * strings
Definition: glcorearb.h:1933
std::string TfStringJoin(ForwardIterator begin, ForwardIterator end, const char *separator=" ")
Definition: stringUtils.h:359
TF_API std::string TfStringCapitalize(const std::string &source)
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)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1394
TfStreamDouble(double d)
Definition: stringUtils.h:590
auto ptr(T p) -> const void *
Definition: format.h:2448
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
T TfUnstringify(const std::string &instring, bool *status=NULL)
Definition: stringUtils.h:603
GLsizei const GLfloat * value
Definition: glcorearb.h:824
GLfloat f
Definition: glcorearb.h:1926
TF_API std::string TfGetBaseName(const std::string &fileName)
Returns the base name of a file (final component of the path).
static TF_API std::string GetName(TfEnum val)
bool Tf_StringStartsWithImpl(char const *s, size_t slen, char const *prefix, size_t prelen)
Definition: stringUtils.h:211
Definition: core.h:1131
TF_API std::string TfStringGetBeforeSuffix(const std::string &name, char delimiter= '.')
bool TfIsValidIdentifier(std::string const &identifier)
Definition: stringUtils.h:684
std::enable_if<!std::is_enum< T >::value, std::string >::type TfStringify(const T &v)
Definition: stringUtils.h:541
TF_API void TfEscapeStringReplaceChar(const char **in, char **out)
type
Definition: core.h:1059
GLboolean r
Definition: glcorearb.h:1222
TF_API std::vector< std::string > TfStringTokenize(const std::string &source, const char *delimiters=" \t\n")
GLdouble s
Definition: glew.h:1395
TF_API int64_t TfStringToInt64(const std::string &txt, bool *outOfRange=NULL)
TF_API std::string TfStringReplace(const std::string &source, const std::string &from, const std::string &to)
TF_API std::string TfStringTrimLeft(const std::string &s, const char *trimChars=" \n\t\r")
TF_API std::string TfStringCatPaths(const std::string &prefix, const std::string &suffix)
TF_API std::string TfEscapeString(const std::string &in)
bool Tf_StringEndsWithImpl(char const *s, size_t slen, char const *suffix, size_t suflen)
Definition: stringUtils.h:232
TF_API std::string TfGetPathName(const std::string &fileName)
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:450