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 
33 #include "pxr/base/arch/attributes.h"
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 
47 PXR_NAMESPACE_OPEN_SCOPE
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.
TfSafeString(const char * ptr)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.
TfIntToString(int i)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 /// Convert a sequence of digits in \p txt to a long int value.  Caller is
136 /// responsible for ensuring that \p txt has content matching:
137 ///
138 /// \code
139 /// -?[0-9]+
140 /// \endcode
141 ///
142 /// If the digit sequence's value is out of range, set \p *outOfRange to true
143 /// (if \p outOfRange is not NULL) and return either
144 /// std::numeric_limits<long>::min() or max(), whichever is closest to the
145 /// true value.
146 TF_API
147 long TfStringToLong(const std::string &txt, bool *outOfRange=NULL);
148 
149 /// \overload
150 
151 TF_API
152 long TfStringToLong(const char *txt, bool *outOfRange=NULL);
153 
154 /// Convert a sequence of digits in \p txt to an unsigned long value.  Caller
155 /// is responsible for ensuring that \p txt has content matching:
156 ///
157 /// \code
158 /// [0-9]+
159 /// \endcode
160 ///
161 /// If the digit sequence's value is out of range, set \p *outOfRange to true
162 /// (if \p outOfRange is not NULL) and return std::numeric_limits<unsigned
163 /// long>::max().
164 TF_API
165 unsigned long TfStringToULong(const std::string &txt, bool *outOfRange=NULL);
166 
167 /// \overload
168 
169 TF_API
170 unsigned long TfStringToULong(const char *txt, bool *outOfRange=NULL);
171 
172 /// Convert a sequence of digits in \p txt to an int64_t value.  Caller must
173 /// ensure that \p txt has content matching:
174 ///
175 /// \code
176 /// -?[0-9]+
177 /// \endcode
178 ///
179 /// If the digit sequence's value is out of range, set \p *outOfRange to true
180 /// (if \p outOfRange is not NULL) and return either
181 /// std::numeric_limits<int64_t>::min() or max(), whichever is closest to the
182 /// true value.
183 TF_API
184 int64_t TfStringToInt64(const std::string &txt, bool *outOfRange=NULL);
185 
186 /// \overload
187 TF_API
188 int64_t TfStringToInt64(const char *txt, bool *outOfRange=NULL);
189 
190 /// Convert a sequence of digits in \p txt to a uint64_t value.  Caller is
191 /// responsible for ensuring that \p txt has content matching:
192 ///
193 /// \code
194 /// [0-9]+
195 /// \endcode
196 ///
197 /// If the digit sequence's value is out of range, set \p *outOfRange to true
198 /// (if \p outOfRange is not NULL) and return std::numeric_limits<unsigned
199 /// long>::max().
200 TF_API
201 uint64_t TfStringToUInt64(const std::string &txt, bool *outOfRange=NULL);
202 
203 /// \overload
204 TF_API
205 uint64_t TfStringToUInt64(const char *txt, bool *outOfRange=NULL);
206 
207 inline bool
Tf_StringStartsWithImpl(char const * s,size_t slen,char const * prefix,size_t prelen)208 Tf_StringStartsWithImpl(char const *s, size_t slen,
209                         char const *prefix, size_t prelen)
210 {
211     return slen >= prelen && strncmp(s, prefix, prelen) == 0;
212 }
213 
214 /// Returns true if \p s starts with \p prefix.
215 inline bool
TfStringStartsWith(const std::string & s,const char * prefix)216 TfStringStartsWith(const std::string& s, const char *prefix)
217 {
218     return Tf_StringStartsWithImpl(
219         s.c_str(), s.length(), prefix, strlen(prefix));
220 }
221 
222 /// \overload
223 inline bool
TfStringStartsWith(const std::string & s,const std::string & prefix)224 TfStringStartsWith(const std::string& s, const std::string& prefix) {
225     return TfStringStartsWith(s, prefix.c_str());
226 }
227 
228 /// \overload
229 TF_API
230 bool TfStringStartsWith(const std::string &s, const TfToken& prefix);
231 
232 inline bool
Tf_StringEndsWithImpl(char const * s,size_t slen,char const * suffix,size_t suflen)233 Tf_StringEndsWithImpl(char const *s, size_t slen,
234                       char const *suffix, size_t suflen)
235 {
236     return slen >= suflen && strcmp(s + (slen - suflen), suffix) == 0;
237 }
238 
239 /// Returns true if \p s ends with \p suffix.
TfStringEndsWith(const std::string & s,const char * suffix)240 inline bool TfStringEndsWith(const std::string& s, const char *suffix)
241 {
242     return Tf_StringEndsWithImpl(s.c_str(), s.length(),
243                                  suffix, strlen(suffix));
244 }
245 
246 /// \overload
247 inline bool
TfStringEndsWith(const std::string & s,const std::string & suffix)248 TfStringEndsWith(const std::string& s, const std::string& suffix)
249 {
250     return TfStringEndsWith(s, suffix.c_str());
251 }
252 
253 /// \overload
254 TF_API
255 bool TfStringEndsWith(const std::string &s, const TfToken& suffix);
256 
257 /// Returns true if \p s contains \p substring.
258 // \ingroup group_tf_String
259 TF_API
260 bool TfStringContains(const std::string& s, const char *substring);
261 
262 /// \overload
263 inline bool
TfStringContains(const std::string & s,const std::string & substring)264 TfStringContains(const std::string &s, const std::string &substring) {
265     return TfStringContains(s, substring.c_str());
266 }
267 
268 /// \overload
269 TF_API
270 bool TfStringContains(const std::string &s, const TfToken& substring);
271 
272 /// Makes all characters in \p source lowercase, and returns the result.
273 TF_API
274 std::string TfStringToLower(const std::string& source);
275 
276 /// Makes all characters in \p source uppercase, and returns the result.
277 TF_API
278 std::string TfStringToUpper(const std::string& source);
279 
280 /// Returns a copy of the \p source string with only its first character
281 /// capitalized. This emulates the behavior of Python's \c str.capitalize().
282 TF_API
283 std::string TfStringCapitalize(const std::string& source);
284 
285 /// Trims characters (by default, whitespace) from the left.
286 ///
287 /// Characters from the beginning of \p s are removed until a character not in
288 /// \p trimChars is found; the result is returned.
289 TF_API
290 std::string TfStringTrimLeft(const std::string& s,
291                              const char* trimChars = " \n\t\r");
292 
293 /// Trims characters (by default, whitespace) from the right.
294 ///
295 /// Characters at the end of \p s are removed until a character not in \p
296 /// trimChars is found; the result is returned.
297 TF_API
298 std::string TfStringTrimRight(const std::string& s,
299                               const char* trimChars = " \n\t\r");
300 
301 /// Trims characters (by default, whitespace) from the beginning and end of
302 /// string.
303 ///
304 /// Characters at the beginning and end of \p s are removed until a character
305 /// not in \p trimChars is found; the result is returned.
306 TF_API
307 std::string TfStringTrim(const std::string& s,
308                          const char* trimChars = " \n\t\r");
309 
310 /// Returns the common prefix of the input strings, if any.
311 ///
312 /// Copies of the input strings are compared.  Returns a new string which is
313 /// the longest prefix common to both input strings.  If the strings have no
314 /// common prefix, an empty string is returned.
315 TF_API
316 std::string TfStringGetCommonPrefix(std::string a, std::string b);
317 
318 /// Returns the suffix of a string
319 ///
320 /// Returns characters after the final character \c delimiter (default ".") of
321 /// a string.  Thus suffix of "abc.def" is "def" using "." as the delimiter.
322 /// If the delimiter does not occur, the empty string is returned.
323 TF_API
324 std::string TfStringGetSuffix(const std::string& name, char delimiter = '.');
325 
326 /// Returns everything up to the suffix of a string
327 ///
328 /// Returns characters before the final character \c delimiter (default ".")
329 /// of a string.  Thus not-suffix of "abc.def" is "abc" using "." as the
330 /// delimiter.  If the delimiter does not occur, the original string is
331 /// returned.
332 TF_API
333 std::string TfStringGetBeforeSuffix(const std::string& name, char delimiter = '.');
334 
335 /// Returns the base name of a file (final component of the path).
336 TF_API
337 std::string TfGetBaseName(const std::string& fileName);
338 
339 /// Returns the path component of a file (complement of TfGetBaseName()).
340 ///
341 /// The returned string ends in a '/' (or possibly a '\' on Windows), unless
342 /// none was found in \c fileName, in which case the empty string is returned.
343 /// In particular, \c TfGetPathName(s)+TfGetBaseName(s) == \c s for any string
344 /// \c s (as long as \c s doesn't end with multiple adjacent slashes, which is
345 /// illegal).
346 TF_API
347 std::string TfGetPathName(const std::string& fileName);
348 
349 /// Replaces all occurrences of string \p from with \p to in \p source
350 ///
351 /// Returns a new string which is created by copying \p string and replacing
352 /// every occurrence of \p from with \p to. Correctly handles the case in which
353 /// \p to contains \p from.
354 TF_API
355 std::string TfStringReplace(const std::string& source, const std::string& from,
356                             const std::string& to);
357 
358 /// Concatenates the strings (\p begin, \p end), with default separator.
359 ///
360 /// Returns the concatenation of the strings in the range \p begin to \p end,
361 /// with \p separator (by default, a space) added between each successive pair
362 /// of strings.
363 template <class ForwardIterator>
364 std::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 
393 /// Concatenates \p strings, with default separator.
394 ///
395 /// Returns the concatenation of the strings in \p strings, with \p separator
396 /// (by default, a space) added between each successive pair of strings.
397 TF_API
398 std::string TfStringJoin(const std::vector<std::string>& strings,
399                          const char* separator = " ");
400 
401 /// Concatenates \p strings, with default separator.
402 ///
403 /// Returns the concatenation of the strings in \p strings, with \p separator
404 /// (by default, a space) added between each successive pair of strings.
405 TF_API
406 std::string TfStringJoin(const std::set<std::string>& strings,
407                          const char* separator = " ");
408 
409 /// Breaks the given string apart, returning a vector of strings.
410 ///
411 /// The string \p source is broken apart into individual words, where a word
412 /// is delimited by the string \p separator. This function behaves like
413 /// pythons string split method.
414 TF_API
415 std::vector<std::string> TfStringSplit(std::string const &src,
416                                        std::string const &separator);
417 
418 /// Breaks the given string apart, returning a vector of strings.
419 ///
420 /// The string \p source is broken apart into individual words, where a word
421 /// is delimited by the characters in \p delimiters.  Delimiters default to
422 /// white space (space, tab, and newline).
423 ///
424 /// No empty strings are returned: delimiters at the start or end are ignored,
425 /// consecutive delimiters are treated as though they were one, and an empty
426 /// input will result in an empty return vector.
427 TF_API
428 std::vector<std::string> TfStringTokenize(const std::string& source,
429                                           const char* delimiters = " \t\n");
430 
431 /// Breaks the given string apart, returning a set of strings.
432 ///
433 /// Same as TfStringTokenize, except this one returns a set.
434 TF_API
435 std::set<std::string> TfStringTokenizeToSet(const std::string& source,
436                                             const char* delimiters = " \t\n");
437 
438 /// Breaks the given quoted string apart, returning a vector of strings.
439 ///
440 /// The string \p source is broken apart into individual words, where a word
441 /// is delimited by the characters in \p delimiters.  This function is similar
442 /// to \c TfStringTokenize, except it considers a quoted string as a single
443 /// word. The function will preserve quotes that are nested within other
444 /// quotes or are preceded by a backslash character. \p errors, if provided,
445 /// contains any error messages. Delimiters default to white space (space,
446 /// tab, and newline).
447 TF_API
448 std::vector<std::string>
449 TfQuotedStringTokenize(const std::string& source,
450                        const char* delimiters = " \t\n",
451                        std::string *errors = NULL);
452 
453 /// Breaks the given string apart by matching delimiters.
454 ///
455 /// The string \p source is broken apart into individual words, where a word
456 /// begins with \p openDelimiter and ends with a matching \p closeDelimiter.
457 /// Any delimiters within the matching delimiters become part of the word, and
458 /// anything outside matching delimiters gets dropped. For example, \c
459 /// TfMatchedStringTokenize("{a} string {to {be} split}", '{', '}') would
460 /// return a vector containing "a" and "to {be} split". If \p openDelimiter and
461 /// \p closeDelimiter cannot be the same. \p errors, if provided, contains any
462 /// error messages.
463 TF_API
464 std::vector<std::string>
465 TfMatchedStringTokenize(const std::string& source,
466                         char openDelimiter,
467                         char closeDelimiter,
468                         char escapeCharacter = '\0',
469                         std::string *errors = NULL);
470 
471 /// This overloaded version of \c TfMatchedStringTokenize does not take an \c
472 /// escapeCharacter parameter but does take \param errors.  It allows \c
473 /// TfMatchedStringTokenize to be called with or without an \c escapeCharacter
474 /// and with or without \c errors.
475 ///
476 /// \overload
477 inline
478 std::vector<std::string>
TfMatchedStringTokenize(const std::string & source,char openDelimiter,char closeDelimiter,std::string * errors)479 TfMatchedStringTokenize(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 
488 /// \class TfDictionaryLessThan
489 ///
490 /// Provides dictionary ordering binary predicate function on strings.
491 ///
492 /// The \c TfDictionaryLessThan class is a functor as defined by the STL
493 /// standard.  It compares strings using "dictionary" order: for example, the
494 /// following strings are in dictionary order:
495 /// ["abacus", "Albert", "albert", "baby", "Bert", "file01", "file001", "file2",
496 /// "file10"]
497 ///
498 /// Note that capitalization matters only if the strings differ by
499 /// capitalization alone.
500 ///
501 /// Characters whose ASCII value are inbetween upper- and lowercase letters,
502 /// such as underscore, are sorted to come after all letters.
503 ///
504 struct TfDictionaryLessThan {
505     /// Return true if \p lhs is less than \p rhs in dictionary order.
506     ///
507     /// Normally this functor is used to supply an ordering functor for STL
508     /// containers: for example,
509     /// \code
510     ///   map<string, DataType, TfDictionaryLessThan>  table;
511     /// \endcode
512     ///
513     /// If you simply need to compare two strings, you can do so as follows:
514     /// \code
515     ///     bool aIsFirst = TfDictionaryLessThan()(aString, bString);
516     /// \endcode
517     TF_API bool operator()(const std::string &lhs, const std::string &rhs) const;
518 };
519 
520 /// Convert an arbitrary type into a string
521 ///
522 /// Use the type's stream output operator to convert it into a string. You are
523 /// free to use the stream operators in ostreamMethods.h, but are not required
524 /// to do so.
525 template <typename T>
526 typename std::enable_if<!std::is_enum<T>::value, std::string>::type
TfStringify(const T & v)527 TfStringify(const T& v)
528 {
529     std::ostringstream stream;
530     stream << v;
531     return stream.str();
532 }
533 
534 /// \overload
535 template <typename T>
536 typename std::enable_if<std::is_enum<T>::value, std::string>::type
TfStringify(const T & v)537 TfStringify(const T& v)
538 {
539     return TfEnum::GetName(v);
540 }
541 
542 /// \overload
543 TF_API std::string TfStringify(bool v);
544 /// \overload
545 TF_API std::string TfStringify(std::string const&);
546 /// \overload
547 TF_API std::string TfStringify(float);
548 /// \overload
549 TF_API std::string TfStringify(double);
550 
551 /// Writes the string representation of \c d to \c buffer of length \c len.
552 /// If \c emitTrailingZero is true, the string representation will end with .0
553 /// in the case where d is an integer otherwise it will be omitted.
554 /// The buffer length must be at least 25 in order to ensure that all doubles
555 /// values can be represented.
556 /// Returns whether the conversion was successful.
557 TF_API bool TfDoubleToString(
558     double d, char* buffer, int len, bool emitTrailingZero);
559 
560 /// \struct TfStreamFloat
561 ///
562 /// A type which offers streaming for floats in a canonical
563 /// format that can safely roundtrip with the minimal number of digits.
564 struct TfStreamFloat {
TfStreamFloatTfStreamFloat565     explicit TfStreamFloat(float f) : value(f) {}
566     float value;
567 };
568 
569 TF_API std::ostream& operator<<(std::ostream& o, TfStreamFloat t);
570 
571 /// \struct TfStreamDouble
572 ///
573 /// A type which offers streaming for doubles in a canonical
574 /// format that can safely roundtrip with the minimal number of digits.
575 struct TfStreamDouble {
TfStreamDoubleTfStreamDouble576     explicit TfStreamDouble(double d) : value(d) {}
577     double value;
578 };
579 
580 TF_API std::ostream& operator<<(std::ostream& o, TfStreamDouble t);
581 
582 /// Convert a string to an arbitrary type
583 ///
584 /// Use the type's stream input operator to get it from a string. If \p status
585 /// is non-NULL and \p instring cannot be converted to a \c T, \p *status is
586 /// set to \c false; otherwise, \p *status is not modified.
587 template <typename T>
588 T
589 TfUnstringify(const std::string &instring, bool* status = NULL)
590 {
591     T v = T();
592     std::istringstream stream(instring);
593     stream >> v;
594     if (status && !stream)
595         *status = false;
596     return v;
597 }
598 
599 /// \overload
600 template <>
601 TF_API
602 bool TfUnstringify(const std::string &instring, bool* status);
603 /// \overload
604 template <>
605 TF_API
606 std::string TfUnstringify(const std::string &instring, bool* status);
607 
608 /// Returns a string with glob characters converted to their regular
609 /// expression equivalents.
610 ///
611 /// Currently, this transforms strings by replacing all instances of '.' with
612 /// '\.', '*' with '.*', and '?' with '.', in that order.
613 TF_API
614 std::string TfStringGlobToRegex(const std::string& s);
615 
616 /// Process escape sequences in ANSI C string constants.
617 ///
618 /// The following escape sequences are accepted:
619 ///
620 /// \li \\\\:    backslash
621 /// \li \\a:     ring the bell
622 /// \li \\b:     backspace
623 /// \li \\f:     form feed
624 /// \li \\n:     new line
625 /// \li \\r:     carriage return
626 /// \li \\t:     tab
627 /// \li \\v:     vertical tab
628 /// \li \\xdd:   hex constant
629 /// \li \\ddd:   octal constant
630 ///
631 /// So, if the two-character sequence "\\n" appears in the string, it is
632 /// replaced by an actual newline.  Each hex and octal constant translates into
633 /// one character in the output string.  Hex constants can be up to 2 digits,
634 /// octal constants can be up to 3 digits.  Both are terminated by a character
635 /// that is not a valid constant.  Note that it is good practice to encode hex
636 /// and octal constants with maximum width (2 and 3 digits, respectively) using
637 /// leading zeroes if necessary.  This avoids problems where characters after
638 /// the hex/octal constant that shouldn't be part of the constant get
639 /// interpreted as part of it.  For example, the sequence "\x2defaced" will
640 /// produce the characters "-efaced" when what was probably intended was the
641 /// character 0x02 (STX) followed by "defaced".
642 //
643 /// Illegal escape sequences are replaced by the character following the
644 /// backslash, so the two character sequence "\\c" would become "c".  Processing
645 /// continues until the input hits a NUL character in the input string -
646 /// anything appearing after the NUL will be ignored.
647 TF_API std::string TfEscapeString(const std::string &in);
648 TF_API void TfEscapeStringReplaceChar(const char** in, char** out);
649 
650 /// Concatenate two strings containing '/' and '..' tokens like a file path or
651 /// scope name.
652 ///
653 /// Tokenize the input strings using a '/' delimiter. Look for '..' tokens in
654 /// the suffix and construct the appropriate result.
655 ///
656 /// Examples:
657 ///
658 /// \li TfStringCatPaths( "foo/bar", "jive" ) => "foo/bar/jive"
659 /// \li TfStringCatPaths( "foo/bar", "../jive" ) => "foo/jive"
660 TF_API
661 std::string TfStringCatPaths( const std::string &prefix,
662                               const std::string &suffix );
663 
664 /// Test whether \a identifier is valid.
665 ///
666 /// An identifier is valid if it follows the C/Python identifier convention;
667 /// that is, it must be at least one character long, must start with a letter
668 /// or underscore, and must contain only letters, underscores, and numerals.
669 inline bool
TfIsValidIdentifier(std::string const & identifier)670 TfIsValidIdentifier(std::string const &identifier)
671 {
672     char const *p = identifier.c_str();
673     auto letter = [](unsigned c) { return ((c-'A') < 26) || ((c-'a') < 26); };
674     auto number = [](unsigned c) { return (c-'0') < 10; };
675     auto under = [](unsigned c) { return c == '_'; };
676     unsigned x = *p;
677     if (!x || number(x)) {
678         return false;
679     }
680     while (letter(x) || number(x) || under(x)) {
681         x = *p++;
682     };
683     return x == 0;
684 }
685 
686 /// Produce a valid identifier (see TfIsValidIdentifier) from \p in by
687 /// replacing invalid characters with '_'.  If \p in is empty, return "_".
688 TF_API
689 std::string
690 TfMakeValidIdentifier(const std::string &in);
691 
692 /// Escapes characters in \a in so that they are valid XML.
693 ///
694 /// Returns the name with special characters (&, <, >, ", ') replaced with the
695 /// corresponding escape sequences.
696 TF_API
697 std::string TfGetXmlEscapedString(const std::string &in);
698 
699 ///@}
700 
701 PXR_NAMESPACE_CLOSE_SCOPE
702 
703 #endif // PXR_BASE_TF_STRING_UTILS_H
704