]>
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.
36 #include "squid-old.h"
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());
253 Must(to
> 0 && to
<= size());
257 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 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"