Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TString.cxx
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/** \class TString
13\ingroup Base
14
15Basic string class.
16
17Cannot be stored in a TCollection... use TObjString instead.
18
19The underlying string is stored as a char* that can be accessed via
20TString::Data().
21TString provides Short String Optimization (SSO) so that short
22strings (<15 on 64-bit and <11 on 32-bit) are contained in the
23TString internal data structure without the need for mallocing the
24required space.
25
26Substring operations are provided by the TSubString class, which
27holds a reference to the original string and its data, along with
28the offset and length of the substring. To retrieve the substring
29as a TString, construct a TString from it, eg:
30~~~ {.cpp}
31 root [0] TString s("hello world")
32 root [1] TString s2( s(0,5) )
33 root [2] s2
34 (class TString)"hello"
35~~~
36*/
37
38#include <ROOT/RConfig.hxx>
39#include <stdlib.h>
40#include <ctype.h>
41#include <list>
42#include <algorithm>
43
44#include "Varargs.h"
45#include "strlcpy.h"
46#include "TString.h"
47#include "TBuffer.h"
48#include "TError.h"
49#include "Bytes.h"
50#include "TClass.h"
51#include "TMD5.h"
52#include "TObjArray.h"
53#include "TObjString.h"
54#include "TVirtualMutex.h"
55#include "ThreadLocalStorage.h"
56
57
58#if defined(R__WIN32)
59#define strtoull _strtoui64
60#endif
61
62#ifdef R__GLOBALSTL
63namespace std { using ::list; }
64#endif
65
67
68// Amount to shift hash values to avoid clustering
70
71////////////////////////////////////////////////////////////////////////////////
72//
73// In what follows, fCap is the length of the underlying representation
74// vector. Hence, the capacity for a null terminated string held in this
75// vector is fCap-1. The variable fSize is the length of the held
76// string, excluding the terminating null.
77//
78// The algorithms make no assumptions about whether internal strings
79// hold embedded nulls. However, they do assume that any string
80// passed in as an argument that does not have a length count is null
81// terminated and therefore has no embedded nulls.
82//
83// The internal string is always null terminated.
84
85////////////////////////////////////////////////////////////////////////////////
86/// TString default ctor.
87
89{
90 Zero();
91}
92
93////////////////////////////////////////////////////////////////////////////////
94/// Create TString able to contain ic characters.
95
97{
98 Init(ic, 0);
99}
100
101////////////////////////////////////////////////////////////////////////////////
102/// Create TString and initialize it with string cs.
103
104TString::TString(const char *cs)
105{
106 if (cs) {
107 Ssiz_t n = strlen(cs);
108 char *data = Init(n, n);
109 memcpy(data, cs, n);
110 } else
111 Init(0, 0);
112}
113
114////////////////////////////////////////////////////////////////////////////////
115/// Create TString and initialize it with string cs.
116
117TString::TString(const std::string &s)
118{
119 Ssiz_t n = s.length();
120 char *data = Init(n, n);
121 memcpy(data, s.c_str(), n);
122}
123
124////////////////////////////////////////////////////////////////////////////////
125/// Create TString and initialize it with the first n characters of cs.
126
127TString::TString(const char *cs, Ssiz_t n)
128{
129 if (!cs) {
130 Error("TString::TString", "NULL input string!");
131 Zero();
132 return;
133 }
134 if (n < 0) {
135 Error("TString::TString", "Negative length!");
136 Zero();
137 return;
138 }
139 if (strlen(cs) < (size_t)n) {
140 Warning("TString::TString", "Input string is shorter than requested size.");
141 }
142 char *data = Init(n, n);
143 memcpy(data, cs, n);
144}
145
146////////////////////////////////////////////////////////////////////////////////
147/// Initialize a string with a single character.
148
150{
151 char *data = Init(1, 1);
152 data[0] = c;
153}
154
155////////////////////////////////////////////////////////////////////////////////
156/// Initialize a string with a single character.
157
159{
160 InitChar(c);
161}
162
163////////////////////////////////////////////////////////////////////////////////
164/// Initialize the first n locations of a TString with character c.
165
167{
168 if (n < 0) {
169 Error("TString::TString", "Negative length!");
170 Zero();
171 return;
172 }
173 char *data = Init(n, n);
174 while (n--) data[n] = c;
175}
176
177////////////////////////////////////////////////////////////////////////////////
178/// Copy constructor.
179
181{
182 if (!s.IsLong())
183 fRep.fRaw = s.fRep.fRaw;
184 else {
185 Ssiz_t n = s.GetLongSize();
186 char *data = Init(n, n);
187 memcpy(data, s.GetLongPointer(), n);
188 }
189}
190
191////////////////////////////////////////////////////////////////////////////////
192/// Move constructor.
193
195{
196 // Short or long, all data is in fRaw.
197 fRep.fRaw = s.fRep.fRaw;
198 s.Init(0,0);
199}
200
201////////////////////////////////////////////////////////////////////////////////
202/// Copy a std::string_view in a TString.
203
204TString::TString(const std::string_view& substr)
205{
206 Ssiz_t len = substr.length();
207 char *data = Init(len, len);
208 memcpy(data, substr.data(), len);
209}
210
211////////////////////////////////////////////////////////////////////////////////
212/// Copy a TSubString in a TString.
213
215{
216 Ssiz_t len = substr.IsNull() ? 0 : substr.Length();
217 char *data = Init(len, len);
218 memcpy(data, substr.Data(), len);
219}
220
221////////////////////////////////////////////////////////////////////////////////
222/// Special constructor to initialize with the concatenation of a1 and a2.
223
224TString::TString(const char *a1, Ssiz_t n1, const char *a2, Ssiz_t n2)
225{
226 if (n1 < 0) {
227 Error("TString::TString", "Negative first length!");
228 Zero();
229 return;
230 }
231 if (n2 < 0) {
232 Error("TString::TString", "Negative second length!");
233 Zero();
234 return;
235 }
236 if (!a1) n1 = 0;
237 if (!a2) n2 = 0;
238 Ssiz_t tot = n1+n2;
239 char *data = Init(tot, tot);
240 if (a1) memcpy(data, a1, n1);
241 if (a2) memcpy(data+n1, a2, n2);
242}
243
244////////////////////////////////////////////////////////////////////////////////
245/// Delete a TString.
246
248{
249 UnLink();
250}
251
252////////////////////////////////////////////////////////////////////////////////
253/// Private member function returning an empty string representation of
254/// size capacity and containing nchar characters.
255
257{
258 if (capacity < 0) {
259 Error("TString::Init", "Negative length!");
260 capacity = 0;
261 }
262 if (nchar < 0) {
263 Error("*TString::Init", "Negative length!");
264 nchar = 0;
265 }
266 if (nchar > capacity) {
267 Error("TString::Init", "capacity is smaller than nchar (%d > %d)", nchar, capacity);
268 nchar = capacity;
269 }
270 if (capacity > MaxSize()) {
271 Error("TString::Init", "capacity too large (%d, max = %d)", capacity, MaxSize());
272 capacity = MaxSize();
273 if (nchar > capacity)
274 nchar = capacity;
275 }
276
277 char *data;
278 if (capacity < kMinCap) {
281 } else {
282 Ssiz_t cap = Recommend(capacity);
283 data = new char[cap+1];
284 SetLongCap(cap+1);
287 }
288 data[nchar] = 0; // terminating null
289
290 return data;
291}
292
293////////////////////////////////////////////////////////////////////////////////
294/// Assign character c to TString.
295
297{
298 if (!c) {
299 UnLink();
300 Zero();
301 return *this;
302 }
303 return Replace(0, Length(), &c, 1);
304}
305
306////////////////////////////////////////////////////////////////////////////////
307/// Assign string cs to TString.
308
310{
311 if (!cs || !*cs) {
312 UnLink();
313 Zero();
314 return *this;
315 }
316 return Replace(0, Length(), cs, strlen(cs));
317}
318
319////////////////////////////////////////////////////////////////////////////////
320/// Assign std::string s to TString.
321
322TString& TString::operator=(const std::string &s)
323{
324 if (s.length()==0) {
325 UnLink();
326 Zero();
327 return *this;
328 }
329 return Replace(0, Length(), s.c_str(), s.length());
330}
331
332////////////////////////////////////////////////////////////////////////////////
333/// Assign std::string s to TString.
334
335TString& TString::operator=(const std::string_view &s)
336{
337 if (s.length()==0) {
338 UnLink();
339 Zero();
340 return *this;
341 }
342 return Replace(0, Length(), s.data(), s.length());
343}
344
345////////////////////////////////////////////////////////////////////////////////
346/// Assignment operator.
347
349{
350 if (this != &rhs) {
351 UnLink();
352 if (!rhs.IsLong())
353 fRep.fRaw = rhs.fRep.fRaw;
354 else {
355 Ssiz_t n = rhs.GetLongSize();
356 char *data = Init(n, n);
357 memcpy(data, rhs.GetLongPointer(), n);
358 }
359 }
360 return *this;
361}
362
363////////////////////////////////////////////////////////////////////////////////
364/// Move-Assignment operator.
365
367{
368 UnLink();
369 fRep.fRaw = rhs.fRep.fRaw;
370 rhs.Zero();
371 return *this;
372}
373
374////////////////////////////////////////////////////////////////////////////////
375/// Assign a TSubString substr to TString.
376
378{
379 Ssiz_t len = substr.IsNull() ? 0 : substr.Length();
380 if (!len) {
381 UnLink();
382 Zero();
383 return *this;
384 }
385 return Replace(0, Length(), substr.Data(), len);
386}
387
388////////////////////////////////////////////////////////////////////////////////
389/// Append character c rep times to string.
390
392{
393 if (!rep) return *this;
394
395 if (rep < 0) {
396 Error("TString::Append", "Negative length!");
397 return *this;
398 }
399 Ssiz_t len = Length();
400 Ssiz_t tot = len + rep; // Final string length
401
402 if (tot > MaxSize()) {
403 Error("TString::Append", "rep too large (%d, max = %d)", rep, MaxSize()-len);
404 tot = MaxSize();
405 rep = tot - len;
406 }
407
408 Ssiz_t capac = Capacity();
409 char *data, *p = GetPointer();
410
411 if (capac - tot >= 0) {
412 SetSize(tot);
413 data = p;
414 } else {
415 Ssiz_t cap = AdjustCapacity(capac, tot);
416 data = new char[cap+1];
417 memcpy(data, p, len);
418 UnLink();
419 SetLongCap(cap+1);
420 SetLongSize(tot);
422 }
423 data[tot] = 0;
424
425 data += len;
426 while (rep--)
427 *data++ = c;
428
429 return *this;
430}
431
432////////////////////////////////////////////////////////////////////////////////
433/// Return string capacity. If nc != current capacity Clone() the string
434/// in a string with the desired capacity.
435
437{
438 if (nc > Length())
439 Clone(nc);
440
441 return Capacity();
442}
443
444////////////////////////////////////////////////////////////////////////////////
445/// Compare a string to char *cs2. Returns returns zero if the two
446/// strings are identical, otherwise returns the difference between
447/// the first two differing bytes (treated as unsigned char values,
448/// so that `\200' is greater than `\0', for example). Zero-length
449/// strings are always identical.
450
451int TString::CompareTo(const char *cs2, ECaseCompare cmp) const
452{
453 if (!cs2) return 1;
454
455 const char *cs1 = Data();
456 Ssiz_t len = Length();
457 Ssiz_t i = 0;
458 if (cmp == kExact) {
459 for (; cs2[i]; ++i) {
460 if (i == len) return -1;
461 if (cs1[i] != cs2[i]) return ((cs1[i] > cs2[i]) ? 1 : -1);
462 }
463 } else { // ignore case
464 for (; cs2[i]; ++i) {
465 if (i == len) return -1;
466 char c1 = tolower((unsigned char)cs1[i]);
467 char c2 = tolower((unsigned char)cs2[i]);
468 if (c1 != c2) return ((c1 > c2) ? 1 : -1);
469 }
470 }
471 return (i < len) ? 1 : 0;
472}
473
474////////////////////////////////////////////////////////////////////////////////
475/// Compare a string to another string. Returns returns zero if the two
476/// strings are identical, otherwise returns the difference between
477/// the first two differing bytes (treated as unsigned char values,
478/// so that `\200' is greater than `\0', for example). Zero-length
479/// strings are always identical.
480
481int TString::CompareTo(const TString &str, ECaseCompare cmp) const
482{
483 const char *s1 = Data();
484 const char *s2 = str.Data();
485 Ssiz_t len = Length();
486 Ssiz_t slen, sleno = str.Length();
487 slen = sleno;
488 if (len < slen) slen = len;
489 if (cmp == kExact) {
490 int result = memcmp(s1, s2, slen);
491 if (result != 0) return result;
492 } else {
493 Ssiz_t i = 0;
494 for (; i < slen; ++i) {
495 char c1 = tolower((unsigned char)s1[i]);
496 char c2 = tolower((unsigned char)s2[i]);
497 if (c1 != c2) return ((c1 > c2) ? 1 : -1);
498 }
499 }
500 // strings are equal up to the length of the shorter one.
501 slen = sleno;
502 if (len == slen) return 0;
503 return (len > slen) ? 1 : -1;
504}
505
506////////////////////////////////////////////////////////////////////////////////
507/// Return number of times character c occurs in the string.
508
510{
511 Int_t count = 0;
512 Int_t len = Length();
513 const char *data = Data();
514 for (Int_t n = 0; n < len; n++)
515 if (data[n] == c) count++;
516
517 return count;
518}
519
520////////////////////////////////////////////////////////////////////////////////
521/// Copy a string.
522
524{
525 TString temp(*this);
526 return temp;
527}
528
529////////////////////////////////////////////////////////////////////////////////
530/// Find first occurrence of a character c.
531
533{
534 const char *f = strchr(Data(), c);
535 return f ? f - Data() : kNPOS;
536}
537
538////////////////////////////////////////////////////////////////////////////////
539/// Find first occurrence of a character in cs.
540
541Ssiz_t TString::First(const char *cs) const
542{
543 const char *f = strpbrk(Data(), cs);
544 return f ? f - Data() : kNPOS;
545}
546
547#ifndef R__BYTESWAP
548////////////////////////////////////////////////////////////////////////////////
549
550inline static UInt_t SwapInt(UInt_t x)
551{
552 return (((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) << 8) |
553 ((x & 0x00ff0000U) >> 8) | ((x & 0xff000000U) >> 24));
554}
555#endif
556
557////////////////////////////////////////////////////////////////////////////////
558/// Utility used by Hash().
559
560inline static void Mash(UInt_t& hash, UInt_t chars)
561{
562 hash = (chars ^
563 ((hash << kHashShift) |
564 (hash >> (kBitsPerByte*sizeof(UInt_t) - kHashShift))));
565}
566
567////////////////////////////////////////////////////////////////////////////////
568/// Return a case-sensitive hash value (endian independent).
569
570UInt_t Hash(const char *str)
571{
572 UInt_t len = str ? strlen(str) : 0;
573 UInt_t hv = len; // Mix in the string length.
574 UInt_t i = hv*sizeof(char)/sizeof(UInt_t);
575
576 if (((ULongptr_t)str)%sizeof(UInt_t) == 0) {
577 // str is word aligned
578 const UInt_t *p = (const UInt_t*)str;
579
580 while (i--) {
581#ifndef R__BYTESWAP
582 UInt_t h = *p++;
583 Mash(hv, SwapInt(h));
584#else
585 Mash(hv, *p++); // XOR in the characters.
586#endif
587 }
588
589 // XOR in any remaining characters:
590 if ((i = len*sizeof(char)%sizeof(UInt_t)) != 0) {
591 UInt_t h = 0;
592 const char* c = (const char*)p;
593 while (i--)
594 h = ((h << kBitsPerByte*sizeof(char)) | *c++);
595 Mash(hv, h);
596 }
597 } else {
598 // str is not word aligned
599 UInt_t h;
600 const unsigned char *p = (const unsigned char*)str;
601
602 while (i--) {
603 memcpy(&h, p, sizeof(UInt_t));
604#ifndef R__BYTESWAP
605 Mash(hv, SwapInt(h));
606#else
607 Mash(hv, h);
608#endif
609 p += sizeof(UInt_t);
610 }
611
612 // XOR in any remaining characters:
613 if ((i = len*sizeof(char)%sizeof(UInt_t)) != 0) {
614 h = 0;
615 const char* c = (const char*)p;
616 while (i--)
617 h = ((h << kBitsPerByte*sizeof(char)) | *c++);
618 Mash(hv, h);
619 }
620 }
621 return hv;
622}
623
624////////////////////////////////////////////////////////////////////////////////
625/// Return a case-sensitive hash value (endian independent).
626
628{
629 UInt_t hv = (UInt_t)Length(); // Mix in the string length.
630 UInt_t i = hv*sizeof(char)/sizeof(UInt_t);
631 const UInt_t *p = (const UInt_t*)Data();
632 {
633 while (i--) {
634#ifndef R__BYTESWAP
635 UInt_t h = *p++;
636 Mash(hv, SwapInt(h)); // XOR in the characters.
637#else
638 Mash(hv, *p++); // XOR in the characters.
639#endif
640 }
641 }
642 // XOR in any remaining characters:
643 if ((i = Length()*sizeof(char)%sizeof(UInt_t)) != 0) {
644 UInt_t h = 0;
645 const char* c = (const char*)p;
646 while (i--)
647 h = ((h << kBitsPerByte*sizeof(char)) | *c++);
648 Mash(hv, h);
649 }
650 return hv;
651}
652
653////////////////////////////////////////////////////////////////////////////////
654/// Return a case-insensitive hash value (endian independent).
655
657{
658 UInt_t hv = (UInt_t)Length(); // Mix in the string length.
659 UInt_t i = hv;
660 const unsigned char *p = (const unsigned char*)Data();
661 while (i--) {
662 Mash(hv, toupper(*p));
663 ++p;
664 }
665 return hv;
666}
667
668////////////////////////////////////////////////////////////////////////////////
669/// Return hash value.
670
672{
673 return (cmp == kExact) ? HashCase() : HashFoldCase();
674}
675
676 // MurmurHash3 - a blazingly fast public domain hash!
677 // See http://code.google.com/p/smhasher/
678 // There are two versions, one optimized for 32 bit and one for 64 bit.
679 // They give different hash results!
680 // We use only the 64 bit version which also works on 32 bit.
681
682 //-----------------------------------------------------------------------------
683 // MurmurHash3 was written by Austin Appleby, and is placed in the public
684 // domain. The author hereby disclaims copyright to this source code.
685
686 // Note - The x86 and x64 versions do _not_ produce the same results, as the
687 // algorithms are optimized for their respective platforms. You can still
688 // compile and run any of them on any platform, but your performance with the
689 // non-native version will be less than optimal.
690
691 //-----------------------------------------------------------------------------
692 // Platform-specific functions and macros
693
694 // From MurmurHash.h:
695
696#if defined(_MSC_VER) && (_MSC_VER < 1800)
697 // Microsoft Visual Studio
698 typedef unsigned char uint8_t;
699 typedef unsigned long uint32_t;
700 typedef unsigned __int64 uint64_t;
701#else // defined(_MSC_VER)
702 // Other compilers
703#include <stdint.h>
704#endif // !defined(_MSC_VER)
705
706 // From MurmurHash.cpp:
707#if defined(_MSC_VER)
708 // Microsoft Visual Studio
709#include <stdlib.h>
710#define ROTL64(x,y) _rotl64(x,y)
711#define BIG_CONSTANT(x) (x)
712#else // defined(_MSC_VER)
713 // Other compilers
714 inline uint64_t rotl64 ( uint64_t x, int8_t r )
715 {
716 return (x << r) | (x >> (64 - r));
717 }
718
719#define ROTL64(x,y) rotl64(x,y)
720#define BIG_CONSTANT(x) (x##LLU)
721#endif // !defined(_MSC_VER)
722
723namespace {
724
725 /////////////////////////////////////////////////////////////////////////////
726 /// Block read - if your platform needs to do endian-swapping or can only
727 /// handle aligned reads, do the conversion here
728
729 R__ALWAYS_INLINE uint64_t getblock(const uint64_t* p, int i)
730 {
731 return p[i];
732 }
733
734 /////////////////////////////////////////////////////////////////////////////
735 /// Finalization mix - force all bits of a hash block to avalanche
736
737 R__ALWAYS_INLINE uint64_t fmix(uint64_t k)
738 {
739 k ^= k >> 33;
740 k *= BIG_CONSTANT(0xff51afd7ed558ccd);
741 k ^= k >> 33;
742 k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53);
743 k ^= k >> 33;
744
745 return k;
746 }
747
748 /////////////////////////////////////////////////////////////////////////////
749 /// "key" is input to be hashed.
750 /// "len" is the number of bytes to hash starting at "key".
751 /// "seed" is a hash seed, "out" is a buffer (128 bytes) that will receive
752 /// the results.
753
754 static void MurmurHash3_x64_128(const void * key, const int len,
755 const uint32_t seed, uint64_t out[2] )
756 {
757 const uint8_t * data = (const uint8_t*)key;
758 const int nblocks = len / 16;
759
760 uint64_t h1 = seed;
761 uint64_t h2 = seed;
762
763 uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
764 uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
765
766 //----------
767 // body
768
769 const uint64_t * blocks = (const uint64_t *)(data);
770
771 for(int i = 0; i < nblocks; i++)
772 {
773 uint64_t k1 = getblock(blocks,i*2+0);
774 uint64_t k2 = getblock(blocks,i*2+1);
775
776 k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
777
778 h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;
779
780 k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
781
782 h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
783 }
784
785 //----------
786 // tail
787
788 const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
789
790 uint64_t k1 = 0;
791 uint64_t k2 = 0;
792
793 switch(len & 15) {
794 case 15: k2 ^= uint64_t(tail[14]) << 48; // fall through
795 case 14: k2 ^= uint64_t(tail[13]) << 40; // fall through
796 case 13: k2 ^= uint64_t(tail[12]) << 32; // fall through
797 case 12: k2 ^= uint64_t(tail[11]) << 24; // fall through
798 case 11: k2 ^= uint64_t(tail[10]) << 16; // fall through
799 case 10: k2 ^= uint64_t(tail[ 9]) << 8; // fall through
800 case 9: k2 ^= uint64_t(tail[ 8]) << 0;
801 k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
802 // fall through
803 case 8: k1 ^= uint64_t(tail[ 7]) << 56; // fall through
804 case 7: k1 ^= uint64_t(tail[ 6]) << 48; // fall through
805 case 6: k1 ^= uint64_t(tail[ 5]) << 40; // fall through
806 case 5: k1 ^= uint64_t(tail[ 4]) << 32; // fall through
807 case 4: k1 ^= uint64_t(tail[ 3]) << 24; // fall through
808 case 3: k1 ^= uint64_t(tail[ 2]) << 16; // fall through
809 case 2: k1 ^= uint64_t(tail[ 1]) << 8; // fall through
810 case 1: k1 ^= uint64_t(tail[ 0]) << 0;
811 k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
812 };
813
814 //----------
815 // finalization
816
817 h1 ^= len; h2 ^= len;
818
819 h1 += h2;
820 h2 += h1;
821
822 h1 = fmix(h1);
823 h2 = fmix(h2);
824
825 h1 += h2;
826 h2 += h1;
827
828 ((uint64_t*)out)[0] = h1;
829 ((uint64_t*)out)[1] = h2;
830 }
831
832}
833
834////////////////////////////////////////////////////////////////////////////////
835/// Calculates hash index from any char string. (static function)
836/// - For string: i = TString::Hash(string,nstring);
837/// - For int: i = TString::Hash(&intword,sizeof(int));
838/// - For pointer: i = TString::Hash(&pointer,sizeof(void*));
839///
840/// This employs two different hash functions, depending on ntxt:
841/// - ntxt == sizeof(void*): a simple bitwise xor to get fast pointer hashes
842/// - else: MurmurHash3_x64_128 http://code.google.com/p/smhasher/
843
844UInt_t TString::Hash(const void *txt, Int_t ntxt)
845{
846 if (ntxt != sizeof(void*)) {
847 uint64_t buf[2] = {0};
848 MurmurHash3_x64_128(txt, ntxt, 0x6384BA69, buf);
849 return (UInt_t) buf[0];
850 } else {
851 // simple, superfast hash for pointers and alike
852 UInt_t ret = (UInt_t)0x6384BA69;
853 // aligned?
854 if (((size_t)txt) % sizeof(void*)) {
855 UInt_t* itxt = (UInt_t*)txt;
856 ret ^= itxt[0];
857 if (sizeof(void*) > sizeof(UInt_t)) {
858 ret ^= itxt[1];
859 }
860 } else {
861 const unsigned char* ctxt = (const unsigned char*) txt;
862 for (unsigned int i = 0; i < 4; ++i) {
863 ret ^= ctxt[i] << (i * 8);
864 }
865 if (sizeof(void*) > sizeof(UInt_t)) {
866 ctxt += 4;
867 for (unsigned int i = 0; i < 4; ++i) {
868 ret ^= ctxt[i] << (i * 8);
869 }
870 }
871 }
872 return ret;
873 }
874}
875
876////////////////////////////////////////////////////////////////////////////////
877/// Returns false if strings are not equal.
878
879static int MemIsEqual(const char *p, const char *q, Ssiz_t n)
880{
881 while (n--)
882 {
883 if (tolower((unsigned char)*p) != tolower((unsigned char)*q))
884 return kFALSE;
885 p++; q++;
886 }
887 return kTRUE;
888}
889
890////////////////////////////////////////////////////////////////////////////////
891/// Search for a string in the TString. Plen is the length of pattern,
892/// startIndex is the index from which to start and cmp selects the type
893/// of case-comparison.
894
895Ssiz_t TString::Index(const char *pattern, Ssiz_t plen, Ssiz_t startIndex,
896 ECaseCompare cmp) const
897{
898 if (plen < 0) {
899 Error("TString::Index", "Negative first pattern length!");
900 return kNPOS;
901 }
902 Ssiz_t slen = Length();
903 if (slen < startIndex + plen) return kNPOS;
904 if (plen == 0) return startIndex;
905 slen -= startIndex + plen;
906 const char *sp = Data() + startIndex;
907 if (cmp == kExact) {
908 char first = *pattern;
909 for (Ssiz_t i = 0; i <= slen; ++i)
910 if (sp[i] == first && memcmp(sp+i+1, pattern+1, plen-1) == 0)
911 return i + startIndex;
912 } else {
913 int first = tolower((unsigned char) *pattern);
914 for (Ssiz_t i = 0; i <= slen; ++i)
915 if (tolower((unsigned char) sp[i]) == first &&
916 MemIsEqual(sp+i+1, pattern+1, plen-1))
917 return i + startIndex;
918 }
919 return kNPOS;
920}
921
922////////////////////////////////////////////////////////////////////////////////
923/// Find last occurrence of a character c.
924
926{
927 const char *f = strrchr(Data(), (unsigned char) c);
928 return f ? f - Data() : kNPOS;
929}
930
931////////////////////////////////////////////////////////////////////////////////
932/// Return the MD5 digest for this string, in a string representation.
933
935{
936 TMD5 md5;
937 md5.Update((const UChar_t*)Data(), Length());
938 UChar_t digest[16];
939 md5.Final(digest);
940 return md5.AsString();
941}
942
943////////////////////////////////////////////////////////////////////////////////
944/// Returns true if string contains one of the regexp characters "^$.[]*+?".
945
947{
948 const char *specials = "^$.[]*+?";
949
950 if (First(specials) == kNPOS)
951 return kFALSE;
952 return kTRUE;
953}
954
955////////////////////////////////////////////////////////////////////////////////
956/// Returns true if string contains one of the wildcard characters "[]*?".
957
959{
960 const char *specials = "[]*?";
961
962 if (First(specials) == kNPOS)
963 return kFALSE;
964 return kTRUE;
965}
966
967////////////////////////////////////////////////////////////////////////////////
968/// Prepend character c rep times to string.
969
971{
972 if (rep <= 0)
973 return *this;
974
975 Ssiz_t len = Length();
976 Ssiz_t tot = len + rep; // Final string length
977
978 if (tot > MaxSize()) {
979 Error("TString::Prepend", "rep too large (%d, max = %d)", rep, MaxSize()-len);
980 tot = MaxSize();
981 rep = tot - len;
982 }
983
984 Ssiz_t capac = Capacity();
985 char *data, *p = GetPointer();
986
987 if (capac - tot >= 0) {
988 memmove(p + rep, p, len);
989 SetSize(tot);
990 data = p;
991 } else {
992 Ssiz_t cap = AdjustCapacity(capac, tot);
993 data = new char[cap+1];
994 memcpy(data+rep, p, len);
995 UnLink();
996 SetLongCap(cap+1);
997 SetLongSize(tot);
999 }
1000 data[tot] = 0;
1001
1002 while (rep--)
1003 *data++ = c;
1004
1005 return *this;
1006}
1007
1008////////////////////////////////////////////////////////////////////////////////
1009/// Remove at most n1 characters from self beginning at pos,
1010/// and replace them with the first n2 characters of cs.
1011
1012TString &TString::Replace(Ssiz_t pos, Ssiz_t n1, const char *cs, Ssiz_t n2)
1013{
1014 Ssiz_t len = Length();
1015 if (pos <= kNPOS || pos > len) {
1016 Error("TString::Replace",
1017 "first argument out of bounds: pos = %d, Length = %d", pos, len);
1018 return *this;
1019 }
1020 if (n1 < 0) {
1021 Error("TString::Replace", "Negative number of characters to remove!");
1022 return *this;
1023 }
1024 if (n2 < 0) {
1025 Error("TString::Replace", "Negative number of replacement characters!");
1026 return *this;
1027 }
1028
1029 n1 = TMath::Min(n1, len - pos);
1030 if (!cs) n2 = 0;
1031
1032 Ssiz_t tot = len - n1 + n2; // Final string length
1033 Ssiz_t rem = len - n1 - pos; // Length of remnant at end of string
1034
1035 Ssiz_t capac = Capacity();
1036 char *p = GetPointer();
1037
1038 if (capac >= tot) {
1039 if (n1 != n2) {
1040 if (rem) {
1041 if (n1 > n2) {
1042 if (n2) memmove(p + pos, cs, n2);
1043 memmove(p + pos + n2, p + pos + n1, rem);
1044 SetSize(tot);
1045 p[tot] = 0;
1046 return *this;
1047 }
1048 if (p + pos < cs && cs < p + len) {
1049 if (p + pos + n1 <= cs)
1050 cs += n2 - n1;
1051 else { // p + pos < cs < p + pos + n1
1052 memmove(p + pos, cs, n1);
1053 pos += n1;
1054 cs += n2;
1055 n2 -= n1;
1056 n1 = 0;
1057 }
1058 }
1059 memmove(p + pos + n2, p + pos + n1, rem);
1060 }
1061 }
1062 if (n2) memmove(p + pos, cs, n2);
1063 SetSize(tot);
1064 p[tot] = 0;
1065 } else {
1066 Ssiz_t cap = AdjustCapacity(capac, tot);
1067 char *data = new char[cap+1];
1068 if (pos) memcpy(data, p, pos);
1069 if (n2 ) memcpy(data + pos, cs, n2);
1070 if (rem) memcpy(data + pos + n2, p + pos + n1, rem);
1071 UnLink();
1072 SetLongCap(cap+1);
1073 SetLongSize(tot);
1075 data[tot] = 0;
1076 }
1077
1078 return *this;
1079}
1080
1081////////////////////////////////////////////////////////////////////////////////
1082/// Find & Replace ls1 symbols of s1 with ls2 symbols of s2 if any.
1083
1084TString& TString::ReplaceAll(const char *s1, Ssiz_t ls1, const char *s2,
1085 Ssiz_t ls2)
1086{
1087 if (s1 && ls1 > 0) {
1088 Ssiz_t index = 0;
1089 while ((index = Index(s1, ls1, index, kExact)) != kNPOS) {
1090 Replace(index, ls1, s2, ls2);
1091 index += ls2;
1092 }
1093 }
1094 return *this;
1095}
1096
1097
1098////////////////////////////////////////////////////////////////////////////////
1099/// Find special characters which are typically used in `printf()` calls
1100/// and replace them by appropriate escape sequences. Result can be
1101/// stored as string argument in ROOT macros. The content of TString will be changed!
1102
1104{
1105 return ReplaceAll("\\","\\\\").ReplaceAll("\"","\\\"");
1106}
1107
1108
1109////////////////////////////////////////////////////////////////////////////////
1110/// Remove char c at begin and/or end of string (like Strip()) but
1111/// modifies directly the string.
1112
1114{
1115 Ssiz_t start = 0; // Index of first character
1116 Ssiz_t end = Length(); // One beyond last character
1117 const char *direct = Data(); // Avoid a dereference w dumb compiler
1118 Ssiz_t send = end;
1119
1120 if (st & kLeading)
1121 while (start < end && direct[start] == c)
1122 ++start;
1123 if (st & kTrailing)
1124 while (start < end && direct[end-1] == c)
1125 --end;
1126 if (end == start) {
1127 UnLink();
1128 Zero();
1129 return *this;
1130 }
1131 if (start)
1132 Remove(0, start);
1133 if (send != end)
1134 Remove(send - start - (send - end), send - end);
1135 return *this;
1136}
1137
1138////////////////////////////////////////////////////////////////////////////////
1139/// Resize the string. Truncate or add blanks as necessary.
1140
1142{
1143 if (n < Length())
1144 Remove(n); // Shrank; truncate the string
1145 else
1146 Append(' ', n-Length()); // Grew or staid the same
1147}
1148
1149////////////////////////////////////////////////////////////////////////////////
1150/// Return a substring of self stripped at beginning and/or end.
1151
1153{
1154 Ssiz_t start = 0; // Index of first character
1155 Ssiz_t end = Length(); // One beyond last character
1156 const char *direct = Data(); // Avoid a dereference w dumb compiler
1157
1158 if (st & kLeading)
1159 while (start < end && direct[start] == c)
1160 ++start;
1161 if (st & kTrailing)
1162 while (start < end && direct[end-1] == c)
1163 --end;
1164 if (end == start) start = end = kNPOS; // make the null substring
1165 return TSubString(*this, start, end-start);
1166}
1167
1168////////////////////////////////////////////////////////////////////////////////
1169/// Change string to lower-case.
1170
1172{
1173 Ssiz_t n = Length();
1174 char *p = GetPointer();
1175 while (n--) {
1176 *p = tolower((unsigned char)*p);
1177 p++;
1178 }
1179}
1180
1181////////////////////////////////////////////////////////////////////////////////
1182/// Change string to upper case.
1183
1185{
1186 Ssiz_t n = Length();
1187 char *p = GetPointer();
1188 while (n--) {
1189 *p = toupper((unsigned char)*p);
1190 p++;
1191 }
1192}
1193
1194////////////////////////////////////////////////////////////////////////////////
1195/// Check to make sure a string index is in range.
1196
1198{
1199 if (i == kNPOS || i > Length())
1200 Error("TString::AssertElement",
1201 "out of bounds: i = %d, Length = %d", i, Length());
1202}
1203
1204////////////////////////////////////////////////////////////////////////////////
1205/// Calculate a nice capacity greater than or equal to newCap.
1206
1208{
1209 Ssiz_t ms = MaxSize();
1210 if (newCap > ms - 1) {
1211 Error("TString::AdjustCapacity", "capacity too large (%d, max = %d)",
1212 newCap, ms);
1213 }
1214 Ssiz_t cap = oldCap < ms / 2 - kAlignment ?
1215 Recommend(TMath::Max(newCap, 2 * oldCap)) : ms - 1;
1216 return cap;
1217}
1218
1219////////////////////////////////////////////////////////////////////////////////
1220/// Clear string without changing its capacity.
1221
1223{
1224 Clobber(Capacity());
1225}
1226
1227////////////////////////////////////////////////////////////////////////////////
1228/// Clear string and make sure it has a capacity of nc.
1229
1231{
1232 if (nc > MaxSize()) {
1233 Error("TString::Clobber", "capacity too large (%d, max = %d)", nc, MaxSize());
1234 nc = MaxSize();
1235 }
1236
1237 if (nc < kMinCap) {
1238 UnLink();
1239 Zero();
1240 } else {
1241 char *data = GetLongPointer();
1242 Ssiz_t cap = Recommend(nc);
1243 if (cap != Capacity()) {
1244 data = new char[cap+1];
1245 UnLink();
1246 SetLongCap(cap+1);
1248 }
1249 SetLongSize(0);
1250 data[0] = 0;
1251 }
1252}
1253
1254////////////////////////////////////////////////////////////////////////////////
1255/// Make self a distinct copy with capacity of at least tot, where tot cannot
1256/// be smaller than the current length. Preserve previous contents.
1257
1259{
1260 Ssiz_t len = Length();
1261 if (len >= tot) return;
1262
1263 if (tot > MaxSize()) {
1264 Error("TString::Clone", "tot too large (%d, max = %d)", tot, MaxSize());
1265 tot = MaxSize();
1266 }
1267
1268 Ssiz_t capac = Capacity();
1269 char *data, *p = GetPointer();
1270
1271 if (capac - tot < 0) {
1272 Ssiz_t cap = Recommend(tot);
1273 data = new char[cap+1];
1274 memcpy(data, p, len);
1275 UnLink();
1276 SetLongCap(cap+1);
1279 data[len] = 0;
1280 }
1281}
1282
1283////////////////////////////////////////////////////////////////////////////////
1284// ROOT I/O
1285
1286////////////////////////////////////////////////////////////////////////////////
1287/// Copy string into I/O buffer.
1288
1289void TString::FillBuffer(char *&buffer) const
1290{
1291 UChar_t nwh;
1292 Int_t nchars = Length();
1293
1294 if (nchars > 254) {
1295 nwh = 255;
1296 tobuf(buffer, nwh);
1297 tobuf(buffer, nchars);
1298 } else {
1299 nwh = UChar_t(nchars);
1300 tobuf(buffer, nwh);
1301 }
1302 const char *data = GetPointer();
1303 for (int i = 0; i < nchars; i++) buffer[i] = data[i];
1304 buffer += nchars;
1305}
1306
1307////////////////////////////////////////////////////////////////////////////////
1308/// Read string from I/O buffer.
1309
1310void TString::ReadBuffer(char *&buffer)
1311{
1312 UnLink();
1313 Zero();
1314
1315 UChar_t nwh;
1316 Int_t nchars;
1317
1318 frombuf(buffer, &nwh);
1319 if (nwh == 255)
1320 frombuf(buffer, &nchars);
1321 else
1322 nchars = nwh;
1323
1324 if (nchars < 0) {
1325 Error("TString::ReadBuffer", "found case with nwh=%d and nchars=%d", nwh, nchars);
1326 return;
1327 }
1328
1329 char *data = Init(nchars, nchars);
1330
1331 for (int i = 0; i < nchars; i++) frombuf(buffer, &data[i]);
1332}
1333
1334////////////////////////////////////////////////////////////////////////////////
1335/// Read TString object from buffer. Simplified version of
1336/// TBuffer::ReadObject (does not keep track of multiple
1337/// references to same string). We need to have it here
1338/// because TBuffer::ReadObject can only handle descendant
1339/// of TObject.
1340
1342{
1343 R__ASSERT(b.IsReading());
1344
1345 // Make sure ReadArray is initialized
1346 b.InitMap();
1347
1348 // Before reading object save start position
1349 UInt_t startpos = UInt_t(b.Length());
1350
1351 UInt_t tag;
1352 TClass *clRef = b.ReadClass(clReq, &tag);
1353
1354 TString *a;
1355 if (!clRef) {
1356
1357 a = nullptr;
1358
1359 } else {
1360
1361 a = (TString *) clRef->New();
1362 if (!a) {
1363 ::Error("TString::ReadObject", "could not create object of class %s",
1364 clRef->GetName());
1365 // Exception
1366 return a;
1367 }
1368
1369 a->Streamer(b);
1370
1371 b.CheckByteCount(startpos, tag, clRef);
1372 }
1373
1374 return a;
1375}
1376
1377////////////////////////////////////////////////////////////////////////////////
1378/// Returns size string will occupy on I/O buffer.
1379
1381{
1382 if (Length() > 254)
1383 return Length()+sizeof(UChar_t)+sizeof(Int_t);
1384 else
1385 return Length()+sizeof(UChar_t);
1386}
1387
1388////////////////////////////////////////////////////////////////////////////////
1389/// Stream a string object.
1390
1392{
1393 if (b.IsReading()) {
1394 b.ReadTString(*this);
1395 } else {
1396 b.WriteTString(*this);
1397 }
1398}
1399
1400////////////////////////////////////////////////////////////////////////////////
1401/// Write TString object to buffer. Simplified version of
1402/// TBuffer::WriteObject (does not keep track of multiple
1403/// references to the same string). We need to have it here
1404/// because TBuffer::ReadObject can only handle descendant
1405/// of TObject
1406
1408{
1409 R__ASSERT(b.IsWriting());
1410
1411 // Make sure WriteMap is initialized
1412 b.InitMap();
1413
1414 if (!a) {
1415
1416 b << (UInt_t) 0;
1417
1418 } else {
1419
1420 // Reserve space for leading byte count
1421 UInt_t cntpos = UInt_t(b.Length());
1422 b.SetBufferOffset(Int_t(cntpos+sizeof(UInt_t)));
1423
1424 TClass *cl = a->IsA();
1425 b.WriteClass(cl);
1426
1427 ((TString *)a)->Streamer(b);
1428
1429 // Write byte count
1430 b.SetByteCount(cntpos);
1431 }
1432}
1433
1434////////////////////////////////////////////////////////////////////////////////
1435/// Read string from TBuffer. Function declared in ClassDef.
1436
1437#if defined(R__TEMPLATE_OVERLOAD_BUG)
1438template <>
1439#endif
1441{
1443 return buf;
1444}
1445
1446////////////////////////////////////////////////////////////////////////////////
1447/// Write TString or derived to TBuffer.
1448
1450{
1451 TString::WriteString(buf, s);
1452 return buf;
1453}
1454
1455////////////////////////////////////////////////////////////////////////////////
1456// Related global functions
1457
1458////////////////////////////////////////////////////////////////////////////////
1459/// Compare TString with a char *.
1460
1461Bool_t operator==(const TString& s1, const char *s2)
1462{
1463 if (!s2) return kFALSE;
1464
1465 const char *data = s1.Data();
1466 Ssiz_t len = s1.Length();
1467 Ssiz_t i;
1468 for (i = 0; s2[i]; ++i)
1469 if (data[i] != s2[i] || i == len) return kFALSE;
1470 return (i == len);
1471}
1472
1473////////////////////////////////////////////////////////////////////////////////
1474/// Return a lower-case version of str.
1475
1477{
1478 Ssiz_t n = str.Length();
1479 TString temp((char)0, n);
1480 const char *uc = str.Data();
1481 char *lc = (char*)temp.Data();
1482 // Guard against tolower() being a macro
1483 while (n--) { *lc++ = tolower((unsigned char)*uc); uc++; }
1484 return temp;
1485}
1486
1487////////////////////////////////////////////////////////////////////////////////
1488/// Return an upper-case version of str.
1489
1491{
1492 Ssiz_t n = str.Length();
1493 TString temp((char)0, n);
1494 const char* uc = str.Data();
1495 char* lc = (char*)temp.Data();
1496 // Guard against toupper() being a macro
1497 while (n--) { *lc++ = toupper((unsigned char)*uc); uc++; }
1498 return temp;
1499}
1500
1501////////////////////////////////////////////////////////////////////////////////
1502/// Use the special concatenation constructor.
1503
1504TString operator+(const TString &s, const char *cs)
1505{
1506 return TString(s.Data(), s.Length(), cs, cs ? strlen(cs) : 0);
1507}
1508
1509////////////////////////////////////////////////////////////////////////////////
1510/// Use the special concatenation constructor.
1511
1512TString operator+(const char *cs, const TString &s)
1513{
1514 return TString(cs, cs ? strlen(cs) : 0, s.Data(), s.Length());
1515}
1516
1517////////////////////////////////////////////////////////////////////////////////
1518/// Use the special concatenation constructor.
1519
1521{
1522 return TString(s1.Data(), s1.Length(), s2.Data(), s2.Length());
1523}
1524
1525////////////////////////////////////////////////////////////////////////////////
1526/// Add char to string.
1527
1529{
1530 return TString(s.Data(), s.Length(), &c, 1);
1531}
1532
1533////////////////////////////////////////////////////////////////////////////////
1534/// Add string to char.
1535
1537{
1538 return TString(&c, 1, s.Data(), s.Length());
1539}
1540
1541////////////////////////////////////////////////////////////////////////////////
1542// Static Member Functions
1543// The static data members access
1544
1545////////////////////////////////////////////////////////////////////////////////
1546
1548{
1549 ::Obsolete("TString::GetInitialCapacity", "v5-30-00", "v5-32-00");
1550 return 15;
1551}
1552
1553////////////////////////////////////////////////////////////////////////////////
1554
1556{
1557 ::Obsolete("TString::GetResizeIncrement", "v5-30-00", "v5-32-00");
1558 return 16;
1559}
1560
1561////////////////////////////////////////////////////////////////////////////////
1562
1564{
1565 ::Obsolete("TString::GetMaxWaste", "v5-30-00", "v5-32-00");
1566 return 15;
1567}
1568
1569////////////////////////////////////////////////////////////////////////////////
1570/// Set default initial capacity for all TStrings. Default is 15.
1571
1573{
1574 ::Obsolete("TString::InitialCapacity", "v5-30-00", "v5-32-00");
1575 return 15;
1576}
1577
1578////////////////////////////////////////////////////////////////////////////////
1579/// Set default resize increment for all TStrings. Default is 16.
1580
1582{
1583 ::Obsolete("TString::ResizeIncrement", "v5-30-00", "v5-32-00");
1584 return 16;
1585}
1586
1587////////////////////////////////////////////////////////////////////////////////
1588/// Set maximum space that may be wasted in a string before doing a resize.
1589/// Default is 15.
1590
1592{
1593 ::Obsolete("TString::MaxWaste", "v5-30-00", "v5-32-00");
1594 return 15;
1595}
1596
1597/** \class TSubString
1598A zero length substring is legal. It can start
1599at any character. It is considered to be "pointing"
1600to just before the character.
1601
1602A "null" substring is a zero length substring that
1603starts with the nonsense index kNPOS. It can
1604be detected with the member function IsNull().
1605*/
1606
1607////////////////////////////////////////////////////////////////////////////////
1608/// Private constructor.
1609
1610TSubString::TSubString(const TString &str, Ssiz_t start, Ssiz_t nextent)
1611 : fStr((TString&)str), fBegin(start), fExtent(nextent)
1612{
1613}
1614
1615////////////////////////////////////////////////////////////////////////////////
1616/// Return sub-string of string starting at start with length len.
1617
1619{
1620 if (start < Length() && len > 0) {
1621 if (start+len > Length())
1622 len = Length() - start;
1623 } else {
1624 start = kNPOS;
1625 len = 0;
1626 }
1627 return TSubString(*this, start, len);
1628}
1629
1630////////////////////////////////////////////////////////////////////////////////
1631/// Returns a substring matching "pattern", or the null substring
1632/// if there is no such match. It would be nice if this could be yet another
1633/// overloaded version of operator(), but this would result in a type
1634/// conversion ambiguity with operator(Ssiz_t, Ssiz_t).
1635
1636TSubString TString::SubString(const char *pattern, Ssiz_t startIndex,
1637 ECaseCompare cmp) const
1638{
1639 Ssiz_t len = pattern ? strlen(pattern) : 0;
1640 Ssiz_t i = Index(pattern, len, startIndex, cmp);
1641 return TSubString(*this, i, i == kNPOS ? 0 : len);
1642}
1643
1644////////////////////////////////////////////////////////////////////////////////
1645/// Return character at pos i from sub-string. Check validity of i.
1646
1648{
1649 AssertElement(i);
1650 return fStr(fBegin+i);
1651}
1652
1653////////////////////////////////////////////////////////////////////////////////
1654/// Return character at pos i from sub-string. No check on i.
1655
1657{
1658 return fStr(fBegin+i);
1659}
1660
1661////////////////////////////////////////////////////////////////////////////////
1662/// Assign string to sub-string.
1663
1665{
1666 if (!IsNull())
1667 fStr.Replace(fBegin, fExtent, str.Data(), str.Length());
1668
1669 return *this;
1670}
1671
1672////////////////////////////////////////////////////////////////////////////////
1673/// Assign char* to sub-string.
1674
1676{
1677 if (!IsNull())
1678 fStr.Replace(fBegin, fExtent, cs, cs ? strlen(cs) : 0);
1679
1680 return *this;
1681}
1682
1683////////////////////////////////////////////////////////////////////////////////
1684/// Compare sub-string to char *.
1685
1686Bool_t operator==(const TSubString& ss, const char *cs)
1687{
1688 if (ss.IsNull()) return *cs =='\0'; // Two null strings compare equal
1689
1690 const char* data = ss.fStr.Data() + ss.fBegin;
1691 Ssiz_t i;
1692 for (i = 0; cs[i]; ++i)
1693 if (cs[i] != data[i] || i == ss.fExtent) return kFALSE;
1694 return (i == ss.fExtent);
1695}
1696
1697////////////////////////////////////////////////////////////////////////////////
1698/// Compare sub-string to string.
1699
1701{
1702 if (ss.IsNull()) return s.IsNull(); // Two null strings compare equal.
1703 if (ss.fExtent != s.Length()) return kFALSE;
1704 return !memcmp(ss.fStr.Data() + ss.fBegin, s.Data(), ss.fExtent);
1705}
1706
1707////////////////////////////////////////////////////////////////////////////////
1708/// Compare two sub-strings.
1709
1711{
1712 if (s1.IsNull()) return s2.IsNull();
1713 if (s1.fExtent != s2.fExtent) return kFALSE;
1714 return !memcmp(s1.fStr.Data()+s1.fBegin, s2.fStr.Data()+s2.fBegin,
1715 s1.fExtent);
1716}
1717
1718////////////////////////////////////////////////////////////////////////////////
1719/// Convert sub-string to lower-case.
1720
1722{
1723 if (!IsNull()) { // Ignore null substrings
1724 char *p = fStr.GetPointer() + fBegin;
1725 Ssiz_t n = fExtent;
1726 while (n--) { *p = tolower((unsigned char)*p); p++;}
1727 }
1728}
1729
1730////////////////////////////////////////////////////////////////////////////////
1731/// Convert sub-string to upper-case.
1732
1734{
1735 if (!IsNull()) { // Ignore null substrings
1736 char *p = fStr.GetPointer() + fBegin;
1737 Ssiz_t n = fExtent;
1738 while (n--) { *p = toupper((unsigned char)*p); p++;}
1739 }
1740}
1741
1742////////////////////////////////////////////////////////////////////////////////
1743/// Output error message.
1744
1746{
1747 Error("TSubString::SubStringError",
1748 "out of bounds: start = %d, n = %d, sr = %d", start, n, sr);
1749}
1750
1751////////////////////////////////////////////////////////////////////////////////
1752/// Check to make sure a sub-string index is in range.
1753
1755{
1756 if (i == kNPOS || i >= Length())
1757 Error("TSubString::AssertElement",
1758 "out of bounds: i = %d, Length = %d", i, Length());
1759}
1760
1761////////////////////////////////////////////////////////////////////////////////
1762/// Returns true if all characters in string are ascii.
1763
1765{
1766 const char *cp = Data();
1767 for (Ssiz_t i = 0; i < Length(); ++i)
1768 if (cp[i] & ~0x7F)
1769 return kFALSE;
1770 return kTRUE;
1771}
1772
1773////////////////////////////////////////////////////////////////////////////////
1774/// Returns true if all characters in string are alphabetic.
1775/// Returns false in case string length is 0.
1776
1778{
1779 const char *cp = Data();
1780 Ssiz_t len = Length();
1781 if (len == 0) return kFALSE;
1782 for (Ssiz_t i = 0; i < len; ++i)
1783 if (!isalpha(cp[i]))
1784 return kFALSE;
1785 return kTRUE;
1786}
1787
1788////////////////////////////////////////////////////////////////////////////////
1789/// Returns true if all characters in string are alphanumeric.
1790/// Returns false in case string length is 0.
1791
1793{
1794 const char *cp = Data();
1795 Ssiz_t len = Length();
1796 if (len == 0) return kFALSE;
1797 for (Ssiz_t i = 0; i < len; ++i)
1798 if (!isalnum(cp[i]))
1799 return kFALSE;
1800 return kTRUE;
1801}
1802
1803////////////////////////////////////////////////////////////////////////////////
1804/// Returns true if all characters in string are digits (0-9) or white spaces,
1805/// i.e. "123456" and "123 456" are both valid integer strings.
1806/// Returns false in case string length is 0 or string contains other
1807/// characters or only whitespace.
1808
1810{
1811 const char *cp = Data();
1812 Ssiz_t len = Length();
1813 if (len == 0) return kFALSE;
1814 Int_t b = 0, d = 0;
1815 for (Ssiz_t i = 0; i < len; ++i) {
1816 if (cp[i] != ' ' && !isdigit(cp[i])) return kFALSE;
1817 if (cp[i] == ' ') b++;
1818 if (isdigit(cp[i])) d++;
1819 }
1820 if (b && !d)
1821 return kFALSE;
1822 return kTRUE;
1823}
1824
1825////////////////////////////////////////////////////////////////////////////////
1826/// Returns kTRUE if string contains a floating point or integer number.
1827/// Examples of valid formats are:
1828/// ~~~ {.cpp}
1829/// 64320
1830/// 64 320
1831/// 6 4 3 2 0
1832/// 6.4320 6,4320
1833/// 6.43e20 6.43E20 6,43e20
1834/// 6.43e-20 6.43E-20 6,43e-20, -6.43e+20
1835/// ~~~
1836
1838{
1839 //we first check if we have an integer, in this case, IsDigit() will be true straight away
1840 if (IsDigit()) return kTRUE;
1841
1842 TString tmp = *this;
1843 //now we look for occurrences of '.', ',', e', 'E', '+', '-' and replace each
1844 //with ' ', if it is a floating point, IsDigit() will then return kTRUE
1845
1846 tmp.ToLower();
1847 Ssiz_t pos = tmp.First('.');
1848 if (pos != kNPOS) tmp.Replace(pos, 1, " ", 1);
1849 pos = tmp.First(',');
1850 if (pos != kNPOS) tmp.Replace(pos, 1, " ", 1);
1851 pos = tmp.Index("e-");
1852 if (pos >= 1) tmp.Replace(pos, 2, " ", 1);
1853 pos = tmp.Index("e+");
1854 if (pos >= 1) tmp.Replace(pos, 2, " ", 1);
1855 pos = tmp.Index("e");
1856 if (pos >= 1) tmp.Replace(pos, 1, " ", 1);
1857 pos = tmp.First('-');
1858 if (pos == 0) tmp.Replace(pos, 1, " ", 1);
1859 pos = tmp.First('+');
1860 if (pos == 0) tmp.Replace(pos, 1, " ", 1);
1861
1862 //test if it is now uniquely composed of numbers
1863 return tmp.IsDigit();
1864}
1865
1866////////////////////////////////////////////////////////////////////////////////
1867/// Returns true if all characters in string are hexadecimal digits
1868/// (0-9,a-f,A-F). Returns false in case string length is 0 or string
1869/// contains other characters.
1870
1872{
1873 const char *cp = Data();
1874 Ssiz_t len = Length();
1875 if (len == 0) return kFALSE;
1876 for (Ssiz_t i = 0; i < len; ++i)
1877 if (!isxdigit(cp[i]))
1878 return kFALSE;
1879 return kTRUE;
1880}
1881
1882////////////////////////////////////////////////////////////////////////////////
1883/// Returns true if all characters in string are binary digits (0,1).
1884/// Returns false in case string length is 0 or string contains other
1885/// characters.
1886
1888{
1889 const char *cp = Data();
1890 Ssiz_t len = Length();
1891 if (len == 0) return kFALSE;
1892 for (Ssiz_t i = 0; i < len; ++i)
1893 if (cp[i] != '0' && cp[i] != '1')
1894 return kFALSE;
1895 return kTRUE;
1896}
1897
1898////////////////////////////////////////////////////////////////////////////////
1899/// Returns true if all characters in string are octal digits (0-7).
1900/// Returns false in case string length is 0 or string contains other
1901/// characters.
1902
1904{
1905 const char *cp = Data();
1906 Ssiz_t len = Length();
1907 if (len == 0) return kFALSE;
1908 for (Ssiz_t i = 0; i < len; ++i)
1909 if (!isdigit(cp[i]) || cp[i]=='8' || cp[i]=='9')
1910 return kFALSE;
1911 return kTRUE;
1912}
1913
1914////////////////////////////////////////////////////////////////////////////////
1915/// Returns true if all characters in string are decimal digits (0-9).
1916/// Returns false in case string length is 0 or string contains other
1917/// characters.
1918
1920{
1921 const char *cp = Data();
1922 Ssiz_t len = Length();
1923 if (len == 0) return kFALSE;
1924 for (Ssiz_t i = 0; i < len; ++i)
1925 if (!isdigit(cp[i]))
1926 return kFALSE;
1927 return kTRUE;
1928}
1929
1930////////////////////////////////////////////////////////////////////////////////
1931/// Returns true if all characters in string are expressed in the base
1932/// specified (range=2-36), i.e. {0,1} for base 2, {0-9,a-f,A-F} for base 16,
1933/// {0-9,a-z,A-Z} for base 36. Returns false in case string length is 0 or
1934/// string contains other characters.
1935
1937{
1938 if (base < 2 || base > 36) {
1939 Error("TString::IsInBaseN", "base %d is not supported. Supported bases are {2,3,...,36}.", base);
1940 return kFALSE;
1941 }
1942 if (Length() == 0) {
1943 Error("TString::IsInBaseN", "input string is empty.") ;
1944 return kFALSE;
1945 }
1946 TString str = TString(Data()) ;
1947 str.ToUpper() ;
1948 TString str_ref0 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
1949 TString str_ref = str_ref0 ;
1950 str_ref.Remove(base) ;
1951 Bool_t isInBase = kTRUE ;
1952 for (Int_t k = 0; k < str.Length(); k++) {
1953 if (! str_ref.Contains(str[k])) {
1954 isInBase = kFALSE ;
1955 break ;
1956 }
1957 }
1958 return (isInBase);
1959}
1960
1961////////////////////////////////////////////////////////////////////////////////
1962/// Return integer value of string.
1963/// Valid strings include only digits and whitespace (see IsDigit()),
1964/// i.e. "123456", "123 456" and "1 2 3 4 56" are all valid
1965/// integer strings whose Atoi() value is 123456.
1966
1968{
1969 //any whitespace ?
1970 Int_t end = Index(" ");
1971 //if no white spaces in string, just use atoi()
1972 if (end == -1) return atoi(Data());
1973 //make temporary string, removing whitespace
1974 Int_t start = 0;
1975 TString tmp;
1976 //loop over all whitespace
1977 while (end > -1) {
1978 tmp += (*this)(start, end-start);
1979 start = end+1; end = Index(" ", start);
1980 }
1981 //finally add part from last whitespace to end of string
1982 end = Length();
1983 tmp += (*this)(start, end-start);
1984 return atoi(tmp.Data());
1985}
1986
1987////////////////////////////////////////////////////////////////////////////////
1988/// Return long long value of string.
1989/// Valid strings include only digits and whitespace (see IsDigit()),
1990/// i.e. "123456", "123 456" and "1 2 3 4 56" are all valid
1991/// integer strings whose Atoll() value is 123456.
1992
1994{
1995 //any whitespace ?
1996 Int_t end = Index(" ");
1997 //if no white spaces in string, just use atoi()
1998#ifndef R__WIN32
1999 if (end == -1) return atoll(Data());
2000#else
2001 if (end == -1) return _atoi64(Data());
2002#endif
2003 //make temporary string, removing whitespace
2004 Int_t start = 0;
2005 TString tmp;
2006 //loop over all whitespace
2007 while (end > -1) {
2008 tmp += (*this)(start, end-start);
2009 start = end+1; end = Index(" ", start);
2010 }
2011 //finally add part from last whitespace to end of string
2012 end = Length();
2013 tmp += (*this)(start, end-start);
2014#ifndef R__WIN32
2015 return atoll(tmp.Data());
2016#else
2017 return _atoi64(tmp.Data());
2018#endif
2019}
2020
2021////////////////////////////////////////////////////////////////////////////////
2022/// Return floating-point value contained in string.
2023/// Examples of valid strings are:
2024/// ~~~ {.cpp}
2025/// 64320
2026/// 64 320
2027/// 6 4 3 2 0
2028/// 6.4320 6,4320
2029/// 6.43e20 6.43E20 6,43e20
2030/// 6.43e-20 6.43E-20 6,43e-20
2031/// ~~~
2032
2034{
2035 //look for a comma and some whitespace
2036 Int_t comma = Index(",");
2037 Int_t end = Index(" ");
2038 //if no commas & no whitespace in string, just use atof()
2039 if (comma == -1 && end == -1) return atof(Data());
2040 TString tmp = *this;
2041 if (comma > -1) {
2042 //replace comma with decimal point
2043 tmp.Replace(comma, 1, ".");
2044 }
2045 //no whitespace ?
2046 if (end == -1) return atof(tmp.Data());
2047 //remove whitespace
2048 Int_t start = 0;
2049 TString tmp2;
2050 while (end > -1) {
2051 tmp2 += tmp(start, end-start);
2052 start = end+1; end = tmp.Index(" ", start);
2053 }
2054 end = tmp.Length();
2055 tmp2 += tmp(start, end-start);
2056 return atof(tmp2.Data());
2057}
2058
2059////////////////////////////////////////////////////////////////////////////////
2060/// Converts an Int_t to a TString with respect to the base specified (2-36).
2061/// Thus it is an enhanced version of sprintf (adapted from versions 0.4 of
2062/// http://www.jb.man.ac.uk/~slowe/cpp/itoa.html).
2063/// Usage: the following statement produce the same output, namely "1111"
2064/// ~~~ {.cpp}
2065/// std::cout << TString::Itoa(15,2) ;
2066/// std::cout << TString::Itoa(0xF,2) ; /// 0x prefix to handle hex
2067/// std::cout << TString::Itoa(017,2) ; /// 0 prefix to handle oct
2068/// ~~~
2069/// In case of error returns the "!" string.
2070
2072{
2073 std::string buf;
2074 // check that the base if valid
2075 if (base < 2 || base > 36) {
2076 Error("TString::Itoa", "base %d is not supported. Supported bases are {2,3,...,36}.",base) ;
2077 return (TString("!"));
2078 }
2079 buf.reserve(35); // Pre-allocate enough space (35=kMaxDigits)
2080 Int_t quotient = value;
2081 // Translating number to string with base:
2082 do {
2083 buf += "0123456789abcdefghijklmnopqrstuvwxyz"[ TMath::Abs(quotient % base) ];
2084 quotient /= base;
2085 } while (quotient);
2086 // Append the negative sign
2087 if (value < 0) buf += '-';
2088 std::reverse(buf.begin(), buf.end());
2089 return (TString(buf.data()));
2090}
2091
2092////////////////////////////////////////////////////////////////////////////////
2093/// Converts a UInt_t (twice the range of an Int_t) to a TString with respect
2094/// to the base specified (2-36). Thus it is an enhanced version of sprintf
2095/// (adapted from versions 0.4 of http://www.jb.man.ac.uk/~slowe/cpp/itoa.html).
2096/// In case of error returns the "!" string.
2097
2099{
2100 std::string buf;
2101 // check that the base if valid
2102 if (base < 2 || base > 36) {
2103 Error("TString::UItoa", "base %d is not supported. Supported bases are {2,3,...,36}.",base);
2104 return (TString("!"));
2105 }
2106 buf.reserve(35); // Pre-allocate enough space (35=kMaxDigits)
2107 UInt_t quotient = value;
2108 // Translating number to string with base:
2109 do {
2110 buf += "0123456789abcdefghijklmnopqrstuvwxyz"[ quotient % base ];
2111 quotient /= base;
2112 } while (quotient);
2113 std::reverse(buf.begin(), buf.end());
2114 return (TString(buf.data()));
2115}
2116
2117////////////////////////////////////////////////////////////////////////////////
2118/// Converts a Long64_t to a TString with respect to the base specified (2-36).
2119/// Thus it is an enhanced version of sprintf (adapted from versions 0.4 of
2120/// http://www.jb.man.ac.uk/~slowe/cpp/itoa.html).
2121/// In case of error returns the "!" string.
2122
2124{
2125 std::string buf;
2126 // check that the base if valid
2127 if (base < 2 || base > 36) {
2128 Error("TString::LLtoa", "base %d is not supported. Supported bases are {2,3,...,36}.",base);
2129 return (TString("!"));
2130 }
2131 buf.reserve(35); // Pre-allocate enough space (35=kMaxDigits)
2132 Long64_t quotient = value;
2133 // Translating number to string with base:
2134 do {
2135 buf += "0123456789abcdefghijklmnopqrstuvwxyz"[ TMath::Abs(quotient % base) ];
2136 quotient /= base;
2137 } while (quotient);
2138 // Append the negative sign
2139 if (value < 0) buf += '-';
2140 std::reverse(buf.begin(), buf.end());
2141 return (TString(buf.data()));
2142}
2143
2144////////////////////////////////////////////////////////////////////////////////
2145/// Converts a ULong64_t (twice the range of an Long64_t) to a TString with
2146/// respect to the base specified (2-36). Thus it is an enhanced version of
2147/// sprintf (adapted from versions 0.4 of http://www.jb.man.ac.uk/~slowe/cpp/itoa.html).
2148/// In case of error returns the "!" string.
2149
2151{
2152 std::string buf;
2153 // check that the base if valid
2154 if (base < 2 || base > 36) {
2155 Error("TString::ULLtoa", "base %d is not supported. Supported bases are {2,3,...,36}.",base);
2156 return (TString("!"));
2157 }
2158 buf.reserve(35); // Pre-allocate enough space (35=kMaxDigits)
2159 ULong64_t quotient = value;
2160 // Translating number to string with base:
2161 do {
2162 buf += "0123456789abcdefghijklmnopqrstuvwxyz"[ quotient % base ];
2163 quotient /= base;
2164 } while (quotient);
2165 std::reverse(buf.begin(), buf.end());
2166 return (TString(buf.data()));
2167}
2168
2169////////////////////////////////////////////////////////////////////////////////
2170/// Converts string from base base_in to base base_out. Supported bases
2171/// are 2-36. At most 64 bit data can be converted.
2172
2173TString TString::BaseConvert(const TString& s_in, Int_t base_in, Int_t base_out)
2174{
2175 TString s_out = "!" ; // return value in case of issue
2176 // checking base range
2177 if (base_in < 2 || base_in > 36 || base_out < 2 || base_out > 36) {
2178 Error("TString::BaseConvert", "only bases 2-36 are supported (base_in=%d, base_out=%d).", base_in, base_out);
2179 return (s_out);
2180 }
2181 // cleaning s_in
2182 TString s_in_ = s_in;
2183 Bool_t isSigned = kFALSE;
2184 if (s_in_[0] == '-') {
2185 isSigned = kTRUE;
2186 s_in_.Remove(0, 1);
2187 }
2188 if (!isSigned && s_in_[0] == '+') s_in_.Remove(0, 1); // !isSigned to avoid strings beginning with "-+"
2189 if (base_in == 16 && s_in_.BeginsWith("0x")) s_in_.Remove(0, 2); // removing hex prefix if any
2190 s_in_ = TString(s_in_.Strip(TString::kLeading, '0')); // removing leading zeros (necessary for length comparison below)
2191 if (!s_in_.Length()) s_in_ += '0';
2192 // checking s_in_ is expressed in the mentioned base
2193 if (!s_in_.IsInBaseN(base_in)) {
2194 Error("TString::BaseConvert", "s_in=\"%s\" is not in base %d", s_in.Data(), base_in);
2195 return (s_out);
2196 }
2197 // checking s_in <= 64 bits
2198 TString s_max = TString::ULLtoa(18446744073709551615ULL, base_in);
2199 if (s_in_.Length() > s_max.Length()) {
2200 // string comparison (s_in_>s_max) does not take care of length
2201 Error("TString::BaseConvert", "s_in=\"%s\" > %s = 2^64-1 in base %d.", s_in.Data(), s_max.Data(), base_in);
2202 return (s_out);
2203 } else if (s_in_.Length() == s_max.Length()) {
2204 // if ( s_in_.Length() < s_max.Length() ) everything's fine
2205 s_in_.ToLower(); // s_max is lower case
2206 if (s_in_ > s_max) {
2207 // string comparison
2208 Error("TString::BaseConvert", "s_in=\"%s\" > %s = 2^64-1 in base %d.", s_in.Data(), s_max.Data(), base_in);
2209 return (s_out);
2210 }
2211 }
2212
2213 // computing s_out
2214 ULong64_t i = ULong64_t(strtoull(s_in.Data(), nullptr, base_in));
2215 s_out = TString::ULLtoa(i, base_out);
2216 if (isSigned) s_out.Prepend("-");
2217 return (s_out);
2218}
2219
2220////////////////////////////////////////////////////////////////////////////////
2221/// Return true if string ends with the specified string.
2222
2223Bool_t TString::EndsWith(const char *s, ECaseCompare cmp) const
2224{
2225 if (!s) return kTRUE;
2226
2227 Ssiz_t l = strlen(s);
2228 if (l > Length()) return kFALSE;
2229 const char *s2 = Data() + Length() - l;
2230
2231 if (cmp == kExact)
2232 return strcmp(s, s2) == 0;
2233 return strcasecmp(s, s2) == 0;
2234}
2235
2236////////////////////////////////////////////////////////////////////////////////
2237/// This function is used to isolate sequential tokens in a TString.
2238/// These tokens are separated in the string by at least one of the
2239/// characters in delim. The returned array contains the tokens
2240/// as TObjString's. The returned array is the owner of the objects,
2241/// and must be deleted by the user.
2242
2244{
2245 std::list<Int_t> splitIndex;
2246
2247 Int_t i, start, nrDiff = 0;
2248 for (i = 0; i < delim.Length(); i++) {
2249 start = 0;
2250 while (start < Length()) {
2251 Int_t pos = Index(delim(i), start);
2252 if (pos == kNPOS) break;
2253 splitIndex.push_back(pos);
2254 start = pos + 1;
2255 }
2256 if (start > 0) nrDiff++;
2257 }
2258 splitIndex.push_back(Length());
2259
2260 if (nrDiff > 1)
2261 splitIndex.sort();
2262
2263 TObjArray *arr = new TObjArray();
2264 arr->SetOwner();
2265
2266 start = -1;
2267 std::list<Int_t>::const_iterator it;
2268#ifndef R__HPUX
2269 for (it = splitIndex.begin(); it != splitIndex.end(); ++it) {
2270#else
2271 for (it = splitIndex.begin(); it != (std::list<Int_t>::const_iterator) splitIndex.end(); ++it) {
2272#endif
2273 Int_t stop = *it;
2274 if (stop - 1 >= start + 1) {
2275 TString tok = (*this)(start+1, stop-start-1);
2276 TObjString *objstr = new TObjString(tok);
2277 arr->Add(objstr);
2278 }
2279 start = stop;
2280 }
2281
2282 return arr;
2283}
2284
2285////////////////////////////////////////////////////////////////////////////////
2286/// Formats a string using a printf style format descriptor.
2287/// Existing string contents will be overwritten.
2288
2289void TString::FormImp(const char *fmt, va_list ap)
2290{
2291 Ssiz_t buflen = 20 + 20 * strlen(fmt); // pick a number, any strictly positive number
2292 Clobber(buflen);
2293
2294 va_list sap;
2295 R__VA_COPY(sap, ap);
2296
2297 int n, vc = 0;
2298again:
2299 n = vsnprintf(GetPointer(), buflen, fmt, ap);
2300 // old vsnprintf's return -1 if string is truncated new ones return
2301 // total number of characters that would have been written
2302 if (n == -1 || n >= buflen) {
2303 if (n == -1)
2304 buflen *= 2;
2305 else
2306 buflen = n+1;
2307 Clobber(buflen);
2308 va_end(ap);
2309 R__VA_COPY(ap, sap);
2310 vc = 1;
2311 goto again;
2312 }
2313 va_end(sap);
2314 if (vc)
2315 va_end(ap);
2316
2317 SetSize(strlen(Data()));
2318}
2319
2320////////////////////////////////////////////////////////////////////////////////
2321/// Formats a string using a printf style format descriptor.
2322/// Existing string contents will be overwritten.
2323/// See also the static version TString::Format
2324/// ~~~ {.cpp}
2325/// TString formatted;
2326/// formatted.Form("%s in <%s>: %s", type, location, msg);
2327///
2328/// lines.emplace_back(TString::Format("Welcome to ROOT %s%%shttp://root.cern.ch",
2329/// gROOT->GetVersion()));
2330/// ~~~
2331///
2332/// Note: this is not to be confused with ::Format and ::Form (in the global namespace)
2333/// which returns a const char* and relies on a thread-local static character buffer.
2334
2335void TString::Form(const char *va_(fmt), ...)
2336{
2337 va_list ap;
2338 va_start(ap, va_(fmt));
2339 FormImp(va_(fmt), ap);
2340 va_end(ap);
2341}
2342
2343////////////////////////////////////////////////////////////////////////////////
2344/// Static method which formats a string using a printf style format
2345/// descriptor and return a TString. Similar to TString::Form() but it is
2346/// not needed to first create a TString.
2347/// ~~~ {.cpp}
2348/// lines.emplace_back(TString::Format("Welcome to ROOT %s%%shttp://root.cern.ch",
2349/// gROOT->GetVersion()));
2350/// TString formatted;
2351/// formatted.Form("%s in <%s>: %s", type, location, msg);
2352/// ~~~
2353///
2354/// Note: this is not to be confused with ::Format and ::Form (in the global namespace)
2355/// which returns a const char* and relies on a thread-local static character buffer.
2356
2357TString TString::Format(const char *va_(fmt), ...)
2358{
2359 va_list ap;
2360 va_start(ap, va_(fmt));
2361 TString str;
2362 str.FormImp(va_(fmt), ap);
2363 va_end(ap);
2364 return str;
2365}
2366
2367//---- Global String Handling Functions ----------------------------------------
2368
2369////////////////////////////////////////////////////////////////////////////////
2370/// Format a string in a formatting buffer (using a printf style
2371/// format descriptor).
2372
2373static char *SlowFormat(const char *format, va_list ap, int hint)
2374{
2375 static const int fld_size = 2048;
2376 TTHREAD_TLS(char*) slowBuffer(nullptr);
2377 TTHREAD_TLS(int) slowBufferSize(0);
2378
2379 if (hint == -1) hint = fld_size;
2380 if (hint > slowBufferSize) {
2381 delete [] slowBuffer;
2382 slowBufferSize = 2 * hint;
2383 if (hint < 0 || slowBufferSize < 0) {
2384 slowBufferSize = 0;
2385 slowBuffer = nullptr;
2386 return nullptr;
2387 }
2388 slowBuffer = new char[slowBufferSize];
2389 }
2390
2391 va_list sap;
2392 R__VA_COPY(sap, ap);
2393
2394 int n = vsnprintf(slowBuffer, slowBufferSize, format, ap);
2395 // old vsnprintf's return -1 if string is truncated new ones return
2396 // total number of characters that would have been written
2397 if (n == -1 || n >= slowBufferSize) {
2398 if (n == -1) n = 2 * slowBufferSize;
2399 if (n == slowBufferSize) n++;
2400 if (n <= 0) {
2401 va_end(sap);
2402 return nullptr; // int overflow!
2403 }
2404 va_end(ap);
2405 R__VA_COPY(ap, sap);
2406 char *buf = SlowFormat(format, ap, n);
2407 va_end(sap);
2408 va_end(ap);
2409 return buf;
2410 }
2411
2412 va_end(sap);
2413
2414 return slowBuffer;
2415}
2416
2417////////////////////////////////////////////////////////////////////////////////
2418/// Format a string in a circular formatting buffer (using a printf style
2419/// format descriptor).
2420
2421static char *Format(const char *format, va_list ap)
2422{
2423 static const int cb_size = 4096;
2424 static const int fld_size = 2048;
2425
2426 // a circular formating buffer
2427 TTHREAD_TLS_ARRAY(char,cb_size,gFormbuf); // gFormbuf[cb_size]; // some slob for form overflow
2428 TTHREAD_TLS(char*) gBfree(nullptr);
2429 TTHREAD_TLS(char*) gEndbuf(nullptr);
2430
2431 if (gBfree == nullptr) {
2432 gBfree = gFormbuf;
2433 gEndbuf = &gFormbuf[cb_size-1];
2434 }
2435 char *buf = gBfree;
2436
2437 if (buf+fld_size > gEndbuf)
2438 buf = gFormbuf;
2439
2440 va_list sap;
2441 R__VA_COPY(sap, ap);
2442
2443 int n = vsnprintf(buf, fld_size, format, ap);
2444 // old vsnprintf's return -1 if string is truncated new ones return
2445 // total number of characters that would have been written
2446 if (n == -1 || n >= fld_size) {
2447 va_end(ap);
2448 R__VA_COPY(ap, sap);
2449 buf = SlowFormat(format, ap, n);
2450 va_end(sap);
2451 va_end(ap);
2452 return buf;
2453 }
2454
2455 va_end(sap);
2456
2457 gBfree = buf+n+1;
2458 return buf;
2459}
2460
2461////////////////////////////////////////////////////////////////////////////////
2462/// Formats a string in a circular formatting buffer. Removes the need to
2463/// create and delete short lived strings. Don't pass Form() pointers
2464/// from user code down to ROOT functions as the circular buffer may
2465/// be overwritten downstream. Use Form() results immediately or use
2466/// TString::Format() instead.
2467
2468char *Form(const char *va_(fmt), ...)
2469{
2470 va_list ap;
2471 va_start(ap,va_(fmt));
2472 char *b = Format(va_(fmt), ap);
2473 va_end(ap);
2474 return b;
2475}
2476
2477////////////////////////////////////////////////////////////////////////////////
2478/// Formats a string in a circular formatting buffer and prints the string.
2479/// Appends a newline. If gPrintViaErrorHandler is true it will print via the
2480/// currently active ROOT error handler.
2481
2482void Printf(const char *va_(fmt), ...)
2483{
2484 va_list ap;
2485 va_start(ap,va_(fmt));
2487 ErrorHandler(kPrint, nullptr, va_(fmt), ap);
2488 else {
2489 char *b = Format(va_(fmt), ap);
2490 printf("%s\n", b);
2491 fflush(stdout);
2492 }
2493 va_end(ap);
2494}
2495
2496////////////////////////////////////////////////////////////////////////////////
2497/// Strip leading and trailing c (blanks by default) from a string.
2498/// The returned string has to be deleted by the user.
2499
2500char *Strip(const char *s, char c)
2501{
2502 if (!s) return nullptr;
2503
2504 int l = strlen(s);
2505 char *buf = new char[l+1];
2506
2507 if (l == 0) {
2508 *buf = '\0';
2509 return buf;
2510 }
2511
2512 // get rid of leading c's
2513 const char *t1 = s;
2514 while (*t1 == c)
2515 t1++;
2516
2517 // get rid of trailing c's
2518 const char *t2 = s + l - 1;
2519 while (*t2 == c && t2 > s)
2520 t2--;
2521
2522 if (t1 > t2) {
2523 *buf = '\0';
2524 return buf;
2525 }
2526 strncpy(buf, t1, (Ssiz_t) (t2-t1+1));
2527 *(buf+(t2-t1+1)) = '\0';
2528
2529 return buf;
2530}
2531
2532////////////////////////////////////////////////////////////////////////////////
2533/// Duplicate the string str. The returned string has to be deleted by
2534/// the user.
2535
2536char *StrDup(const char *str)
2537{
2538 if (!str) return nullptr;
2539
2540 auto len = strlen(str)+1;
2541 char *s = new char[len];
2542 if (s) strlcpy(s, str, len);
2543
2544 return s;
2545}
2546
2547////////////////////////////////////////////////////////////////////////////////
2548/// Remove all blanks from the string str. The returned string has to be
2549/// deleted by the user.
2550
2551char *Compress(const char *str)
2552{
2553 if (!str) return nullptr;
2554
2555 const char *p = str;
2556 char *s, *s1 = new char[strlen(str)+1];
2557 s = s1;
2558
2559 while (*p) {
2560 if (*p != ' ')
2561 *s++ = *p;
2562 p++;
2563 }
2564 *s = '\0';
2565
2566 return s1;
2567}
2568
2569////////////////////////////////////////////////////////////////////////////////
2570/// Escape specchars in src with escchar and copy to dst.
2571
2572int EscChar(const char *src, char *dst, int dstlen, char *specchars,
2573 char escchar)
2574{
2575 const char *p;
2576 char *q, *end = dst+dstlen-1;
2577
2578 for (p = src, q = dst; *p && q < end; ) {
2579 if (strchr(specchars, *p)) {
2580 *q++ = escchar;
2581 if (q < end)
2582 *q++ = *p++;
2583 } else
2584 *q++ = *p++;
2585 }
2586 *q = '\0';
2587
2588 if (*p != 0)
2589 return -1;
2590 return q-dst;
2591}
2592
2593////////////////////////////////////////////////////////////////////////////////
2594/// Un-escape specchars in src from escchar and copy to dst.
2595
2596int UnEscChar(const char *src, char *dst, int dstlen, char *specchars, char)
2597{
2598 const char *p;
2599 char *q, *end = dst+dstlen-1;
2600
2601 for (p = src, q = dst; *p && q < end; ) {
2602 if (strchr(specchars, *p))
2603 p++;
2604 else
2605 *q++ = *p++;
2606 }
2607 *q = '\0';
2608
2609 if (*p != 0)
2610 return -1;
2611 return q-dst;
2612}
2613
2614#ifdef NEED_STRCASECMP
2615////////////////////////////////////////////////////////////////////////////////
2616/// Case insensitive string compare.
2617
2618int strcasecmp(const char *str1, const char *str2)
2619{
2620 return strncasecmp(str1, str2, str2 ? strlen(str2)+1 : 0);
2621}
2622
2623////////////////////////////////////////////////////////////////////////////////
2624/// Case insensitive string compare of n characters.
2625
2626int strncasecmp(const char *str1, const char *str2, Ssiz_t n)
2627{
2628 while (n > 0) {
2629 int c1 = *str1;
2630 int c2 = *str2;
2631
2632 if (isupper(c1))
2633 c1 = tolower(c1);
2634
2635 if (isupper(c2))
2636 c2 = tolower(c2);
2637
2638 if (c1 != c2)
2639 return c1 - c2;
2640
2641 str1++;
2642 str2++;
2643 n--;
2644 }
2645 return 0;
2646}
2647#endif
2648
2649////////////////////////////////////////////////////////////////////////////////
2650/// Print a TString in the cling interpreter:
2651
2652std::string cling::printValue(const TString* val) {
2653 TString s = TString::Format("\"%s\"[%d]", val->Data(), (int)val->Length());
2654 return s.Data();
2655}
2656
2657////////////////////////////////////////////////////////////////////////////////
2658/// Print a TString in the cling interpreter:
2659
2660std::string cling::printValue(const TSubString* val) {
2661 TString s = TString::Format("\"%.*s\"[%d]", (int)val->Length(), val->Data(), (int)val->Length());
2662 return s.Data();
2663}
2664
2665////////////////////////////////////////////////////////////////////////////////
2666/// Print a TString in the cling interpreter:
2667
2668std::string cling::printValue(const std::string_view* val) {
2669 std::string str(*val);
2670 TString s = TString::Format("\"%s\"[%d]", str.c_str(), (int)val->length());
2671 return s.Data();
2672}
void frombuf(char *&buf, Bool_t *x)
Definition Bytes.h:278
void tobuf(char *&buf, Bool_t x)
Definition Bytes.h:55
#define R__ALWAYS_INLINE
Definition RConfig.hxx:577
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
#define f(i)
Definition RSha256.hxx:104
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
#define s1(x)
Definition RSha256.hxx:91
#define h(i)
Definition RSha256.hxx:106
const Ssiz_t kNPOS
Definition RtypesCore.h:124
int Int_t
Definition RtypesCore.h:45
unsigned char UChar_t
Definition RtypesCore.h:38
int Ssiz_t
Definition RtypesCore.h:67
const Bool_t kFALSE
Definition RtypesCore.h:101
unsigned int UInt_t
Definition RtypesCore.h:46
unsigned long ULongptr_t
Definition RtypesCore.h:83
const ULong_t kBitsPerByte
Definition RtypesCore.h:123
long long Long64_t
Definition RtypesCore.h:80
unsigned long long ULong64_t
Definition RtypesCore.h:81
const Bool_t kTRUE
Definition RtypesCore.h:100
#define ClassImp(name)
Definition Rtypes.h:377
TBuffer & operator<<(TBuffer &buf, const Tmpl *obj)
Definition TBuffer.h:399
#define R__ASSERT(e)
Definition TError.h:118
void ErrorHandler(int level, const char *location, const char *fmt, std::va_list va)
General error handler function. It calls the user set error handler.
Definition TError.cxx:109
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
constexpr Int_t kPrint
Definition TError.h:43
void Obsolete(const char *function, const char *asOfVers, const char *removedFromVers)
Use this function to declare a function obsolete.
Definition TError.cxx:177
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:229
Bool_t gPrintViaErrorHandler
Definition TError.cxx:33
winID h TVirtualViewer3D TVirtualGLPainter p
winID h direct
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t format
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t nchar
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t src
float * q
const UInt_t kHashShift
Definition TString.cxx:69
uint64_t rotl64(uint64_t x, int8_t r)
Definition TString.cxx:714
TString ToLower(const TString &str)
Return a lower-case version of str.
Definition TString.cxx:1476
TString operator+(const TString &s, const char *cs)
Use the special concatenation constructor.
Definition TString.cxx:1504
#define ROTL64(x, y)
Definition TString.cxx:719
static int MemIsEqual(const char *p, const char *q, Ssiz_t n)
Returns false if strings are not equal.
Definition TString.cxx:879
TBuffer & operator>>(TBuffer &buf, TString *&s)
Read string from TBuffer. Function declared in ClassDef.
Definition TString.cxx:1440
#define BIG_CONSTANT(x)
Definition TString.cxx:720
Bool_t operator==(const TString &s1, const char *s2)
Compare TString with a char *.
Definition TString.cxx:1461
UInt_t Hash(const char *str)
Return a case-sensitive hash value (endian independent).
Definition TString.cxx:570
static char * SlowFormat(const char *format, va_list ap, int hint)
Format a string in a formatting buffer (using a printf style format descriptor).
Definition TString.cxx:2373
TString ToUpper(const TString &str)
Return an upper-case version of str.
Definition TString.cxx:1490
static UInt_t SwapInt(UInt_t x)
Definition TString.cxx:550
static void Mash(UInt_t &hash, UInt_t chars)
Utility used by Hash().
Definition TString.cxx:560
char * Compress(const char *str)
Remove all blanks from the string str.
Definition TString.cxx:2551
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:2596
void Printf(const char *fmt,...)
Formats a string in a circular formatting buffer and prints the string.
Definition TString.cxx:2482
char * StrDup(const char *str)
Duplicate the string str.
Definition TString.cxx:2536
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:2572
#define R__VA_COPY(to, from)
Definition Varargs.h:54
#define va_(arg)
Definition Varargs.h:41
Buffer base class used for serializing objects.
Definition TBuffer.h:43
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:81
void Streamer(void *obj, TBuffer &b, const TClass *onfile_class=nullptr) const
Definition TClass.h:603
void * New(ENewType defConstructor=kClassNew, Bool_t quiet=kFALSE) const
Return a pointer to a newly allocated object of this class.
Definition TClass.cxx:4978
TClass * IsA() const override
Definition TClass.h:614
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
This code implements the MD5 message-digest algorithm.
Definition TMD5.h:44
const char * AsString() const
Return message digest as string.
Definition TMD5.cxx:220
void Update(const UChar_t *buf, UInt_t len)
Update TMD5 object to reflect the concatenation of another buffer full of bytes.
Definition TMD5.cxx:108
void Final()
MD5 finalization, ends an MD5 message-digest operation, writing the the message digest and zeroizing ...
Definition TMD5.cxx:167
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
An array of TObjects.
Definition TObjArray.h:31
void Add(TObject *obj) override
Definition TObjArray.h:68
Collectable string class.
Definition TObjString.h:28
Basic string class.
Definition TString.h:139
TString Copy() const
Copy a string.
Definition TString.cxx:523
static TString UItoa(UInt_t value, Int_t base)
Converts a UInt_t (twice the range of an Int_t) to a TString with respect to the base specified (2-36...
Definition TString.cxx:2098
Ssiz_t Length() const
Definition TString.h:421
friend class TSubString
Definition TString.h:142
static TString LLtoa(Long64_t value, Int_t base)
Converts a Long64_t to a TString with respect to the base specified (2-36).
Definition TString.cxx:2123
Rep_t fRep
Definition TString.h:225
void SetShortSize(Ssiz_t s)
Definition TString.h:245
char & operator()(Ssiz_t i)
Definition TString.h:726
Bool_t IsDec() const
Returns true if all characters in string are decimal digits (0-9).
Definition TString.cxx:1919
Bool_t IsLong() const
Definition TString.h:240
void ToLower()
Change string to lower-case.
Definition TString.cxx:1171
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.
Definition TString.cxx:451
static Ssiz_t MaxWaste(Ssiz_t mw=15)
Set maximum space that may be wasted in a string before doing a resize.
Definition TString.cxx:1591
Int_t Atoi() const
Return integer value of string.
Definition TString.cxx:1967
void SetLongSize(Ssiz_t s)
Definition TString.h:248
static const Ssiz_t kNPOS
Definition TString.h:280
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition TString.cxx:2223
TSubString Strip(EStripType s=kTrailing, char c=' ') const
Return a substring of self stripped at beginning and/or end.
Definition TString.cxx:1152
TString()
TString default ctor.
Definition TString.cxx:88
Bool_t IsHex() const
Returns true if all characters in string are hexadecimal digits (0-9,a-f,A-F).
Definition TString.cxx:1871
Double_t Atof() const
Return floating-point value contained in string.
Definition TString.cxx:2033
TString & ReplaceSpecialCppChars()
Find special characters which are typically used in printf() calls and replace them by appropriate es...
Definition TString.cxx:1103
Bool_t IsFloat() const
Returns kTRUE if string contains a floating point or integer number.
Definition TString.cxx:1837
void Clear()
Clear string without changing its capacity.
Definition TString.cxx:1222
TSubString SubString(const char *pat, Ssiz_t start=0, ECaseCompare cmp=kExact) const
Returns a substring matching "pattern", or the null substring if there is no such match.
Definition TString.cxx:1636
TString & Replace(Ssiz_t pos, Ssiz_t n, const char *s)
Definition TString.h:694
Ssiz_t First(char c) const
Find first occurrence of a character c.
Definition TString.cxx:532
const char * Data() const
Definition TString.h:380
static TString * ReadString(TBuffer &b, const TClass *clReq)
Read TString object from buffer.
Definition TString.cxx:1341
Bool_t IsDigit() const
Returns true if all characters in string are digits (0-9) or white spaces, i.e.
Definition TString.cxx:1809
Bool_t MaybeRegexp() const
Returns true if string contains one of the regexp characters "^$.[]*+?".
Definition TString.cxx:946
static Ssiz_t ResizeIncrement(Ssiz_t ri=16)
Set default resize increment for all TStrings. Default is 16.
Definition TString.cxx:1581
UInt_t HashCase() const
Return a case-sensitive hash value (endian independent).
Definition TString.cxx:627
Bool_t IsOct() const
Returns true if all characters in string are octal digits (0-7).
Definition TString.cxx:1903
virtual ~TString()
Delete a TString.
Definition TString.cxx:247
Ssiz_t Capacity() const
Definition TString.h:368
static Ssiz_t GetMaxWaste()
Definition TString.cxx:1563
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:704
static Ssiz_t AdjustCapacity(Ssiz_t oldCap, Ssiz_t newCap)
Calculate a nice capacity greater than or equal to newCap.
Definition TString.cxx:1207
TString MD5() const
Return the MD5 digest for this string, in a string representation.
Definition TString.cxx:934
void Resize(Ssiz_t n)
Resize the string. Truncate or add blanks as necessary.
Definition TString.cxx:1141
@ kLeading
Definition TString.h:278
@ kTrailing
Definition TString.h:278
ECaseCompare
Definition TString.h:279
@ kExact
Definition TString.h:279
Bool_t IsAlpha() const
Returns true if all characters in string are alphabetic.
Definition TString.cxx:1777
UInt_t HashFoldCase() const
Return a case-insensitive hash value (endian independent).
Definition TString.cxx:656
Ssiz_t Last(char c) const
Find last occurrence of a character c.
Definition TString.cxx:925
void ToUpper()
Change string to upper case.
Definition TString.cxx:1184
Bool_t IsAscii() const
Returns true if all characters in string are ascii.
Definition TString.cxx:1764
TObjArray * Tokenize(const TString &delim) const
This function is used to isolate sequential tokens in a TString.
Definition TString.cxx:2243
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition TString.h:627
static Ssiz_t GetResizeIncrement()
Definition TString.cxx:1555
void SetLongCap(Ssiz_t s)
Definition TString.h:251
@ kAlignment
Definition TString.h:234
TString & Prepend(const char *cs)
Definition TString.h:673
Bool_t IsBin() const
Returns true if all characters in string are binary digits (0,1).
Definition TString.cxx:1887
void UnLink() const
Definition TString.h:265
Bool_t IsNull() const
Definition TString.h:418
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.
Definition TString.cxx:2173
static TString ULLtoa(ULong64_t value, Int_t base)
Converts a ULong64_t (twice the range of an Long64_t) to a TString with respect to the base specified...
Definition TString.cxx:2150
Int_t CountChar(Int_t c) const
Return number of times character c occurs in the string.
Definition TString.cxx:509
UInt_t Hash(ECaseCompare cmp=kExact) const
Return hash value.
Definition TString.cxx:671
static void WriteString(TBuffer &b, const TString *a)
Write TString object to buffer.
Definition TString.cxx:1407
virtual void FillBuffer(char *&buffer) const
Copy string into I/O buffer.
Definition TString.cxx:1289
TString & operator=(char s)
Assign character c to TString.
Definition TString.cxx:296
TString & Remove(Ssiz_t pos)
Definition TString.h:685
static Ssiz_t InitialCapacity(Ssiz_t ic=15)
Set default initial capacity for all TStrings. Default is 15.
Definition TString.cxx:1572
virtual void Streamer(TBuffer &)
Stream a string object.
Definition TString.cxx:1391
char * GetShortPointer()
Definition TString.h:256
TString & Append(const char *cs)
Definition TString.h:576
Bool_t IsInBaseN(Int_t base) const
Returns true if all characters in string are expressed in the base specified (range=2-36),...
Definition TString.cxx:1936
char * Init(Ssiz_t capacity, Ssiz_t nchar)
Private member function returning an empty string representation of size capacity and containing ncha...
Definition TString.cxx:256
Bool_t MaybeWildcard() const
Returns true if string contains one of the wildcard characters "[]*?".
Definition TString.cxx:958
void InitChar(char c)
Initialize a string with a single character.
Definition TString.cxx:149
static Ssiz_t MaxSize()
Definition TString.h:263
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2357
char * GetLongPointer()
Definition TString.h:254
static TString Itoa(Int_t value, Int_t base)
Converts an Int_t to a TString with respect to the base specified (2-36).
Definition TString.cxx:2071
virtual Int_t Sizeof() const
Returns size string will occupy on I/O buffer.
Definition TString.cxx:1380
void Clone(Ssiz_t nc)
Make self a distinct copy with capacity of at least tot, where tot cannot be smaller than the current...
Definition TString.cxx:1258
void SetSize(Ssiz_t s)
Definition TString.h:250
void Zero()
Definition TString.h:266
void SetLongPointer(char *p)
Definition TString.h:253
Ssiz_t GetLongSize() const
Definition TString.h:249
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2335
static TClass * Class()
static Ssiz_t GetInitialCapacity()
Definition TString.cxx:1547
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:636
void AssertElement(Ssiz_t nc) const
Check to make sure a string index is in range.
Definition TString.cxx:1197
virtual void ReadBuffer(char *&buffer)
Read string from I/O buffer.
Definition TString.cxx:1310
Bool_t IsAlnum() const
Returns true if all characters in string are alphanumeric.
Definition TString.cxx:1792
void FormImp(const char *fmt, va_list ap)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2289
char * GetPointer()
Definition TString.h:258
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:651
void Clobber(Ssiz_t nc)
Clear string and make sure it has a capacity of nc.
Definition TString.cxx:1230
static Ssiz_t Recommend(Ssiz_t s)
Definition TString.h:236
Long64_t Atoll() const
Return long long value of string.
Definition TString.cxx:1993
A zero length substring is legal.
Definition TString.h:85
TSubString(const TString &s, Ssiz_t start, Ssiz_t len)
Private constructor.
Definition TString.cxx:1610
TSubString & operator=(const char *s)
Assign char* to sub-string.
Definition TString.cxx:1675
Bool_t IsNull() const
Definition TString.h:129
void ToUpper()
Convert sub-string to upper-case.
Definition TString.cxx:1733
TString & fStr
Definition TString.h:95
void SubStringError(Ssiz_t, Ssiz_t, Ssiz_t) const
Output error message.
Definition TString.cxx:1745
Ssiz_t fBegin
Definition TString.h:96
char & operator[](Ssiz_t i)
Return character at pos i from sub-string. Check validity of i.
Definition TString.cxx:1647
Ssiz_t fExtent
Definition TString.h:97
void AssertElement(Ssiz_t i) const
Check to make sure a sub-string index is in range.
Definition TString.cxx:1754
void ToLower()
Convert sub-string to lower-case.
Definition TString.cxx:1721
const char * Data() const
Definition TString.h:738
char & operator()(Ssiz_t i)
Return character at pos i from sub-string. No check on i.
Definition TString.cxx:1656
Ssiz_t Length() const
Definition TString.h:122
return c1
Definition legend1.C:41
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
TH1F * h1
Definition legend1.C:5
return c2
Definition legend2.C:14
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Definition TMathBase.h:250
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition TMathBase.h:198
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:123
Definition first.py:1
RawStr_t fRaw
Definition TString.h:219
TLine l
Definition textangle.C:4
auto * t1
Definition textangle.C:20