]>
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.
38 #include "TextException.h"
43 Must(size() < INT_MAX
);
48 // low-level buffer allocation,
49 // does not free old buffer and does not adjust or look at len_
51 String::allocBuffer(String::size_type sz
)
53 PROF_start(StringInitBuf
);
55 char *newBuffer
= (char*)memAllocString(sz
, &sz
);
56 setBuffer(newBuffer
, sz
);
57 PROF_stop(StringInitBuf
);
60 // low-level buffer assignment
61 // does not free old buffer and does not adjust or look at len_
63 String::setBuffer(char *aBuf
, String::size_type aSize
)
66 assert(aSize
< 65536);
71 String::String (char const *aString
) : size_(0), len_(0), buf_(NULL
)
74 allocAndFill(aString
, strlen(aString
));
77 StringRegistry::Instance().add(this);
82 String::operator =(char const *aString
)
89 String::operator = (String
const &old
)
91 clean(); // TODO: optimize to avoid cleaning the buffer we can use
93 allocAndFill(old
.rawBuf(), old
.size());
98 String::operator == (String
const &that
) const
100 if (0 == this->cmp(that
))
107 String::operator != (String
const &that
) const
109 if (0 == this->cmp(that
))
115 // public interface, makes sure that we clean the old buffer first
117 String::limitInit(const char *str
, int len
)
119 clean(); // TODO: optimize to avoid cleaning the buffer we can use
120 allocAndFill(str
, len
);
123 // Allocates the buffer to fit the supplied string and fills it.
126 String::allocAndFill(const char *str
, int len
)
128 PROF_start(StringAllocAndFill
);
130 allocBuffer(len
+ 1);
132 xmemcpy(buf_
, str
, len
);
134 PROF_stop(StringAllocAndFill
);
137 String::String (String
const &old
) : size_(0), len_(0), buf_(NULL
)
140 allocAndFill(old
.rawBuf(), old
.size());
143 StringRegistry::Instance().add(this);
150 PROF_start(StringClean
);
154 memFreeString(size_
, buf_
);
161 PROF_stop(StringClean
);
169 StringRegistry::Instance().remove(this);
174 String::reset(const char *str
)
176 PROF_start(StringReset
);
177 clean(); // TODO: optimize to avoid cleaning the buffer if we can reuse it
179 allocAndFill(str
, strlen(str
));
180 PROF_stop(StringReset
);
184 String::append(const char *str
, int len
)
187 assert(str
&& len
>= 0);
189 PROF_start(StringAppend
);
190 if (len_
+ len
< size_
) {
191 strncat(buf_
, str
, len
);
194 // Create a temporary string and absorb it later.
196 snew
.len_
= len_
+ len
;
197 snew
.allocBuffer(snew
.len_
+ 1);
200 xmemcpy(snew
.buf_
, rawBuf(), len_
);
203 xmemcpy(snew
.buf_
+ len_
, str
, len
);
205 snew
.buf_
[snew
.len_
] = '\0';
209 PROF_stop(StringAppend
);
213 String::append(char const *str
)
216 append (str
, strlen(str
));
220 String::append (char chr
)
225 append (myString
, 1);
229 String::append(String
const &old
)
231 append (old
.rawBuf(), old
.len_
);
235 String::absorb(String
&old
)
238 setBuffer(old
.buf_
, old
.size_
);
246 String::substr(String::size_type from
, String::size_type to
) const
248 Must(from
>= 0 && from
< size());
249 Must(to
> 0 && to
<= size());
253 rv
.limitInit(rawBuf()+from
,to
-from
);
260 String::stat(StoreEntry
*entry
) const
262 storeAppendPrintf(entry
, "%p : %d/%d \"%.*s\"\n",this,len_
, size_
, size(), rawBuf());
266 StringRegistry::Instance()
273 ptrcmp(C
const &lhs
, C
const &rhs
)
278 StringRegistry::StringRegistry()
281 CacheManager::GetInstance()->registerAction("strings",
282 "Strings in use in squid", Stat
, 0, 1);
289 (String
const *entry
)
291 entries
.insert(entry
, ptrcmp
);
296 StringRegistry::remove
297 (String
const *entry
)
299 entries
.remove(entry
, ptrcmp
);
302 StringRegistry
StringRegistry::Instance_
;
304 extern String::size_type
memStringCount();
307 StringRegistry::Stat(StoreEntry
*entry
)
309 storeAppendPrintf(entry
, "%lu entries, %lu reported from MemPool\n", (unsigned long) Instance().entries
.elements
, (unsigned long) memStringCount());
310 Instance().entries
.head
->walk(Stater
, entry
);
314 StringRegistry::Stater(String
const * const & nodedata
, void *state
)
316 StoreEntry
*entry
= (StoreEntry
*) state
;
317 nodedata
->stat(entry
);
322 /* TODO: move onto String */
324 stringHasWhitespace(const char *s
)
326 return strpbrk(s
, w_space
) != NULL
;
329 /* TODO: move onto String */
331 stringHasCntl(const char *s
)
335 while ((c
= (unsigned char) *s
++) != '\0') {
339 if (c
>= 0x7f && c
<= 0x9f)
347 * Similar to strtok, but has some rudimentary knowledge
351 strwordtok(char *buf
, char **t
)
353 unsigned char *word
= NULL
;
354 unsigned char *p
= (unsigned char *) buf
;
360 p
= (unsigned char *) *t
;
365 while (*p
&& xisspace(*p
))
413 if (!quoted
&& xisspace(*p
)) {
428 return (char *) word
;
432 checkNullString(const char *p
)
434 return p
? p
: "(NULL)";
438 String::pos(char const *aString
) const
442 return strstr(termedBuf(), aString
);
446 String::pos(char const ch
) const
450 return strchr(termedBuf(), ch
);
454 String::rpos(char const ch
) const
458 return strrchr(termedBuf(), (ch
));
462 String::find(char const ch
) const
472 String::find(char const *aString
) const
482 String::rfind(char const ch
) const
494 #include "String.cci"