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