]>
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.
37 #include "base/TextException.h"
39 #include "mgr/Registration.h"
40 #include "profiler/Profiler.h"
51 Must(size() < INT_MAX
);
56 // low-level buffer allocation,
57 // does not free old buffer and does not adjust or look at len_
59 String::allocBuffer(String::size_type sz
)
61 PROF_start(StringInitBuf
);
63 char *newBuffer
= (char*)memAllocString(sz
, &sz
);
64 setBuffer(newBuffer
, sz
);
65 PROF_stop(StringInitBuf
);
68 // low-level buffer assignment
69 // does not free old buffer and does not adjust or look at len_
71 String::setBuffer(char *aBuf
, String::size_type aSize
)
74 assert(aSize
< 65536);
79 String::String (char const *aString
) : size_(0), len_(0), buf_(NULL
)
82 allocAndFill(aString
, strlen(aString
));
85 StringRegistry::Instance().add(this);
90 String::operator =(char const *aString
)
97 String::operator = (String
const &old
)
99 clean(); // TODO: optimize to avoid cleaning the buffer we can use
101 allocAndFill(old
.rawBuf(), old
.size());
106 String::operator == (String
const &that
) const
108 if (0 == this->cmp(that
))
115 String::operator != (String
const &that
) const
117 if (0 == this->cmp(that
))
123 // public interface, makes sure that we clean the old buffer first
125 String::limitInit(const char *str
, int len
)
127 clean(); // TODO: optimize to avoid cleaning the buffer we can use
128 allocAndFill(str
, len
);
131 // Allocates the buffer to fit the supplied string and fills it.
134 String::allocAndFill(const char *str
, int len
)
136 PROF_start(StringAllocAndFill
);
138 allocBuffer(len
+ 1);
140 memcpy(buf_
, str
, len
);
142 PROF_stop(StringAllocAndFill
);
145 String::String (String
const &old
) : size_(0), len_(0), buf_(NULL
)
148 allocAndFill(old
.rawBuf(), old
.size());
151 StringRegistry::Instance().add(this);
158 PROF_start(StringClean
);
161 /* TODO if mempools has already closed this will FAIL!! */
163 memFreeString(size_
, buf_
);
170 PROF_stop(StringClean
);
178 StringRegistry::Instance().remove(this);
183 String::reset(const char *str
)
185 PROF_start(StringReset
);
186 clean(); // TODO: optimize to avoid cleaning the buffer if we can reuse it
188 allocAndFill(str
, strlen(str
));
189 PROF_stop(StringReset
);
193 String::append(const char *str
, int len
)
196 assert(str
&& len
>= 0);
198 PROF_start(StringAppend
);
199 if (len_
+ len
< size_
) {
200 strncat(buf_
, str
, len
);
203 // Create a temporary string and absorb it later.
205 assert(len_
+ len
< 65536); // otherwise snew.len_ overflows below
206 snew
.len_
= len_
+ len
;
207 snew
.allocBuffer(snew
.len_
+ 1);
210 memcpy(snew
.buf_
, rawBuf(), len_
);
213 memcpy(snew
.buf_
+ len_
, str
, len
);
215 snew
.buf_
[snew
.len_
] = '\0';
219 PROF_stop(StringAppend
);
223 String::append(char const *str
)
226 append (str
, strlen(str
));
230 String::append (char chr
)
235 append (myString
, 1);
239 String::append(String
const &old
)
241 append (old
.rawBuf(), old
.len_
);
245 String::absorb(String
&old
)
248 setBuffer(old
.buf_
, old
.size_
);
256 String::substr(String::size_type from
, String::size_type to
) const
258 // Must(from >= 0 && from < size());
260 Must(to
> 0 && to
<= size());
264 rv
.limitInit(rawBuf()+from
,to
-from
);
271 String::stat(StoreEntry
*entry
) const
273 storeAppendPrintf(entry
, "%p : %d/%d \"%.*s\"\n",this,len_
, size_
, size(), rawBuf());
277 StringRegistry::Instance()
284 ptrcmp(C
const &lhs
, C
const &rhs
)
289 StringRegistry::StringRegistry()
292 Mgr::RegisterAction("strings",
293 "Strings in use in squid", Stat
, 0, 1);
298 StringRegistry::add(String
const *entry
)
300 entries
.insert(entry
, ptrcmp
);
304 StringRegistry::remove(String
const *entry
)
306 entries
.remove(entry
, ptrcmp
);
309 StringRegistry
StringRegistry::Instance_
;
311 extern String::size_type
memStringCount();
314 StringRegistry::Stat(StoreEntry
*entry
)
316 storeAppendPrintf(entry
, "%lu entries, %lu reported from MemPool\n", (unsigned long) Instance().entries
.elements
, (unsigned long) memStringCount());
317 Instance().entries
.head
->walk(Stater
, entry
);
321 StringRegistry::Stater(String
const * const & nodedata
, void *state
)
323 StoreEntry
*entry
= (StoreEntry
*) state
;
324 nodedata
->stat(entry
);
329 /* TODO: move onto String */
331 stringHasWhitespace(const char *s
)
333 return strpbrk(s
, w_space
) != NULL
;
336 /* TODO: move onto String */
338 stringHasCntl(const char *s
)
342 while ((c
= (unsigned char) *s
++) != '\0') {
346 if (c
>= 0x7f && c
<= 0x9f)
354 * Similar to strtok, but has some rudimentary knowledge
358 strwordtok(char *buf
, char **t
)
360 unsigned char *word
= NULL
;
361 unsigned char *p
= (unsigned char *) buf
;
367 p
= (unsigned char *) *t
;
372 while (*p
&& xisspace(*p
))
421 if (!quoted
&& xisspace(*p
)) {
438 return (char *) word
;
442 checkNullString(const char *p
)
444 return p
? p
: "(NULL)";
448 String::pos(char const *aString
) const
452 return strstr(termedBuf(), aString
);
456 String::pos(char const ch
) const
460 return strchr(termedBuf(), ch
);
464 String::rpos(char const ch
) const
468 return strrchr(termedBuf(), (ch
));
472 String::find(char const ch
) const
482 String::find(char const *aString
) const
492 String::rfind(char const ch
) const
504 #include "String.cci"