]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/SquidString.h
2 * Copyright (C) 1996-2018 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"
18 /* squid string placeholder (for printf) */
20 #define SQUIDSTRINGPH "%.*s"
21 #define SQUIDSTRINGPRINT(s) (s).psize(),(s).rawBuf()
22 #endif /* SQUIDSTRINGPH */
30 String(String
const &);
31 String(String
&& S
) : size_(S
.size_
), len_(S
.len_
), buf_(S
.buf_
) {
32 S
.buf_
= nullptr; // S is about to be destructed
37 typedef size_t size_type
; //storage size intentionally unspecified
38 const static size_type npos
= static_cast<size_type
>(-1);
40 String
&operator =(char const *);
41 String
&operator =(String
const &);
42 String
&operator =(String
&& S
) {
49 S
.buf_
= nullptr; // S is about to be destructed
54 bool operator ==(String
const &) const;
55 bool operator !=(String
const &) const;
58 * Retrieve a single character in the string.
59 \param aPos Position of character to retrieve.
61 char operator [](unsigned int aPos
) const {
66 /// The absolute size limit on data held in a String.
67 /// Since Strings can be nil-terminated implicitly it is best to ensure
68 /// the useful content length is strictly less than this limit.
69 static size_type
SizeMaxXXX() { return SizeMax_
; }
71 size_type
size() const { return len_
; }
73 /// variant of size() suited to be used for printf-alikes.
74 /// throws when size() >= INT_MAX
76 Must(size() < INT_MAX
);
81 * Returns a raw pointer to the underlying backing store. The caller has been
82 * verified not to make any assumptions about null-termination
84 char const * rawBuf() const { return buf_
; }
87 * Returns a raw pointer to the underlying backing store.
88 * The caller requires it to be null-terminated.
90 char const * termedBuf() const { return buf_
; }
92 void limitInit(const char *str
, int len
); // TODO: rename to assign()
94 void reset(char const *str
);
95 void append(char const *buf
, int len
);
96 void append(char const *buf
);
97 void append(char const);
98 void append(String
const &);
99 void absorb(String
&old
);
100 const char * pos(char const *aString
) const;
101 const char * pos(char const ch
) const;
102 ///offset from string start of the first occurrence of ch
103 /// returns String::npos if ch is not found
104 size_type
find(char const ch
) const;
105 size_type
find(char const *aString
) const;
106 const char * rpos(char const ch
) const;
107 size_type
rfind(char const ch
) const;
108 int cmp(char const *) const;
109 int cmp(char const *, size_type count
) const;
110 int cmp(String
const &) const;
111 int caseCmp(char const *) const;
112 int caseCmp(char const *, size_type count
) const;
113 int caseCmp(String
const &str
) const {
114 return caseCmp(str
.rawBuf(),str
.size());
117 /// Whether creating a totalLen-character string is safe (i.e., unlikely to assert).
118 /// Optional extras can be used for overflow-safe length addition.
119 /// Implementation has to add 1 because many String allocation methods do.
120 static bool CanGrowTo(size_type totalLen
, const size_type extras
= 0) { return SafeAdd(totalLen
, extras
) && SafeAdd(totalLen
, 1); }
121 /// whether appending growthLen characters is safe (i.e., unlikely to assert)
122 bool canGrowBy(const size_type growthLen
) const { return CanGrowTo(size(), growthLen
); }
124 String
substr(size_type from
, size_type to
) const;
126 void cut(size_type newLength
);
129 void allocAndFill(const char *str
, int len
);
130 void allocBuffer(size_type sz
);
131 void setBuffer(char *buf
, size_type sz
);
133 bool defined() const {return buf_
!=NULL
;}
134 bool undefined() const {return !defined();}
136 /* never reference these directly! */
137 size_type size_
= 0; /* buffer size; limited by SizeMax_ */
139 size_type len_
= 0; /* current length */
141 static const size_type SizeMax_
= 65535; ///< 64K limit protects some fixed-size buffers
142 /// returns true after increasing the first argument by extra if the sum does not exceed SizeMax_
143 static bool SafeAdd(size_type
&base
, size_type extra
) { if (extra
<= SizeMax_
&& base
<= SizeMax_
- extra
) { base
+= extra
; return true; } return false; }
145 char *buf_
= nullptr;
147 void set(char const *loc
, char const ch
) {
148 if (loc
< buf_
|| loc
> (buf_
+ size_
))
153 void cutPointer(char const *loc
) {
154 if (loc
< buf_
|| loc
> (buf_
+ size_
))
161 inline std::ostream
& operator<<(std::ostream
&os
, String
const &aString
)
163 os
.write(aString
.rawBuf(),aString
.size());
167 inline bool operator<(const String
&a
, const String
&b
)
172 const char *checkNullString(const char *p
);
173 int stringHasWhitespace(const char *);
174 int stringHasCntl(const char *);
175 char *strwordtok(char *buf
, char **t
);
177 #endif /* SQUID_STRING_H */