#include "squid.h"
#include "CharacterSet.h"
-const CharacterSet &
+CharacterSet &
CharacterSet::operator +=(const CharacterSet &src)
{
Storage::const_iterator s = src.chars_.begin();
return *this;
}
+CharacterSet
+CharacterSet::operator +(const CharacterSet &src) const
+{
+ CharacterSet rv(*this);
+ rv += src;
+ return rv;
+}
+
CharacterSet &
CharacterSet::add(const unsigned char c)
{
return *this;
}
+CharacterSet &
+CharacterSet::addRange(const unsigned char low, const unsigned char high)
+{
+ assert(low <= high);
+ unsigned char c = low;
+ while (c <= high) {
+ chars_[static_cast<uint8_t>(c)] = 1;
+ ++c;
+ }
+ return *this;
+}
+
CharacterSet::CharacterSet(const char *label, const char * const c)
: name(label == NULL ? "anonymous" : label), chars_(Storage(256,0))
{
for (size_t i = 0; i < clen; ++i)
add(c[i]);
}
+
+const CharacterSet
+CharacterSet::ALPHA("ALPHA","ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"),
+CharacterSet::BIT("BIT","01"),
+CharacterSet::CRLF("CRLF","\r\n"),
+CharacterSet::DIGIT("DIGIT","0123456789"),
+CharacterSet::HEXDIG("HEXDIG","0123456789aAbBcCdDeEfF"),
+CharacterSet::WSP("WSP"," \t")
+;
/// add a given character to the character set
CharacterSet & add(const unsigned char c);
+ /// add a character range to the set from low to high included
+ CharacterSet & addRange(const unsigned char low, const 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;
+ // A-Za-z
+ static const CharacterSet ALPHA;
+ // 0-1
+ static const CharacterSet BIT;
+ // CRLF
+ static const CharacterSet CRLF;
+ // 0-9
+ static const CharacterSet DIGIT;
+ // 0-9aAbBcCdDeEfF
+ static const CharacterSet HEXDIG;
+ // <space><tab>
+ static const CharacterSet WSP;
+
+
private:
/** index of characters in this set
*
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
+testCharacterSet_LDFLAGS = $(LIBADD_DL)
+testCharacterSet_LDADD=\
+ $(SQUID_CPPUNIT_LIBS) \
+ $(SQUID_CPPUNIT_LA) \
+ libbase.la \
+ $(COMPAT_LIB) \
+ $(XTRA_LIBS)
+testCharacterSet_DEPENDENCIES= $(SQUID_CPPUNIT_LA)
--- /dev/null
+#define SQUID_UNIT_TEST 1
+
+#include "squid.h"
+#include "base/CharacterSet.h"
+#include "testCharacterSet.h"
+
+#include <string>
+
+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']);
+}
--- /dev/null
+#ifndef SQUID_BASE_TESTCHARACTERSET_H
+#define SQUID_BASE_TESTCHARACTERSET_H
+
+#define SQUID_UNIT_TEST 1
+
+#include <cppunit/extensions/HelperMacros.h>
+
+class testCharacterSet : public CPPUNIT_NS::TestFixture
+{
+ CPPUNIT_TEST_SUITE( testCharacterSet );
+ CPPUNIT_TEST( CharacterSetConstruction );
+ CPPUNIT_TEST( CharacterSetAdd );
+ CPPUNIT_TEST( CharacterSetAddRange );
+ CPPUNIT_TEST_SUITE_END();
+
+protected:
+ void CharacterSetConstruction();
+ void CharacterSetAdd();
+ void CharacterSetAddRange();
+};
+
+#endif /* SQUID_BASE_TESTCHARACTERSET_H */