BOOST-Coderefeactoring (:
-
Hallo,
habe hier mal was programmiert. Ich wollte einfach mal ein bisschen testen was mit boost::algorithm und boost::tokenizer möglich ist. Allerdings bin ich mit verschiedenen Dingen nicht einverstanden und hätte gerne gewusst wie man solche Dinge besser löst. Erstmal der Code:#include <iostream> #include <boost/bind.hpp> #include <boost/scoped_ptr.hpp> #include <vector> #include <string> #include <algorithm> #include <boost/algorithm/string.hpp> #include <locale> #include <clocale> #include <boost/function.hpp> #include <boost/tokenizer.hpp> class StringProcessor { private: typedef boost::tokenizer<boost::char_separator<char>> Tokenizer; private: boost::scoped_ptr<std::string> m_TextBuffer; /// Main string const std::vector<std::string> vec_TextBuffer; /// Vector with main strings boost::scoped_ptr<Tokenizer> m_Tokenizer; /// Main tokenizer public: ///// Only get the cultural-information ///// StringProcessor(const std::string &CulturalCircle) : m_TextBuffer(new std::string("")) { std::setlocale(LC_ALL, CulturalCircle.c_str()); /// Sets the cultural circle } ///// Get std::string by reference ///// StringProcessor(const std::string &TextBuffer, const std::string &CulturalCircle) : m_TextBuffer(new std::string(TextBuffer)) , m_Tokenizer(new Tokenizer(TextBuffer)) { std::setlocale(LC_ALL, CulturalCircle.c_str()); /// Sets the cultural circle } ///// Get std::string by std::vector ///// StringProcessor(const std::vector<std::string> &TextBuffer, const std::string &CulturalCircle) : m_TextBuffer(new std::string) , vec_TextBuffer(TextBuffer) { std::setlocale(LC_ALL, CulturalCircle.c_str()); /// Sets the global cultural circle std::vector<std::string>::const_iterator it(TextBuffer.begin()); for(; it != TextBuffer.end(); ++it) *m_TextBuffer += *it; m_Tokenizer.reset(new Tokenizer(*m_TextBuffer)); PrintConverts(); } void PrintConverts() const; }; void StringProcessor::PrintConverts() const { std::vector<std::string> TextBuffer; std::string::const_iterator itBegin(m_TextBuffer->begin()); unsigned int TextBufInkr(0); /*///// Normal string algorithm from BOOST /////*/ ///// Split the string on every whitespace into a vector ///// boost::algorithm::split(TextBuffer, *m_TextBuffer, boost::algorithm::is_space()); if((*itBegin == ' ') && ((*m_TextBuffer)[m_TextBuffer->size()] == ' ')) { ///// Trims the std::string from the left and right ///// std::cout<< "=====Deletes the first and last whitespace=====" <<std::endl; boost::algorithm::trim_left_copy(*m_TextBuffer); std::cout<< boost::algorithm::trim_right_copy(*m_TextBuffer) <<std::endl; std::cout<< "=====Deletes the first and last whitespace=====" <<std::endl; } else if(*itBegin == ' ') { ///// Trims the std::string from the left ///// std::cout<< "=====Deletes the first whitespace=====" <<std::endl; std::cout<< boost::algorithm::trim_left_copy(*m_TextBuffer) <<std::endl; std::cout<< "=====/Deletes the first whitespace=====" <<std::endl; } else if( ((*m_TextBuffer)[m_TextBuffer->size()] == ' ')) { ///// Trims the std::string from the right ///// std::cout<< "=====Deletes the last whitespace=====" <<std::endl; boost::algorithm::trim_right_copy(*m_TextBuffer); std::cout<< "=====/Deletes the last whitespace=====" <<std::endl; } if(!vec_TextBuffer.empty()) { ///// Marks every white space ///// std::cout<< "=====Whitespaces Tracker=====" <<std::endl; std::cout<< boost::algorithm::join(vec_TextBuffer, "[whitespace] ") <<std::endl; std::cout<< "=====/Whitespaces Tracker=====" <<std::endl; } /*///// Tokenizer algorithm /////*/ ///// Interprets the token ///// for(Tokenizer::iterator it(m_Tokenizer->begin()); it != m_Tokenizer->end(); ++it, TextBufInkr++) std::cout<< "[" << TextBufInkr <<". Token]" << *it <<std::endl; } int main() { std::vector<std::string> TestVector; TestVector.push_back(" Hallo, "); TestVector.push_back("mein Name ist "); TestVector.push_back("Häbbärt! "); StringProcessor strproc(TestVector, "German"); std::cin.get(); };Zeilen die mir Sorgen machen:
-19: Wie besser lösen?
-105: Hier gibt es eine Assertion ("(unsigned)(c + 1) <= 256"). Warum?Ansonsten:
Der Sinn der einzelnen Ctors soll erstmal nicht im Vordergrund stehen. Fände es toll wenn einer einfach mal sagt was er nicht so toll findet und sogar begründen könnte.Danke! :xmas1: :xmas2:
EDIT: Wth!? Bin ich jetzt doof oder funktionieren die Codetags nicht!?
EDIT²: Jeah, pumuckl ist der king.
EDIT³: *push*
-
das liegt an deinen [deletes...][/deletes...] pseudo-BB-tags, damit kommt der parser wohl nicht ganz klar.