aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-02-22 17:58:01 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-02-22 17:58:01 +0000
commitc7b11a6f56a178e2ac879b2e88023b35dcb01d48 (patch)
treeab4e5b23eaeee664e62d2425c21b94fafafbd6c8
parent51108688d1690196cafa189af8b7a14998ea132d (diff)
downloadpaludis-c7b11a6f56a178e2ac879b2e88023b35dcb01d48.tar.gz
paludis-c7b11a6f56a178e2ac879b2e88023b35dcb01d48.tar.xz
Commit match_sequence skeleton. This currently breaks gcc3.4.
-rw-r--r--paludis/files.m41
-rw-r--r--paludis/match_sequence.cc153
-rw-r--r--paludis/match_sequence.hh60
-rw-r--r--paludis/match_sequence_TEST.cc51
4 files changed, 265 insertions, 0 deletions
diff --git a/paludis/files.m4 b/paludis/files.m4
index 3e9de75..f966677 100644
--- a/paludis/files.m4
+++ b/paludis/files.m4
@@ -49,6 +49,7 @@ add(`line_config_file', `hh', `cc', `test')
add(`log', `hh', `cc', `test')
add(`mask_reasons', `hh', `cc')
add(`match_package', `hh', `cc')
+add(`match_sequence', `hh', `cc', `test')
add(`package_database', `hh', `cc', `test')
add(`package_name_part', `hh', `cc', `test')
add(`paludis', `hh', `cc')
diff --git a/paludis/match_sequence.cc b/paludis/match_sequence.cc
new file mode 100644
index 0000000..3197c7b
--- /dev/null
+++ b/paludis/match_sequence.cc
@@ -0,0 +1,153 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2006 Ciaran McCreesh <ciaranm@gentoo.org>
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "match_sequence.hh"
+
+using namespace paludis;
+
+struct MatchRule::Rule :
+ InternalCounted<MatchRule::Rule>
+{
+ virtual std::string::size_type local_match(const std::string &,
+ std::string::size_type) const = 0;
+
+ virtual ~Rule()
+ {
+ }
+};
+
+struct MatchRule::StringRule :
+ MatchRule::Rule
+{
+ const std::string s;
+
+ StringRule(const std::string & ss) :
+ s(ss)
+ {
+ }
+
+ std::string::size_type
+ local_match(const std::string & t, std::string::size_type p) const
+ {
+ if (p + s.length() <= t.length())
+ return (0 == t.compare(p, s.length(), s)) ? s.length() : std::string::npos;
+ else
+ return std::string::npos;
+ }
+};
+
+struct MatchRule::SequenceRule :
+ MatchRule::Rule
+{
+ const MatchRule r1, r2;
+
+ SequenceRule(const MatchRule & rr1, const MatchRule & rr2) :
+ r1(rr1),
+ r2(rr2)
+ {
+ }
+
+ std::string::size_type
+ local_match(const std::string & t, std::string::size_type p) const
+ {
+ std::string::size_type l1(r1._rule->local_match(t, p)), l2(0);
+ if (std::string::npos != l1)
+ l2 = r2._rule->local_match(t, p + l1);
+ return (std::string::npos == l2) ? l2 : p + l1 + l2;
+ }
+};
+
+struct MatchRule::ZeroOrMoreRule :
+ MatchRule::Rule
+{
+ const MatchRule r1;
+
+ ZeroOrMoreRule(const MatchRule & rr1) :
+ r1(rr1)
+ {
+ }
+
+ std::string::size_type
+ local_match(const std::string & t, std::string::size_type p) const
+ {
+ std::string::size_type result(p), q;
+ while (std::string::npos != ((q = r1._rule->local_match(t, result))))
+ {
+ if (0 == q)
+ break;
+ result += q;
+ }
+
+ return result;
+ }
+};
+
+struct MatchRule::EolRule :
+ MatchRule::Rule
+{
+ std::string::size_type
+ local_match(const std::string & t, std::string::size_type p) const
+ {
+ return p >= t.length() ? 0 : std::string::npos;
+ }
+};
+
+MatchRule::MatchRule(const std::string & s) :
+ _rule(CountedPtr<Rule>(new StringRule(s)))
+{
+}
+
+MatchRule::MatchRule(const MatchRule & other) :
+ _rule(other._rule)
+{
+}
+
+MatchRule::MatchRule(CountedPtr<Rule> r) :
+ _rule(r)
+{
+}
+
+const MatchRule
+MatchRule::eol()
+{
+ return MatchRule(CountedPtr<Rule>(new EolRule));
+}
+
+MatchRule::~MatchRule()
+{
+}
+
+const MatchRule
+MatchRule::operator>> (const MatchRule & other) const
+{
+ return MatchRule(CountedPtr<Rule>(new SequenceRule(*this, other)));
+}
+
+const MatchRule
+MatchRule::operator* () const
+{
+ return MatchRule(CountedPtr<Rule>(new ZeroOrMoreRule(*this)));
+}
+
+bool
+MatchRule::match(const std::string & s) const
+{
+ return (std::string::npos != _rule->local_match(s, 0));
+}
+
diff --git a/paludis/match_sequence.hh b/paludis/match_sequence.hh
new file mode 100644
index 0000000..b679930
--- /dev/null
+++ b/paludis/match_sequence.hh
@@ -0,0 +1,60 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2006 Ciaran McCreesh <ciaranm@gentoo.org>
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef PALUDIS_GUARD_PALUDIS_MATCH_SEQUENCE_HH
+#define PALUDIS_GUARD_PALUDIS_MATCH_SEQUENCE_HH 1
+
+#include <string>
+#include <paludis/counted_ptr.hh>
+
+namespace paludis
+{
+ class MatchRule
+ {
+ private:
+ struct Rule;
+ struct StringRule;
+ struct SequenceRule;
+ struct ZeroOrMoreRule;
+ struct EolRule;
+
+ CountedPtr<Rule> _rule;
+
+ void operator= (const MatchRule &);
+
+ MatchRule(CountedPtr<Rule>);
+
+ public:
+ MatchRule(const std::string &);
+
+ MatchRule(const MatchRule &);
+
+ static const MatchRule eol();
+
+ ~MatchRule();
+
+ const MatchRule operator>> (const MatchRule &) const;
+
+ const MatchRule operator* () const;
+
+ bool match(const std::string &) const;
+ };
+}
+
+#endif
diff --git a/paludis/match_sequence_TEST.cc b/paludis/match_sequence_TEST.cc
new file mode 100644
index 0000000..d0d8f7f
--- /dev/null
+++ b/paludis/match_sequence_TEST.cc
@@ -0,0 +1,51 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2006 Ciaran McCreesh <ciaranm@gentoo.org>
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "match_sequence.hh"
+#include <test/test_framework.hh>
+#include <test/test_runner.hh>
+
+using namespace test;
+using namespace paludis;
+
+namespace test_case
+{
+ struct MatchRuleTest : TestCase
+ {
+ MatchRuleTest() : TestCase("match rule") { }
+
+ void run()
+ {
+ typedef MatchRule X;
+
+ X r1(*X("\t") >> X("econf") >> X::eol());
+
+ TEST_CHECK(r1.match("econf"));
+ TEST_CHECK(r1.match("\teconf"));
+ TEST_CHECK(r1.match("\t\teconf"));
+ TEST_CHECK(! r1.match(" econf"));
+ TEST_CHECK(! r1.match("\tecong"));
+ TEST_CHECK(! r1.match("\t"));
+ TEST_CHECK(! r1.match("econf moo"));
+
+ X r2(*X("\t") >> X("econf") >> *X(" ") >> X("||") >> *X(" ") >> X("die"));
+ }
+ } test_match_rule;
+}
+