]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/base/CharacterSet.h
2f9490de834118a68e38dfdc18367d699fa70f18
2 * Copyright (C) 1996-2017 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 #ifndef _SQUID_SRC_PARSER_CHARACTERSET_H
10 #define _SQUID_SRC_PARSER_CHARACTERSET_H
12 #include <initializer_list>
16 /// optimized set of C chars, with quick membership test and merge support
20 typedef std::vector
<uint8_t> Storage
;
22 /// a character set with a given label and contents
23 explicit CharacterSet(const char *label
= "anonymous", const char * const chars
= "");
25 /// define a character set with the given label ("anonymous" if nullptr)
26 /// containing characters defined in the supplied ranges
28 CharacterSet(const char *label
, unsigned char low
, unsigned char high
);
30 /// define a character set with the given label ("anonymous" if nullptr)
31 /// containing characters defined in the supplied list of low-high ranges
33 CharacterSet(const char *label
, std::initializer_list
<std::pair
<uint8_t,uint8_t>> ranges
);
35 /// whether the set lacks any members
36 bool isEmpty() const { return chars_
.empty(); }
38 /// whether a given character exists in the set
39 bool operator[](unsigned char c
) const {return chars_
[static_cast<uint8_t>(c
)] != 0;}
41 /// add a given character to the character set
42 CharacterSet
& add(const unsigned char c
);
44 /// add a list of character ranges, expressed as pairs [low,high], including both ends
45 CharacterSet
& addRange(unsigned char low
, unsigned char high
);
47 /// set addition: add to this set all characters that are also in rhs
48 CharacterSet
&operator +=(const CharacterSet
&rhs
);
50 /// set subtraction: remove all characters that are also in rhs
51 CharacterSet
&operator -=(const CharacterSet
&rhs
);
53 /// return a new CharacterSet containing characters not in this set
54 /// use the supplied label if provided, default is "complement_of_some_other_set"
55 CharacterSet
complement(const char *complementLabel
= nullptr) const;
57 /// change name; handy in const declarations that use operators
58 CharacterSet
&rename(const char *label
) { name
= label
; return *this; }
60 /// \note Ignores label
61 bool operator == (const CharacterSet
&cs
) const { return chars_
== cs
.chars_
; }
62 /// \note Ignores label
63 bool operator != (const CharacterSet
&cs
) const { return !operator==(cs
); }
65 /// prints all chars in arbitrary order, without any quoting/escaping
66 void printChars(std::ostream
&os
) const;
68 /// optional set label for debugging (default: "anonymous")
71 // common character sets, RFC 5234
73 static const CharacterSet ALPHA
;
75 static const CharacterSet BIT
;
77 static const CharacterSet CR
;
79 static const CharacterSet CTL
;
81 static const CharacterSet DIGIT
;
83 static const CharacterSet DQUOTE
;
85 static const CharacterSet HEXDIG
;
87 static const CharacterSet HTAB
;
89 static const CharacterSet LF
;
91 static const CharacterSet SP
;
92 // visible (printable) characters
93 static const CharacterSet VCHAR
;
95 static const CharacterSet WSP
;
97 // HTTP character sets, RFC 7230
99 static const CharacterSet CTEXT
;
100 // XXX: maybe field-vchar = VCHAR / obs-text
101 // any VCHAR except for SPECIAL
102 static const CharacterSet TCHAR
;
104 static const CharacterSet SPECIAL
;
106 static const CharacterSet QDTEXT
;
108 static const CharacterSet OBSTEXT
;
110 // HTTP character sets, RFC 7232
112 static const CharacterSet ETAGC
;
114 // HTTP character sets, RFC 7235
115 // token68 (internal charaters only, excludes '=' terminator)
116 static const CharacterSet TOKEN68C
;
119 /** index of characters in this set
121 * \note guaranteed to be always 256 slots big, as forced in the
122 * constructor. This assumption is relied upon in various methods
127 /** CharacterSet addition
129 * \return a new CharacterSet containing all characters present both in lhs
130 * and rhs, labeled as lhs is
133 operator+ (CharacterSet lhs
, const CharacterSet
&rhs
);
135 /** CharacterSet subtraction
137 * \return a new CharacterSet containing all characters present in lhs
138 * and not present in rhs, labeled as lhs is
141 operator- (CharacterSet lhs
, const CharacterSet
&rhs
);
144 operator <<(std::ostream
&, const CharacterSet
&);
146 #endif /* _SQUID_SRC_PARSER_CHARACTERSET_H */