]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/String.cc
2 * Copyright (C) 1996-2021 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"
18 // low-level buffer allocation,
19 // does not free old buffer and does not adjust or look at len_
21 String::allocBuffer(String::size_type sz
)
24 char *newBuffer
= (char*)memAllocString(sz
, &sz
);
25 setBuffer(newBuffer
, sz
);
28 // low-level buffer assignment
29 // does not free old buffer and does not adjust or look at len_
31 String::setBuffer(char *aBuf
, String::size_type aSize
)
34 assert(aSize
<= SizeMax_
);
42 StringRegistry::Instance().add(this);
46 String::String(char const *aString
)
49 allocAndFill(aString
, strlen(aString
));
51 StringRegistry::Instance().add(this);
56 String::operator =(char const *aString
)
63 String::operator =(String
const &old
)
65 clean(); // TODO: optimize to avoid cleaning the buffer we can use
67 allocAndFill(old
.rawBuf(), old
.size());
72 String::operator ==(String
const &that
) const
74 if (0 == this->cmp(that
))
81 String::operator !=(String
const &that
) const
83 if (0 == this->cmp(that
))
89 // public interface, makes sure that we clean the old buffer first
91 String::assign(const char *str
, int len
)
93 clean(); // TODO: optimize to avoid cleaning the buffer we can use
94 allocAndFill(str
, len
);
97 // Allocates the buffer to fit the supplied string and fills it.
100 String::allocAndFill(const char *str
, int len
)
103 allocBuffer(len
+ 1);
105 memcpy(buf_
, str
, len
);
109 String::String(String
const &old
) : size_(0), len_(0), buf_(NULL
)
112 allocAndFill(old
.rawBuf(), old
.size());
115 StringRegistry::Instance().add(this);
122 /* TODO if mempools has already closed this will FAIL!! */
124 memFreeString(size_
, buf_
);
138 StringRegistry::Instance().remove(this);
143 String::reset(char const *str
)
145 clean(); // TODO: optimize to avoid cleaning the buffer if we can reuse it
147 allocAndFill(str
, strlen(str
));
151 String::append( char const *str
, int len
)
153 assert(str
&& len
>= 0);
155 if (len_
+ len
+ 1 /*'\0'*/ < size_
) {
156 xstrncpy(buf_
+len_
, str
, len
+1);
159 // Create a temporary string and absorb it later.
161 assert(canGrowBy(len
)); // otherwise snew.len_ may overflow below
162 snew
.len_
= len_
+ len
;
163 snew
.allocBuffer(snew
.len_
+ 1);
166 memcpy(snew
.buf_
, rawBuf(), len_
);
169 memcpy(snew
.buf_
+ len_
, str
, len
);
171 snew
.buf_
[snew
.len_
] = '\0';
178 String::append(char const *str
)
181 append(str
, strlen(str
));
185 String::append(char const chr
)
194 String::append(String
const &old
)
196 append(old
.rawBuf(), old
.len_
);
200 String::absorb(String
&old
)
203 setBuffer(old
.buf_
, old
.size_
);
211 String::substr(String::size_type from
, String::size_type to
) const
213 // Must(from >= 0 && from < size());
215 Must(to
> 0 && to
<= size());
219 rv
.assign(rawBuf()+from
, to
-from
);
224 String::cut(String::size_type newLength
)
226 // size_type is size_t, unsigned. No need to check for newLength <0
227 if (newLength
> len_
) return;
231 // buf_ may be nullptr on zero-length strings.
232 if (len_
== 0 && !buf_
)
235 buf_
[newLength
] = '\0';
238 /// compare NULL and empty strings because str*cmp() may fail on NULL strings
239 /// and because we need to return consistent results for strncmp(count == 0).
241 nilCmp(const bool thisIsNilOrEmpty
, const bool otherIsNilOrEmpty
, int &result
)
243 if (!thisIsNilOrEmpty
&& !otherIsNilOrEmpty
)
244 return false; // result does not matter
246 if (thisIsNilOrEmpty
&& otherIsNilOrEmpty
)
248 else if (thisIsNilOrEmpty
)
250 else // otherIsNilOrEmpty
257 String::cmp(char const *aString
) const
260 if (nilCmp(!size(), (!aString
|| !*aString
), result
))
263 return strcmp(termedBuf(), aString
);
267 String::cmp(char const *aString
, String::size_type count
) const
270 if (nilCmp((!size() || !count
), (!aString
|| !*aString
|| !count
), result
))
273 return strncmp(termedBuf(), aString
, count
);
277 String::cmp(String
const &aString
) const
280 if (nilCmp(!size(), !aString
.size(), result
))
283 return strcmp(termedBuf(), aString
.termedBuf());
287 String::caseCmp(char const *aString
) const
290 if (nilCmp(!size(), (!aString
|| !*aString
), result
))
293 return strcasecmp(termedBuf(), aString
);
297 String::caseCmp(char const *aString
, String::size_type count
) const
300 if (nilCmp((!size() || !count
), (!aString
|| !*aString
|| !count
), result
))
303 return strncasecmp(termedBuf(), aString
, count
);
308 String::stat(StoreEntry
*entry
) const
310 storeAppendPrintf(entry
, "%p : %d/%d \"%.*s\"\n",this,len_
, size_
, size(), rawBuf());
314 StringRegistry::Instance()
321 ptrcmp(C
const &lhs
, C
const &rhs
)
326 StringRegistry::StringRegistry()
329 Mgr::RegisterAction("strings",
330 "Strings in use in squid", Stat
, 0, 1);
335 StringRegistry::add(String
const *entry
)
337 entries
.insert(entry
, ptrcmp
);
341 StringRegistry::remove(String
const *entry
)
343 entries
.remove(entry
, ptrcmp
);
346 StringRegistry
StringRegistry::Instance_
;
348 String::size_type
memStringCount();
351 StringRegistry::Stat(StoreEntry
*entry
)
353 storeAppendPrintf(entry
, "%lu entries, %lu reported from MemPool\n", (unsigned long) Instance().entries
.elements
, (unsigned long) memStringCount());
354 Instance().entries
.head
->walk(Stater
, entry
);
358 StringRegistry::Stater(String
const * const & nodedata
, void *state
)
360 StoreEntry
*entry
= (StoreEntry
*) state
;
361 nodedata
->stat(entry
);
366 /* TODO: move onto String */
368 stringHasWhitespace(const char *s
)
370 return strpbrk(s
, w_space
) != NULL
;
373 /* TODO: move onto String */
375 stringHasCntl(const char *s
)
379 while ((c
= (unsigned char) *s
++) != '\0') {
383 if (c
>= 0x7f && c
<= 0x9f)
391 * Similar to strtok, but has some rudimentary knowledge
395 strwordtok(char *buf
, char **t
)
397 unsigned char *word
= NULL
;
398 unsigned char *p
= (unsigned char *) buf
;
404 p
= (unsigned char *) *t
;
409 while (*p
&& xisspace(*p
))
459 if (!quoted
&& xisspace(*p
)) {
476 return (char *) word
;
480 checkNullString(const char *p
)
482 return p
? p
: "(NULL)";
486 String::pos(char const *aString
) const
490 return strstr(termedBuf(), aString
);
494 String::pos(char const ch
) const
498 return strchr(termedBuf(), ch
);
502 String::rpos(char const ch
) const
506 return strrchr(termedBuf(), (ch
));
510 String::find(char const ch
) const
520 String::find(char const *aString
) const
530 String::rfind(char const ch
) const