]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/String.cc
2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 67 String */
12 #include "base/TextException.h"
13 #include "mgr/Registration.h"
14 #include "profiler/Profiler.h"
22 Must(size() < INT_MAX
);
26 // low-level buffer allocation,
27 // does not free old buffer and does not adjust or look at len_
29 String::allocBuffer(String::size_type sz
)
31 PROF_start(StringInitBuf
);
33 char *newBuffer
= (char*)memAllocString(sz
, &sz
);
34 setBuffer(newBuffer
, sz
);
35 PROF_stop(StringInitBuf
);
38 // low-level buffer assignment
39 // does not free old buffer and does not adjust or look at len_
41 String::setBuffer(char *aBuf
, String::size_type aSize
)
44 assert(aSize
<= SizeMax_
);
49 String::String(char const *aString
) : size_(0), len_(0), buf_(NULL
)
52 allocAndFill(aString
, strlen(aString
));
55 StringRegistry::Instance().add(this);
60 String::operator =(char const *aString
)
67 String::operator =(String
const &old
)
69 clean(); // TODO: optimize to avoid cleaning the buffer we can use
71 allocAndFill(old
.rawBuf(), old
.size());
76 String::operator ==(String
const &that
) const
78 if (0 == this->cmp(that
))
85 String::operator !=(String
const &that
) const
87 if (0 == this->cmp(that
))
93 // public interface, makes sure that we clean the old buffer first
95 String::limitInit(const char *str
, int len
)
97 clean(); // TODO: optimize to avoid cleaning the buffer we can use
98 allocAndFill(str
, len
);
101 // Allocates the buffer to fit the supplied string and fills it.
104 String::allocAndFill(const char *str
, int len
)
106 PROF_start(StringAllocAndFill
);
108 allocBuffer(len
+ 1);
110 memcpy(buf_
, str
, len
);
112 PROF_stop(StringAllocAndFill
);
115 String::String(String
const &old
) : size_(0), len_(0), buf_(NULL
)
118 allocAndFill(old
.rawBuf(), old
.size());
121 StringRegistry::Instance().add(this);
128 PROF_start(StringClean
);
130 /* TODO if mempools has already closed this will FAIL!! */
132 memFreeString(size_
, buf_
);
139 PROF_stop(StringClean
);
147 StringRegistry::Instance().remove(this);
152 String::reset(char const *str
)
154 PROF_start(StringReset
);
155 clean(); // TODO: optimize to avoid cleaning the buffer if we can reuse it
157 allocAndFill(str
, strlen(str
));
158 PROF_stop(StringReset
);
162 String::append( char const *str
, int len
)
164 assert(str
&& len
>= 0);
166 PROF_start(StringAppend
);
167 if (len_
+ len
< size_
) {
168 strncat(buf_
, str
, len
);
171 // Create a temporary string and absorb it later.
173 assert(canGrowBy(len
)); // otherwise snew.len_ may overflow below
174 snew
.len_
= len_
+ len
;
175 snew
.allocBuffer(snew
.len_
+ 1);
178 memcpy(snew
.buf_
, rawBuf(), len_
);
181 memcpy(snew
.buf_
+ len_
, str
, len
);
183 snew
.buf_
[snew
.len_
] = '\0';
187 PROF_stop(StringAppend
);
191 String::append(char const *str
)
194 append(str
, strlen(str
));
198 String::append(char const chr
)
207 String::append(String
const &old
)
209 append(old
.rawBuf(), old
.len_
);
213 String::absorb(String
&old
)
216 setBuffer(old
.buf_
, old
.size_
);
224 String::substr(String::size_type from
, String::size_type to
) const
226 // Must(from >= 0 && from < size());
228 Must(to
> 0 && to
<= size());
232 rv
.limitInit(rawBuf()+from
,to
-from
);
238 String::stat(StoreEntry
*entry
) const
240 storeAppendPrintf(entry
, "%p : %d/%d \"%.*s\"\n",this,len_
, size_
, size(), rawBuf());
244 StringRegistry::Instance()
251 ptrcmp(C
const &lhs
, C
const &rhs
)
256 StringRegistry::StringRegistry()
259 Mgr::RegisterAction("strings",
260 "Strings in use in squid", Stat
, 0, 1);
265 StringRegistry::add(String
const *entry
)
267 entries
.insert(entry
, ptrcmp
);
271 StringRegistry::remove(String
const *entry
)
273 entries
.remove(entry
, ptrcmp
);
276 StringRegistry
StringRegistry::Instance_
;
278 String::size_type
memStringCount();
281 StringRegistry::Stat(StoreEntry
*entry
)
283 storeAppendPrintf(entry
, "%lu entries, %lu reported from MemPool\n", (unsigned long) Instance().entries
.elements
, (unsigned long) memStringCount());
284 Instance().entries
.head
->walk(Stater
, entry
);
288 StringRegistry::Stater(String
const * const & nodedata
, void *state
)
290 StoreEntry
*entry
= (StoreEntry
*) state
;
291 nodedata
->stat(entry
);
296 /* TODO: move onto String */
298 stringHasWhitespace(const char *s
)
300 return strpbrk(s
, w_space
) != NULL
;
303 /* TODO: move onto String */
305 stringHasCntl(const char *s
)
309 while ((c
= (unsigned char) *s
++) != '\0') {
313 if (c
>= 0x7f && c
<= 0x9f)
321 * Similar to strtok, but has some rudimentary knowledge
325 strwordtok(char *buf
, char **t
)
327 unsigned char *word
= NULL
;
328 unsigned char *p
= (unsigned char *) buf
;
334 p
= (unsigned char *) *t
;
339 while (*p
&& xisspace(*p
))
389 if (!quoted
&& xisspace(*p
)) {
406 return (char *) word
;
410 checkNullString(const char *p
)
412 return p
? p
: "(NULL)";
416 String::pos(char const *aString
) const
420 return strstr(termedBuf(), aString
);
424 String::pos(char const ch
) const
428 return strchr(termedBuf(), ch
);
432 String::rpos(char const ch
) const
436 return strrchr(termedBuf(), (ch
));
440 String::find(char const ch
) const
450 String::find(char const *aString
) const
460 String::rfind(char const ch
) const
470 #include "String.cci"