-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdOucString.cc
1314 lines (1102 loc) · 34.7 KB
/
XrdOucString.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************/
/* */
/* X r d O u c S t r i n g . h h */
/* */
/* (c) 2005 F. Furano (INFN Padova), G. Ganis (CERN) */
/* */
/* This file is part of the XRootD software suite. */
/* */
/* XRootD is free software: you can redistribute it and/or modify it under */
/* the terms of the GNU Lesser General Public License as published by the */
/* Free Software Foundation, either version 3 of the License, or (at your */
/* option) any later version. */
/* */
/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
/* License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
/* */
/* The copyright holder's institutional names and contributor's names may not */
/* be used to endorse or promote products derived from this software without */
/* specific prior written permission of the institution or contributor. */
/* All Rights Reserved. See XrdInfo.cc for complete License Terms */
/******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <climits>
#include "XrdOuc/XrdOucString.hh"
/******************************************************************************/
/* */
/* Light string manipulation class */
/* */
/******************************************************************************/
#define kMAXINT64LEN 25
#if !defined(WINDOWS)
//
// Macro for 'form'-like operations
#define XOSINTFORM(f,b) \
int buf_len = 256; \
va_list ap; \
va_start(ap, f); \
again: \
b = (char *)realloc(b, buf_len); \
int n = vsnprintf(b, buf_len, f, ap); \
if (n == -1 || n >= buf_len) { \
if (n == -1) \
buf_len *= 2; \
else \
buf_len = n+1; \
va_end(ap); \
va_start(ap, f); \
goto again; \
} \
va_end(ap);
// End-Of-Macro for 'form'-like operations
#endif
// Default blksize for (re-)allocations; active if > 0.
// Use XrdOucString::setblksize() to activate
int XrdOucString::blksize = -1;
//________________________________________________________________________
int XrdOucString::adjust(int ls, int &j, int &k, int nmx)
{
// Check indeces and return effective length
// If nmx > 0, indecs are adjusted to get the effective length
// smaller than nmx.
// Check range for beginning
j = (j < 0) ? 0 : j;
// Check range for end
k = (k == -1 || k > (ls-1)) ? (ls-1) : k;
// The new length
int nlen = k - j + 1;
nlen = (nlen > 0) ? nlen : 0;
// Check max, if required
if (nmx > 0 && nmx < nlen) {
k = j + 1 + nmx;
nlen = nmx;
}
// We are done
return nlen;
}
//________________________________________________________________________
char *XrdOucString::bufalloc(int nsz)
{
// Makes sure that the internal capacity is enough to contain
// 'nsz' bytes (including the null-termination).
// Buffer is allocated if not yet existing or reallocated if
// necessary.
// If 'nsz' is negative or null, existing buffer is freed, if any
// Returns pointer to buffer.
char *nstr = 0;
// New size must be positive; if not, cleanup
if (nsz <= 0) {
if (str) free(str);
init();
return nstr;
}
int sz = nsz;
// Check the blksize option is activated
if (blksize > 1) {
int blks = nsz / blksize;
sz = (blks+1) * blksize;
}
// Resize, if different from what we have
if (sz != siz) {
if ((nstr = (char *)realloc(str, sz)))
siz = sz;
} else
// Do nothing
nstr = str;
// We are done
return nstr;
}
//___________________________________________________________________________
XrdOucString::XrdOucString(const char c, int ls)
{
// Constructor
// Create space to store char c as a null-terminated string.
// If ls > 0 create space for ls+1 bytes.
init();
// If required, allocate the buffer to the requested size
if (ls > 0)
str = bufalloc(ls+1);
else
str = bufalloc(2);
if (str) {
str[0] = c;
str[1] = 0;
len = 1;
}
}
//___________________________________________________________________________
XrdOucString::XrdOucString(const char *s, int ls)
{
// Constructor
// Create space to store the null terminated string s.
// If ls > 0 create space for ls+1 bytes, store the first
// ls bytes of s (truncating, if needed), and null-terminate.
// This is useful to import non null-terminated string buffers
// of known length.
init();
// If required, allocate the buffer to the requested size
if (ls > 0)
str = bufalloc(ls+1);
int lr = s ? strlen(s) : 0;
if (lr >= 0)
assign(s,0,ls-1);
}
//___________________________________________________________________________
XrdOucString::XrdOucString(const XrdOucString &s)
{
// Copy constructor
init();
assign(s.c_str(),0,-1);
}
//______________________________________________________________________________
XrdOucString::XrdOucString(const XrdOucString &s, int j, int k, int ls)
{
// Copy constructor (portion of string s: from j to k, inclusive)
init();
// If required, allocate the buffer to the requested size
if (ls > 0)
str = bufalloc(ls+1);
int lr = s.length();
if (lr > 0) {
// Adjust range (to fit in the allocated buffer, if any)
if (adjust(lr, j, k, ls) > 0)
// assign the string portion
assign(s.c_str(),j,k);
}
}
//___________________________________________________________________________
XrdOucString::~XrdOucString()
{
// Destructor
if (str) free(str);
}
//___________________________________________________________________________
void XrdOucString::setbuffer(char *buf)
{
// Adopt buffer 'buf'
if (str) free(str);
init();
if (buf) {
str = buf;
len = strlen(buf);
siz = len + 1;
str = (char *)realloc(str, siz);
}
}
#if !defined(WINDOWS)
//______________________________________________________________________________
int XrdOucString::form(const char *fmt, ...)
{
// Recreate the string according to 'fmt' and the arguments
// Return -1 in case of failure, or the new length.
// Decode the arguments
XOSINTFORM(fmt, str);
siz = buf_len;
// Re-adjust the length
len = strlen(str);
str = bufalloc(len+1);
// Return the new length (in n)
return n;
}
//______________________________________________________________________________
int XrdOucString::form(XrdOucString &str, const char *fmt, ...)
{
// Format a string in 'str' according to 'fmt' and the arguments
// Decode the arguments
char *buf = 0;
XOSINTFORM(fmt, buf);
// Adopt the new formatted buffer in the string
str.setbuffer(buf);
// Done
return n;
}
#endif
//______________________________________________________________________________
int XrdOucString::find(const char c, int start, bool forward)
{
// Find index of first occurence of char c starting from position start
// Return index if found, STR_NPOS if not.
int rc = STR_NPOS;
// STR_NPOS indicates start from the end
if (start == STR_NPOS)
start = len - 1;
// Make sure start makes sense
if (start < 0 || start > (len-1))
return rc;
// Now loop
int i = start;
if (forward) {
// forward search
for (; i < len; i++) {
if (str[i] == c)
return i;
}
} else {
// backward search
for (; i >= 0; i--) {
if (str[i] == c)
return i;
}
}
// Nothing found
return rc;
}
//______________________________________________________________________________
int XrdOucString::find(XrdOucString s, int start)
{
// Find index of first occurence of string s, starting
// from position start.
// Return index if found, STR_NPOS if not.
return find((const char *)s.c_str(),start);
}
//______________________________________________________________________________
int XrdOucString::find(const char *s, int start)
{
// Find index of first occurence of null-terminated string s, starting
// from position start.
// Return index if found, STR_NPOS if not.
int rc = STR_NPOS;
// Make sure start makes sense
if (start < 0 || start > (len-1))
return rc;
// Make sure the string is defined
if (!s)
return rc;
// length of substring
int ls = strlen(s);
// if only one meaningful char, use dedicated method
if (ls == 1)
return find(s[0],start);
// Make sure that it can fit
if (ls > (len-start))
return rc;
// Now loop
int i = start;
for (; i < len; i++) {
if (str[i] == s[0])
if (!strncmp(str+i+1,s+1,ls-1))
return i;
}
// Nothing found
return rc;
}
//______________________________________________________________________________
int XrdOucString::rfind(XrdOucString s, int start)
{
// Find index of first occurence of string s in backward
// direction starting from position start.
// If start == STR_NPOS, search starts from end of string (default).
// Return index if found, STR_NPOS if not.
return rfind(s.c_str(),start);
}
//______________________________________________________________________________
int XrdOucString::rfind(const char *s, int start)
{
// Find index of first occurence of null-terminated string s in
// backwards direction starting from position start.
// If start == STR_NPOS, search starts from end of string (default).
// Return index if found, STR_NPOS if not.
int rc = STR_NPOS;
// STR_NPOS indicates start from the end
if (start == STR_NPOS)
start = len - 1;
// Make sure start makes sense
if (start < 0 || start > (len-1))
return rc;
// Make sure the string is defined
if (!s)
return rc;
// length of substring
int ls = strlen(s);
// if only one meaningful char, use dedicated method
if (ls == 1)
return find(s[0],start,0);
// Make sure that it can fit
if (ls > len)
return rc;
// Start from the first meaningful position
if (ls > (len-start))
start = len-ls;
// Now loop
int i = start;
for (; i >= 0; i--) {
if (str[i] == s[0])
if (!strncmp(str+i+1,s+1,ls-1))
return i;
}
// Nothing found
return rc;
}
//______________________________________________________________________________
bool XrdOucString::endswith(char c)
{
// returns 1 if the stored string ends with string s
return ((len > 0) ? (rfind(c) == (int)(len-1)) : 0);
}
//______________________________________________________________________________
bool XrdOucString::endswith(const char *s)
{
// returns 1 if the stored string ends with string s
int ls = -1;
if (s && (ls = (int)strlen(s)) <= len)
return ((rfind(s) == (int)(len-strlen(s))) ? 1 : 0);
return 0;
}
//___________________________________________________________________________
int XrdOucString::matches(const char *s, char wch)
{
// Check if local string is compatible with 's' which may
// contain wild char wch (default: '*'). For example, if local string
// is 'mouse.at.home' the match will be true for 'mouse.*',
// and false for 'mouse.*.cinema' .
// If does not contain wild characters, this is just a comparison
// based on strncmp.
// Returns the number of characters matching or 0.
// Make sure s is defined and we have a local string
if (!s || !str)
return 0;
// string size
int ls = strlen(s);
// If no wild card, just make a simple strcmp comparison
if (!strchr(s,wch)) {
if (!strcmp(str,s))
return ls;
else
return 0;
}
// If s == wch the match is always true
if (ls == 1)
return 1;
int rc = 1;
// Starting position for the check
int cs = 0;
// token delimiters and size
int tb = 0; // begin
char *ps = (char *)strchr(s+tb,wch);
bool next = 1;
while (next) {
// token end
int te = ps ? (ps - s) : ls;
// token size
int ts = te - tb;
if (ts) {
bool found = 0;
while (cs < len) {
if (!strncmp(str+cs,s+tb,ts)) {
cs += ts;
found = 1;
break;
}
cs++;
}
if (!found) {
rc = 0;
break;
}
}
// next token begin, if any
tb = te + 1;
ps = (tb < ls) ? (char *)strchr(s+tb, wch) : 0;
next = (ps || (tb < ls)) ? 1 : 0;
}
// If s does not end with a wild card
// make sure that everything has been checked
if (s[ls-1] != wch && cs < len)
rc = 0;
// The number of chars matching is the number of chars in s
// which are not '*'
int nm = 0;
if (rc > 0) {
nm = ls;
int n = ls;
while (n--) {
if (s[n] == wch) nm--;
}
}
return nm;
}
//______________________________________________________________________________
void XrdOucString::assign(const char *s, int j, int k)
{
// Assign portion of buffer s to local string.
// For k == -1 assign all string starting from position j (inclusive).
// Use j == 0 and k == -1 to assign the full string.
int ls = s ? strlen(s) : 0;
if (!s) {
// We are passed an empty string
if (str) {
// empty the local string, leaving capacity as it is
str[0] = 0;
len = 0;
}
} else {
// Adjust range and get length of portion to copy
int nlen = adjust(ls, j, k);
// Resize, if needed
if (nlen > (siz-1))
str = bufalloc(nlen+1);
if (str) {
if (nlen > 0) {
strncpy(str,s+j,nlen);
str[nlen] = 0;
len = nlen;
} else {
// empty the local string, leaving capacity as it is
str[0] = 0;
len = 0;
}
}
}
}
//______________________________________________________________________________
void XrdOucString::assign(const XrdOucString s, int j, int k)
{
// Assign portion of buffer s to local string.
assign(s.c_str(),j,k);
}
//___________________________________________________________________________
int XrdOucString::keep(int start, int size)
{
// Keep size bytes starting from position start
// If size == 0, keep any bytes from start on.
// Return number of bytes kept ( <=size )
int rc = 0;
// Make sure start makes sense
int st = start;
if (st < 0 || st > (len-1))
return rc;
// Make sure size makes sense
if (size < 0)
return rc;
int nlen = 0;
if (size == 0) {
nlen = len - st;
} else {
nlen = (size > (len - st)) ? (len - st) : size;
}
// Do nothing if all the bytes requested
if (nlen >= len)
return len;
// Allocated new string
if (nlen > (siz-1))
str = bufalloc(nlen+1);
if (str) {
// Copy the bytes
memmove(str,str+st,nlen);
// Null terminate
str[nlen] = 0;
// Assign new string
len = nlen;
// Return number of bytes kept
return nlen;
} else
return rc;
}
//___________________________________________________________________________
void XrdOucString::append(const char *s)
{
// Append string pointed by s to local string.
// Memory is reallocated.
return insert(s);
}
//___________________________________________________________________________
void XrdOucString::append(const XrdOucString s)
{
// Append string s to local string.
// Memory is reallocated.
return insert(s);
}
//___________________________________________________________________________
void XrdOucString::append(const char c)
{
// Append char c to local string.
// Memory is reallocated.
return insert(c);
}
//___________________________________________________________________________
void XrdOucString::append(const int i)
{
// Append string representing integer i to local string.
return insert(i);
}
//___________________________________________________________________________
void XrdOucString::insert(const char *s, int start, int ls)
{
// Insert null-terminated string pointed by s in local string starting
// at position start (default append, i.e. start == len).
// Memory is reallocated.
// If ls > 0, insert only the first ls bytes of s
// Check start
int at = start;
at = (at < 0 || at > len) ? len : at;
if (s) {
int lstr = (ls > 0) ? ls : strlen(s);
if (str) {
int lnew = len + lstr;
if (lnew > (siz-1))
str = bufalloc(lnew+1);
if (str) {
// Move the rest of the existing string, if any
if (at < len)
memmove(str+at+lstr,str+at,(len-at));
// Add new string now
memcpy(str+at,s,lstr);
// Null termination
str[lnew] = 0;
len = lnew;
}
} else {
if ((str = bufalloc(lstr+1))) {
strncpy(str,s,lstr);
str[lstr] = 0;
len = lstr;
}
}
}
}
//___________________________________________________________________________
void XrdOucString::insert(const XrdOucString s, int start)
{
// Insert string s in local string starting at position start (default
// append, i.e. start == len).
return insert(s.c_str(), start);
}
//___________________________________________________________________________
void XrdOucString::insert(const char c, int start)
{
// Insert char c in local string starting at position start (default
// append, i.e. start == len).
char sc[2] = {0};
sc[0] = c;
return insert((const char *)&sc[0], start);
}
//___________________________________________________________________________
void XrdOucString::insert(const int i, int start)
{
// Insert string representing integer i in local string starting at
// position start (default
char si[kMAXINT64LEN] = {0};
sprintf(si,"%d",i);
return insert((const char *)&si[0], start);
}
//___________________________________________________________________________
int XrdOucString::replace(const XrdOucString s1, const char *s2, int from, int to)
{
// Replace any occurrence of s1 with s2 from position 'from' to position
// 'to' (inclusive).
// Return signed size of length modification (in bytes)
return replace(s1.c_str(),s2,from,to);
}
//___________________________________________________________________________
int XrdOucString::replace(const char *s1, const XrdOucString s2, int from, int to)
{
// Replace any occurrence of s1 with s2 from position 'from' to position
// 'to' (inclusive).
// Return signed size of length modification (in bytes)
return replace(s1,s2.c_str(),from,to);
}
//___________________________________________________________________________
int XrdOucString::replace(const XrdOucString s1,
const XrdOucString s2, int from, int to)
{
// Replace any occurrence of s1 with s2 from position 'from' to position
// 'to' (inclusive).
// Return signed size of length modification (in bytes)
return replace(s1.c_str(),s2.c_str(),from,to);
}
//___________________________________________________________________________
int XrdOucString::replace(const char *s1, const char *s2, int from, int to)
{
// Replace any occurrence of s1 with s2 from position 'from' to position
// 'to' (inclusive).
// Return signed size of length modification (in bytes)
// We must have something to replace
if (!str || len <= 0)
return 0;
// The string to replace must be defined and not empty
int l1 = s1 ? strlen(s1) : 0;
if (l1 <= 0)
return 0;
// Check and adjust indeces
if (adjust(len,from,to) <= 0)
return 0;
// length of replacing string
int l2 = s2 ? strlen(s2) : 0;
// If new string is longer we need number of occurencies
int nr = 0;
if (l1 < l2) {
int at = find(s1,from);
while (at > -1 && at <= (to-l1+1)) {
nr++;
at = find(s1,at+l1);
}
}
// New size
int nlen = (nr > 0) ? (len + nr*(l2-l1)) : len ;
// Reallocate, if needed
if (nlen > (siz-1))
str = bufalloc(nlen+1);
// Now act
int dd = l2-l1;
int dl = 0;
if (str) {
if (dd < 0) {
int nc = 0;
int at = find(s1,from);
while (at > -1 && at <= (to-l1+1)) {
int atn = find(s1,at+l1);
atn = (atn == -1 || atn > (to-l1+1)) ? len : atn;
int ln = atn - at - l1;
char *pc = str+at+nc*dd;
if (l2 > 0)
memcpy(pc,s2,l2);
if (ln > 0)
memmove(pc+l2,str+at+l1,ln);
nc++;
at = atn;
}
dl = nc*dd;
} else if (dd == 0) {
int at = find(s1,from);
while (at > -1 && at <= (to-l1+1)) {
memcpy(str+at,s2,l2);
at = find(s1,at+l1);
}
} else if (dd > 0) {
int nc = nr;
int at = rfind(s1,to);
int atn = len;
while (at > -1 && at >= from) {
int ln = atn - at - l1;
char *pc = str + at + l1 + nc*dd;
if (ln > 0)
memmove(pc,str+at+l1,ln);
if (l2 > 0)
memcpy(pc-l2,s2,l2);
nc--;
atn = at;
at = rfind(s1,at-l1);
}
dl = nr*dd;
}
}
// Variation of string length
len += dl;
// Insure null-termination
str[len] = 0;
// We are done
return dl;
}
//___________________________________________________________________________
int XrdOucString::erase(int start, int size)
{
// Remove size bytes starting from position start.
// If size == 0, remove any bytes from start on.
// Return number of bytes removed ( <=size )
int rc = 0;
// Make sure start makes sense
int st = start;
if (st < 0 || st > (len-1))
return rc;
// Make sure size makes sense
if (size < 0)
return rc;
int nrem = 0;
if (size == 0) {
nrem = len - st;
} else {
nrem = (size > (len-st)) ? (len-st) : size;
}
// Do nothing if no byte removal has been requested
if (nrem <= 0)
return rc;
// Calculate new length and allocated new string
int nlen = len - nrem;
// Copy the remaining bytes, if any
if (len-st-nrem)
memmove(str+st,str+st+nrem,len-st-nrem);
// Null terminate
str[nlen] = 0;
// Assign new length
len = nlen;
// Return number of bytes removed
return nrem;
}
//___________________________________________________________________________
int XrdOucString::erase(const char *s, int from, int to)
{
// Remove any occurence of string s within from and to inclusive.
// Use from == 0 and to == -1 to remove all occurences (default).
// Return number of bytes removed ( <=size )
return -replace(s,0,from,to);
}
//___________________________________________________________________________
int XrdOucString::erase(XrdOucString s, int from, int to)
{
// Remove any occurence of string s within from and to inclusive.
// Use from == 0 and to == -1 to remove all occurences (default).
// Return number of bytes removed ( <=size )
return -replace(s.c_str(),0,from,to);
}
//___________________________________________________________________________
void XrdOucString::lower(int start, int size)
{
// Set to lower case size chars starting from position start.
// If size == 0, lower all bytes from start on.
// Make sure start makes sense
int st = start;
if (st < 0 || st > (len-1))
return;
// Make sure size makes sense
if (size < 0)
return;
int nlw = 0;
if (size == 0) {
nlw = len - st;
} else {
nlw = (size > (len-st)) ? (len-st) : size;
}
// Do nothing if no byte removal has been requested
if (nlw <= 0)
return;
// Set to lower
int i = st;
for (; i < st + nlw ; i++ ) {
if (str[i] > 0x40 && str[i] < 0x5b)
str[i] += 0x20;
}
}
//___________________________________________________________________________
void XrdOucString::upper(int start, int size)
{
// Set to upper case size chars starting from position start.
// If size == 0, upper all bytes from start on.
// Make sure start makes sense
int st = start;
if (st < 0 || st > (len-1))
return;
// Make sure size makes sense
if (size < 0)
return;
int nup = 0;
if (size == 0) {
nup = len - st;
} else {
nup = (size > (len-st)) ? (len-st) : size;
}
// Do nothing if no byte removal has been requested
if (nup <= 0)
return;
// Set to upper
int i = st;
for (; i < st + nup ; i++ ) {
if (str[i] > 0x60 && str[i] < 0x7b)
str[i] -= 0x20;
}
}
//___________________________________________________________________________
void XrdOucString::hardreset()
{
// Reset string making sure to erase completely the information.
if (str) {
volatile char *buf = 0;
for (buf = (volatile char *)str; len; buf[--len] = 0) {}
len = 0;
}
len = 0;
}
//___________________________________________________________________________
void XrdOucString::reset(const char c, int j, int k)
{
// Reset string making sure to erase completely the information.
j = (j >= 0 && j < siz) ? j : 0;
k = (k >= j && k < siz) ? k : siz-1;
if (str) {
volatile char *buf = (volatile char *)str;
int i = j;
for (; i <= k; i++)
buf[i] = c;
}
while (str[len-1] == 0)
--len;
}
//______________________________________________________________________________
XrdOucString& XrdOucString::operator=(const int i)
{
// Assign string representing integer i to local string
char s[kMAXINT64LEN] = {0};
sprintf(s,"%d",i);
assign((const char *)&s[0],0,-1);
return *this;
}
//______________________________________________________________________________
XrdOucString& XrdOucString::operator=(const char c)
{
// Assign char c to local string.
const char s[] = {c,0};
assign(s,0,-1);
return *this;
}