]>
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"
48 Must(size() < INT_MAX
);
52 // low-level buffer allocation,
53 // does not free old buffer and does not adjust or look at len_
55 String::allocBuffer(String::size_type sz
)
57 PROF_start(StringInitBuf
);
59 char *newBuffer
= (char*)memAllocString(sz
, &sz
);
60 setBuffer(newBuffer
, sz
);
61 PROF_stop(StringInitBuf
);
64 // low-level buffer assignment
65 // does not free old buffer and does not adjust or look at len_
67 String::setBuffer(char *aBuf
, String::size_type aSize
)
70 assert(aSize
< 65536);
75 String::String(char const *aString
) : size_(0), len_(0), buf_(NULL
)
78 allocAndFill(aString
, strlen(aString
));
81 StringRegistry::Instance().add(this);
86 String::operator =(char const *aString
)
93 String::operator =(String
const &old
)
95 clean(); // TODO: optimize to avoid cleaning the buffer we can use
97 allocAndFill(old
.rawBuf(), old
.size());
102 String::operator ==(String
const &that
) const
104 if (0 == this->cmp(that
))
111 String::operator !=(String
const &that
) const
113 if (0 == this->cmp(that
))
119 // public interface, makes sure that we clean the old buffer first
121 String::limitInit(const char *str
, int len
)
123 clean(); // TODO: optimize to avoid cleaning the buffer we can use
124 allocAndFill(str
, len
);
127 // Allocates the buffer to fit the supplied string and fills it.
130 String::allocAndFill(const char *str
, int len
)
132 PROF_start(StringAllocAndFill
);
134 allocBuffer(len
+ 1);
136 memcpy(buf_
, str
, len
);
138 PROF_stop(StringAllocAndFill
);
141 String::String(String
const &old
) : size_(0), len_(0), buf_(NULL
)
144 allocAndFill(old
.rawBuf(), old
.size());
147 StringRegistry::Instance().add(this);
154 PROF_start(StringClean
);
157 /* TODO if mempools has already closed this will FAIL!! */
159 memFreeString(size_
, buf_
);
166 PROF_stop(StringClean
);
174 StringRegistry::Instance().remove(this);
179 String::reset(char const *str
)
181 PROF_start(StringReset
);
182 clean(); // TODO: optimize to avoid cleaning the buffer if we can reuse it
184 allocAndFill(str
, strlen(str
));
185 PROF_stop(StringReset
);
189 String::append( char const *str
, int len
)
192 assert(str
&& len
>= 0);
194 PROF_start(StringAppend
);
195 if (len_
+ len
< size_
) {
196 strncat(buf_
, str
, len
);
199 // Create a temporary string and absorb it later.
201 assert(len_
+ len
< 65536); // otherwise snew.len_ overflows below
202 snew
.len_
= len_
+ len
;
203 snew
.allocBuffer(snew
.len_
+ 1);
206 memcpy(snew
.buf_
, rawBuf(), len_
);
209 memcpy(snew
.buf_
+ len_
, str
, len
);
211 snew
.buf_
[snew
.len_
] = '\0';
215 PROF_stop(StringAppend
);
219 String::append(char const *str
)
222 append(str
, strlen(str
));
226 String::append(char const chr
)
235 String::append(String
const &old
)
237 append(old
.rawBuf(), old
.len_
);
241 String::absorb(String
&old
)
244 setBuffer(old
.buf_
, old
.size_
);
252 String::substr(String::size_type from
, String::size_type to
) const
254 // Must(from >= 0 && from < size());
256 Must(to
> 0 && to
<= size());
260 rv
.limitInit(rawBuf()+from
,to
-from
);
266 String::stat(StoreEntry
*entry
) const
268 storeAppendPrintf(entry
, "%p : %d/%d \"%.*s\"\n",this,len_
, size_
, size(), rawBuf());
272 StringRegistry::Instance()
279 ptrcmp(C
const &lhs
, C
const &rhs
)
284 StringRegistry::StringRegistry()
287 Mgr::RegisterAction("strings",
288 "Strings in use in squid", Stat
, 0, 1);
293 StringRegistry::add(String
const *entry
)
295 entries
.insert(entry
, ptrcmp
);
299 StringRegistry::remove(String
const *entry
)
301 entries
.remove(entry
, ptrcmp
);
304 StringRegistry
StringRegistry::Instance_
;
306 String::size_type
memStringCount();
309 StringRegistry::Stat(StoreEntry
*entry
)
311 storeAppendPrintf(entry
, "%lu entries, %lu reported from MemPool\n", (unsigned long) Instance().entries
.elements
, (unsigned long) memStringCount());
312 Instance().entries
.head
->walk(Stater
, entry
);
316 StringRegistry::Stater(String
const * const & nodedata
, void *state
)
318 StoreEntry
*entry
= (StoreEntry
*) state
;
319 nodedata
->stat(entry
);
324 /* TODO: move onto String */
326 stringHasWhitespace(const char *s
)
328 return strpbrk(s
, w_space
) != NULL
;
331 /* TODO: move onto String */
333 stringHasCntl(const char *s
)
337 while ((c
= (unsigned char) *s
++) != '\0') {
341 if (c
>= 0x7f && c
<= 0x9f)
349 * Similar to strtok, but has some rudimentary knowledge
353 strwordtok(char *buf
, char **t
)
355 unsigned char *word
= NULL
;
356 unsigned char *p
= (unsigned char *) buf
;
362 p
= (unsigned char *) *t
;
367 while (*p
&& xisspace(*p
))
416 if (!quoted
&& xisspace(*p
)) {
433 return (char *) word
;
437 checkNullString(const char *p
)
439 return p
? p
: "(NULL)";
443 String::pos(char const *aString
) const
447 return strstr(termedBuf(), aString
);
451 String::pos(char const ch
) const
455 return strchr(termedBuf(), ch
);
459 String::rpos(char const ch
) const
463 return strrchr(termedBuf(), (ch
));
467 String::find(char const ch
) const
477 String::find(char const *aString
) const
487 String::rfind(char const ch
) const
497 #include "String.cci"