Dear Lord! There's always infinite amazement into which total and utter
authorMichael Matz <matz@suse.de>
Mon, 11 Feb 2008 04:30:08 +0000 (04:30 +0000)
committerMichael Matz <matz@suse.de>
Mon, 11 Feb 2008 04:30:08 +0000 (04:30 +0000)
crap code can transform by using boost.  This removes 325 lines, adds
47 new ones (for a slow implementation of XML escaping) and now my
"zypper in kde4-bozo" takes 14 seconds instead of 128.  Yes, nine times
faster!  There should be a severe penalty for using boost for ANYTHING.

zypp/parser/parser_utils.hpp [deleted file]
zypp/parser/xml_escape_parser.cpp

diff --git a/zypp/parser/parser_utils.hpp b/zypp/parser/parser_utils.hpp
deleted file mode 100644 (file)
index 492b4de..0000000
+++ /dev/null
@@ -1,193 +0,0 @@
-/*\r
-IoBind Library License:\r
---------------------------\r
-\r
-The zlib/libpng License Copyright (c) 2003 Jonathan de Halleux\r
-\r
-This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.\r
-\r
-Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:\r
-\r
-1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.\r
-\r
-2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.\r
-\r
-3. This notice may not be removed or altered from any source distribution\r
-*/\r
-\r
-\r
-#ifndef IOBIND_PARSER_UTILS_HPP\r
-#define IOBIND_PARSER_UTILS_HPP\r
-\r
-#include <boost/spirit.hpp>\r
-#include <boost/call_traits.hpp>\r
-#include <string>\r
-\r
-namespace iobind{\r
-namespace parser{\r
-namespace detail{\r
-\r
-template<\r
-       typename Container,\r
-       typename Policy\r
-> \r
-class append_with_policy\r
-{\r
-public:\r
-    append_with_policy( Container& container_, Policy const& policy_)\r
-        : m_container(container_), m_policy(policy_)\r
-    {};\r
-\r
-    // the method called by the parser    \r
-    template <typename IteratorT>\r
-    void operator()(IteratorT const& first, IteratorT const& last) const\r
-    {\r
-               m_container.insert(m_container.end(), m_policy.encode( std::string(first, last) ) );\r
-    }\r
-\r
-private:\r
-    Container& m_container;\r
-       Policy const& m_policy;\r
-};\r
-\r
-template<\r
-       typename Container,\r
-       typename Policy\r
-> \r
-class insert_with_policy\r
-{\r
-public:\r
-    insert_with_policy( size_t& index_, Container& container_, Policy const& policy_)\r
-        : m_index(index_), m_container(container_), m_policy(policy_)\r
-    {};\r
-\r
-    // the method called by the parser    \r
-    template <typename IteratorT>\r
-    void operator()(IteratorT const& first, IteratorT const& last) const\r
-    {\r
-               if (m_index < m_container.size())\r
-                       m_container[m_index++]=m_policy.encode( std::string(first, last) );\r
-#ifdef _DEBUG\r
-               else\r
-                       std::cerr<<"insert_with_policy: could not add data"<<std::endl;\r
-#endif\r
-       }\r
-\r
-private:\r
-       size_t& m_index;\r
-    Container& m_container;\r
-       Policy const& m_policy;\r
-};\r
-\r
-template<\r
-       typename Pair,\r
-       typename FirstPolicy,\r
-       typename SecondPolicy\r
-> \r
-class assign_pair_with_policy\r
-{\r
-public:\r
\r
-       explicit assign_pair_with_policy( \r
-               Pair& pair_, \r
-               FirstPolicy const& first_policy_,\r
-               SecondPolicy const& second_policy_,\r
-               std::string const& first_,\r
-               std::string const& second_\r
-               )\r
-        : \r
-               m_pair(pair_), \r
-               m_first_policy(first_policy_),\r
-               m_second_policy(second_policy_),\r
-               m_first(first_),\r
-               m_second(second_)\r
-    {};\r
-\r
-    // the method called by the parser    \r
-    template <typename IteratorT>\r
-    void operator()(IteratorT first, IteratorT last) const\r
-    {\r
-               m_pair=Pair(\r
-                       m_first_policy.encode(m_first.c_str()),\r
-                       m_second_policy.encode(m_second.c_str())\r
-                       );\r
-    }\r
-\r
-private:\r
-    Pair& m_pair;\r
-       FirstPolicy const& m_first_policy;\r
-       SecondPolicy const& m_second_policy;\r
-       std::string const& m_first;\r
-       std::string const& m_second;\r
-};\r
-\r
-class concat_string\r
-{\r
-public:\r
-    // key_ and val_ should point to the string modified in keyvalue_grammar\r
-    // kvc_ is the map of key - values\r
-    concat_string( std::ostream& out_)\r
-        : out(out_)\r
-    {  };\r
-\r
-    // the method called by the parser    \r
-    template <typename IteratorT>\r
-    void operator()(IteratorT first, IteratorT last) const\r
-    {\r
-               out<<std::string(first,last);\r
-    }\r
-\r
-       template <typename IteratorT>\r
-       void operator()(IteratorT single) const\r
-       {\r
-               out<<single;\r
-       }\r
-private:\r
-    std::ostream& out;\r
-};\r
-\r
-class concat_symbol\r
-{\r
-public:\r
-    // key_ and val_ should point to the string modified in keyvalue_grammar\r
-    // kvc_ is the map of key - values\r
-    concat_symbol( std::ostream& out_)\r
-        : out(out_)\r
-    {  };\r
-\r
-    // the method called by the parser    \r
-       void operator()(std::string const& str) const\r
-       {\r
-               out<<str;\r
-       }\r
-private:\r
-    std::ostream& out;\r
-};\r
-\r
-class concat_pre_post_symbol\r
-{\r
-public:\r
-    // key_ and val_ should point to the string modified in keyvalue_grammar\r
-    // kvc_ is the map of key - values\r
-    concat_pre_post_symbol( std::ostream& out_, std::string const& pre_, std::string const& post_)\r
-        : m_out(out_),m_pre(pre_), m_post(post_)\r
-    {  };\r
-\r
-    // the method called by the parser    \r
-       void operator()(std::string const& str_) const\r
-       {\r
-               m_out<<m_pre<<str_<<m_post;\r
-       }\r
-private:\r
-    std::ostream& m_out;\r
-       std::string m_pre;\r
-       std::string m_post;\r
-};\r
-\r
-};//details\r
-};//parser\r
-};//iobind\r
-\r
-#endif\r
-\r
-\r
index 6d0cd67..e8fe60b 100644 (file)
-/*\r
-IoBind Library License:\r
---------------------------\r
-\r
-The zlib/libpng License Copyright (c) 2003 Jonathan de Halleux\r
-\r
-This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.\r
-\r
-Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:\r
-\r
-1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.\r
-\r
-2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.\r
-\r
-3. This notice may not be removed or altered from any source distribution\r
+/*---------------------------------------------------------------------\\r
+|                          ____ _   __ __ ___                          |\r
+|                         |__  / \ / / . \ . \                         |\r
+|                           / / \ V /|  _/  _/                         |\r
+|                          / /__ | | | | | |                           |\r
+|                         /_____||_| |_| |_|                           |\r
+|                                                                      |\r
+\---------------------------------------------------------------------*/\r
+/** \file      zypp/parser/xml_escape_parser.cpp\r
+ *\r
 */\r
 \r
-\r
 #include <string>\r
-#include <sstream>\r
-#include <boost/format.hpp>\r
-#include <boost/spirit.hpp> \r
-#include "parser_utils.hpp"\r
 #include "xml_escape_parser.hpp"\r
 \r
-\r
 namespace iobind{\r
 namespace parser{\r
-namespace detail{\r
 \r
-struct escapes : boost::spirit::symbols<std::string>\r
+std::string xml_escape_parser::escape(const std::string &istr) const\r
 {\r
-    escapes()\r
+  size_t i;\r
+  std::string str = istr;\r
+  i = str.find_first_of("<>&'\"");\r
+  while (i != std::string::npos)\r
     {\r
-        add\r
-            ("<"    , "lt")\r
-            (">"    , "gt")\r
-            ("&"    , "amp")\r
-            ("'"    , "apos")\r
-            ("\""    , "quot")\r
-        ;\r
+      switch (str[i])\r
+        {\r
+         case '<': str.replace(i, 1, "&lt;"); i += 3; break;\r
+         case '>': str.replace(i, 1, "&gt;"); i += 3; break;\r
+         case '&': str.replace(i, 1, "&amp;"); i += 4; break;\r
+         case '\'': str.replace(i, 1, "&apos;"); i += 5; break;\r
+         case '"': str.replace(i, 1, "&quot;"); i += 5; break;\r
+       }\r
+      i = str.find_first_of("<>&'\"", i + 1);\r
     }\r
+  return str;\r
+}\r
 \r
-} escapes_p;\r
-\r
-struct unescapes : boost::spirit::symbols<std::string>\r
+std::string xml_escape_parser::unescape(const std::string &istr) const\r
 {\r
-    unescapes()\r
+  size_t i;\r
+  std::string str = istr;\r
+  i = str.find_first_of("&");\r
+  while (i != std::string::npos)\r
     {\r
-        add\r
-            ("lt", "<")\r
-            ("gt",">")\r
-            ("amp","&")\r
-            ("apos","\'")\r
-            ("quot","\"")\r
-        ;\r
+      if (str[i] == '&')\r
+        {\r
+         if (!str.compare(i + 1, 3, "lt;"))\r
+           str.replace(i, 4, 1, '<');\r
+         else if (!str.compare(i + 1, 3, "gt;"))\r
+           str.replace(i, 4, 1, '>');\r
+         else if (!str.compare(i + 1, 4, "amp;"))\r
+           str.replace(i, 5, 1, '&');\r
+         else if (!str.compare(i + 1, 5, "apos;"))\r
+           str.replace(i, 6, 1, '\'');\r
+         else if (!str.compare(i + 1, 5, "quot;"))\r
+           str.replace(i, 6, 1, '"');\r
+       }\r
+      i = str.find_first_of("&", i + 1);\r
     }\r
-} unescapes_p;\r
-\r
-struct unescape_from_xml_grammar : boost::spirit::grammar<unescape_from_xml_grammar>\r
-{\r
-       std::ostream& out;\r
-\r
-       explicit unescape_from_xml_grammar( std::ostream& out_)\r
-               :out(out_){};\r
-\r
-   template <typename ScannerT>\r
-   struct definition\r
-   {    \r
-        definition(unescape_from_xml_grammar const& self)  \r
-               {\r
-                       using namespace boost::spirit;\r
-\r
-                       begin = ch_p('&');\r
-                       end = ch_p(';');\r
-                       // the rest is ok\r
-                       encoded_string=\r
-                               *( \r
-                                        begin >> unescapes_p[concat_symbol(self.out)] >> end\r
-                                  | anychar_p[concat_string(self.out)]\r
-                                );\r
-               };\r
-\r
-               boost::spirit::rule<ScannerT> encoded_string, begin, end;\r
-               boost::spirit::rule<ScannerT> const& start() const { return encoded_string; };\r
-   };\r
-};\r
-\r
-struct escape_to_xml_grammar : boost::spirit::grammar<escape_to_xml_grammar>\r
-{\r
-       std::ostream& out;\r
-\r
-       explicit escape_to_xml_grammar( std::ostream& out_)\r
-               :out(out_){};\r
-  \r
-   template <typename ScannerT>\r
-   struct definition\r
-   {    \r
-        definition(escape_to_xml_grammar const& self)  \r
-               {\r
-                       using namespace boost::spirit;\r
-                       concat_pre_post_symbol concatener(self.out, "&", ";");\r
-\r
-                       // the rest is ok\r
-                       encoded_string=\r
-                               *( \r
-                                        escapes_p[concatener]\r
-                                  | anychar_p[concat_string(self.out)]\r
-                                );\r
-               };\r
-\r
-               boost::spirit::rule<ScannerT> encoded_string;\r
-               boost::spirit::rule<ScannerT> const& start() const { return encoded_string; };\r
-   };\r
-};\r
-\r
-}; //details\r
-\r
-\r
-std::string xml_escape_parser::escape( std::string const& str) const\r
-{\r
-       using namespace boost::spirit;\r
-\r
-       std::ostringstream out;\r
-\r
-       parse_info<> info = boost::spirit::parse(\r
-               str.c_str(), \r
-                          detail::escape_to_xml_grammar(out)\r
-                          );\r
-\r
-       return out.str();\r
-};\r
-\r
-std::string xml_escape_parser::unescape( std::string const& str) const\r
-{\r
-       using namespace boost::spirit;\r
-\r
-       std::ostringstream out;\r
-\r
-       parse_info<> info = boost::spirit::parse(\r
-               str.c_str(), \r
-               detail::unescape_from_xml_grammar(out)\r
-                          );\r
-\r
-       return out.str();\r
-};\r
+  return str;\r
+}\r
 }; // parser\r
 }; // iobind\r
-\r