]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/SquidString.h
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 */
11 #ifndef SQUID_STRING_H
12 #define SQUID_STRING_H
14 #include "base/TextException.h"
19 /* squid string placeholder (for printf) */
21 #define SQUIDSTRINGPH "%.*s"
22 #define SQUIDSTRINGPRINT(s) (s).psize(),(s).rawBuf()
23 #endif /* SQUIDSTRINGPH */
31 String(String
const &);
32 String(String
&& S
) : size_(S
.size_
), len_(S
.len_
), buf_(S
.buf_
) {
33 S
.buf_
= nullptr; // S is about to be destructed
38 typedef size_t size_type
; //storage size intentionally unspecified
39 const static size_type npos
= static_cast<size_type
>(-1);
41 String
&operator =(char const *);
42 String
&operator =(String
const &);
43 String
&operator =(String
&& S
) {
50 S
.buf_
= nullptr; // S is about to be destructed
55 bool operator ==(String
const &) const;
56 bool operator !=(String
const &) const;
59 * Retrieve a single character in the string.
60 \param aPos Position of character to retrieve.
62 char operator [](unsigned int aPos
) const {
67 /// The absolute size limit on data held in a String.
68 /// Since Strings can be nil-terminated implicitly it is best to ensure
69 /// the useful content length is strictly less than this limit.
70 static size_type
SizeMaxXXX() { return SizeMax_
; }
72 size_type
size() const { return len_
; }
74 /// variant of size() suited to be used for printf-alikes.
75 /// throws when size() >= INT_MAX
77 Must(size() < INT_MAX
);
82 * Returns a raw pointer to the underlying backing store. The caller has been
83 * verified not to make any assumptions about null-termination
85 char const * rawBuf() const { return buf_
; }
88 * Returns a raw pointer to the underlying backing store.
89 * The caller requires it to be null-terminated.
91 char const * termedBuf() const { return buf_
; }
93 void assign(const char *str
, int len
);
95 void reset(char const *str
);
96 void append(char const *buf
, int len
);
97 void append(char const *buf
);
98 void append(char const);
99 void append(String
const &);
100 void absorb(String
&old
);
101 const char * pos(char const *aString
) const;
102 const char * pos(char const ch
) const;
103 ///offset from string start of the first occurrence of ch
104 /// returns String::npos if ch is not found
105 size_type
find(char const ch
) const;
106 size_type
find(char const *aString
) const;
107 const char * rpos(char const ch
) const;
108 size_type
rfind(char const ch
) const;
109 int cmp(char const *) const;
110 int cmp(char const *, size_type count
) const;
111 int cmp(String
const &) const;
112 int caseCmp(char const *) const;
113 int caseCmp(char const *, size_type count
) const;
114 int caseCmp(String
const &str
) const {
115 return caseCmp(str
.rawBuf(),str
.size());
118 /// Whether creating a totalLen-character string is safe (i.e., unlikely to assert).
119 /// Optional extras can be used for overflow-safe length addition.
120 /// Implementation has to add 1 because many String allocation methods do.
121 static bool CanGrowTo(size_type totalLen
, const size_type extras
= 0) { return SafeAdd(totalLen
, extras
) && SafeAdd(totalLen
, 1); }
122 /// whether appending growthLen characters is safe (i.e., unlikely to assert)
123 bool canGrowBy(const size_type growthLen
) const { return CanGrowTo(size(), growthLen
); }
125 String
substr(size_type from
, size_type to
) const;
127 void cut(size_type newLength
);
130 void allocAndFill(const char *str
, int len
);
131 void allocBuffer(size_type sz
);
132 void setBuffer(char *buf
, size_type sz
);
134 bool defined() const {return buf_
!=NULL
;}
135 bool undefined() const {return !defined();}
137 /* never reference these directly! */
138 size_type size_
= 0; /* buffer size; limited by SizeMax_ */
140 size_type len_
= 0; /* current length */
142 static const size_type SizeMax_
= 65535; ///< 64K limit protects some fixed-size buffers
143 /// returns true after increasing the first argument by extra if the sum does not exceed SizeMax_
144 static bool SafeAdd(size_type
&base
, size_type extra
) { if (extra
<= SizeMax_
&& base
<= SizeMax_
- extra
) { base
+= extra
; return true; } return false; }
146 char *buf_
= nullptr;
148 void set(char const *loc
, char const ch
) {
149 if (loc
< buf_
|| loc
> (buf_
+ size_
))
154 void cutPointer(char const *loc
) {
155 if (loc
< buf_
|| loc
> (buf_
+ size_
))
162 inline std::ostream
& operator<<(std::ostream
&os
, String
const &aString
)
164 os
.write(aString
.rawBuf(),aString
.size());
168 inline bool operator<(const String
&a
, const String
&b
)
173 const char *checkNullString(const char *p
);
174 int stringHasWhitespace(const char *);
175 int stringHasCntl(const char *);
176 char *strwordtok(char *buf
, char **t
);
178 #endif /* SQUID_STRING_H */