]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/String.cc
5 * DEBUG: section 67 String
6 * AUTHOR: Duane Wessels
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
37 #include "base/TextException.h"
38 #include "mgr/Registration.h"
44 Must(size() < INT_MAX
);
49 // low-level buffer allocation,
50 // does not free old buffer and does not adjust or look at len_
52 String::allocBuffer(String::size_type sz
)
54 PROF_start(StringInitBuf
);
56 char *newBuffer
= (char*)memAllocString(sz
, &sz
);
57 setBuffer(newBuffer
, sz
);
58 PROF_stop(StringInitBuf
);
61 // low-level buffer assignment
62 // does not free old buffer and does not adjust or look at len_
64 String::setBuffer(char *aBuf
, String::size_type aSize
)
67 assert(aSize
< 65536);
72 String::String (char const *aString
) : size_(0), len_(0), buf_(NULL
)
75 allocAndFill(aString
, strlen(aString
));
78 StringRegistry::Instance().add(this);
83 String::operator =(char const *aString
)
90 String::operator = (String
const &old
)
92 clean(); // TODO: optimize to avoid cleaning the buffer we can use
94 allocAndFill(old
.rawBuf(), old
.size());
99 String::operator == (String
const &that
) const
101 if (0 == this->cmp(that
))
108 String::operator != (String
const &that
) const
110 if (0 == this->cmp(that
))
116 // public interface, makes sure that we clean the old buffer first
118 String::limitInit(const char *str
, int len
)
120 clean(); // TODO: optimize to avoid cleaning the buffer we can use
121 allocAndFill(str
, len
);
124 // Allocates the buffer to fit the supplied string and fills it.
127 String::allocAndFill(const char *str
, int len
)
129 PROF_start(StringAllocAndFill
);
131 allocBuffer(len
+ 1);
133 memcpy(buf_
, str
, len
);
135 PROF_stop(StringAllocAndFill
);
138 String::String (String
const &old
) : size_(0), len_(0), buf_(NULL
)
141 allocAndFill(old
.rawBuf(), old
.size());
144 StringRegistry::Instance().add(this);
151 PROF_start(StringClean
);
154 /* TODO if mempools has already closed this will FAIL!! */
156 memFreeString(size_
, buf_
);
163 PROF_stop(StringClean
);
171 StringRegistry::Instance().remove(this);
176 String::reset(const char *str
)
178 PROF_start(StringReset
);
179 clean(); // TODO: optimize to avoid cleaning the buffer if we can reuse it
181 allocAndFill(str
, strlen(str
));
182 PROF_stop(StringReset
);
186 String::append(const char *str
, int len
)
189 assert(str
&& len
>= 0);
191 PROF_start(StringAppend
);
192 if (len_
+ len
< size_
) {
193 strncat(buf_
, str
, len
);
196 // Create a temporary string and absorb it later.
198 assert(len_
+ len
< 65536); // otherwise snew.len_ overflows below
199 snew
.len_
= len_
+ len
;
200 snew
.allocBuffer(snew
.len_
+ 1);
203 memcpy(snew
.buf_
, rawBuf(), len_
);
206 memcpy(snew
.buf_
+ len_
, str
, len
);
208 snew
.buf_
[snew
.len_
] = '\0';
212 PROF_stop(StringAppend
);
216 String::append(char const *str
)
219 append (str
, strlen(str
));
223 String::append (char chr
)
228 append (myString
, 1);
232 String::append(String
const &old
)
234 append (old
.rawBuf(), old
.len_
);
238 String::absorb(String
&old
)
241 setBuffer(old
.buf_
, old
.size_
);
249 String::substr(String::size_type from
, String::size_type to
) const
251 Must(from
>= 0 && from
< size());
252 Must(to
> 0 && to
<= size());
256 rv
.limitInit(rawBuf()+from
,to
-from
);
263 String::stat(StoreEntry
*entry
) const
265 storeAppendPrintf(entry
, "%p : %d/%d \"%.*s\"\n",this,len_
, size_
, size(), rawBuf());
269 StringRegistry::Instance()
276 ptrcmp(C
const &lhs
, C
const &rhs
)
281 StringRegistry::StringRegistry()
284 Mgr::RegisterAction("strings",
285 "Strings in use in squid", Stat
, 0, 1);
292 (String
const *entry
)
294 entries
.insert(entry
, ptrcmp
);
299 StringRegistry::remove
300 (String
const *entry
)
302 entries
.remove(entry
, ptrcmp
);
305 StringRegistry
StringRegistry::Instance_
;
307 extern String::size_type
memStringCount();
310 StringRegistry::Stat(StoreEntry
*entry
)
312 storeAppendPrintf(entry
, "%lu entries, %lu reported from MemPool\n", (unsigned long) Instance().entries
.elements
, (unsigned long) memStringCount());
313 Instance().entries
.head
->walk(Stater
, entry
);
317 StringRegistry::Stater(String
const * const & nodedata
, void *state
)
319 StoreEntry
*entry
= (StoreEntry
*) state
;
320 nodedata
->stat(entry
);
325 /* TODO: move onto String */
327 stringHasWhitespace(const char *s
)
329 return strpbrk(s
, w_space
) != NULL
;
332 /* TODO: move onto String */
334 stringHasCntl(const char *s
)
338 while ((c
= (unsigned char) *s
++) != '\0') {
342 if (c
>= 0x7f && c
<= 0x9f)
350 * Similar to strtok, but has some rudimentary knowledge
354 strwordtok(char *buf
, char **t
)
356 unsigned char *word
= NULL
;
357 unsigned char *p
= (unsigned char *) buf
;
363 p
= (unsigned char *) *t
;
368 while (*p
&& xisspace(*p
))
416 if (!quoted
&& xisspace(*p
)) {
431 return (char *) word
;
435 checkNullString(const char *p
)
437 return p
? p
: "(NULL)";
441 String::pos(char const *aString
) const
445 return strstr(termedBuf(), aString
);
449 String::pos(char const ch
) const
453 return strchr(termedBuf(), ch
);
457 String::rpos(char const ch
) const
461 return strrchr(termedBuf(), (ch
));
465 String::find(char const ch
) const
475 String::find(char const *aString
) const
485 String::rfind(char const ch
) const
497 #include "String.cci"