diff --git a/alib/src/string/String.cpp b/alib/src/string/String.cpp index 7f746927d4ed6e420a2450077297a7e069625494..db3a74b2f45acba3d5b88a6082d6e7a8ade938d8 100644 --- a/alib/src/string/String.cpp +++ b/alib/src/string/String.cpp @@ -5,6 +5,8 @@ * Author: Jan Travnicek */ +#include <algorithm> + #include "String.h" #include "StringPrinter.h" #include "../AlibException.h" @@ -19,16 +21,21 @@ String::String() { void String::addAlphabetSymbol(const alphabet::Symbol& symbol) { std::pair<std::set<alphabet::Symbol>::iterator, bool> ret = this->alphabet.insert(symbol); if (!ret.second) - throw alib::AlibException("Input symbol \"" + symbol.getSymbol() + "\" already exists."); + throw alib::AlibException("Alphabet symbol \"" + symbol.getSymbol() + "\" already exists."); } void String::removeAlphabetSymbol(const alphabet::Symbol& symbol) { - auto it = this->alphabet.find (symbol); - if(it != this->alphabet.end()) - throw alib::AlibException("Input symbol \"" + symbol.getSymbol() + "\" used in string."); + auto it = this->alphabet.find( symbol ); + if( it == this->alphabet.end()) + throw alib::AlibException("Symbol \"" + symbol.getSymbol() + "\" is not in alphabet."); + + auto it2 = std::find( this->symbols.begin( ), this->symbols.end( ), symbol ); + if(it2 != this->symbols.end()) + throw alib::AlibException("Symbol \"" + symbol.getSymbol() + "\" is used in string."); + this->alphabet.erase(symbol); } - + const std::set<alphabet::Symbol>& String::getAlphabet() const { return this->alphabet; } @@ -45,11 +52,11 @@ bool String::isEmpty() const { return this->symbols.size() == 0; } -void String::toXML(std::ostream& out) { +void String::toXML(std::ostream& out) const { StringPrinter::toXML(*this, out); } -std::ostream& operator <<(std::ostream& out, String& string) { +std::ostream& operator <<(std::ostream& out, const String& string) { string.toXML(out); return out; } diff --git a/alib/src/string/String.h b/alib/src/string/String.h index 12277cece68d4c05cbde6632cc8ea1acc77d0578..dc4f603fa024fa85b25b8f0b6df29f50e3980a22 100644 --- a/alib/src/string/String.h +++ b/alib/src/string/String.h @@ -28,39 +28,39 @@ private: public: String(); - + /** * Adds input symbol to the alphabet. * @param symbol Symbol to add - * @throws AutomatonException when symbol already exists + * @throws AlibException if symbol already exists */ void addAlphabetSymbol(const alphabet::Symbol& symbol); /** * Removes input symbol from the the alphabet. * @param symbol Symbol to remove - * @throws AutomatonException when symbol is not present in input alphabet - * or when symbol is part of the transition + * @throws AlibException if symbol is not present in input alphabet + * or when symbol is present in content of the string. */ void removeAlphabetSymbol(const alphabet::Symbol& symbol); - + /** * @return the input alphabet */ const std::set<alphabet::Symbol>& getAlphabet() const; /** - * @return Root node of the regular expression tree + * @return List of symbols forming string. */ std::list<alphabet::Symbol>& getContent(); /** - * @return Root node of the regular expression tree + * @return List of symbols forming string (const version). */ const std::list<alphabet::Symbol>& getContent() const; /** - * @return true if regexp represents empty language + * @return true if string is an empty word (its length is 0) */ bool isEmpty() const; @@ -68,14 +68,14 @@ public: * Prints XML representation of the String to the output stream. * @param out output stream to which print the String */ - void toXML(std::ostream& out); + void toXML(std::ostream& out) const; /** * Prints XML representation of the String to the output stream. * @param out output stream to which print the String - * @param regexp String to print + * @param string String to print */ - friend std::ostream& operator<<(std::ostream& out, String& regexp); + friend std::ostream& operator<<(std::ostream& out, const String& string); }; diff --git a/alib/src/string/StringPrinter.cpp b/alib/src/string/StringPrinter.cpp index f0970f4873b28145ffced5d1d2e84765d8508c7f..3746e5f6a438ad379107c7f9cfa6a3e2ef9ce2f0 100644 --- a/alib/src/string/StringPrinter.cpp +++ b/alib/src/string/StringPrinter.cpp @@ -12,14 +12,14 @@ namespace string { const std::string StringPrinter::INDENTATION = "\t"; -void StringPrinter::toXML(String& string, std::ostream& out) { +void StringPrinter::toXML(const String& string, std::ostream& out) { out << "<string>\n"; printAlphabet(string, out, INDENTATION); printContent(string, out, INDENTATION); out << "</string>\n"; } -void StringPrinter::printAlphabet(String& string, std::ostream& out, std::string prefix) { +void StringPrinter::printAlphabet(const String& string, std::ostream& out, const std::string & prefix) { out << prefix << "<alphabet>\n"; for (auto element : string.getAlphabet()) { printSymbol(element, out, prefix + INDENTATION); @@ -27,7 +27,7 @@ void StringPrinter::printAlphabet(String& string, std::ostream& out, std::string out << prefix << "</alphabet>\n"; } -void StringPrinter::printContent(String& string, std::ostream& out, std::string prefix) { +void StringPrinter::printContent(const String& string, std::ostream& out, const std::string & prefix) { out << prefix << "<content>\n"; for (auto element : string.getContent()) { printSymbol(element, out, prefix + INDENTATION); @@ -35,10 +35,8 @@ void StringPrinter::printContent(String& string, std::ostream& out, std::string out << prefix << "</content>\n"; } -void StringPrinter::printSymbol(alphabet::Symbol& element, std::ostream& out, std::string prefix) { - out << prefix << "<symbol>\n"; - out << prefix << element.getSymbol(); - out << prefix << "</symbol>\n"; +void StringPrinter::printSymbol(const alphabet::Symbol& element, std::ostream& out, const std::string & prefix) { + out << prefix << "<symbol>" << element.getSymbol() << "</symbol>\n"; } } /* namespace string */ diff --git a/alib/src/string/StringPrinter.h b/alib/src/string/StringPrinter.h index e5f178c53cbbe9c14ce0955cc476f1cb2f0be257..192fa9fa1988f1eeb45859260ede00b345196400 100644 --- a/alib/src/string/StringPrinter.h +++ b/alib/src/string/StringPrinter.h @@ -15,21 +15,22 @@ namespace notions { namespace string { /** - * This class contains methods to print XML representation of regular expression to the output stream. + * This class contains methods to print XML representation of string to the output stream. */ class StringPrinter { protected: static const std::string INDENTATION; - static void printContent(String& element, std::ostream& out, std::string prefix); - static void printAlphabet(String& content, std::ostream& out, std::string prefix); - static void printSymbol(alphabet::Symbol& symbol, std::ostream& out, std::string prefix); + + static void printContent(const String& element, std::ostream& out, const std::string & prefix); + static void printAlphabet(const String& content, std::ostream& out, const std::string & prefix); + static void printSymbol(const alphabet::Symbol& symbol, std::ostream& out, const std::string & prefix); public: /** - * Prints XML representation of RegExp to the output stream. - * @param regexp RegExp to print - * @param out output stream to which print the RegExp + * Prints XML representation of String to the output stream. + * @param string String to print + * @param out output stream to which print the String */ - static void toXML(notions::string::String& regexp, std::ostream& out); + static void toXML(const notions::string::String& regexp, std::ostream& out); }; } /* namespace string */