]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/String.cc
3 * DEBUG: section 67 String
4 * AUTHOR: Duane Wessels
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
35 #include "base/TextException.h"
37 #include "mgr/Registration.h"
38 #include "profiler/Profiler.h"
46 Must(size() < INT_MAX
);
50 // low-level buffer allocation,
51 // does not free old buffer and does not adjust or look at len_
53 String::allocBuffer(String::size_type sz
)
55 PROF_start(StringInitBuf
);
57 char *newBuffer
= (char*)memAllocString(sz
, &sz
);
58 setBuffer(newBuffer
, sz
);
59 PROF_stop(StringInitBuf
);
62 // low-level buffer assignment
63 // does not free old buffer and does not adjust or look at len_
65 String::setBuffer(char *aBuf
, String::size_type aSize
)
68 assert(aSize
< 65536);
73 String::String(char const *aString
) : size_(0), len_(0), buf_(NULL
)
76 allocAndFill(aString
, strlen(aString
));
79 StringRegistry::Instance().add(this);
84 String::operator =(char const *aString
)
91 String::operator =(String
const &old
)
93 clean(); // TODO: optimize to avoid cleaning the buffer we can use
95 allocAndFill(old
.rawBuf(), old
.size());
100 String::operator ==(String
const &that
) const
102 if (0 == this->cmp(that
))
109 String::operator !=(String
const &that
) const
111 if (0 == this->cmp(that
))
117 // public interface, makes sure that we clean the old buffer first
119 String::limitInit(const char *str
, int len
)
121 clean(); // TODO: optimize to avoid cleaning the buffer we can use
122 allocAndFill(str
, len
);
125 // Allocates the buffer to fit the supplied string and fills it.
128 String::allocAndFill(const char *str
, int len
)
130 PROF_start(StringAllocAndFill
);
132 allocBuffer(len
+ 1);
134 memcpy(buf_
, str
, len
);
136 PROF_stop(StringAllocAndFill
);
139 String::String(String
const &old
) : size_(0), len_(0), buf_(NULL
)
142 allocAndFill(old
.rawBuf(), old
.size());
145 StringRegistry::Instance().add(this);
152 PROF_start(StringClean
);
155 /* TODO if mempools has already closed this will FAIL!! */
157 memFreeString(size_
, buf_
);
164 PROF_stop(StringClean
);
172 StringRegistry::Instance().remove(this);
177 String::reset(char const *str
)
179 PROF_start(StringReset
);
180 clean(); // TODO: optimize to avoid cleaning the buffer if we can reuse it
182 allocAndFill(str
, strlen(str
));
183 PROF_stop(StringReset
);
187 String::append( char const *str
, int len
)
190 assert(str
&& len
>= 0);
192 PROF_start(StringAppend
);
193 if (len_
+ len
< size_
) {
194 strncat(buf_
, str
, len
);
197 // Create a temporary string and absorb it later.
199 assert(len_
+ len
< 65536); // otherwise snew.len_ overflows below
200 snew
.len_
= len_
+ len
;
201 snew
.allocBuffer(snew
.len_
+ 1);
204 memcpy(snew
.buf_
, rawBuf(), len_
);
207 memcpy(snew
.buf_
+ len_
, str
, len
);
209 snew
.buf_
[snew
.len_
] = '\0';
213 PROF_stop(StringAppend
);
217 String::append(char const *str
)
220 append(str
, strlen(str
));
224 String::append(char const chr
)
233 String::append(String
const &old
)
235 append(old
.rawBuf(), old
.len_
);
239 String::absorb(String
&old
)
242 setBuffer(old
.buf_
, old
.size_
);
250 String::substr(String::size_type from
, String::size_type to
) const
252 // Must(from >= 0 && from < size());
254 Must(to
> 0 && to
<= size());
258 rv
.limitInit(rawBuf()+from
,to
-from
);
264 String::stat(StoreEntry
*entry
) const
266 storeAppendPrintf(entry
, "%p : %d/%d \"%.*s\"\n",this,len_
, size_
, size(), rawBuf());
270 StringRegistry::Instance()
277 ptrcmp(C
const &lhs
, C
const &rhs
)
282 StringRegistry::StringRegistry()
285 Mgr::RegisterAction("strings",
286 "Strings in use in squid", Stat
, 0, 1);
291 StringRegistry::add(String
const *entry
)
293 entries
.insert(entry
, ptrcmp
);
297 StringRegistry::remove(String
const *entry
)
299 entries
.remove(entry
, ptrcmp
);
302 StringRegistry
StringRegistry::Instance_
;
304 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
))
415 if (!quoted
&& xisspace(*p
)) {
432 return (char *) word
;
436 checkNullString(const char *p
)
438 return p
? p
: "(NULL)";
442 String::pos(char const *aString
) const
446 return strstr(termedBuf(), aString
);
450 String::pos(char const ch
) const
454 return strchr(termedBuf(), ch
);
458 String::rpos(char const ch
) const
462 return strrchr(termedBuf(), (ch
));
466 String::find(char const ch
) const
476 String::find(char const *aString
) const
486 String::rfind(char const ch
) const
496 #include "String.cci"