]>
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
);
55 // low-level buffer allocation,
56 // does not free old buffer and does not adjust or look at len_
58 String::allocBuffer(String::size_type sz
)
60 PROF_start(StringInitBuf
);
62 char *newBuffer
= (char*)memAllocString(sz
, &sz
);
63 setBuffer(newBuffer
, sz
);
64 PROF_stop(StringInitBuf
);
67 // low-level buffer assignment
68 // does not free old buffer and does not adjust or look at len_
70 String::setBuffer(char *aBuf
, String::size_type aSize
)
73 assert(aSize
< 65536);
78 String::String (char const *aString
) : size_(0), len_(0), buf_(NULL
)
81 allocAndFill(aString
, strlen(aString
));
84 StringRegistry::Instance().add(this);
89 String::operator =(char const *aString
)
96 String::operator = (String
const &old
)
98 clean(); // TODO: optimize to avoid cleaning the buffer we can use
100 allocAndFill(old
.rawBuf(), old
.size());
105 String::operator == (String
const &that
) const
107 if (0 == this->cmp(that
))
114 String::operator != (String
const &that
) const
116 if (0 == this->cmp(that
))
122 // public interface, makes sure that we clean the old buffer first
124 String::limitInit(const char *str
, int len
)
126 clean(); // TODO: optimize to avoid cleaning the buffer we can use
127 allocAndFill(str
, len
);
130 // Allocates the buffer to fit the supplied string and fills it.
133 String::allocAndFill(const char *str
, int len
)
135 PROF_start(StringAllocAndFill
);
137 allocBuffer(len
+ 1);
139 memcpy(buf_
, str
, len
);
141 PROF_stop(StringAllocAndFill
);
144 String::String (String
const &old
) : size_(0), len_(0), buf_(NULL
)
147 allocAndFill(old
.rawBuf(), old
.size());
150 StringRegistry::Instance().add(this);
157 PROF_start(StringClean
);
160 /* TODO if mempools has already closed this will FAIL!! */
162 memFreeString(size_
, buf_
);
169 PROF_stop(StringClean
);
177 StringRegistry::Instance().remove(this);
182 String::reset(const char *str
)
184 PROF_start(StringReset
);
185 clean(); // TODO: optimize to avoid cleaning the buffer if we can reuse it
187 allocAndFill(str
, strlen(str
));
188 PROF_stop(StringReset
);
192 String::append(const char *str
, int len
)
195 assert(str
&& len
>= 0);
197 PROF_start(StringAppend
);
198 if (len_
+ len
< size_
) {
199 strncat(buf_
, str
, len
);
202 // Create a temporary string and absorb it later.
204 assert(len_
+ len
< 65536); // otherwise snew.len_ overflows below
205 snew
.len_
= len_
+ len
;
206 snew
.allocBuffer(snew
.len_
+ 1);
209 memcpy(snew
.buf_
, rawBuf(), len_
);
212 memcpy(snew
.buf_
+ len_
, str
, len
);
214 snew
.buf_
[snew
.len_
] = '\0';
218 PROF_stop(StringAppend
);
222 String::append(char const *str
)
225 append (str
, strlen(str
));
229 String::append (char chr
)
234 append (myString
, 1);
238 String::append(String
const &old
)
240 append (old
.rawBuf(), old
.len_
);
244 String::absorb(String
&old
)
247 setBuffer(old
.buf_
, old
.size_
);
255 String::substr(String::size_type from
, String::size_type to
) const
257 // Must(from >= 0 && from < size());
259 Must(to
> 0 && to
<= size());
263 rv
.limitInit(rawBuf()+from
,to
-from
);
269 String::stat(StoreEntry
*entry
) const
271 storeAppendPrintf(entry
, "%p : %d/%d \"%.*s\"\n",this,len_
, size_
, size(), rawBuf());
275 StringRegistry::Instance()
282 ptrcmp(C
const &lhs
, C
const &rhs
)
287 StringRegistry::StringRegistry()
290 Mgr::RegisterAction("strings",
291 "Strings in use in squid", Stat
, 0, 1);
296 StringRegistry::add(String
const *entry
)
298 entries
.insert(entry
, ptrcmp
);
302 StringRegistry::remove(String
const *entry
)
304 entries
.remove(entry
, ptrcmp
);
307 StringRegistry
StringRegistry::Instance_
;
309 extern String::size_type
memStringCount();
312 StringRegistry::Stat(StoreEntry
*entry
)
314 storeAppendPrintf(entry
, "%lu entries, %lu reported from MemPool\n", (unsigned long) Instance().entries
.elements
, (unsigned long) memStringCount());
315 Instance().entries
.head
->walk(Stater
, entry
);
319 StringRegistry::Stater(String
const * const & nodedata
, void *state
)
321 StoreEntry
*entry
= (StoreEntry
*) state
;
322 nodedata
->stat(entry
);
327 /* TODO: move onto String */
329 stringHasWhitespace(const char *s
)
331 return strpbrk(s
, w_space
) != NULL
;
334 /* TODO: move onto String */
336 stringHasCntl(const char *s
)
340 while ((c
= (unsigned char) *s
++) != '\0') {
344 if (c
>= 0x7f && c
<= 0x9f)
352 * Similar to strtok, but has some rudimentary knowledge
356 strwordtok(char *buf
, char **t
)
358 unsigned char *word
= NULL
;
359 unsigned char *p
= (unsigned char *) buf
;
365 p
= (unsigned char *) *t
;
370 while (*p
&& xisspace(*p
))
419 if (!quoted
&& xisspace(*p
)) {
436 return (char *) word
;
440 checkNullString(const char *p
)
442 return p
? p
: "(NULL)";
446 String::pos(char const *aString
) const
450 return strstr(termedBuf(), aString
);
454 String::pos(char const ch
) const
458 return strchr(termedBuf(), ch
);
462 String::rpos(char const ch
) const
466 return strrchr(termedBuf(), (ch
));
470 String::find(char const ch
) const
480 String::find(char const *aString
) const
490 String::rfind(char const ch
) const
500 #include "String.cci"