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