]> git.ipfire.org Git - thirdparty/squid.git/blob - src/SquidString.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / SquidString.h
1 /*
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9 /* DEBUG: section 67 String */
10
11 #ifndef SQUID_STRING_H
12 #define SQUID_STRING_H
13
14 #include <ostream>
15
16 /* squid string placeholder (for printf) */
17 #ifndef SQUIDSTRINGPH
18 #define SQUIDSTRINGPH "%.*s"
19 #define SQUIDSTRINGPRINT(s) (s).psize(),(s).rawBuf()
20 #endif /* SQUIDSTRINGPH */
21
22 class String
23 {
24
25 public:
26 _SQUID_INLINE_ String();
27 String(char const *);
28 String(String const &);
29 ~String();
30
31 typedef size_t size_type; //storage size intentionally unspecified
32 const static size_type npos = static_cast<size_type>(-1);
33
34 String &operator =(char const *);
35 String &operator =(String const &);
36 bool operator ==(String const &) const;
37 bool operator !=(String const &) const;
38
39 /**
40 * Retrieve a single character in the string.
41 \param pos Position of character to retrieve.
42 */
43 _SQUID_INLINE_ char operator [](unsigned int pos) const;
44
45 _SQUID_INLINE_ size_type size() const;
46 /// variant of size() suited to be used for printf-alikes.
47 /// throws when size() > MAXINT
48 int psize() const;
49
50 /**
51 * Returns a raw pointer to the underlying backing store. The caller has been
52 * verified not to make any assumptions about null-termination
53 */
54 _SQUID_INLINE_ char const * rawBuf() const;
55 /**
56 * Returns a raw pointer to the underlying backing store.
57 * The caller requires it to be null-terminated.
58 */
59 _SQUID_INLINE_ char const * termedBuf() const;
60 void limitInit(const char *str, int len); // TODO: rename to assign()
61 void clean();
62 void reset(char const *str);
63 void append(char const *buf, int len);
64 void append(char const *buf);
65 void append(char const);
66 void append(String const &);
67 void absorb(String &old);
68 const char * pos(char const *aString) const;
69 const char * pos(char const ch) const;
70 ///offset from string start of the first occurrence of ch
71 /// returns String::npos if ch is not found
72 size_type find(char const ch) const;
73 size_type find(char const *aString) const;
74 const char * rpos(char const ch) const;
75 size_type rfind(char const ch) const;
76 _SQUID_INLINE_ int cmp(char const *) const;
77 _SQUID_INLINE_ int cmp(char const *, size_type count) const;
78 _SQUID_INLINE_ int cmp(String const &) const;
79 _SQUID_INLINE_ int caseCmp(char const *) const;
80 _SQUID_INLINE_ int caseCmp(char const *, size_type count) const;
81 _SQUID_INLINE_ int caseCmp(String const &) const;
82
83 String substr(size_type from, size_type to) const;
84
85 _SQUID_INLINE_ void cut(size_type newLength);
86
87 private:
88 void allocAndFill(const char *str, int len);
89 void allocBuffer(size_type sz);
90 void setBuffer(char *buf, size_type sz);
91
92 bool defined() const {return buf_!=NULL;}
93 bool undefined() const {return !defined();}
94
95 _SQUID_INLINE_ bool nilCmp(bool, bool, int &) const;
96
97 /* never reference these directly! */
98 size_type size_; /* buffer size; 64K limit */
99
100 size_type len_; /* current length */
101
102 char *buf_;
103
104 _SQUID_INLINE_ void set(char const *loc, char const ch);
105 _SQUID_INLINE_ void cutPointer(char const *loc);
106
107 };
108
109 _SQUID_INLINE_ std::ostream & operator<<(std::ostream& os, String const &aString);
110
111 _SQUID_INLINE_ bool operator<(const String &a, const String &b);
112
113 #if _USE_INLINE_
114 #include "String.cci"
115 #endif
116
117 const char *checkNullString(const char *p);
118 int stringHasWhitespace(const char *);
119 int stringHasCntl(const char *);
120 char *strwordtok(char *buf, char **t);
121
122 #endif /* SQUID_STRING_H */
123