------------------------------------------------------------ revno: 13206 [merge] revision-id: kinkie@squid-cache.org-20140105215917-n6226jgettlqb4s7 parent: kinkie@squid-cache.org-20140103175235-fo37ieq0a1td0ata parent: kinkie@squid-cache.org-20140105215813-lzoih1k26s565ono committer: Francesco Chemolli branch nick: trunk timestamp: Sun 2014-01-05 22:59:17 +0100 message: Merge from CharacterSet ------------------------------------------------------------ Use --include-merges or -n0 to see merged revisions. ------------------------------------------------------------ # Bazaar merge directive format 2 (Bazaar 0.90) # revision_id: kinkie@squid-cache.org-20140105215917-n6226jgettlqb4s7 # target_branch: http://bzr.squid-cache.org/bzr/squid3/trunk/ # testament_sha1: 92f4164506f4250742b05e507e4d82aa83e85058 # timestamp: 2014-01-05 22:55:22 +0000 # source_branch: http://bzr.squid-cache.org/bzr/squid3/trunk/ # base_revision_id: kinkie@squid-cache.org-20140103175235-\ # fo37ieq0a1td0ata # # Begin patch === modified file 'src/base/CharacterSet.cc' --- src/base/CharacterSet.cc 2013-12-18 15:22:06 +0000 +++ src/base/CharacterSet.cc 2014-01-05 21:58:13 +0000 @@ -1,7 +1,7 @@ #include "squid.h" #include "CharacterSet.h" -const CharacterSet & +CharacterSet & CharacterSet::operator +=(const CharacterSet &src) { Storage::const_iterator s = src.chars_.begin(); @@ -16,6 +16,14 @@ return *this; } +CharacterSet +CharacterSet::operator +(const CharacterSet &src) const +{ + CharacterSet rv(*this); + rv += src; + return rv; +} + CharacterSet & CharacterSet::add(const unsigned char c) { @@ -23,6 +31,16 @@ return *this; } +CharacterSet & +CharacterSet::addRange(unsigned char low, unsigned char high) +{ + while (low <= high) { + chars_[static_cast(low)] = 1; + ++low; + } + return *this; +} + CharacterSet::CharacterSet(const char *label, const char * const c) : name(label == NULL ? "anonymous" : label), chars_(Storage(256,0)) { @@ -30,3 +48,28 @@ for (size_t i = 0; i < clen; ++i) add(c[i]); } + +CharacterSet::CharacterSet(const char *label, unsigned char low, unsigned char high) +: name(label == NULL ? "anonymous" : label), chars_(Storage(256,0)) +{ + addRange(low,high); +} + +const CharacterSet +CharacterSet::ALPHA("ALPHA", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), +CharacterSet::BIT("BIT","01"), +CharacterSet::CR("CR","\r"), +CharacterSet::LF("LF","\n"), +CharacterSet::DIGIT("DIGIT","0123456789"), +CharacterSet::DQUOTE("DQUOTE","\""), +CharacterSet::HTAB("HTAB","\t"), +CharacterSet::HEXDIG("HEXDIG","0123456789aAbBcCdDeEfF"), +CharacterSet::SP("SP"," "), +CharacterSet::VCHAR("VCHAR", 0x21, 0x7e), +CharacterSet::WSP("WSP"," \t"), +CharacterSet::TCHAR("TCHAR","!#$%&'*+-.^_`|~0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), +CharacterSet::SPECIAL("SPECIAL","()<>@,;:\\\"/[]?={}") +// QDTEXT and OBSTEXT are omitted for now as they require c++11 constructors +//,CharacterSet::QDTEXT("QDTEXT",{{9,9},{0x20,0x21},{0x23,0x5b},{0x5d,0x7e},{0x80,0xff}}) +//,CharacterSet::OBSTEXT("OBSTEXT",0x80,0xff) +; === modified file 'src/base/CharacterSet.h' --- src/base/CharacterSet.h 2013-12-18 15:22:06 +0000 +++ src/base/CharacterSet.h 2014-01-05 21:58:13 +0000 @@ -13,18 +13,62 @@ /// with specified initial contents CharacterSet(const char *label, const char * const initial); + /// define a character set with the given label ("anonymous" if NULL) + /// containing characters defined in the supplied ranges + /// \see addRange + CharacterSet(const char *label, unsigned char low, unsigned char high); + /// whether a given character exists in the set bool operator[](unsigned char c) const {return chars_[static_cast(c)] != 0;} /// add a given character to the character set CharacterSet & add(const unsigned char c); + /// add a list of character ranges, expressed as pairs [low,high], including both ends + CharacterSet & addRange(unsigned char low, unsigned char high); + /// add all characters from the given CharacterSet to this one - const CharacterSet &operator +=(const CharacterSet &src); + CharacterSet &operator +=(const CharacterSet &src); + + /// return a new CharacterSet containing the union of two sets + CharacterSet operator +(const CharacterSet &src) const; /// optional set label for debugging (default: "anonymous") const char * name; + // common character sets, insipired to RFC5234 + // A-Za-z + static const CharacterSet ALPHA; + // 0-1 + static const CharacterSet BIT; + // carriage return + static const CharacterSet CR; + // line feed + static const CharacterSet LF; + // double quote + static const CharacterSet DQUOTE; + // 0-9 + static const CharacterSet DIGIT; + // 0-9aAbBcCdDeEfF + static const CharacterSet HEXDIG; + // horizontal tab + static const CharacterSet HTAB; + // white space + static const CharacterSet SP; + // visible (printable) characters + static const CharacterSet VCHAR; + // + static const CharacterSet WSP; + // character sets from draft httpbis + // any VCHAR except for SPECIAL + static const CharacterSet TCHAR; + // special VCHARs + static const CharacterSet SPECIAL; + // qdtext (ready but not enabled as it requires a c++11 constructor) + //static const CharacterSet QDTEXT; + // obs-text (ready but not enabled as it requires a c++11 constructor) + //static const CharacterSet OBSTEXT; + private: /** index of characters in this set * === modified file 'src/base/Makefile.am' --- src/base/Makefile.am 2013-12-17 10:19:44 +0000 +++ src/base/Makefile.am 2014-01-01 17:15:10 +0000 @@ -26,3 +26,27 @@ TextException.h \ Vector.cc \ Vector.h + +EXTRA_PROGRAMS = \ + testCharacterSet + +check_PROGRAMS += testCharacterSet +TESTS += testCharacterSet + +testCharacterSet_SOURCES = \ + CharacterSet.h \ + testCharacterSet.h \ + testCharacterSet.cc +nodist_testCharacterSet_SOURCES = \ + $(top_srcdir)/src/tests/testMain.cc \ + $(top_srcdir)/src/tests/stub_debug.cc \ + $(top_srcdir)/src/tests/stub_MemBuf.cc \ + $(top_srcdir)/src/tests/stub_cbdata.cc +testCharacterSet_LDFLAGS = $(LIBADD_DL) +testCharacterSet_LDADD=\ + $(SQUID_CPPUNIT_LIBS) \ + $(SQUID_CPPUNIT_LA) \ + libbase.la \ + $(COMPAT_LIB) \ + $(XTRA_LIBS) +testCharacterSet_DEPENDENCIES= $(SQUID_CPPUNIT_LA) === added file 'src/base/testCharacterSet.cc' --- src/base/testCharacterSet.cc 1970-01-01 00:00:00 +0000 +++ src/base/testCharacterSet.cc 2013-12-30 13:22:05 +0000 @@ -0,0 +1,82 @@ +#define SQUID_UNIT_TEST 1 + +#include "squid.h" +#include "base/CharacterSet.h" +#include "testCharacterSet.h" + +#include + +CPPUNIT_TEST_SUITE_REGISTRATION( testCharacterSet ); + +void +testCharacterSet::CharacterSetConstruction() +{ + { + CharacterSet t(NULL,""); + CPPUNIT_ASSERT_EQUAL(std::string("anonymous"),std::string(t.name)); + } + { + CharacterSet t("test",""); + CPPUNIT_ASSERT_EQUAL(std::string("test"),std::string(t.name)); + } + { + CharacterSet t("test",""); + for (int j = 0; j < 255; ++j) + CPPUNIT_ASSERT_EQUAL(false,t[j]); + } + { + CharacterSet t("test","0"); + CPPUNIT_ASSERT_EQUAL(true,t['0']); + for (int j = 0; j < 255; ++j) + if (j != '0') + CPPUNIT_ASSERT_EQUAL(false,t[j]); + } +} + +void +testCharacterSet::CharacterSetAdd() +{ + CharacterSet t("test","0"); + t.add(0); + CPPUNIT_ASSERT_EQUAL(true,t['\0']); + CPPUNIT_ASSERT_EQUAL(true,t['0']); +} + +void +testCharacterSet::CharacterSetAddRange() +{ + CharacterSet t("test",""); + t.addRange('0','9'); + CPPUNIT_ASSERT_EQUAL(true,t['0']); + CPPUNIT_ASSERT_EQUAL(true,t['5']); + CPPUNIT_ASSERT_EQUAL(true,t['9']); + CPPUNIT_ASSERT_EQUAL(false,t['a']); +} + +void +testCharacterSet::CharacterSetConstants() +{ + CPPUNIT_ASSERT_EQUAL(true,CharacterSet::ALPHA['a']); + CPPUNIT_ASSERT_EQUAL(true,CharacterSet::ALPHA['z']); + CPPUNIT_ASSERT_EQUAL(true,CharacterSet::ALPHA['A']); + CPPUNIT_ASSERT_EQUAL(true,CharacterSet::ALPHA['Z']); + CPPUNIT_ASSERT_EQUAL(false,CharacterSet::ALPHA['5']); +} + +void +testCharacterSet::CharacterSetUnion() +{ + { + CharacterSet hex("hex",""); + hex += CharacterSet::DIGIT; + hex += CharacterSet(NULL,"aAbBcCdDeEfF"); + for (int j = 0; j < 255; ++j) + CPPUNIT_ASSERT_EQUAL(CharacterSet::HEXDIG[j],hex[j]); + } + { + CharacterSet hex(NULL,""); + hex = CharacterSet::DIGIT + CharacterSet(NULL,"aAbBcCdDeEfF"); + for (int j = 0; j < 255; ++j) + CPPUNIT_ASSERT_EQUAL(CharacterSet::HEXDIG[j],hex[j]); + } +} === added file 'src/base/testCharacterSet.h' --- src/base/testCharacterSet.h 1970-01-01 00:00:00 +0000 +++ src/base/testCharacterSet.h 2013-12-30 09:44:52 +0000 @@ -0,0 +1,26 @@ +#ifndef SQUID_BASE_TESTCHARACTERSET_H +#define SQUID_BASE_TESTCHARACTERSET_H + +#define SQUID_UNIT_TEST 1 + +#include + +class testCharacterSet : public CPPUNIT_NS::TestFixture +{ + CPPUNIT_TEST_SUITE( testCharacterSet ); + CPPUNIT_TEST( CharacterSetConstruction ); + CPPUNIT_TEST( CharacterSetAdd ); + CPPUNIT_TEST( CharacterSetAddRange ); + CPPUNIT_TEST( CharacterSetConstants ); + CPPUNIT_TEST( CharacterSetUnion ); + CPPUNIT_TEST_SUITE_END(); + +protected: + void CharacterSetConstruction(); + void CharacterSetAdd(); + void CharacterSetAddRange(); + void CharacterSetConstants(); + void CharacterSetUnion(); +}; + +#endif /* SQUID_BASE_TESTCHARACTERSET_H */