]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/String.cc
2 * Copyright (C) 1996-2023 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.
10 #include "mem/forward.h"
11 #include "SquidString.h"
15 // low-level buffer allocation,
16 // does not free old buffer and does not adjust or look at len_
18 String::allocBuffer(String::size_type sz
)
21 auto *newBuffer
= static_cast<char*>(memAllocBuf(sz
, &sz
));
22 setBuffer(newBuffer
, sz
);
25 // low-level buffer assignment
26 // does not free old buffer and does not adjust or look at len_
28 String::setBuffer(char *aBuf
, String::size_type aSize
)
31 assert(aSize
<= SizeMax_
);
36 String::String(char const *aString
)
39 allocAndFill(aString
, strlen(aString
));
43 String::operator =(char const *aString
)
50 String::operator =(String
const &old
)
52 clean(); // TODO: optimize to avoid cleaning the buffer we can use
54 allocAndFill(old
.rawBuf(), old
.size());
59 String::operator ==(String
const &that
) const
61 if (0 == this->cmp(that
))
68 String::operator !=(String
const &that
) const
70 if (0 == this->cmp(that
))
76 // public interface, makes sure that we clean the old buffer first
78 String::assign(const char *str
, int len
)
80 clean(); // TODO: optimize to avoid cleaning the buffer we can use
81 allocAndFill(str
, len
);
84 // Allocates the buffer to fit the supplied string and fills it.
87 String::allocAndFill(const char *str
, int len
)
92 memcpy(buf_
, str
, len
);
96 String::String(String
const &old
) : size_(0), len_(0), buf_(nullptr)
99 allocAndFill(old
.rawBuf(), old
.size());
105 /* TODO if mempools has already closed this will FAIL!! */
107 memFreeBuf(size_
, buf_
);
122 String::reset(char const *str
)
124 clean(); // TODO: optimize to avoid cleaning the buffer if we can reuse it
126 allocAndFill(str
, strlen(str
));
130 String::append( char const *str
, int len
)
132 assert(str
&& len
>= 0);
134 if (len_
+ len
+ 1 /*'\0'*/ < size_
) {
135 xstrncpy(buf_
+len_
, str
, len
+1);
138 // Create a temporary string and absorb it later.
140 assert(canGrowBy(len
)); // otherwise snew.len_ may overflow below
141 snew
.len_
= len_
+ len
;
142 snew
.allocBuffer(snew
.len_
+ 1);
145 memcpy(snew
.buf_
, rawBuf(), len_
);
148 memcpy(snew
.buf_
+ len_
, str
, len
);
150 snew
.buf_
[snew
.len_
] = '\0';
157 String::append(char const *str
)
160 append(str
, strlen(str
));
164 String::append(char const chr
)
173 String::append(String
const &old
)
175 append(old
.rawBuf(), old
.len_
);
179 String::absorb(String
&old
)
182 setBuffer(old
.buf_
, old
.size_
);
190 String::substr(String::size_type from
, String::size_type to
) const
192 // Must(from >= 0 && from < size());
194 Must(to
> 0 && to
<= size());
198 rv
.assign(rawBuf()+from
, to
-from
);
203 String::cut(String::size_type newLength
)
205 // size_type is size_t, unsigned. No need to check for newLength <0
206 if (newLength
> len_
) return;
210 // buf_ may be nullptr on zero-length strings.
211 if (len_
== 0 && !buf_
)
214 buf_
[newLength
] = '\0';
217 /// compare NULL and empty strings because str*cmp() may fail on NULL strings
218 /// and because we need to return consistent results for strncmp(count == 0).
220 nilCmp(const bool thisIsNilOrEmpty
, const bool otherIsNilOrEmpty
, int &result
)
222 if (!thisIsNilOrEmpty
&& !otherIsNilOrEmpty
)
223 return false; // result does not matter
225 if (thisIsNilOrEmpty
&& otherIsNilOrEmpty
)
227 else if (thisIsNilOrEmpty
)
229 else // otherIsNilOrEmpty
236 String::cmp(char const *aString
) const
239 if (nilCmp(!size(), (!aString
|| !*aString
), result
))
242 return strcmp(termedBuf(), aString
);
246 String::cmp(char const *aString
, String::size_type count
) const
249 if (nilCmp((!size() || !count
), (!aString
|| !*aString
|| !count
), result
))
252 return strncmp(termedBuf(), aString
, count
);
256 String::cmp(String
const &aString
) const
259 if (nilCmp(!size(), !aString
.size(), result
))
262 return strcmp(termedBuf(), aString
.termedBuf());
266 String::caseCmp(char const *aString
) const
269 if (nilCmp(!size(), (!aString
|| !*aString
), result
))
272 return strcasecmp(termedBuf(), aString
);
276 String::caseCmp(char const *aString
, String::size_type count
) const
279 if (nilCmp((!size() || !count
), (!aString
|| !*aString
|| !count
), result
))
282 return strncasecmp(termedBuf(), aString
, count
);
285 /* TODO: move onto String */
287 stringHasWhitespace(const char *s
)
289 return strpbrk(s
, w_space
) != nullptr;
292 /* TODO: move onto String */
294 stringHasCntl(const char *s
)
298 while ((c
= (unsigned char) *s
++) != '\0') {
302 if (c
>= 0x7f && c
<= 0x9f)
310 * Similar to strtok, but has some rudimentary knowledge
314 strwordtok(char *buf
, char **t
)
316 unsigned char *word
= nullptr;
317 unsigned char *p
= (unsigned char *) buf
;
323 p
= (unsigned char *) *t
;
328 while (*p
&& xisspace(*p
))
378 if (!quoted
&& xisspace(*p
)) {
395 return (char *) word
;
399 checkNullString(const char *p
)
401 return p
? p
: "(NULL)";
405 String::pos(char const *aString
) const
409 return strstr(termedBuf(), aString
);
413 String::pos(char const ch
) const
417 return strchr(termedBuf(), ch
);
421 String::rpos(char const ch
) const
425 return strrchr(termedBuf(), (ch
));
429 String::find(char const ch
) const
439 String::find(char const *aString
) const
449 String::rfind(char const ch
) const