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