]>
Commit | Line | Data |
---|---|---|
412da427 | 1 | /* |
412da427 FC |
2 | * SQUID Web Proxy Cache http://www.squid-cache.org/ |
3 | * ---------------------------------------------------------- | |
4 | * | |
5 | * Squid is the result of efforts by numerous individuals from | |
6 | * the Internet community; see the CONTRIBUTORS file for full | |
7 | * details. Many organizations have provided support for Squid's | |
8 | * development; see the SPONSORS file for full details. Squid is | |
9 | * Copyrighted (C) 2001 by the Regents of the University of | |
10 | * California; see the COPYRIGHT file for full details. Squid | |
11 | * incorporates software developed and/or copyrighted by other | |
12 | * sources; see the CREDITS file for full details. | |
13 | * | |
14 | * This program is free software; you can redistribute it and/or modify | |
15 | * it under the terms of the GNU General Public License as published by | |
16 | * the Free Software Foundation; either version 2 of the License, or | |
17 | * (at your option) any later version. | |
18 | * | |
19 | * This program is distributed in the hope that it will be useful, | |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | * GNU General Public License for more details. | |
23 | * | |
24 | * You should have received a copy of the GNU General Public License | |
25 | * along with this program; if not, write to the Free Software | |
26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
27 | */ | |
28 | ||
29 | #ifndef SQUID_SBUF_H | |
30 | #define SQUID_SBUF_H | |
31 | ||
32 | #include "base/InstanceId.h" | |
412da427 | 33 | #include "MemBlob.h" |
62842d40 | 34 | #include "SBufExceptions.h" |
412da427 FC |
35 | #include "SquidString.h" |
36 | ||
62842d40 FC |
37 | #if HAVE_CLIMITS |
38 | #include <climits> | |
39 | #elif HAVE_LIMITS_H | |
40 | #include <limits.h> | |
41 | #endif | |
412da427 FC |
42 | #if HAVE_UNISTD_H |
43 | #include <unistd.h> | |
44 | #endif | |
45 | #if HAVE_STDARG_H | |
46 | #include <stdarg.h> | |
47 | #endif | |
48 | #if HAVE_IOSFWD | |
49 | #include <iosfwd> | |
50 | #endif | |
51 | ||
496a9e97 | 52 | /* SBuf placeholder for printf */ |
412da427 FC |
53 | #ifndef SQUIDSBUFPH |
54 | #define SQUIDSBUFPH "%.*s" | |
55 | #define SQUIDSBUFPRINT(s) (s).plength(),(s).rawContent() | |
56 | #endif /* SQUIDSBUFPH */ | |
57 | ||
58 | typedef enum { | |
59 | caseSensitive, | |
60 | caseInsensitive | |
61 | } SBufCaseSensitive; | |
62 | ||
63 | /** | |
64 | * Container for various SBuf class-wide statistics. | |
65 | * | |
66 | * The stats are not completely accurate; they're mostly meant to | |
67 | * understand whether Squid is leaking resources | |
68 | * and whether SBuf is paying off the expected gains. | |
69 | */ | |
70 | class SBufStats | |
71 | { | |
72 | public: | |
7cbd3256 FC |
73 | uint64_t alloc; ///<number of calls to SBuf constructors |
74 | uint64_t allocCopy; ///<number of calls to SBuf copy-constructor | |
75 | uint64_t allocFromString; ///<number of copy-allocations from Strings | |
76 | uint64_t allocFromCString; ///<number of copy-allocations from c-strings | |
77 | uint64_t assignFast; ///<number of no-copy assignment operations | |
78 | uint64_t clear; ///<number of clear operations | |
79 | uint64_t append; ///<number of append operations | |
80 | uint64_t toStream; ///<number of write operations to ostreams | |
81 | uint64_t setChar; ///<number of calls to setAt | |
82 | uint64_t getChar; ///<number of calls to at() and operator[] | |
83 | uint64_t compareSlow; ///<number of comparison operations requiring data scan | |
84 | uint64_t compareFast; ///<number of comparison operations not requiring data scan | |
85 | uint64_t copyOut; ///<number of data-copies to other forms of buffers | |
86 | uint64_t rawAccess; ///<number of accesses to raw contents | |
87 | uint64_t chop; ///<number of chop operations | |
88 | uint64_t trim; ///<number of trim operations | |
89 | uint64_t find; ///<number of find operations | |
90 | uint64_t scanf; ///<number of scanf operations | |
91 | uint64_t caseChange; ///<number of toUpper and toLower operations | |
92 | uint64_t cowFast; ///<number of cow operations not actually requiring a copy | |
93 | uint64_t cowSlow; ///<number of cow operations requiring a copy | |
94 | uint64_t live; ///<number of currently-allocated SBuf | |
412da427 | 95 | |
496a9e97 | 96 | ///Dump statistics to an ostream. |
412da427 FC |
97 | std::ostream& dump(std::ostream &os) const; |
98 | SBufStats(); | |
99 | ||
100 | SBufStats& operator +=(const SBufStats&); | |
101 | }; | |
102 | ||
103 | /** | |
104 | * A String or Buffer. | |
105 | * Features: refcounted backing store, cheap copy and sub-stringing | |
106 | * operations, copy-on-write to isolate change operations to each instance. | |
107 | * Where possible, we're trying to mimic std::string's interface. | |
108 | */ | |
109 | class SBuf | |
110 | { | |
111 | public: | |
7cbd3256 | 112 | typedef MemBlob::size_type size_type; |
50827187 | 113 | static const size_type npos = 0xffffffff; // max(uint32_t) |
412da427 FC |
114 | |
115 | /// Maximum size of a SBuf. By design it MUST be < MAX(size_type)/2. Currently 256Mb. | |
116 | static const size_type maxSize = 0xfffffff; | |
117 | ||
118 | /// create an empty (zero-size) SBuf | |
119 | SBuf(); | |
120 | SBuf(const SBuf &S); | |
121 | ||
122 | /** Constructor: import c-style string | |
123 | * | |
124 | * Create a new SBuf containing a COPY of the contents of the | |
125 | * c-string | |
126 | * \param S the c string to be copied | |
7cbd3256 | 127 | * \param n how many bytes to import into the SBuf. If it is npos |
412da427 FC |
128 | * or unspecified, imports to end-of-cstring |
129 | * \note it is the caller's responsibility not to go out of bounds | |
50827187 | 130 | * \note bounds is 0 <= pos < length(); caller must pay attention to signedness |
412da427 | 131 | */ |
7cbd3256 | 132 | explicit SBuf(const char *S, size_type n = npos); |
412da427 FC |
133 | |
134 | /** Constructor: import SquidString, copying contents. | |
135 | * | |
136 | * This method will be removed once SquidString has gone. | |
137 | */ | |
496a9e97 FC |
138 | explicit SBuf(const String &S); |
139 | ||
140 | /// Constructor: import std::string. Contents are copied. | |
141 | explicit SBuf(const std::string &s); | |
412da427 FC |
142 | |
143 | ~SBuf(); | |
62842d40 | 144 | |
412da427 FC |
145 | /** Explicit assignment. |
146 | * | |
147 | * Current SBuf will share backing store with the assigned one. | |
148 | */ | |
149 | SBuf& assign(const SBuf &S); | |
62842d40 | 150 | |
412da427 FC |
151 | /** Assignment operator. |
152 | * | |
153 | * Current SBuf will share backing store with the assigned one. | |
154 | */ | |
62842d40 | 155 | SBuf& operator =(const SBuf & S) {return assign(S);} |
412da427 FC |
156 | |
157 | /** Import a c-string into a SBuf, copying the data. | |
158 | * | |
159 | * It is the caller's duty to free the imported string, if needed. | |
160 | * \param S the c string to be copied | |
7cbd3256 | 161 | * \param n how many bytes to import into the SBuf. If it is npos |
412da427 FC |
162 | * or unspecified, imports to end-of-cstring |
163 | * \note it is the caller's responsibility not to go out of bounds | |
7cbd3256 FC |
164 | * \note to assign a std::string use the pattern: |
165 | * assign(stdstr.data(), stdstd.length()) | |
412da427 | 166 | */ |
7cbd3256 | 167 | SBuf& assign(const char *S, size_type n = npos); |
412da427 FC |
168 | |
169 | /** Assignment operator. Copy a NULL-terminated c-style string into a SBuf. | |
170 | * | |
171 | * Copy a c-style string into a SBuf. Shortcut for SBuf.assign(S) | |
172 | * It is the caller's duty to free the imported string, if needed. | |
7cbd3256 | 173 | * \note not \0-clean |
412da427 | 174 | */ |
62842d40 | 175 | SBuf& operator =(const char *S) {return assign(S);} |
412da427 | 176 | |
412da427 FC |
177 | /** reset the SBuf as if it was just created. |
178 | * | |
179 | * Resets the SBuf to empty, memory is freed lazily. | |
180 | */ | |
181 | void clear(); | |
182 | ||
183 | /** Append operation | |
184 | * | |
185 | * Append the supplied SBuf to the current one; extend storage as needed. | |
186 | */ | |
187 | SBuf& append(const SBuf & S); | |
188 | ||
189 | /** Append operation for C-style strings. | |
190 | * | |
191 | * Append the supplied c-string to the SBuf; extend storage | |
192 | * as needed. | |
193 | * | |
194 | * \param S the c string to be copied. Can be NULL. | |
a452bc8b FC |
195 | * \param Ssize how many bytes to import into the SBuf. If it is npos |
196 | * or unspecified, imports to end-of-cstring. If S is NULL, | |
197 | * Ssize is ignored. | |
7cbd3256 FC |
198 | * \note to append a std::string use the pattern |
199 | * cstr_append(stdstr.data(), stdstd.length()) | |
412da427 | 200 | */ |
7cbd3256 | 201 | SBuf& append(const char * S, size_type Ssize = npos); |
412da427 FC |
202 | |
203 | /** Assignment operation with printf(3)-style definition | |
204 | * \note arguments may be evaluated more than once, be careful | |
205 | * of side-effects | |
206 | */ | |
207 | SBuf& Printf(const char *fmt, ...); | |
208 | ||
209 | /** Append operation with printf-style arguments | |
210 | * \note arguments may be evaluated more than once, be careful | |
211 | * of side-effects | |
212 | */ | |
213 | SBuf& appendf(const char *fmt, ...); | |
496a9e97 | 214 | |
412da427 FC |
215 | /** Append operation, with vsprintf(3)-style arguments. |
216 | * \note arguments may be evaluated more than once, be careful | |
217 | * of side-effects | |
218 | */ | |
219 | SBuf& vappendf(const char *fmt, va_list vargs); | |
220 | ||
496a9e97 | 221 | /// print the SBuf contents to the supplied ostream |
412da427 FC |
222 | std::ostream& print(std::ostream &os) const; |
223 | ||
496a9e97 | 224 | /** print SBuf contents and debug information about the SBuf to an ostream |
412da427 FC |
225 | * |
226 | * Debug function, dumps to a stream informations on the current SBuf, | |
227 | * including low-level details and statistics. | |
228 | */ | |
229 | std::ostream& dump(std::ostream &os) const; | |
230 | ||
231 | /** random-access read to any char within the SBuf | |
232 | * | |
233 | * does not check access bounds. If you need that, use at() | |
234 | */ | |
fdd1982c | 235 | char operator [](size_type pos) const {++stats.getChar; return store_->mem[off_+pos];} |
412da427 FC |
236 | |
237 | /** random-access read to any char within the SBuf. | |
238 | * | |
239 | * \throw OutOfBoundsException when access is out of bounds | |
50827187 | 240 | * \note bounds is 0 <= pos < length(); caller must pay attention to signedness |
412da427 | 241 | */ |
fdd1982c | 242 | char at(size_type pos) const {checkAccessBounds(pos); return operator[](pos);} |
412da427 FC |
243 | |
244 | /** direct-access set a byte at a specified operation. | |
245 | * | |
246 | * \param pos the position to be overwritten | |
247 | * \param toset the value to be written | |
248 | * \throw OutOfBoundsException when pos is of bounds | |
50827187 | 249 | * \note bounds is 0 <= pos < length(); caller must pay attention to signedness |
412da427 FC |
250 | * \note performs a copy-on-write if needed. |
251 | */ | |
252 | void setAt(size_type pos, char toset); | |
253 | ||
254 | /** compare to other SBuf, str(case)cmp-style | |
255 | * | |
256 | * \param isCaseSensitive one of caseSensitive or caseInsensitive | |
7cbd3256 | 257 | * \param n compare up to this many bytes. if npos (default), compare whole SBufs |
412da427 FC |
258 | * \retval >0 argument of the call is greater than called SBuf |
259 | * \retval <0 argument of the call is smaller than called SBuf | |
260 | * \retval 0 argument of the call has the same contents of called SBuf | |
261 | */ | |
7cbd3256 FC |
262 | int compare(const SBuf &S, SBufCaseSensitive isCaseSensitive, size_type n = npos) const; |
263 | ||
264 | /// shorthand version for compare | |
265 | inline int cmp(const SBuf &S, size_type n = npos) const { | |
266 | return compare(S,caseSensitive,n); | |
267 | } | |
268 | ||
269 | /// shorthand version for case-insensitive comparison | |
270 | inline int caseCmp(const SBuf &S, size_type n = npos) const { | |
271 | return compare(S,caseInsensitive,n); | |
272 | } | |
412da427 FC |
273 | |
274 | /** check whether the entire supplied argument is a prefix of the SBuf. | |
275 | * \param S the prefix to match against | |
276 | * \param isCaseSensitive one of caseSensitive or caseInsensitive | |
277 | * \retval true argument is a prefix of the SBuf | |
278 | */ | |
279 | bool startsWith(const SBuf &S, SBufCaseSensitive isCaseSensitive = caseSensitive) const; | |
280 | ||
412da427 FC |
281 | bool operator ==(const SBuf & S) const; |
282 | bool operator !=(const SBuf & S) const; | |
7cbd3256 FC |
283 | bool operator <(const SBuf &S) const {return (cmp(S) < 0);} |
284 | bool operator >(const SBuf &S) const {return (cmp(S) > 0);} | |
285 | bool operator <=(const SBuf &S) const {return (cmp(S) <= 0);} | |
286 | bool operator >=(const SBuf &S) const {return (cmp(S) >= 0);} | |
412da427 FC |
287 | |
288 | /** Consume bytes at the head of the SBuf | |
289 | * | |
290 | * Consume N chars at SBuf head, or to SBuf's end, | |
291 | * whichever is shorter. If more bytes are consumed than available, | |
292 | * the SBuf is emptied | |
293 | * \param n how many bytes to remove; could be zero. | |
7cbd3256 | 294 | * npos (or no argument) means 'to the end of SBuf' |
412da427 FC |
295 | * \return a new SBuf containing the consumed bytes. |
296 | */ | |
297 | SBuf consume(size_type n = npos); | |
298 | ||
496a9e97 | 299 | /// gets global statistic informations |
412da427 FC |
300 | static const SBufStats& GetStats(); |
301 | ||
302 | /** Copy SBuf contents into user-supplied C buffer. | |
303 | * | |
304 | * Export a copy of the SBuf's contents into the user-supplied | |
305 | * buffer, up to the user-supplied-length. No zero-termination is performed | |
306 | * \return num the number of actually-copied chars. | |
307 | */ | |
308 | size_type copy(char *dest, size_type n) const; | |
309 | ||
310 | /** exports a pointer to the SBuf internal storage. | |
311 | * \warning ACCESSING RAW STORAGE IS DANGEROUS! | |
312 | * | |
496a9e97 FC |
313 | * Returns a ead-only pointer to SBuf's content. No terminating null |
314 | * character is appended (use c_str() for that). | |
412da427 FC |
315 | * The returned value points to an internal location whose contents |
316 | * are guaranteed to remain unchanged only until the next call | |
317 | * to a non-constant member function of the SBuf object. Such a | |
318 | * call may be implicit (e.g., when SBuf is destroyed | |
319 | * upon leaving the current context). | |
320 | * This is a very UNSAFE way of accessing the data. | |
321 | * This call never returns NULL. | |
322 | * \see c_str | |
323 | * \note the memory management system guarantees that the exported region | |
324 | * of memory will remain valid if the caller keeps holding | |
325 | * a valid reference to the SBuf object and does not write or append to | |
326 | * it. For example: | |
327 | * \code | |
328 | * SBuf foo("some string"); | |
329 | * const char *bar = foo.rawContent(); | |
330 | * doSomething(bar); //safe | |
331 | * foo.append(" other string"); | |
332 | * doSomething(bar); //unsafe | |
333 | * \endcode | |
334 | */ | |
335 | const char* rawContent() const; | |
336 | ||
337 | /** Exports a writable pointer to the SBuf internal storage. | |
338 | * \warning Use with EXTREME caution, this is a dangerous operation. | |
339 | * | |
340 | * Returns a pointer to the first unused byte in the SBuf's storage, | |
ac7947fd FC |
341 | * which can be be used for appending. At least minSize bytes will |
342 | * be available for writing. | |
343 | * The returned pointer must not be stored by the caller, as it will | |
496a9e97 | 344 | * be invalidated by the first call to a non-const method call |
412da427 | 345 | * on the SBuf. |
ac7947fd | 346 | * This call guarantees to never return NULL. |
8aa34dad FC |
347 | * \see reserveSpace |
348 | * \note Unlike reserveSpace(), this method does not guarantee exclusive | |
349 | * buffer ownership. It is instead optimized for a one writer | |
350 | * (appender), many readers scenario by avoiding unnecessary | |
351 | * copying and allocations. | |
412da427 FC |
352 | * \throw SBufTooBigException if the user tries to allocate too big a SBuf |
353 | */ | |
ac7947fd | 354 | char *rawSpace(size_type minSize); |
412da427 | 355 | |
50827187 | 356 | /** Obtain how much free space is available in the backing store. |
8aa34dad | 357 | * |
50827187 FC |
358 | * \note: unless the client just cow()ed, it is not guaranteed that |
359 | * the free space can be used. | |
8aa34dad FC |
360 | */ |
361 | size_type spaceSize() const { return store_->spaceSize(); } | |
362 | ||
412da427 FC |
363 | /** Force a SBuf's size |
364 | * \warning use with EXTREME caution, this is a dangerous operation | |
365 | * | |
366 | * Adapt the SBuf internal state after external interference | |
367 | * such as writing into it via rawSpace. | |
7cbd3256 FC |
368 | * \throw TextException if SBuf doesn't have exclusive ownership of store |
369 | * \throw SBufTooBigException if new size is bigger than available store space | |
412da427 FC |
370 | */ |
371 | void forceSize(size_type newSize); | |
372 | ||
373 | /** exports a null-terminated reference to the SBuf internal storage. | |
374 | * \warning ACCESSING RAW STORAGE IS DANGEROUS! DO NOT EVER USE | |
375 | * THE RETURNED POINTER FOR WRITING | |
376 | * | |
377 | * The returned value points to an internal location whose contents | |
378 | * are guaranteed to remain unchanged only until the next call | |
379 | * to a non-constant member function of the SBuf object. Such a | |
380 | * call may be implicit (e.g., when SBuf is destroyed | |
381 | * upon leaving the current context). | |
382 | * This is a very UNSAFE way of accessing the data. | |
383 | * This call never returns NULL. | |
384 | * \see rawContent | |
385 | * \note the memory management system guarantees that the exported region | |
496a9e97 FC |
386 | * of memory will remain valid will remain valid only if the |
387 | * caller keeps holding a valid reference to the SBuf object and | |
388 | * does not write or append to it | |
412da427 FC |
389 | */ |
390 | const char* c_str(); | |
391 | ||
496a9e97 | 392 | /// Returns the number of bytes stored in SBuf. |
62842d40 | 393 | size_type length() const {return len_;} |
412da427 FC |
394 | |
395 | /** Get the length of the SBuf, as a signed integer | |
396 | * | |
397 | * Compatibility function for printf(3) which requires a signed int | |
398 | * \throw SBufTooBigException if the SBuf is too big for a signed integer | |
399 | */ | |
62842d40 FC |
400 | int plength() const { |
401 | if (length()>INT_MAX) | |
402 | throw SBufTooBigException(__FILE__, __LINE__); | |
403 | return static_cast<int>(length()); | |
404 | } | |
412da427 FC |
405 | |
406 | /** Check whether the SBuf is empty | |
407 | * | |
408 | * \return true if length() == 0 | |
409 | */ | |
62842d40 | 410 | bool isEmpty() const {return (len_==0);} |
412da427 | 411 | |
8aa34dad | 412 | /** Request to guarantee the SBuf's free store space. |
412da427 FC |
413 | * |
414 | * After the reserveSpace request, the SBuf is guaranteed to have at | |
7cbd3256 | 415 | * least minSpace bytes of unused backing store following the currently |
ac7947fd | 416 | * used portion and single ownership of the backing store. |
412da427 FC |
417 | * \throw SBufTooBigException if the user tries to allocate too big a SBuf |
418 | */ | |
e842ecce | 419 | void reserveSpace(size_type minSpace) { |
6fd0701a | 420 | Must(minSpace <= maxSize); |
a44b8b80 | 421 | Must(length() <= maxSize - minSpace); |
e842ecce FC |
422 | reserveCapacity(length()+minSpace); |
423 | } | |
412da427 | 424 | |
8aa34dad | 425 | /** Request to guarantee the SBuf's store capacity |
412da427 FC |
426 | * |
427 | * After this method is called, the SBuf is guaranteed to have at least | |
7cbd3256 | 428 | * minCapacity bytes of total buffer size, including the currently-used |
577bcebc FC |
429 | * portion; it is also guaranteed that after this call this SBuf |
430 | * has unique ownership of the underlying memory store. | |
412da427 FC |
431 | * \throw SBufTooBigException if the user tries to allocate too big a SBuf |
432 | */ | |
433 | void reserveCapacity(size_type minCapacity); | |
434 | ||
435 | /** slicing method | |
436 | * | |
496a9e97 FC |
437 | * Removes SBuf prefix and suffix, leaving a sequence of 'n' |
438 | * bytes starting from position 'pos', first byte is at pos 0. | |
439 | * It is an in-place-modifying version of substr. | |
412da427 | 440 | * \param pos start sub-stringing from this byte. If it is |
496a9e97 | 441 | * npos or it is greater than the SBuf length, the SBuf is cleared and |
50827187 | 442 | * an empty SBuf is returned. |
412da427 | 443 | * \param n maximum number of bytes of the resulting SBuf. |
7cbd3256 | 444 | * npos means "to end of SBuf". |
496a9e97 | 445 | * if it is 0, the SBuf is cleared and an empty SBuf is returned. |
496a9e97 FC |
446 | * if it overflows the end of the SBuf, it is capped to the end of SBuf |
447 | * \see substr, trim | |
412da427 FC |
448 | */ |
449 | SBuf& chop(size_type pos, size_type n = npos); | |
450 | ||
451 | /** Remove characters in the toremove set at the beginning, end or both | |
452 | * | |
453 | * \param toremove characters to be removed. Stops chomping at the first | |
454 | * found char not in the set | |
455 | * \param atBeginning if true (default), strips at the beginning of the SBuf | |
456 | * \param atEnd if true (default), strips at the end of the SBuf | |
457 | */ | |
458 | SBuf& trim(const SBuf &toRemove, bool atBeginning = true, bool atEnd = true); | |
459 | ||
460 | /** Extract a part of the current SBuf. | |
461 | * | |
496a9e97 FC |
462 | * Return a fresh a fresh copy of a portion the current SBuf, which is |
463 | * left untouched. The same parameter convetions apply as for chop. | |
464 | * \see trim, chop | |
412da427 FC |
465 | */ |
466 | SBuf substr(size_type pos, size_type n = npos) const; | |
467 | ||
468 | /** Find first occurrence of character in SBuf | |
469 | * | |
470 | * Returns the index in the SBuf of the first occurrence of char c. | |
7cbd3256 | 471 | * \return npos if the char was not found |
412da427 | 472 | * \param startPos if specified, ignore any occurrences before that position |
496a9e97 | 473 | * if startPos is npos or greater than length() npos is always returned |
7cbd3256 | 474 | * if startPos is less than zero, it is ignored |
412da427 FC |
475 | */ |
476 | size_type find(char c, size_type startPos = 0) const; | |
477 | ||
478 | /** Find first occurrence of SBuf in SBuf. | |
479 | * | |
480 | * Returns the index in the SBuf of the first occurrence of the | |
481 | * sequence contained in the str argument. | |
482 | * \param startPos if specified, ignore any occurrences before that position | |
496a9e97 | 483 | * if startPos is npos or greater than length() npos is always returned |
7cbd3256 | 484 | * \return npos if the SBuf was not found |
412da427 FC |
485 | */ |
486 | size_type find(const SBuf & str, size_type startPos = 0) const; | |
487 | ||
488 | /** Find last occurrence of character in SBuf | |
489 | * | |
490 | * Returns the index in the SBuf of the last occurrence of char c. | |
7cbd3256 | 491 | * \return npos if the char was not found |
412da427 | 492 | * \param endPos if specified, ignore any occurrences after that position. |
496a9e97 | 493 | * if npos or greater than length(), the whole SBuf is considered |
412da427 FC |
494 | */ |
495 | size_type rfind(char c, size_type endPos = npos) const; | |
496 | ||
497 | /** Find last occurrence of SBuf in SBuf | |
498 | * | |
499 | * Returns the index in the SBuf of the last occurrence of the | |
500 | * sequence contained in the str argument. | |
7cbd3256 | 501 | * \return npos if the sequence was not found |
412da427 | 502 | * \param endPos if specified, ignore any occurrences after that position |
496a9e97 | 503 | * if npos or greater than length(), the whole SBuf is considered |
412da427 FC |
504 | */ |
505 | size_type rfind(const SBuf &str, size_type endPos = npos) const; | |
506 | ||
507 | /** Find first occurrence of character of set in SBuf | |
508 | * | |
509 | * Finds the first occurrence of ANY of the characters in the supplied set in | |
510 | * the SBuf. | |
7cbd3256 | 511 | * \return npos if no character in the set could be found |
412da427 | 512 | * \param startPos if specified, ignore any occurrences before that position |
7cbd3256 | 513 | * if npos, then npos is always returned |
412da427 FC |
514 | */ |
515 | size_type find_first_of(const SBuf &set, size_type startPos = 0) const; | |
516 | ||
517 | /** sscanf-alike | |
518 | * | |
519 | * sscanf re-implementation. Non-const, and not \0-clean. | |
520 | * \return same as sscanf | |
521 | * \see man sscanf(3) | |
522 | */ | |
523 | int scanf(const char *format, ...); | |
524 | ||
525 | /** Lower-case SBuf | |
526 | * | |
527 | * Returns a lower-cased COPY of the SBuf | |
528 | * \see man tolower(3) | |
529 | */ | |
530 | SBuf toLower() const; | |
531 | ||
532 | /** Upper-case SBuf | |
533 | * | |
534 | * Returns an upper-cased COPY of the SBuf | |
535 | * \see man toupper(3) | |
536 | */ | |
537 | SBuf toUpper() const; | |
538 | ||
539 | /** String export function | |
496a9e97 FC |
540 | * converts the SBuf to a legacy String, by copy. |
541 | * \deprecated | |
412da427 FC |
542 | */ |
543 | String toString() const; | |
544 | ||
7cbd3256 FC |
545 | /// std::string export function |
546 | std::string toStdString() const { return std::string(buf(),length()); } | |
547 | ||
496a9e97 FC |
548 | // TODO: possibly implement erase() similar to std::string's erase |
549 | // TODO: possibly implement a replace() call | |
412da427 FC |
550 | private: |
551 | ||
552 | MemBlob::Pointer store_; ///< memory block, possibly shared with other SBufs | |
553 | size_type off_; ///< our content start offset from the beginning of shared store_ | |
554 | size_type len_; ///< number of our content bytes in shared store_ | |
555 | static SBufStats stats; ///< class-wide statistics | |
556 | ||
7cbd3256 FC |
557 | /// SBuf object identifier; does not change when contents do, |
558 | /// including during assignment | |
559 | const InstanceId<SBuf> id; | |
412da427 | 560 | |
62842d40 FC |
561 | /** obtain prototype store |
562 | * | |
563 | * Just-created SBufs all share to the same MemBlob. | |
564 | * This call instantiates and returns it. | |
565 | */ | |
566 | static MemBlob::Pointer GetStorePrototype(); | |
567 | ||
568 | /** | |
569 | * obtains a char* to the beginning of this SBuf in memory. | |
570 | * \note the obtained string is NOT null-terminated. | |
571 | */ | |
572 | char * buf() const {return (store_->mem+off_);} | |
573 | ||
574 | /** returns the pointer to the first char after this SBuf end | |
575 | * | |
576 | * No checks are made that the space returned is safe, checking that is | |
577 | * up to the caller. | |
578 | */ | |
579 | char * bufEnd() const {return (store_->mem+off_+len_);} | |
580 | ||
581 | /** | |
582 | * Try to guesstimate how big a MemBlob to allocate. | |
583 | * The result is guarranteed to be to be at least the desired size. | |
584 | */ | |
fdd1982c | 585 | size_type estimateCapacity(size_type desired) const {return (2*desired);} |
412da427 | 586 | |
412da427 FC |
587 | void reAlloc(size_type newsize); |
588 | ||
a452bc8b | 589 | void cow(size_type minsize = npos); |
412da427 FC |
590 | |
591 | void checkAccessBounds(size_type pos) const; | |
62842d40 | 592 | |
7cbd3256 FC |
593 | /** Low-level append operation |
594 | * | |
595 | * Takes as input a contiguous area of memory and appends its contents | |
596 | * to the SBuf, taking care of memory management. Does no bounds checking | |
597 | * on the supplied memory buffer, it is the duty of the caller to ensure | |
598 | * that the supplied area is valid. | |
599 | */ | |
600 | SBuf& lowAppend(const char * memArea, size_type areaSize); | |
412da427 FC |
601 | }; |
602 | ||
496a9e97 | 603 | /// ostream output operator |
62842d40 FC |
604 | inline std::ostream & |
605 | operator <<(std::ostream& os, const SBuf& S) | |
606 | { | |
607 | return S.print(os); | |
608 | } | |
412da427 FC |
609 | |
610 | #endif /* SQUID_SBUF_H */ |