xrootd
Loading...
Searching...
No Matches
XrdOucString.hh
Go to the documentation of this file.
1#ifndef __OUC_STRING_H__
2#define __OUC_STRING_H__
3/******************************************************************************/
4/* */
5/* X r d O u c S t r i n g . h h */
6/* */
7/* (c) 2005 F. Furano (INFN Padova), G. Ganis (CERN) */
8/* */
9/* This file is part of the XRootD software suite. */
10/* */
11/* XRootD is free software: you can redistribute it and/or modify it under */
12/* the terms of the GNU Lesser General Public License as published by the */
13/* Free Software Foundation, either version 3 of the License, or (at your */
14/* option) any later version. */
15/* */
16/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
17/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
18/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
19/* License for more details. */
20/* */
21/* You should have received a copy of the GNU Lesser General Public License */
22/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
23/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
24/* */
25/* The copyright holder's institutional names and contributor's names may not */
26/* be used to endorse or promote products derived from this software without */
27/* specific prior written permission of the institution or contributor. */
28/* All Rights Reserved. See XrdInfo.cc for complete License Terms */
29/******************************************************************************/
30
31/******************************************************************************/
32/* */
33/* Light string manipulation class */
34/* */
35/* This class has three private members: a buffer (char *) and two integers, */
36/* indicating the effective length of the null-terminated string (len), and */
37/* the buffer capacity (siz), i.e. the allocated size. The capacity is set */
38/* at construction either at the value needed by the initializing string or */
39/* to the value requested by the user; by default the capacity is never */
40/* decreased during manipulations (it is increased if required by the */
41/* operation). The capacity can be changed at any time by calling resize(). */
42/* The user can choose a granularity other than 1 to increase the capacity */
43/* by calling XrdOucString::setblksize(nbs) with nbs > 1: this will make */
44/* new allocations to happen in blocks multiple of nbs bytes. */
45/* */
46/* 1. Constructors */
47/* */
48/* XrdOucString(int lmx = 0) */
49/* - create an empty string; set capacity to lmx+1 if lmx > 0 */
50/* XrdOucString(const char *s, int lmx = 0) */
51/* - create a string containing s; capacity is set to strlen(s)+1 or */
52/* to lmx+1 if lmx > 0; in the latter case truncation may occur. */
53/* XrdOucString(const char c, int lmx = 0) */
54/* - create a string char c; capacity is set to 2 or to lmx+1 if lmx > 0.*/
55/* XrdOucString(const XrdOucString &s) */
56/* - create string copying from XrdOucString s . */
57/* XrdOucString(const XrdOucString &s, int j, int k = -1, int lmx = 0) */
58/* - create string copying a portion of XrdOucString s; portion is */
59/* defined by j to k inclusive; if k == -1 the portion copied will be */
60/* from j to end-of-string; if j or k are inconsistent they are taken */
61/* as 0 or len-1, respectively; capacity is set to k-j bytes or to */
62/* to lmx+1 if lmx > 0; in the latter case truncation of the portion */
63/* may occur. */
64/* */
65/* 2. Access to information */
66/* */
67/* const char *c_str() const */
68/* - return pointer to stored string */
69/* int length() const */
70/* - return length stored string */
71/* int capacity() const */
72/* - return capacity of the allocated buffer */
73/* */
74/* char &operator[](int j) */
75/* - array-like operator returning char at position j; abort is invoked */
76/* if j is not in the correct range */
77/* */
78/* int find(const char c, int start = 0, bool forward = 1); */
79/* - find first occurence of char c starting from position start in */
80/* forward (forward == 1, default) or backward (forward == 0) */
81/* direction; returns STR_NPOS if nothing is found */
82/* int find(const char *s, int start = 0) */
83/* - find first occurence of string s starting from position start in */
84/* forward direction; returns STR_NPOS if nothing is found */
85/* int find(XrdOucString s, int start = 0) */
86/* - find first occurence of XrdOucString s starting from position start */
87/* in forward direction; returns STR_NPOS if nothing is found */
88/* */
89/* int rfind(const char c, int start = STR_NPOS) */
90/* - find first occurence of char c starting from position start in */
91/* backward direction; returns STR_NPOS if nothing is found. */
92/* int rfind(const char *s, int start = STR_NPOS) */
93/* - find first occurence of string s starting from position start in */
94/* backward direction; returns STR_NPOS if nothing is found; */
95/* if start == STR_NPOS search starts from position len-strlen(s) */
96/* int rfind(XrdOucString s, int start = STR_NPOS) */
97/* - find first occurence of XrdOucString s starting from position start */
98/* in backward direction; returns STR_NPOS if nothing is found; */
99/* if start == STR_NPOS search starts from position len-s.lenght() */
100/* */
101/* bool beginswith(char c) */
102/* - returns 1 if the stored string starts with char c */
103/* bool beginswith(const char *s) */
104/* - returns 1 if the stored string starts with string s */
105/* bool beginswith(XrdOucString s) */
106/* - returns 1 if the stored string starts with XrdOucString s */
107/* */
108/* bool endswith(char c) */
109/* - returns 1 if the stored string ends with char c */
110/* bool endswith(const char *s) */
111/* - returns 1 if the stored string ends with string s */
112/* bool endswith(XrdOucString s) */
113/* - returns 1 if the stored string ends with XrdOucString s */
114/* */
115/* int matches(const char *s, char wch = '*') */
116/* - check if stored string is compatible with s allowing for wild char */
117/* wch (default: '*'); return the number of matching characters. */
118/* */
119/* 3. Modifiers */
120/* */
121/* void resize(int lmx = 0) */
122/* - resize buffer capacity to lmx+1 bytes; if lmx <= 0, free the buffer.*/
123/* */
124/* void append(const int i) */
125/* - append to stored string the string representation of integer i, */
126/* e.g. if string is initially "x*", after append(5) it will be "x*5". */
127/* void append(const char c) */
128/* - append char c to stored string, e.g. if string is initially "pop", */
129/* after append('_') it will be "pop_". */
130/* void append(const char *s) */
131/* - append string s to stored string, e.g. if string is initially "pop",*/
132/* after append("star") it will be "popstar". */
133/* void append(const XrdOucString s) */
134/* - append s.c_str() to stored string, e.g. if string is initially */
135/* "anti", after append("star") it will be "antistar". */
136/* */
137/* void assign(const char *s, int j, int k = -1) */
138/* - copy to allocated buffer a portion of string s; portion is defined */
139/* by j to k inclusive; if k == -1 the portion copied will be from j */
140/* to end-of-string; if j or k are inconsistent they are taken as 0 or */
141/* len-1, respectively; if necessary, capacity is increased to k-j */
142/* bytes. */
143/* void assign(const XrdOucString s, int j, int k = -1) */
144/* - copy to allocated buffer a portion of s.c_str(); portion is defined */
145/* by j to k inclusive; if k == -1 the portion copied will be from j */
146/* to end-of-string; if j or k are inconsistent they are taken as 0 or */
147/* len-1, respectively; if necessary, capacity is increased to k-j */
148/* bytes. */
149/* */
150/* int keep(int start = 0, int size = 0) */
151/* - drop chars outside the range of size bytes starting at start */
152/* */
153/* void insert(const int i, int start = -1) */
154/* - insert the string representation of integer i at position start of */
155/* the stored string, e.g. if string is initially "*x", after */
156/* insert(5,0) it will be "5*x"; default action is append. */
157/* void insert(const char c, int start = -1) */
158/* - insert the char c at position start of the stored string, e.g. */
159/* if string is initially "pok", after insert('_',0) it will be "_poc";*/
160/* default action is append. */
161/* void insert(const char *s, int start = -1, int lmx = 0) */
162/* - insert string s at position start of the stored string, e.g. */
163/* if string is initially "forth", after insert("backand",0) it will be*/
164/* "backandforth"; default action is append. */
165/* void insert(const XrdOucString s, int start = -1) */
166/* - insert string s.c_str() at position start of the stored string. */
167/* */
168/* int replace(const char *s1, const char *s2, */
169/* int from = 0, int to = -1); */
170/* - replace all occurrencies of string s1 with string s2 in the region */
171/* from position 'from' to position 'to' inclusive; the method is */
172/* optimized to minimize the memory movements; with s2 == 0 or "" */
173/* removes all instances of s1 in the specified region. */
174/* int replace(const XrdOucString s1, const char *s2, */
175/* int from = 0, int to = -1); */
176/* int replace(const char *s1, const XrdOucString s2, */
177/* int from = 0, int to = -1); */
178/* int replace(const XrdOucString s1, const XrdOucString s2, */
179/* int from = 0, int to = -1); */
180/* - interfaces to replace(const char *, const char *, int, int) */
181/* */
182/* int erase(int start = 0, int size = 0) */
183/* - erase size bytes starting at start */
184/* int erase(const char *s, int from = 0, int to = -1) */
185/* - erase occurences of string s within position 'from' and position */
186/* 'to' (inclusive), e.g if stored string is "aabbccefccddgg", then */
187/* erase("cc",0,9) will result in string "aabbefccddgg". */
188/* int erase(XrdOucString s, int from = 0, int to = -1) */
189/* - erase occurences of s.c_str() within position 'from' and position */
190/* 'to' (inclusive). */
191/* int erasefromstart(int sz = 0) */
192/* - erase sz bytes from the start. */
193/* int erasefromend(int sz = 0) */
194/* - erase sz bytes from the end. */
195/* */
196/* void lower(int pos, int size = 0) */
197/* - set to lower case size bytes from position start. */
198/* void upper(int pos, int size = 0) */
199/* - set to upper case size bytes from position start. */
200/* */
201/* void hardreset() */
202/* - memset to 0 the len meaningful bytes of the buffer. */
203/* */
204/* int tokenize(XrdOucString &tok, int from, char del) */
205/* - search for tokens delimited by 'del' (def ':') in string s; search */
206/* starts from 'from' and the token is returned in 'tok'. */
207/* */
208/* 4. Assignement operators */
209/* XrdOucString &operator=(const int i) */
210/* XrdOucString &operator=(const char c) */
211/* XrdOucString &operator=(const char *s) */
212/* XrdOucString &operator=(const XrdOucString s) */
213/* */
214/* 5. Addition operators */
215/* XrdOucString &operator+(const int i) */
216/* XrdOucString &operator+(const char c) */
217/* XrdOucString &operator+(const char *s) */
218/* XrdOucString &operator+(const XrdOucString s) */
219/* XrdOucString &operator+=(const int i) */
220/* XrdOucString &operator+=(const char c) */
221/* XrdOucString &operator+=(const char *s) */
222/* XrdOucString &operator+=(const XrdOucString s) */
223/* XrdOucString const operator+(const char *s1, const XrdOucString s2) */
224/* XrdOucString const operator+(const char c, const XrdOucString s) */
225/* XrdOucString const operator+(const int i, const XrdOucString s) */
226/* */
227/* 6. Equality operators */
228/* int operator==(const int i) */
229/* int operator==(const char c) */
230/* int operator==(const char *s) */
231/* int operator==(const XrdOucString s) */
232/* */
233/* 7. Inequality operators */
234/* int operator!=(const int i) */
235/* int operator!=(const char c) */
236/* int operator!=(const char *s) */
237/* int operator!=(const XrdOucString s) */
238/* */
239/* 8. Static methods to change / monitor the blksize */
240/* static int getblksize(); */
241/* static void setblksize(const int bs); */
242/* */
243/******************************************************************************/
245
246#include <cstdio>
247#include <cstdlib>
248#include <cstdarg>
249
250using namespace std;
251
252#define STR_NPOS -1
253
255
256private:
257 char *str;
258 int len;
259 int siz;
260
261 // Mininal block size to be used in (re-)allocations
262 // Option switched off by default; use XrdOucString::setblksize()
263 // and XrdOucString::getblksize() to change / monitor
264 static int blksize;
265
266 // Private methods
267 int adjust(int ls, int &j, int &k, int nmx = 0);
268 char *bufalloc(int nsz);
269 inline void init() { str = 0; len = 0; siz = 0; }
270
271public:
272 XrdOucString(int lmx = 0) { init(); if (lmx > 0) str = bufalloc(lmx+1); }
273 XrdOucString(const char *s, int lmx = 0);
274 XrdOucString(const char c, int lmx = 0);
276 XrdOucString(const XrdOucString &s, int j, int k = -1, int lmx = 0);
277 virtual ~XrdOucString();
278
279 // Info access
280 const char *c_str() const { return (const char *)str; }
281 int length() const { return len; }
282 int capacity() const { return siz; }
283 char &operator[](int j);
284 int find(const char c, int start = 0, bool forward = 1);
285 int find(const char *s, int start = 0);
286 int find(XrdOucString s, int start = 0);
287 int rfind(const char c, int start = STR_NPOS)
288 { return find(c, start, 0); }
289 int rfind(const char *s, int start = STR_NPOS);
290 int rfind(XrdOucString s, int start = STR_NPOS);
291 bool beginswith(char c) { return (find(c) == 0); }
292 bool beginswith(const char *s) { return (find(s) == 0); }
293 bool beginswith(XrdOucString s) { return (find(s) == 0); }
294 bool endswith(char c);
295 bool endswith(const char *s);
296 bool endswith(XrdOucString s) { return (endswith(s.c_str())); }
297 int matches(const char *s, char wch = '*');
298
299 // Tokenizer
300 int tokenize(XrdOucString &tok, int from, char del = ':');
301
302 // Modifiers
303 void resize(int lmx = 0) { int ns = (lmx > 0) ? lmx + 1 : 0;
304 str = bufalloc(ns); }
305 void append(const int i);
306 void append(const char c);
307 void append(const char *s);
308 void append(const XrdOucString s);
309 void assign(const char *s, int j, int k = -1);
310 void assign(const XrdOucString s, int j, int k = -1);
311#if !defined(WINDOWS)
312 int form(const char *fmt, ...);
313#endif
314 int keep(int start = 0, int size = 0);
315 void insert(const int i, int start = -1);
316 void insert(const char c, int start = -1);
317 void insert(const char *s, int start = -1, int lmx = 0);
318 void insert(const XrdOucString s, int start = -1);
319 int replace(const char *s1, const char *s2,
320 int from = 0, int to = -1);
321 int replace(const XrdOucString s1, const XrdOucString s2,
322 int from = 0, int to = -1);
323 int replace(const XrdOucString s1, const char *s2,
324 int from = 0, int to = -1);
325 int replace(const char *s1, const XrdOucString s2,
326 int from = 0, int to = -1);
327 int erase(int start = 0, int size = 0);
328 int erase(const char *s, int from = 0, int to = -1);
329 int erase(XrdOucString s, int from = 0, int to = -1);
330 int erasefromstart(int sz = 0) { return erase(0,sz); }
331 int erasefromend(int sz = 0) { return erase(len-sz,sz); }
332 void lower(int pos, int size = 0);
333 void upper(int pos, int size = 0);
334 void reset(const char c, int j = 0, int k = -1);
335 void hardreset();
336 void setbuffer(char *buf);
337
338 // Assignement operators
339 XrdOucString &operator=(const int i);
340 XrdOucString &operator=(const char c);
341 XrdOucString &operator=(const char *s);
343
344 // Add operators
345 friend XrdOucString operator+(const XrdOucString &s1, const int i);
346 friend XrdOucString operator+(const XrdOucString &s1, const char c);
347 friend XrdOucString operator+(const XrdOucString &s1, const char *s);
348 friend XrdOucString operator+(const XrdOucString &s1, const XrdOucString &s);
350 XrdOucString &operator+=(const char c);
351 XrdOucString &operator+=(const char *s);
353
354 // Equality operators
355 int operator==(const int i);
356 int operator==(const char c);
357 int operator==(const char *s);
359
360 // Inequality operators
361 int operator!=(const int i) { return !(*this == i); }
362 int operator!=(const char c) { return !(*this == c); }
363 int operator!=(const char *s) { return !(*this == s); }
364 int operator!=(const XrdOucString s) { return !(*this == s); }
365
366 // Miscellanea
367 bool isdigit(int from = 0, int to = -1);
368 long atoi(int from = 0, int to = -1);
369
370 // Static methods to change / monitor the default blksize
371 static int getblksize();
372 static void setblksize(const int bs);
373
374#if !defined(WINDOWS)
375 // format a string
376 static int form(XrdOucString &str, const char *fmt, ...);
377#endif
378};
379
380// Operator << is useful to print a string into a stream
381std::ostream &operator<< (std::ostream &, const XrdOucString s);
382
383XrdOucString const operator+(const char *s1, const XrdOucString s2);
384XrdOucString const operator+(const char c, const XrdOucString s);
385XrdOucString const operator+(const int i, const XrdOucString s);
386
387#endif
388
#define STR_NPOS
Definition XrdOucString.hh:252
std::ostream & operator<<(std::ostream &, const XrdOucString s)
XrdOucString const operator+(const char *s1, const XrdOucString s2)
Definition XrdOucString.hh:254
int operator!=(const int i)
Definition XrdOucString.hh:361
int siz
Definition XrdOucString.hh:259
int operator!=(const XrdOucString s)
Definition XrdOucString.hh:364
void insert(const int i, int start=-1)
bool beginswith(XrdOucString s)
Definition XrdOucString.hh:293
static int getblksize()
XrdOucString & operator=(const char *s)
XrdOucString & operator+=(const char *s)
int operator!=(const char *s)
Definition XrdOucString.hh:363
void assign(const char *s, int j, int k=-1)
void upper(int pos, int size=0)
char & operator[](int j)
XrdOucString & operator+=(const char c)
friend XrdOucString operator+(const XrdOucString &s1, const char *s)
void init()
Definition XrdOucString.hh:269
int capacity() const
Definition XrdOucString.hh:282
int erasefromstart(int sz=0)
Definition XrdOucString.hh:330
XrdOucString(const char *s, int lmx=0)
bool endswith(XrdOucString s)
Definition XrdOucString.hh:296
void insert(const char c, int start=-1)
int erase(const char *s, int from=0, int to=-1)
void setbuffer(char *buf)
int erasefromend(int sz=0)
Definition XrdOucString.hh:331
friend XrdOucString operator+(const XrdOucString &s1, const int i)
bool endswith(char c)
bool beginswith(char c)
Definition XrdOucString.hh:291
int erase(int start=0, int size=0)
int operator==(const XrdOucString s)
void reset(const char c, int j=0, int k=-1)
int matches(const char *s, char wch=' *')
int rfind(const char c, int start=STR_NPOS)
Definition XrdOucString.hh:287
XrdOucString & operator+=(const int i)
int find(const char *s, int start=0)
void hardreset()
int replace(const char *s1, const char *s2, int from=0, int to=-1)
int operator!=(const char c)
Definition XrdOucString.hh:362
int find(const char c, int start=0, bool forward=1)
friend XrdOucString operator+(const XrdOucString &s1, const char c)
int rfind(XrdOucString s, int start=STR_NPOS)
static int form(XrdOucString &str, const char *fmt,...)
int length() const
Definition XrdOucString.hh:281
int operator==(const char *s)
bool isdigit(int from=0, int to=-1)
long atoi(int from=0, int to=-1)
XrdOucString(int lmx=0)
Definition XrdOucString.hh:272
friend XrdOucString operator+(const XrdOucString &s1, const XrdOucString &s)
void append(const int i)
int replace(const char *s1, const XrdOucString s2, int from=0, int to=-1)
void append(const char c)
char * str
Definition XrdOucString.hh:257
void append(const XrdOucString s)
int form(const char *fmt,...)
virtual ~XrdOucString()
int tokenize(XrdOucString &tok, int from, char del=':')
int adjust(int ls, int &j, int &k, int nmx=0)
int len
Definition XrdOucString.hh:258
int operator==(const char c)
bool endswith(const char *s)
int find(XrdOucString s, int start=0)
XrdOucString(const XrdOucString &s)
void lower(int pos, int size=0)
XrdOucString & operator=(const int i)
static void setblksize(const int bs)
XrdOucString & operator+=(const XrdOucString s)
int operator==(const int i)
int replace(const XrdOucString s1, const char *s2, int from=0, int to=-1)
bool beginswith(const char *s)
Definition XrdOucString.hh:292
void assign(const XrdOucString s, int j, int k=-1)
void resize(int lmx=0)
Definition XrdOucString.hh:303
char * bufalloc(int nsz)
int rfind(const char *s, int start=STR_NPOS)
void insert(const XrdOucString s, int start=-1)
void insert(const char *s, int start=-1, int lmx=0)
int keep(int start=0, int size=0)
int replace(const XrdOucString s1, const XrdOucString s2, int from=0, int to=-1)
static int blksize
Definition XrdOucString.hh:264
void append(const char *s)
XrdOucString & operator=(const char c)
const char * c_str() const
Definition XrdOucString.hh:280
int erase(XrdOucString s, int from=0, int to=-1)
XrdOucString & operator=(const XrdOucString s)
XrdOucString(const XrdOucString &s, int j, int k=-1, int lmx=0)
XrdOucString(const char c, int lmx=0)
Definition XrdOucJson.hh:4517