]>
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.
39 // low-level buffer allocation,
40 // does not free old buffer and does not adjust or look at len_
42 String::allocBuffer(size_t sz
)
44 PROF_start(StringInitBuf
);
46 char *newBuffer
= (char*)memAllocString(sz
, &sz
);
47 setBuffer(newBuffer
, sz
);
48 PROF_stop(StringInitBuf
);
51 // low-level buffer assignment
52 // does not free old buffer and does not adjust or look at len_
54 String::setBuffer(char *aBuf
, size_t aSize
)
57 assert(aSize
< 65536);
62 String::String (char const *aString
) : size_(0), len_(0), buf_(NULL
)
65 allocAndFill(aString
, strlen(aString
));
68 StringRegistry::Instance().add(this);
73 String::operator =(char const *aString
)
80 String::operator = (String
const &old
)
82 clean(); // TODO: optimize to avoid cleaning the buffer we can use
84 allocAndFill(old
.rawBuf(), old
.size());
89 String::operator == (String
const &that
) const
91 if (0 == this->cmp(that
))
98 String::operator != (String
const &that
) const
100 if (0 == this->cmp(that
))
106 // public interface, makes sure that we clean the old buffer first
108 String::limitInit(const char *str
, int len
)
110 clean(); // TODO: optimize to avoid cleaning the buffer we can use
111 allocAndFill(str
, len
);
114 // Allocates the buffer to fit the supplied string and fills it.
117 String::allocAndFill(const char *str
, int len
)
119 PROF_start(StringAllocAndFill
);
121 allocBuffer(len
+ 1);
123 xmemcpy(buf_
, str
, len
);
125 PROF_stop(StringAllocAndFill
);
128 String::String (String
const &old
) : size_(0), len_(0), buf_(NULL
)
131 allocAndFill(old
.rawBuf(), old
.size());
134 StringRegistry::Instance().add(this);
141 PROF_start(StringClean
);
145 memFreeString(size_
, buf_
);
152 PROF_stop(StringClean
);
160 StringRegistry::Instance().remove(this);
165 String::reset(const char *str
)
167 PROF_start(StringReset
);
168 clean(); // TODO: optimize to avoid cleaning the buffer if we can reuse it
170 allocAndFill(str
, strlen(str
));
171 PROF_stop(StringReset
);
175 String::append(const char *str
, int len
)
178 assert(str
&& len
>= 0);
180 PROF_start(StringAppend
);
181 if (len_
+ len
< size_
) {
182 strncat(buf_
, str
, len
);
185 // Create a temporary string and absorb it later.
187 snew
.len_
= len_
+ len
;
188 snew
.allocBuffer(snew
.len_
+ 1);
191 xmemcpy(snew
.buf_
, rawBuf(), len_
);
194 xmemcpy(snew
.buf_
+ len_
, str
, len
);
196 snew
.buf_
[snew
.len_
] = '\0';
200 PROF_stop(StringAppend
);
204 String::append(char const *str
)
207 append (str
, strlen(str
));
211 String::append (char chr
)
216 append (myString
, 1);
220 String::append(String
const &old
)
222 append (old
.rawBuf(), old
.len_
);
226 String::absorb(String
&old
)
229 setBuffer(old
.buf_
, old
.size_
);
238 String::stat(StoreEntry
*entry
) const
240 storeAppendPrintf(entry
, "%p : %d/%d \"%.*s\"\n",this,len_
, size_
, size(), rawBuf());
244 StringRegistry::Instance()
251 ptrcmp(C
const &lhs
, C
const &rhs
)
256 StringRegistry::StringRegistry()
259 CacheManager::GetInstance()->registerAction("strings",
260 "Strings in use in squid", Stat
, 0, 1);
267 (String
const *entry
)
269 entries
.insert(entry
, ptrcmp
);
274 StringRegistry::remove
275 (String
const *entry
)
277 entries
.remove(entry
, ptrcmp
);
280 StringRegistry
StringRegistry::Instance_
;
282 extern size_t memStringCount();
285 StringRegistry::Stat(StoreEntry
*entry
)
287 storeAppendPrintf(entry
, "%lu entries, %lu reported from MemPool\n", (unsigned long) Instance().entries
.elements
, (unsigned long) memStringCount());
288 Instance().entries
.head
->walk(Stater
, entry
);
292 StringRegistry::Stater(String
const * const & nodedata
, void *state
)
294 StoreEntry
*entry
= (StoreEntry
*) state
;
295 nodedata
->stat(entry
);
300 /* TODO: move onto String */
302 stringHasWhitespace(const char *s
)
304 return strpbrk(s
, w_space
) != NULL
;
307 /* TODO: move onto String */
309 stringHasCntl(const char *s
)
313 while ((c
= (unsigned char) *s
++) != '\0') {
317 if (c
>= 0x7f && c
<= 0x9f)
325 * Similar to strtok, but has some rudimentary knowledge
329 strwordtok(char *buf
, char **t
)
331 unsigned char *word
= NULL
;
332 unsigned char *p
= (unsigned char *) buf
;
338 p
= (unsigned char *) *t
;
343 while (*p
&& xisspace(*p
))
391 if (!quoted
&& xisspace(*p
)) {
406 return (char *) word
;
410 checkNullString(const char *p
)
412 return p
? p
: "(NULL)";
416 #include "String.cci"