From: Michael Matz Date: Mon, 11 Feb 2008 04:30:08 +0000 (+0000) Subject: Dear Lord! There's always infinite amazement into which total and utter X-Git-Tag: BASE-SuSE-Linux-11_0-Branch~586^2~33 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2f21db99632ed78776987b9ff6e78e1d73046e9b;p=platform%2Fupstream%2Flibzypp.git Dear Lord! There's always infinite amazement into which total and utter 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. --- diff --git a/zypp/parser/parser_utils.hpp b/zypp/parser/parser_utils.hpp deleted file mode 100644 index 492b4de..0000000 --- a/zypp/parser/parser_utils.hpp +++ /dev/null @@ -1,193 +0,0 @@ -/* -IoBind Library License: --------------------------- - -The zlib/libpng License Copyright (c) 2003 Jonathan de Halleux - -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. - -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: - -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. - -2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. - -3. This notice may not be removed or altered from any source distribution -*/ - - -#ifndef IOBIND_PARSER_UTILS_HPP -#define IOBIND_PARSER_UTILS_HPP - -#include -#include -#include - -namespace iobind{ -namespace parser{ -namespace detail{ - -template< - typename Container, - typename Policy -> -class append_with_policy -{ -public: - append_with_policy( Container& container_, Policy const& policy_) - : m_container(container_), m_policy(policy_) - {}; - - // the method called by the parser - template - void operator()(IteratorT const& first, IteratorT const& last) const - { - m_container.insert(m_container.end(), m_policy.encode( std::string(first, last) ) ); - } - -private: - Container& m_container; - Policy const& m_policy; -}; - -template< - typename Container, - typename Policy -> -class insert_with_policy -{ -public: - insert_with_policy( size_t& index_, Container& container_, Policy const& policy_) - : m_index(index_), m_container(container_), m_policy(policy_) - {}; - - // the method called by the parser - template - void operator()(IteratorT const& first, IteratorT const& last) const - { - if (m_index < m_container.size()) - m_container[m_index++]=m_policy.encode( std::string(first, last) ); -#ifdef _DEBUG - else - std::cerr<<"insert_with_policy: could not add data"< -class assign_pair_with_policy -{ -public: - - explicit assign_pair_with_policy( - Pair& pair_, - FirstPolicy const& first_policy_, - SecondPolicy const& second_policy_, - std::string const& first_, - std::string const& second_ - ) - : - m_pair(pair_), - m_first_policy(first_policy_), - m_second_policy(second_policy_), - m_first(first_), - m_second(second_) - {}; - - // the method called by the parser - template - void operator()(IteratorT first, IteratorT last) const - { - m_pair=Pair( - m_first_policy.encode(m_first.c_str()), - m_second_policy.encode(m_second.c_str()) - ); - } - -private: - Pair& m_pair; - FirstPolicy const& m_first_policy; - SecondPolicy const& m_second_policy; - std::string const& m_first; - std::string const& m_second; -}; - -class concat_string -{ -public: - // key_ and val_ should point to the string modified in keyvalue_grammar - // kvc_ is the map of key - values - concat_string( std::ostream& out_) - : out(out_) - { }; - - // the method called by the parser - template - void operator()(IteratorT first, IteratorT last) const - { - out< - void operator()(IteratorT single) const - { - out< -#include -#include -#include -#include "parser_utils.hpp" #include "xml_escape_parser.hpp" - namespace iobind{ namespace parser{ -namespace detail{ -struct escapes : boost::spirit::symbols +std::string xml_escape_parser::escape(const std::string &istr) const { - escapes() + size_t i; + std::string str = istr; + i = str.find_first_of("<>&'\""); + while (i != std::string::npos) { - add - ("<" , "lt") - (">" , "gt") - ("&" , "amp") - ("'" , "apos") - ("\"" , "quot") - ; + switch (str[i]) + { + case '<': str.replace(i, 1, "<"); i += 3; break; + case '>': str.replace(i, 1, ">"); i += 3; break; + case '&': str.replace(i, 1, "&"); i += 4; break; + case '\'': str.replace(i, 1, "'"); i += 5; break; + case '"': str.replace(i, 1, """); i += 5; break; + } + i = str.find_first_of("<>&'\"", i + 1); } + return str; +} -} escapes_p; - -struct unescapes : boost::spirit::symbols +std::string xml_escape_parser::unescape(const std::string &istr) const { - unescapes() + size_t i; + std::string str = istr; + i = str.find_first_of("&"); + while (i != std::string::npos) { - add - ("lt", "<") - ("gt",">") - ("amp","&") - ("apos","\'") - ("quot","\"") - ; + if (str[i] == '&') + { + if (!str.compare(i + 1, 3, "lt;")) + str.replace(i, 4, 1, '<'); + else if (!str.compare(i + 1, 3, "gt;")) + str.replace(i, 4, 1, '>'); + else if (!str.compare(i + 1, 4, "amp;")) + str.replace(i, 5, 1, '&'); + else if (!str.compare(i + 1, 5, "apos;")) + str.replace(i, 6, 1, '\''); + else if (!str.compare(i + 1, 5, "quot;")) + str.replace(i, 6, 1, '"'); + } + i = str.find_first_of("&", i + 1); } -} unescapes_p; - -struct unescape_from_xml_grammar : boost::spirit::grammar -{ - std::ostream& out; - - explicit unescape_from_xml_grammar( std::ostream& out_) - :out(out_){}; - - template - struct definition - { - definition(unescape_from_xml_grammar const& self) - { - using namespace boost::spirit; - - begin = ch_p('&'); - end = ch_p(';'); - // the rest is ok - encoded_string= - *( - begin >> unescapes_p[concat_symbol(self.out)] >> end - | anychar_p[concat_string(self.out)] - ); - }; - - boost::spirit::rule encoded_string, begin, end; - boost::spirit::rule const& start() const { return encoded_string; }; - }; -}; - -struct escape_to_xml_grammar : boost::spirit::grammar -{ - std::ostream& out; - - explicit escape_to_xml_grammar( std::ostream& out_) - :out(out_){}; - - template - struct definition - { - definition(escape_to_xml_grammar const& self) - { - using namespace boost::spirit; - concat_pre_post_symbol concatener(self.out, "&", ";"); - - // the rest is ok - encoded_string= - *( - escapes_p[concatener] - | anychar_p[concat_string(self.out)] - ); - }; - - boost::spirit::rule encoded_string; - boost::spirit::rule const& start() const { return encoded_string; }; - }; -}; - -}; //details - - -std::string xml_escape_parser::escape( std::string const& str) const -{ - using namespace boost::spirit; - - std::ostringstream out; - - parse_info<> info = boost::spirit::parse( - str.c_str(), - detail::escape_to_xml_grammar(out) - ); - - return out.str(); -}; - -std::string xml_escape_parser::unescape( std::string const& str) const -{ - using namespace boost::spirit; - - std::ostringstream out; - - parse_info<> info = boost::spirit::parse( - str.c_str(), - detail::unescape_from_xml_grammar(out) - ); - - return out.str(); -}; + return str; +} }; // parser }; // iobind -