]> git.ipfire.org Git - thirdparty/squid.git/blame - src/base/CharacterSet.cc
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / base / CharacterSet.cc
CommitLineData
bbc27441 1/*
5b74111a 2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
bbc27441
AJ
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
c8046ec7 9#include "squid.h"
c8046ec7
FC
10#include "CharacterSet.h"
11
b5cb2dbf 12#include <algorithm>
47efdfc6 13#include <iostream>
b5cb2dbf
AR
14#include <functional>
15
18538c13 16CharacterSet &
c8046ec7
FC
17CharacterSet::operator +=(const CharacterSet &src)
18{
d27140db
FC
19 Storage::const_iterator s = src.chars_.begin();
20 const Storage::const_iterator e = src.chars_.end();
21 Storage::iterator d = chars_.begin();
22 while (s != e) {
23 if (*s)
24 *d = 1;
25 ++s;
26 ++d;
27 }
c8046ec7
FC
28 return *this;
29}
dcd4fdac 30
47efdfc6
FC
31CharacterSet &
32CharacterSet::operator -=(const CharacterSet &src)
18538c13 33{
47efdfc6
FC
34 Storage::const_iterator s = src.chars_.begin();
35 const Storage::const_iterator e = src.chars_.end();
36 Storage::iterator d = chars_.begin();
37 while (s != e) {
38 if (*s)
39 *d = 0;
40 ++s;
41 ++d;
42 }
43 return *this;
18538c13
FC
44}
45
dcd4fdac
FC
46CharacterSet &
47CharacterSet::add(const unsigned char c)
48{
49 chars_[static_cast<uint8_t>(c)] = 1;
50 return *this;
51}
52
18538c13 53CharacterSet &
decd2fc6 54CharacterSet::addRange(unsigned char low, unsigned char high)
18538c13 55{
d26d8af4
FC
56 //manual loop splitting is needed to cover case where high is 255
57 // otherwise low will wrap, resulting in infinite loop
58 while (low < high) {
decd2fc6
FC
59 chars_[static_cast<uint8_t>(low)] = 1;
60 ++low;
18538c13 61 }
d26d8af4 62 chars_[static_cast<uint8_t>(high)] = 1;
18538c13
FC
63 return *this;
64}
65
b5cb2dbf
AR
66CharacterSet
67CharacterSet::complement(const char *label) const
68{
69 CharacterSet result((label ? label : "complement_of_some_other_set"), "");
70 // negate each of our elements and add them to the result storage
71 std::transform(chars_.begin(), chars_.end(), result.chars_.begin(),
72 std::logical_not<Storage::value_type>());
73 return result;
74}
75
86c63190 76CharacterSet::CharacterSet(const char *label, const char * const c) :
47efdfc6 77 name(label ? label: "anonymous"),
f53969cc 78 chars_(Storage(256,0))
dcd4fdac
FC
79{
80 const size_t clen = strlen(c);
81 for (size_t i = 0; i < clen; ++i)
82 add(c[i]);
83}
18538c13 84
86c63190 85CharacterSet::CharacterSet(const char *label, unsigned char low, unsigned char high) :
47efdfc6 86 name(label ? label: "anonymous"),
f53969cc 87 chars_(Storage(256,0))
8664ceb4 88{
decd2fc6 89 addRange(low,high);
8664ceb4
FC
90}
91
47efdfc6
FC
92CharacterSet::CharacterSet(const char *label, std::initializer_list<std::pair<uint8_t, uint8_t>> ranges) :
93 name(label ? label: "anonymous"),
94 chars_(Storage(256,0))
95{
96 for (auto range: ranges)
97 addRange(range.first, range.second);
98}
99
4eac3407
CT
100void
101CharacterSet::printChars(std::ostream &os) const
102{
103 for (size_t idx = 0; idx < 256; ++idx) {
104 if (chars_[idx])
105 os << static_cast<char>(idx);
106 }
107}
108
47efdfc6
FC
109CharacterSet
110operator+ (CharacterSet lhs, const CharacterSet &rhs)
111{
112 lhs += rhs;
113 return lhs;
114}
115
116CharacterSet
117operator- (CharacterSet lhs, const CharacterSet &rhs)
118{
119 lhs -= rhs;
120 return lhs;
121}
122
123std::ostream&
124operator <<(std::ostream &s, const CharacterSet &c)
125{
126 s << "CharacterSet(" << c.name << ')';
127 return s;
128}
129
18538c13 130const CharacterSet
98b721ce 131// RFC 5234
0049f30f
SM
132CharacterSet::ALPHA("ALPHA", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
133 CharacterSet::BIT("BIT","01"),
134 CharacterSet::CR("CR","\r"),
135CharacterSet::CTL("CTL", {{0x01,0x1f},{0x7f,0x7f}}),
136CharacterSet::DIGIT("DIGIT","0123456789"),
137CharacterSet::DQUOTE("DQUOTE","\""),
138CharacterSet::HEXDIG("HEXDIG","0123456789aAbBcCdDeEfF"),
139CharacterSet::HTAB("HTAB","\t"),
140CharacterSet::LF("LF","\n"),
141CharacterSet::SP("SP"," "),
142CharacterSet::VCHAR("VCHAR", 0x21, 0x7e),
143// RFC 7230
144CharacterSet::WSP("WSP"," \t"),
145CharacterSet::CTEXT("ctext", {{0x09,0x09},{0x20,0x20},{0x2a,0x5b},{0x5d,0x7e},{0x80,0xff}}),
146CharacterSet::TCHAR("TCHAR","!#$%&'*+-.^_`|~0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
147CharacterSet::SPECIAL("SPECIAL","()<>@,;:\\\"/[]?={}"),
148CharacterSet::QDTEXT("QDTEXT", {{0x09,0x09},{0x20,0x21},{0x23,0x5b},{0x5d,0x7e},{0x80,0xff}}),
149CharacterSet::OBSTEXT("OBSTEXT",0x80,0xff),
150// RFC 7232
151CharacterSet::ETAGC("ETAGC", {{0x21,0x21},{0x23,0x7e},{0x80,0xff}}),
152// RFC 7235
153CharacterSet::TOKEN68C("TOKEN68C","-._~+/0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
154;
155