Logo ROOT   6.14/05
Reference Guide
TString.h
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Fons Rademakers 04/08/95
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TString
13 #define ROOT_TString
14 
15 
16 //////////////////////////////////////////////////////////////////////////
17 // //
18 // TString //
19 // //
20 // Basic string class. //
21 // //
22 // Cannot be stored in a TCollection... use TObjString instead. //
23 // //
24 //////////////////////////////////////////////////////////////////////////
25 
26 #include "TMathBase.h"
27 
28 #include "ROOT/RStringView.hxx"
29 #include "ROOT/TypeTraits.hxx"
30 
31 #include <iosfwd>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <string>
35 
36 class TRegexp;
37 class TPRegexp;
38 class TString;
39 class TSubString;
40 class TObjArray;
41 class TVirtualMutex;
42 class TBufferFile;
43 
44 TString operator+(const TString &s1, const TString &s2);
45 TString operator+(const TString &s, const char *cs);
46 TString operator+(const char *cs, const TString &s);
47 TString operator+(const TString &s, char c);
48 TString operator+(char c, const TString &s);
49 Bool_t operator==(const TString &s1, const TString &s2);
50 Bool_t operator==(const TString &s1, const char *s2);
51 Bool_t operator==(const TSubString &s1, const TSubString &s2);
52 Bool_t operator==(const TSubString &s1, const TString &s2);
53 Bool_t operator==(const TSubString &s1, const char *s2);
54 /*
55 template<class T>
56 struct is_signed_numeral : std::integral_constant<bool,
57  std::is_integral<T>::value && std::is_signed<T>::value
58 > {};
59 
60 template<class T>
61 struct is_unsigned_numeral : std::integral_constant<bool,
62  std::is_integral<T>::value && !std::is_signed<T>::value
63 > {};
64 
65 template<class T>
66 using is_float_numeral = std::is_floating_point<T>;
67 */
68 
69 //////////////////////////////////////////////////////////////////////////
70 // //
71 // TSubString //
72 // //
73 // The TSubString class allows selected elements to be addressed. //
74 // There are no public constructors. //
75 // //
76 //////////////////////////////////////////////////////////////////////////
77 class TSubString {
78 
79 friend class TStringLong;
80 friend class TString;
81 
82 friend Bool_t operator==(const TSubString &s1, const TSubString &s2);
83 friend Bool_t operator==(const TSubString &s1, const TString &s2);
84 friend Bool_t operator==(const TSubString &s1, const char *s2);
85 
86 private:
87  TString &fStr; // Referenced string
88  Ssiz_t fBegin; // Index of starting character
89  Ssiz_t fExtent; // Length of TSubString
90 
91  // NB: the only constructor is private
92  TSubString(const TString &s, Ssiz_t start, Ssiz_t len);
93 
94 protected:
95  void SubStringError(Ssiz_t, Ssiz_t, Ssiz_t) const;
96  void AssertElement(Ssiz_t i) const; // Verifies i is valid index
97 
98 public:
100  : fStr(s.fStr), fBegin(s.fBegin), fExtent(s.fExtent) { }
101 
102  TSubString &operator=(const char *s); // Assignment from a char*
103  TSubString &operator=(const TString &s); // Assignment from a TString
104  TSubString &operator=(const TSubString &s); // Assignment from a TSubString
105  char &operator()(Ssiz_t i); // Index with optional bounds checking
106  char &operator[](Ssiz_t i); // Index with bounds checking
107  char operator()(Ssiz_t i) const; // Index with optional bounds checking
108  char operator[](Ssiz_t i) const; // Index with bounds checking
109 
110  operator std::string_view() const { return std::string_view(Data(),fExtent); }
111  operator std::string() const { return std::string(Data(),fExtent); }
112 
113  const char *Data() const;
114  Ssiz_t Length() const { return fExtent; }
115  Ssiz_t Start() const { return fBegin; }
116  TString& String() { return fStr; }
117  void ToLower(); // Convert self to lower-case
118  void ToUpper(); // Convert self to upper-case
119 
120  // For detecting null substrings
121  Bool_t IsNull() const { return fBegin == kNPOS; }
122  int operator!() const { return fBegin == kNPOS; }
123 };
124 
125 
126 //////////////////////////////////////////////////////////////////////////
127 // //
128 // TString //
129 // //
130 //////////////////////////////////////////////////////////////////////////
131 class TString {
132 
133 friend class TStringLong;
134 friend class TSubString;
135 friend class TBufferFile;
136 
137 friend TString operator+(const TString &s1, const TString &s2);
138 friend TString operator+(const TString &s, const char *cs);
139 friend TString operator+(const char *cs, const TString &s);
140 friend TString operator+(const TString &s, char c);
141 friend TString operator+(char c, const TString &s);
142 
143 template<class T>
144 friend typename std::enable_if<ROOT::TypeTraits::IsSignedNumeral<T>::value,TString>::type
145 operator+(TString s, T i);
146 template<class T>
147 friend typename std::enable_if<ROOT::TypeTraits::IsUnsignedNumeral<T>::value,TString>::type
148 operator+(TString s, T u);
149 template<class T>
150 friend typename std::enable_if<ROOT::TypeTraits::IsFloatNumeral<T>::value,TString>::type
151 operator+(TString s, T f);
152 template<class T>
153 friend typename std::enable_if<ROOT::TypeTraits::IsSignedNumeral<T>::value,TString>::type
154 operator+(T i, const TString &s);
155 template<class T>
156 friend typename std::enable_if<ROOT::TypeTraits::IsUnsignedNumeral<T>::value,TString>::type
157 operator+(T u, const TString &s);
158 template<class T>
159 friend typename std::enable_if<ROOT::TypeTraits::IsFloatNumeral<T>::value,TString>::type
160 operator+(T f, const TString &s);
161 
162 friend Bool_t operator==(const TString &s1, const TString &s2);
163 friend Bool_t operator==(const TString &s1, const char *s2);
164 
165 private:
166 #ifdef R__BYTESWAP
167  enum { kShortMask = 0x01, kLongMask = 0x1 };
168 #else
169  enum { kShortMask = 0x80, kLongMask = 0x80000000 };
170 #endif
171 
172  struct LongStr_t
173  {
174  Ssiz_t fCap; // Max string length (including null)
175  Ssiz_t fSize; // String length (excluding null)
176  char *fData; // Long string data
177  };
178 
179  enum { kMinCap = (sizeof(LongStr_t) - 1)/sizeof(char) > 2 ?
180  (sizeof(LongStr_t) - 1)/sizeof(char) : 2 };
181 
182  struct ShortStr_t
183  {
184  unsigned char fSize; // String length (excluding null)
185  char fData[kMinCap]; // Short string data
186  };
187 
188  union UStr_t { LongStr_t fL; ShortStr_t fS; };
189 
190  enum { kNwords = sizeof(UStr_t) / sizeof(Ssiz_t)};
191 
192  struct RawStr_t
193  {
194  Ssiz_t fWords[kNwords];
195  };
196 
197  struct Rep_t
198  {
199  union
200  {
204  };
205  };
206 
207 protected:
208 #ifndef __CINT__
209  Rep_t fRep; //! String data
210 #endif
211 
212  // Special concatenation constructor
213  TString(const char *a1, Ssiz_t n1, const char *a2, Ssiz_t n2);
214  void AssertElement(Ssiz_t nc) const; // Index in range
215  void Clobber(Ssiz_t nc); // Remove old contents
216  void InitChar(char c); // Initialize from char
217 
218  enum { kAlignment = 16 };
219  static Ssiz_t Align(Ssiz_t s) { return (s + (kAlignment-1)) & ~(kAlignment-1); }
220  static Ssiz_t Recommend(Ssiz_t s) { return (s < kMinCap ? kMinCap : Align(s+1)) - 1; }
221  static Ssiz_t AdjustCapacity(Ssiz_t oldCap, Ssiz_t newCap);
222 
223 private:
224  Bool_t IsLong() const { return Bool_t(fRep.fShort.fSize & kShortMask); }
225 #ifdef R__BYTESWAP
226  void SetShortSize(Ssiz_t s) { fRep.fShort.fSize = (unsigned char)(s << 1); }
227  Ssiz_t GetShortSize() const { return fRep.fShort.fSize >> 1; }
228 #else
229  void SetShortSize(Ssiz_t s) { fRep.fShort.fSize = (unsigned char)s; }
230  Ssiz_t GetShortSize() const { return fRep.fShort.fSize; }
231 #endif
232  void SetLongSize(Ssiz_t s) { fRep.fLong.fSize = s; }
233  Ssiz_t GetLongSize() const { return fRep.fLong.fSize; }
234  void SetSize(Ssiz_t s) { IsLong() ? SetLongSize(s) : SetShortSize(s); }
235  void SetLongCap(Ssiz_t s) { fRep.fLong.fCap = kLongMask | s; }
236  Ssiz_t GetLongCap() const { return fRep.fLong.fCap & ~kLongMask; }
237  void SetLongPointer(char *p) { fRep.fLong.fData = p; }
238  char *GetLongPointer() { return fRep.fLong.fData; }
239  const char *GetLongPointer() const { return fRep.fLong.fData; }
240  char *GetShortPointer() { return fRep.fShort.fData; }
241  const char *GetShortPointer() const { return fRep.fShort.fData; }
242  char *GetPointer() { return IsLong() ? GetLongPointer() : GetShortPointer(); }
243  const char *GetPointer() const { return IsLong() ? GetLongPointer() : GetShortPointer(); }
244 #ifdef R__BYTESWAP
245  static Ssiz_t MaxSize() { return kMaxInt - 1; }
246 #else
247  static Ssiz_t MaxSize() { return (kMaxInt >> 1) - 1; }
248 #endif
249  void UnLink() const { if (IsLong()) delete [] fRep.fLong.fData; }
250  void Zero() {
251  Ssiz_t (&a)[kNwords] = fRep.fRaw.fWords;
252  for (UInt_t i = 0; i < kNwords; ++i)
253  a[i] = 0;
254  }
255  char *Init(Ssiz_t capacity, Ssiz_t nchar);
256  void Clone(Ssiz_t nc); // Make self a distinct copy w. capacity nc
257  void FormImp(const char *fmt, va_list ap);
258  UInt_t HashCase() const;
259  UInt_t HashFoldCase() const;
260 
261 public:
262  enum EStripType { kLeading = 0x1, kTrailing = 0x2, kBoth = 0x3 };
263  enum ECaseCompare { kExact, kIgnoreCase };
264  static const Ssiz_t kNPOS = ::kNPOS;
265 
266  TString(); // Null string
267  explicit TString(Ssiz_t ic); // Suggested capacity
268  TString(const TString &s); // Copy constructor
269  TString(TString &&s) noexcept; // Move constructor
270  TString(const char *s); // Copy to embedded null
271  TString(const char *s, Ssiz_t n); // Copy past any embedded nulls
272  TString(const std::string &s);
273  TString(char c);
274  TString(char c, Ssiz_t s);
275  TString(const std::string_view &sub);
276  TString(const TSubString &sub);
277 
278  virtual ~TString();
279 
280  // ROOT I/O interface
281  virtual void FillBuffer(char *&buffer) const;
282  virtual void ReadBuffer(char *&buffer);
283  virtual Int_t Sizeof() const;
284 
285  static TString *ReadString(TBuffer &b, const TClass *clReq);
286  static void WriteString(TBuffer &b, const TString *a);
287 
288  friend TBuffer &operator<<(TBuffer &b, const TString *obj);
289 
290  // C I/O interface
291  Bool_t Gets(FILE *fp, Bool_t chop=kTRUE);
292  void Puts(FILE *fp);
293 
294  // Type conversion
295  operator const char*() const { return GetPointer(); }
296 #if (__cplusplus >= 201700L) && (!defined(__clang_major__) || __clang_major__ > 5)
297  // Clang 5.0 support for explicit conversion is still inadequate even in c++17 mode.
298  // (It leads to extraneous ambiguous overload errors)
299  explicit operator std::string() const { return std::string(GetPointer(),Length()); }
300  explicit operator ROOT::Internal::TStringView() const { return ROOT::Internal::TStringView(GetPointer(),Length()); }
301  operator std::string_view() const { return std::string_view(GetPointer(),Length()); }
302 #else
303  operator ROOT::Internal::TStringView() const { return ROOT::Internal::TStringView(GetPointer(),Length()); }
304 #endif
305 
306  // Assignment
307  TString &operator=(char s); // Replace string
308  TString &operator=(const char *s);
309  TString &operator=(const TString &s);
310  TString &operator=(TString &&s) noexcept;
311  TString &operator=(const std::string &s);
313  TString &operator=(const TSubString &s);
314  TString &operator+=(const char *s); // Append string
315  TString &operator+=(const TString &s);
316  TString &operator+=(char c);
317 
318  template<class T>
319  typename std::enable_if<ROOT::TypeTraits::IsSignedNumeral<T>::value,TString>::type
320  &operator+=(T i);
321  template<class T>
322  typename std::enable_if<ROOT::TypeTraits::IsUnsignedNumeral<T>::value,TString>::type
323  &operator+=(T u);
324  template<class T>
325  typename std::enable_if<ROOT::TypeTraits::IsFloatNumeral<T>::value,TString>::type
326  &operator+=(T f);
327 
328  // Indexing operators
329  char &operator[](Ssiz_t i); // Indexing with bounds checking
330  char &operator()(Ssiz_t i); // Indexing with optional bounds checking
331  char operator[](Ssiz_t i) const;
332  char operator()(Ssiz_t i) const;
333  TSubString operator()(Ssiz_t start, Ssiz_t len) const; // Sub-string operator
334  TSubString operator()(const TRegexp &re) const; // Match the RE
335  TSubString operator()(const TRegexp &re, Ssiz_t start) const;
336  TSubString operator()(TPRegexp &re) const; // Match the Perl compatible Regular Expression
337  TSubString operator()(TPRegexp &re, Ssiz_t start) const;
338  TSubString SubString(const char *pat, Ssiz_t start = 0,
339  ECaseCompare cmp = kExact) const;
340 
341  // Non-static member functions
342  TString &Append(const char *cs);
343  TString &Append(const char *cs, Ssiz_t n);
344  TString &Append(const TString &s);
345  TString &Append(const TString &s, Ssiz_t n);
346  TString &Append(char c, Ssiz_t rep = 1); // Append c rep times
347  Int_t Atoi() const;
348  Long64_t Atoll() const;
349  Double_t Atof() const;
350  Bool_t BeginsWith(const char *s, ECaseCompare cmp = kExact) const;
351  Bool_t BeginsWith(const TString &pat, ECaseCompare cmp = kExact) const;
352  Ssiz_t Capacity() const { return (IsLong() ? GetLongCap() : kMinCap) - 1; }
353  Ssiz_t Capacity(Ssiz_t n);
354  TString &Chop();
355  void Clear();
356  int CompareTo(const char *cs, ECaseCompare cmp = kExact) const;
357  int CompareTo(const TString &st, ECaseCompare cmp = kExact) const;
358  Bool_t Contains(const char *pat, ECaseCompare cmp = kExact) const;
359  Bool_t Contains(const TString &pat, ECaseCompare cmp = kExact) const;
360  Bool_t Contains(const TRegexp &pat) const;
361  Bool_t Contains(TPRegexp &pat) const;
362  Int_t CountChar(Int_t c) const;
363  TString Copy() const;
364  const char *Data() const { return GetPointer(); }
365  Bool_t EndsWith(const char *pat, ECaseCompare cmp = kExact) const;
366  Bool_t EqualTo(const char *cs, ECaseCompare cmp = kExact) const;
367  Bool_t EqualTo(const TString &st, ECaseCompare cmp = kExact) const;
368  Ssiz_t First(char c) const;
369  Ssiz_t First(const char *cs) const;
370  void Form(const char *fmt, ...)
371 #if defined(__GNUC__) && !defined(__CINT__)
372  __attribute__((format(printf, 2, 3))) /* 1 is the this pointer */
373 #endif
374  ;
375  UInt_t Hash(ECaseCompare cmp = kExact) const;
376  Ssiz_t Index(const char *pat, Ssiz_t i = 0,
377  ECaseCompare cmp = kExact) const;
378  Ssiz_t Index(const TString &s, Ssiz_t i = 0,
379  ECaseCompare cmp = kExact) const;
380  Ssiz_t Index(const char *pat, Ssiz_t patlen, Ssiz_t i,
381  ECaseCompare cmp) const;
382  Ssiz_t Index(const TString &s, Ssiz_t patlen, Ssiz_t i,
383  ECaseCompare cmp) const;
384  Ssiz_t Index(const TRegexp &pat, Ssiz_t i = 0) const;
385  Ssiz_t Index(const TRegexp &pat, Ssiz_t *ext, Ssiz_t i = 0) const;
386  Ssiz_t Index(TPRegexp &pat, Ssiz_t i = 0) const;
387  Ssiz_t Index(TPRegexp &pat, Ssiz_t *ext, Ssiz_t i = 0) const;
388  TString &Insert(Ssiz_t pos, const char *s);
389  TString &Insert(Ssiz_t pos, const char *s, Ssiz_t extent);
390  TString &Insert(Ssiz_t pos, const TString &s);
391  TString &Insert(Ssiz_t pos, const TString &s, Ssiz_t extent);
392  Bool_t IsAscii() const;
393  Bool_t IsAlpha() const;
394  Bool_t IsAlnum() const;
395  Bool_t IsDigit() const;
396  Bool_t IsFloat() const;
397  Bool_t IsHex() const;
398  Bool_t IsBin() const;
399  Bool_t IsOct() const;
400  Bool_t IsDec() const;
401  Bool_t IsInBaseN(Int_t base) const;
402  Bool_t IsNull() const { return Length() == 0; }
403  Bool_t IsWhitespace() const { return (Length() == CountChar(' ')); }
404  Ssiz_t Last(char c) const;
405  Ssiz_t Length() const { return IsLong() ? GetLongSize() : GetShortSize(); }
406  Bool_t MaybeRegexp() const;
407  Bool_t MaybeWildcard() const;
408  TString MD5() const;
409  TString &Prepend(const char *cs); // Prepend a character string
410  TString &Prepend(const char *cs, Ssiz_t n);
411  TString &Prepend(const TString &s);
412  TString &Prepend(const TString &s, Ssiz_t n);
413  TString &Prepend(char c, Ssiz_t rep = 1); // Prepend c rep times
414  std::istream &ReadFile(std::istream &str); // Read to EOF or null character
415  std::istream &ReadLine(std::istream &str,
416  Bool_t skipWhite = kTRUE); // Read to EOF or newline
417  std::istream &ReadString(std::istream &str); // Read to EOF or null character
418  std::istream &ReadToDelim(std::istream &str, char delim = '\n'); // Read to EOF or delimitor
419  std::istream &ReadToken(std::istream &str); // Read separated by white space
420  TString &Remove(Ssiz_t pos); // Remove pos to end of string
421  TString &Remove(Ssiz_t pos, Ssiz_t n); // Remove n chars starting at pos
422  TString &Remove(EStripType s, char c); // Like Strip() but changing string directly
423  TString &Replace(Ssiz_t pos, Ssiz_t n, const char *s);
424  TString &Replace(Ssiz_t pos, Ssiz_t n, const char *s, Ssiz_t ns);
425  TString &Replace(Ssiz_t pos, Ssiz_t n, const TString &s);
426  TString &Replace(Ssiz_t pos, Ssiz_t n1, const TString &s, Ssiz_t n2);
427  TString &ReplaceAll(const TString &s1, const TString &s2); // Find&Replace all s1 with s2 if any
428  TString &ReplaceAll(const TString &s1, const char *s2); // Find&Replace all s1 with s2 if any
429  TString &ReplaceAll(const char *s1, const TString &s2); // Find&Replace all s1 with s2 if any
430  TString &ReplaceAll(const char *s1, const char *s2); // Find&Replace all s1 with s2 if any
431  TString &ReplaceAll(const char *s1, Ssiz_t ls1, const char *s2, Ssiz_t ls2); // Find&Replace all s1 with s2 if any
432  void Resize(Ssiz_t n); // Truncate or add blanks as necessary
433  TSubString Strip(EStripType s = kTrailing, char c = ' ') const;
434  TString &Swap(TString &other); // Swap the contents of this and other without reallocation
435  void ToLower(); // Change self to lower-case
436  void ToUpper(); // Change self to upper-case
437  TObjArray *Tokenize(const TString &delim) const;
438  Bool_t Tokenize(TString &tok, Ssiz_t &from, const char *delim = " ") const;
439  std::string_view View() const { return std::string_view(GetPointer(),Length()); }
440 
441  // Static member functions
442  static UInt_t Hash(const void *txt, Int_t ntxt); // Calculates hash index from any char string.
443  static Ssiz_t InitialCapacity(Ssiz_t ic = 15); // Initial allocation capacity
444  static Ssiz_t MaxWaste(Ssiz_t mw = 15); // Max empty space before reclaim
445  static Ssiz_t ResizeIncrement(Ssiz_t ri = 16); // Resizing increment
446  static Ssiz_t GetInitialCapacity();
447  static Ssiz_t GetResizeIncrement();
448  static Ssiz_t GetMaxWaste();
449  static TString Itoa ( Int_t value, Int_t base); // Converts int to string with respect to the base specified (2-36)
450  static TString UItoa ( UInt_t value, Int_t base);
451  static TString LLtoa ( Long64_t value, Int_t base);
452  static TString ULLtoa (ULong64_t value, Int_t base);
453  static TString BaseConvert(const TString& s_in, Int_t base_in, Int_t base_out); // Converts string from base base_in to base base_out (supported bases 2-36)
454  static TString Format(const char *fmt, ...)
455 #if defined(__GNUC__) && !defined(__CINT__)
456  __attribute__((format(printf, 1, 2)))
457 #endif
458  ;
459 
460  ClassDef(TString,2) //Basic string class
461 };
462 
463 // Related global functions
464 std::istream &operator>>(std::istream &str, TString &s);
465 std::ostream &operator<<(std::ostream &str, const TString &s);
466 #if defined(R__TEMPLATE_OVERLOAD_BUG)
467 template <>
468 #endif
469 TBuffer &operator>>(TBuffer &buf, TString *&sp);
470 TBuffer &operator<<(TBuffer &buf, const TString * sp);
471 
472 // Conversion operator (per se).
473 inline std::string& operator+=(std::string &left, const TString &right)
474 {
475  return left.append(right.Data());
476 }
477 
478 TString ToLower(const TString &s); // Return lower-case version of argument
479 TString ToUpper(const TString &s); // Return upper-case version of argument
480 
481 inline UInt_t Hash(const TString &s) { return s.Hash(); }
482 inline UInt_t Hash(const TString *s) { return s->Hash(); }
483  UInt_t Hash(const char *s);
484 
485 extern char *Form(const char *fmt, ...) // format in circular buffer
486 #if defined(__GNUC__) && !defined(__CINT__)
487 __attribute__((format(printf, 1, 2)))
488 #endif
489 ;
490 extern void Printf(const char *fmt, ...) // format and print
491 #if defined(__GNUC__) && !defined(__CINT__)
492 __attribute__((format(printf, 1, 2)))
493 #endif
494 ;
495 extern char *Strip(const char *str, char c = ' '); // strip c off str, free with delete []
496 extern char *StrDup(const char *str); // duplicate str, free with delete []
497 extern char *Compress(const char *str); // remove blanks from string, free with delele []
498 extern int EscChar(const char *src, char *dst, int dstlen, char *specchars,
499  char escchar); // copy from src to dst escaping specchars by escchar
500 extern int UnEscChar(const char *src, char *dst, int dstlen, char *specchars,
501  char escchar); // copy from src to dst removing escchar from specchars
502 
503 #ifdef NEED_STRCASECMP
504 extern int strcasecmp(const char *str1, const char *str2);
505 extern int strncasecmp(const char *str1, const char *str2, Ssiz_t n);
506 #endif
507 
508 //////////////////////////////////////////////////////////////////////////
509 // //
510 // Inlines //
511 // //
512 //////////////////////////////////////////////////////////////////////////
513 
514 template<class T>
515 inline typename std::enable_if<ROOT::TypeTraits::IsSignedNumeral<T>::value,TString>::type
517 { return s += i; }
518 
519 template<class T>
520 inline typename std::enable_if<ROOT::TypeTraits::IsUnsignedNumeral<T>::value,TString>::type
522 { return s += u; }
523 
524 template<class T>
525 inline typename std::enable_if<ROOT::TypeTraits::IsFloatNumeral<T>::value,TString>::type
527 { return s += f; }
528 
529 template<class T>
530 inline typename std::enable_if<ROOT::TypeTraits::IsSignedNumeral<T>::value,TString>::type
531 operator+(T i, const TString &s)
532 {
533  char buffer[32];
534  // coverity[secure_coding] Buffer is large enough (2^64 = 20 digits).
535  snprintf(buffer, sizeof(buffer), "%lld", static_cast<Long64_t>(i));
536  return TString(buffer, strlen(buffer), s.Data(), s.Length());
537 }
538 
539 template<class T>
540 inline typename std::enable_if<ROOT::TypeTraits::IsUnsignedNumeral<T>::value,TString>::type
541 operator+(T u, const TString &s)
542 {
543  char buffer[32];
544  // coverity[secure_coding] Buffer is large enough (2^64 = 20 digits).
545  snprintf(buffer, sizeof(buffer), "%llu", static_cast<ULong64_t>(u));
546  return TString(buffer, strlen(buffer), s.Data(), s.Length());
547 }
548 
549 template<class T>
550 inline typename std::enable_if<ROOT::TypeTraits::IsFloatNumeral<T>::value,TString>::type
551 operator+(T f, const TString &s)
552 {
553  char buffer[32];
554  // coverity[secure_coding] Buffer is large enough: width specified in format
555  snprintf(buffer, sizeof(buffer), "%.17Lg", static_cast<LongDouble_t>(f));
556  return TString(buffer, strlen(buffer), s.Data(), s.Length());
557 }
558 
559 inline TString &TString::Append(const char *cs)
560 { return Replace(Length(), 0, cs, cs ? strlen(cs) : 0); }
561 
562 inline TString &TString::Append(const char *cs, Ssiz_t n)
563 { return Replace(Length(), 0, cs, n); }
564 
565 inline TString &TString::Append(const TString &s)
566 { return Replace(Length(), 0, s.Data(), s.Length()); }
567 
569 { return Replace(Length(), 0, s.Data(), TMath::Min(n, s.Length())); }
570 
571 inline TString &TString::operator+=(const char *cs)
572 { return Append(cs, cs ? strlen(cs) : 0); }
573 
575 { return Append(s.Data(), s.Length()); }
576 
578 { return Append(c); }
579 
580 template<class T>
581 inline typename std::enable_if<ROOT::TypeTraits::IsSignedNumeral<T>::value,TString>::type
583 {
584  char buffer[32];
585  // coverity[secure_coding] Buffer is large enough (2^64 = 20 digits).
586  snprintf(buffer, sizeof(buffer), "%lld", static_cast<Long64_t>(i));
587  return operator+=(buffer);
588 }
589 
590 template<class T>
591 inline typename std::enable_if<ROOT::TypeTraits::IsUnsignedNumeral<T>::value,TString>::type
593 {
594  char buffer[32];
595  // coverity[secure_coding] Buffer is large enough (2^64 = 20 digits).
596  snprintf(buffer, sizeof(buffer), "%llu", static_cast<ULong64_t>(u));
597  return operator+=(buffer);
598 }
599 
600 template<class T>
601 inline typename std::enable_if<ROOT::TypeTraits::IsFloatNumeral<T>::value,TString>::type
603 {
604  char buffer[32];
605  // coverity[secure_coding] Buffer is large enough: width specified in format
606  snprintf(buffer, sizeof(buffer), "%.17Lg", static_cast<LongDouble_t>(f));
607  return operator+=(buffer);
608 }
609 
610 inline Bool_t TString::BeginsWith(const char *s, ECaseCompare cmp) const
611 { return Index(s, s ? strlen(s) : (Ssiz_t)0, (Ssiz_t)0, cmp) == 0; }
612 
613 inline Bool_t TString::BeginsWith(const TString &pat, ECaseCompare cmp) const
614 { return Index(pat.Data(), pat.Length(), (Ssiz_t)0, cmp) == 0; }
615 
616 inline Bool_t TString::Contains(const TString &pat, ECaseCompare cmp) const
617 { return Index(pat.Data(), pat.Length(), (Ssiz_t)0, cmp) != kNPOS; }
618 
619 inline Bool_t TString::Contains(const char *s, ECaseCompare cmp) const
620 { return Index(s, s ? strlen(s) : 0, (Ssiz_t)0, cmp) != kNPOS; }
621 
622 inline Bool_t TString::Contains(const TRegexp &pat) const
623 { return Index(pat, (Ssiz_t)0) != kNPOS; }
624 
626 { return Index(pat, (Ssiz_t)0) != kNPOS; }
627 
628 inline Bool_t TString::EqualTo(const char *cs, ECaseCompare cmp) const
629 { return (CompareTo(cs, cmp) == 0) ? kTRUE : kFALSE; }
630 
631 inline Bool_t TString::EqualTo(const TString &st, ECaseCompare cmp) const
632 { return (CompareTo(st, cmp) == 0) ? kTRUE : kFALSE; }
633 
634 inline Ssiz_t TString::Index(const char *s, Ssiz_t i, ECaseCompare cmp) const
635 { return Index(s, s ? strlen(s) : 0, i, cmp); }
636 
637 inline Ssiz_t TString::Index(const TString &s, Ssiz_t i, ECaseCompare cmp) const
638 { return Index(s.Data(), s.Length(), i, cmp); }
639 
640 inline Ssiz_t TString::Index(const TString &pat, Ssiz_t patlen, Ssiz_t i,
641  ECaseCompare cmp) const
642 { return Index(pat.Data(), patlen, i, cmp); }
643 
644 inline TString &TString::Insert(Ssiz_t pos, const char *cs)
645 { return Replace(pos, 0, cs, cs ? strlen(cs) : 0); }
646 
647 inline TString &TString::Insert(Ssiz_t pos, const char *cs, Ssiz_t n)
648 { return Replace(pos, 0, cs, n); }
649 
650 inline TString &TString::Insert(Ssiz_t pos, const TString &s)
651 { return Replace(pos, 0, s.Data(), s.Length()); }
652 
653 inline TString &TString::Insert(Ssiz_t pos, const TString &s, Ssiz_t n)
654 { return Replace(pos, 0, s.Data(), TMath::Min(n, s.Length())); }
655 
656 inline TString &TString::Prepend(const char *cs)
657 { return Replace(0, 0, cs, cs ? strlen(cs) : 0); }
658 
659 inline TString &TString::Prepend(const char *cs, Ssiz_t n)
660 { return Replace(0, 0, cs, n); }
661 
663 { return Replace(0, 0, s.Data(), s.Length()); }
664 
666 { return Replace(0, 0, s.Data(), TMath::Min(n, s.Length())); }
667 
669 { return Replace(pos, TMath::Max(0, Length()-pos), 0, 0); }
670 
672 { return Replace(pos, n, 0, 0); }
673 
675 { return Remove(TMath::Max(0, Length()-1)); }
676 
677 inline TString &TString::Replace(Ssiz_t pos, Ssiz_t n, const char *cs)
678 { return Replace(pos, n, cs, cs ? strlen(cs) : 0); }
679 
681 { return Replace(pos, n, s.Data(), s.Length()); }
682 
683 inline TString &TString::Replace(Ssiz_t pos, Ssiz_t n1, const TString &s,
684  Ssiz_t n2)
685 { return Replace(pos, n1, s.Data(), TMath::Min(s.Length(), n2)); }
686 
687 inline TString &TString::ReplaceAll(const TString &s1, const TString &s2)
688 { return ReplaceAll(s1.Data(), s1.Length(), s2.Data(), s2.Length()) ; }
689 
690 inline TString &TString::ReplaceAll(const TString &s1, const char *s2)
691 { return ReplaceAll(s1.Data(), s1.Length(), s2, s2 ? strlen(s2) : 0); }
692 
693 inline TString &TString::ReplaceAll(const char *s1, const TString &s2)
694 { return ReplaceAll(s1, s1 ? strlen(s1) : 0, s2.Data(), s2.Length()); }
695 
696 inline TString &TString::ReplaceAll(const char *s1,const char *s2)
697 { return ReplaceAll(s1, s1 ? strlen(s1) : 0, s2, s2 ? strlen(s2) : 0); }
698 
699 inline TString &TString::Swap(TString &other) {
700  // Swap the contents of other and this without reallocation.
701 #ifndef __CINT__
702  Rep_t tmp = other.fRep;
703  other.fRep = fRep;
704  fRep = tmp;
705 #endif
706  return *this;
707 }
708 
710 { return GetPointer()[i]; }
711 
712 inline char TString::operator()(Ssiz_t i) const
713 { return GetPointer()[i]; }
714 
716 { AssertElement(i); return GetPointer()[i]; }
717 
718 inline char TString::operator[](Ssiz_t i) const
719 { AssertElement(i); return GetPointer()[i]; }
720 
721 inline const char *TSubString::Data() const
722 {
723  // Return a pointer to the beginning of the substring. Note that the
724  // terminating null is in the same place as for the original
725  // TString, so this method is not appropriate for converting the
726  // TSubString to a string. To do that, construct a TString from the
727  // TSubString. For example:
728  //
729  // root [0] TString s("hello world")
730  // root [1] TSubString sub=s(0, 5)
731  // root [2] sub.Data()
732  // (const char* 0x857c8b8)"hello world"
733  // root [3] TString substr(sub)
734  // root [4] substr
735  // (class TString)"hello"
736 
737  return fStr.Data() + fBegin;
738 }
739 
740 // Access to elements of sub-string with bounds checking
741 inline char TSubString::operator[](Ssiz_t i) const
742 { AssertElement(i); return fStr.GetPointer()[fBegin+i]; }
743 
744 inline char TSubString::operator()(Ssiz_t i) const
745 { return fStr.GetPointer()[fBegin+i]; }
746 
748 { fStr = s.fStr; fBegin = s.fBegin; fExtent = s.fExtent; return *this; }
749 
750 
751 // String Logical operators
752 inline Bool_t operator==(const TString &s1, const TString &s2)
753 {
754  return ((s1.Length() == s2.Length()) &&
755  !memcmp(s1.Data(), s2.Data(), s1.Length()));
756 }
757 
758 inline Bool_t operator!=(const TString &s1, const TString &s2)
759 { return !(s1 == s2); }
760 
761 inline Bool_t operator<(const TString &s1, const TString &s2)
762 { return s1.CompareTo(s2) < 0; }
763 
764 inline Bool_t operator>(const TString &s1, const TString &s2)
765 { return s1.CompareTo(s2) > 0; }
766 
767 inline Bool_t operator<=(const TString &s1, const TString &s2)
768 { return s1.CompareTo(s2) <= 0; }
769 
770 inline Bool_t operator>=(const TString &s1, const TString &s2)
771 { return s1.CompareTo(s2) >= 0; }
772 
773 // Bool_t operator==(const TString &s1, const char *s2);
774 inline Bool_t operator!=(const TString &s1, const char *s2)
775 { return !(s1 == s2); }
776 
777 inline Bool_t operator<(const TString &s1, const char *s2)
778 { return s1.CompareTo(s2) < 0; }
779 
780 inline Bool_t operator>(const TString &s1, const char *s2)
781 { return s1.CompareTo(s2) > 0; }
782 
783 inline Bool_t operator<=(const TString &s1, const char *s2)
784 { return s1.CompareTo(s2) <= 0; }
785 
786 inline Bool_t operator>=(const TString &s1, const char *s2)
787 { return s1.CompareTo(s2) >= 0; }
788 
789 inline Bool_t operator==(const char *s1, const TString &s2)
790 { return (s2 == s1); }
791 
792 inline Bool_t operator!=(const char *s1, const TString &s2)
793 { return !(s2 == s1); }
794 
795 inline Bool_t operator<(const char *s1, const TString &s2)
796 { return s2.CompareTo(s1) > 0; }
797 
798 inline Bool_t operator>(const char *s1, const TString &s2)
799 { return s2.CompareTo(s1) < 0; }
800 
801 inline Bool_t operator<=(const char *s1, const TString &s2)
802 { return s2.CompareTo(s1) >= 0; }
803 
804 inline Bool_t operator>=(const char *s1, const TString &s2)
805 { return s2.CompareTo(s1) <= 0; }
806 
807 // SubString Logical operators
808 // Bool_t operator==(const TSubString &s1, const TSubString &s2);
809 // Bool_t operator==(const TSubString &s1, const char *s2);
810 // Bool_t operator==(const TSubString &s1, const TString &s2);
811 inline Bool_t operator==(const TString &s1, const TSubString &s2)
812 { return (s2 == s1); }
813 
814 inline Bool_t operator==(const char *s1, const TSubString &s2)
815 { return (s2 == s1); }
816 
817 inline Bool_t operator!=(const TSubString &s1, const char *s2)
818 { return !(s1 == s2); }
819 
820 inline Bool_t operator!=(const TSubString &s1, const TString &s2)
821 { return !(s1 == s2); }
822 
823 inline Bool_t operator!=(const TSubString &s1, const TSubString &s2)
824 { return !(s1 == s2); }
825 
826 inline Bool_t operator!=(const TString &s1, const TSubString &s2)
827 { return !(s2 == s1); }
828 
829 inline Bool_t operator!=(const char *s1, const TSubString &s2)
830 { return !(s2 == s1); }
831 
832 #ifndef WIN32
833 // To avoid ambiguities.
834 inline Bool_t operator==(const char *s1, const std::string_view &s2)
835 {
836  return std::string_view(s1) == s2;
837 }
838 
839 inline Bool_t operator==(const std::string_view &s1, const char *s2)
840 {
841  return s1 == std::string_view(s2);
842 }
843 #endif
844 
845 namespace llvm {
846  class raw_ostream;
847 }
848 
849 namespace cling {
850  std::string printValue(const TString* val);
851  std::string printValue(const TSubString* val);
852  std::string printValue(const std::string_view* val);
853 }
854 
855 #endif
A zero length substring is legal.
Definition: TString.h:77
Ssiz_t fWords[kNwords]
Definition: TString.h:194
Ssiz_t Capacity() const
Definition: TString.h:352
void ToLower()
Convert sub-string to lower-case.
Definition: TString.cxx:1650
An array of TObjects.
Definition: TObjArray.h:37
unsigned char fSize
Definition: TString.h:184
int operator!() const
Definition: TString.h:122
TSubString & operator=(const char *s)
Assign char* to sub-string.
Definition: TString.cxx:1604
const char * GetShortPointer() const
Definition: TString.h:241
The concrete implementation of TBuffer for writing/reading to/from a ROOT file or socket...
Definition: TBufferFile.h:46
Definition: TString.h:845
long long Long64_t
Definition: RtypesCore.h:69
UInt_t Hash(const TString &s)
Definition: TString.h:481
char * Compress(const char *str)
Remove all blanks from the string str.
Definition: TString.cxx:2479
Bool_t EqualTo(const char *cs, ECaseCompare cmp=kExact) const
Definition: TString.h:628
void ToUpper()
Convert sub-string to upper-case.
Definition: TString.cxx:1662
void Zero()
Definition: TString.h:250
std::ostream & operator<<(std::ostream &str, const TString &s)
Write string to stream.
Definition: Stringio.cxx:171
double T(double x)
Definition: ChebyshevPol.h:34
friend Bool_t operator==(const TSubString &s1, const TSubString &s2)
Compare two sub-strings.
Definition: TString.cxx:1639
const Ssiz_t kNPOS
Definition: RtypesCore.h:111
ATTENTION: this class is obsolete.
Definition: TStringLong.h:31
T ReadBuffer(TBufferFile *buf)
One of the template functions used to read objects from messages.
Definition: MPSendRecv.h:157
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:687
unsigned int Replace(std::string &s, const std::string what, const std::string withWhat)
TString & fStr
Definition: TString.h:87
char * GetShortPointer()
Definition: TString.h:240
char & operator[](Ssiz_t i)
Definition: TString.h:715
int UnEscChar(const char *src, char *dst, int dstlen, char *specchars, char escchar)
Un-escape specchars in src from escchar and copy to dst.
Definition: TString.cxx:2524
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
Regular expression class.
Definition: TRegexp.h:31
This class implements a mutex interface.
Definition: TVirtualMutex.h:34
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition: TString.h:634
Ssiz_t Start() const
Definition: TString.h:115
Basic string class.
Definition: TString.h:131
#define f(i)
Definition: RSha256.hxx:104
Short_t Min(Short_t a, Short_t b)
Definition: TMathBase.h:168
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
Ssiz_t Length() const
Definition: TString.h:114
TString & operator+=(const char *s)
Definition: TString.h:571
char * GetPointer()
Definition: TString.h:242
std::istream & operator>>(std::istream &str, TString &s)
Read string from stream.
Definition: Stringio.cxx:163
RawStr_t fRaw
Definition: TString.h:203
Rep_t fRep
Definition: TString.h:209
std::string printValue(const std::string_view *val)
Print a TString in the cling interpreter:
Definition: TString.cxx:2596
TString & Prepend(const char *cs)
Definition: TString.h:656
const char * Data() const
Definition: TString.h:721
TSubString(const TSubString &s)
Definition: TString.h:99
TString & Insert(Ssiz_t pos, const char *s)
Definition: TString.h:644
UInt_t Hash(ECaseCompare cmp=kExact) const
Return hash value.
Definition: TString.cxx:626
Bool_t operator>=(const TString &s1, const TString &s2)
Definition: TString.h:770
TString & Replace(Ssiz_t pos, Ssiz_t n, const char *s)
Definition: TString.h:677
#define ClassDef(name, id)
Definition: Rtypes.h:320
void Init(TClassEdit::TInterpreterLookupHelper *helper)
Definition: TClassEdit.cxx:121
Ssiz_t GetLongCap() const
Definition: TString.h:236
friend class TString
Definition: TString.h:80
ECaseCompare
Definition: TString.h:263
TString & Append(const char *cs)
Definition: TString.h:559
static Ssiz_t Recommend(Ssiz_t s)
Definition: TString.h:220
Bool_t IsNull() const
Definition: TString.h:121
TString operator+(const TString &s1, const TString &s2)
Use the special concatenation constructor.
Definition: TString.cxx:1449
std::string_view View() const
Definition: TString.h:439
basic_string_view< char > string_view
Bool_t operator!=(const TString &s1, const TString &s2)
Definition: TString.h:758
char & operator()(Ssiz_t i)
Definition: TString.h:709
Bool_t operator<=(const TString &s1, const TString &s2)
Definition: TString.h:767
char & operator[](Ssiz_t i)
Return character at pos i from sub-string. Check validity of i.
Definition: TString.cxx:1576
Ssiz_t GetShortSize() const
Definition: TString.h:230
LongStr_t fLong
Definition: TString.h:201
char & operator()(Ssiz_t i)
Return character at pos i from sub-string. No check on i.
Definition: TString.cxx:1585
auto * a
Definition: textangle.C:12
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:610
static Ssiz_t MaxSize()
Definition: TString.h:247
Ssiz_t fExtent
Definition: TString.h:89
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)
Ssiz_t Length() const
Definition: TString.h:405
void SetLongCap(Ssiz_t s)
Definition: TString.h:235
#define s1(x)
Definition: RSha256.hxx:91
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:75
char * Strip(const char *str, char c=' ')
Strip leading and trailing c (blanks by default) from a string.
Definition: TString.cxx:2429
void UnLink() const
Definition: TString.h:249
void SetLongPointer(char *p)
Definition: TString.h:237
ShortStr_t fShort
Definition: TString.h:202
ShortStr_t fS
Definition: TString.h:188
char * StrDup(const char *str)
Duplicate the string str.
Definition: TString.cxx:2465
const Bool_t kFALSE
Definition: RtypesCore.h:88
const char * GetPointer() const
Definition: TString.h:243
TString & Remove(Ssiz_t pos)
Definition: TString.h:668
int Ssiz_t
Definition: RtypesCore.h:63
RooCmdArg Index(RooCategory &icat)
char * GetLongPointer()
Definition: TString.h:238
void Copy(void *source, void *dest)
TString & Swap(TString &other)
Definition: TString.h:699
void SetLongSize(Ssiz_t s)
Definition: TString.h:232
double Double_t
Definition: RtypesCore.h:55
void Printf(const char *fmt,...)
Ssiz_t GetLongSize() const
Definition: TString.h:233
int type
Definition: TGX11.cxx:120
unsigned long long ULong64_t
Definition: RtypesCore.h:70
Print a TSeq at the prompt:
Definition: TDatime.h:115
Ssiz_t fBegin
Definition: TString.h:88
basic_string_view< char > string_view
Definition: RStringView.hxx:35
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:619
static constexpr double s
void SetShortSize(Ssiz_t s)
Definition: TString.h:229
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.
Definition: TString.cxx:406
void SetSize(Ssiz_t s)
Definition: TString.h:234
Bool_t IsNull() const
Definition: TString.h:402
char fData[kMinCap]
Definition: TString.h:185
int EscChar(const char *src, char *dst, int dstlen, char *specchars, char escchar)
Escape specchars in src with escchar and copy to dst.
Definition: TString.cxx:2500
void SubStringError(Ssiz_t, Ssiz_t, Ssiz_t) const
Output error message.
Definition: TString.cxx:1674
Bool_t operator>(const TString &s1, const TString &s2)
Definition: TString.h:764
Bool_t operator==(const TString &s1, const TString &s2)
Definition: TString.h:752
static Ssiz_t Align(Ssiz_t s)
Definition: TString.h:219
TSubString(const TString &s, Ssiz_t start, Ssiz_t len)
Private constructor.
Definition: TString.cxx:1539
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:200
const Int_t kMaxInt
Definition: RtypesCore.h:99
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
#define snprintf
Definition: civetweb.c:1351
#define c(i)
Definition: RSha256.hxx:101
const char * GetLongPointer() const
Definition: TString.h:239
Bool_t IsLong() const
Definition: TString.h:224
Bool_t IsWhitespace() const
Definition: TString.h:403
Bool_t operator<(const TString &s1, const TString &s2)
Definition: TString.h:761
const Bool_t kTRUE
Definition: RtypesCore.h:87
void AssertElement(Ssiz_t i) const
Check to make sure a sub-string index is in range.
Definition: TString.cxx:1683
static constexpr double ns
const Int_t n
Definition: legend1.C:16
TString & String()
Definition: TString.h:116
EStripType
Definition: TString.h:262
TString & Chop()
Definition: TString.h:674
static char * Format(const char *format, va_list ap)
Format a string in a circular formatting buffer (using a printf style format descriptor).
Definition: TString.cxx:2350
std::string & operator+=(std::string &left, const TString &right)
Definition: TString.h:473
const char * Data() const
Definition: TString.h:364