diff --git a/libwccl/ops/conditional.h b/libwccl/ops/conditional.h index 785c1d0f7bb13886af503cdc19d8e6e3226b1e7a..dfae9327ba6987c879b83c3b3b190b7bb9dfbea6 100644 --- a/libwccl/ops/conditional.h +++ b/libwccl/ops/conditional.h @@ -9,6 +9,9 @@ #include <libwccl/ops/formatters.h> #include <libwccl/ops/constant.h> +#include <sstream> +#include <boost/format.hpp> + namespace Wccl { /** @@ -37,16 +40,7 @@ public: * String representation of conditional operator in form of: * "if cond_expr_string then iftrue_expr_string else iffalse_expr_string" */ - virtual std::string to_string(const Corpus2::Tagset& tagset) const { - std::string s(this->operator_name(tagset)); - s.append(" "); - s.append(cond_expr_->to_string(tagset)); - s.append(" then "); - s.append(iftrue_expr_->to_string(tagset)); - s.append(" else "); - s.append(iffalse_expr_->to_string(tagset)); - return s; - } + virtual std::string to_string(const Corpus2::Tagset& tagset) const; /** * String representation of conditional operator in form of: @@ -54,16 +48,7 @@ public: * This version does not require tagset, but may be inclomplete * and/or contain internal info. */ - virtual std::string to_raw_string() const { - std::string s(this->raw_operator_name()); - s.append(" "); - s.append(cond_expr_->to_raw_string()); - s.append(" then "); - s.append(iftrue_expr_->to_raw_string()); - s.append(" else "); - s.append(iffalse_expr_->to_raw_string()); - return s; - } + virtual std::string to_raw_string() const; virtual const std::string raw_operator_name() const { return "if"; @@ -118,14 +103,7 @@ public: * String representation of conditional operator in form of: * "? if_true_expr_string ? cond_expr_string" */ - virtual std::string to_string(const Corpus2::Tagset& tagset) const { - std::string s(this->operator_name(tagset)); - s.append(" "); - s.append(this->iftrue_expr_->to_string(tagset)); - s.append(" ? "); - s.append(this->cond_expr_->to_string(tagset)); - return s; - } + virtual std::string to_string(const Corpus2::Tagset& tagset) const; /** * String representation of conditional operator in form of: @@ -133,20 +111,59 @@ public: * This version does not require tagset, but may be inclomplete * and/or contain internal info. */ - virtual std::string to_raw_string() const { - std::string s(this->raw_operator_name()); - s.append(" "); - s.append(this->iftrue_expr_->to_raw_string()); - s.append(" ? "); - s.append(this->cond_expr_->to_raw_string()); - return s; - } + virtual std::string to_raw_string() const; virtual const std::string raw_operator_name() const { return "?"; } }; +template<class T> +std::string Conditional<T>::to_raw_string() const +{ + std::stringstream ss; + ss << boost::format("%1% %2% then %3% else %4%") + % this->raw_operator_name() + % this->cond_expr_->to_raw_string() + % this->iftrue_expr_->to_raw_string() + % this->iffalse_expr_->to_raw_string(); + return ss.str(); +} + +template<class T> +std::string Conditional<T>::to_string(const Corpus2::Tagset &tagset) const +{ + std::stringstream ss; + ss << boost::format("%1% %2% then %3% else %4%") + % this->operator_name(tagset) + % this->cond_expr_->to_string(tagset) + % this->iftrue_expr_->to_string(tagset) + % this->iffalse_expr_->to_string(tagset); + return ss.str(); +} + + +template<class T> +std::string ConditionalOp<T>::to_raw_string() const +{ + std::stringstream ss; + ss << boost::format("%1% %2% ? %3%") + % this->raw_operator_name() + % this->iftrue_expr_->to_raw_string() + % this->cond_expr_->to_raw_string(); + return ss.str(); +} + +template<class T> +std::string ConditionalOp<T>::to_string(const Corpus2::Tagset &tagset) const +{ + std::stringstream ss; + ss << boost::format("%1% %2% ? %3%") + % this->operator_name(tagset) + % this->iftrue_expr_->to_string(tagset) + % this->cond_expr_->to_string(tagset); + return ss.str(); +} } /* end ns Wccl */ diff --git a/libwccl/ops/formatters.cpp b/libwccl/ops/formatters.cpp index 4adf2da37d6fa73e6cabf8dff028a2475cffa290..2b6daca6ffe73d69a15397eab3a1d40276691e59 100644 --- a/libwccl/ops/formatters.cpp +++ b/libwccl/ops/formatters.cpp @@ -1,4 +1,5 @@ #include <libwccl/ops/formatters.h> +#include <sstream> namespace Wccl { @@ -10,11 +11,10 @@ std::string UnaryFunctionFormatter::to_raw_string( const char* open_bracket, const char* close_bracket) { - std::string s(f.raw_operator_name()); - s.append(open_bracket); - s.append(arg_expr.to_raw_string()); - s.append(close_bracket); - return s; + std::stringstream ss; + ss << f.raw_operator_name() << open_bracket << arg_expr.to_raw_string() + << close_bracket; + return ss.str(); } std::string UnaryFunctionFormatter::to_string( @@ -24,11 +24,10 @@ std::string UnaryFunctionFormatter::to_string( const char* open_bracket, const char* close_bracket) { - std::string s(f.operator_name(tagset)); - s.append(open_bracket); - s.append(arg_expr.to_string(tagset)); - s.append(close_bracket); - return s; + std::stringstream ss; + ss << f.operator_name(tagset) << open_bracket << arg_expr.to_string(tagset) + << close_bracket; + return ss.str(); } // ----- BinaryFunctionFormatter ------ @@ -39,13 +38,10 @@ std::string BinaryFunctionFormatter::to_string( const FunctionBase& arg1_expr, const FunctionBase& arg2_expr) { - std::string s(f.operator_name(tagset)); - s.append("("); - s.append(arg1_expr.to_string(tagset)); - s.append(", "); - s.append(arg2_expr.to_string(tagset)); - s.append(")"); - return s; + std::stringstream ss; + ss << f.operator_name(tagset) << "(" << arg1_expr.to_string(tagset) + << ", " << arg2_expr.to_string(tagset) << ")"; + return ss.str(); } std::string BinaryFunctionFormatter::to_raw_string( @@ -53,13 +49,10 @@ std::string BinaryFunctionFormatter::to_raw_string( const FunctionBase& arg1_expr, const FunctionBase& arg2_expr) { - std::string s(f.raw_operator_name()); - s.append("("); - s.append(arg1_expr.to_raw_string()); - s.append(", "); - s.append(arg2_expr.to_raw_string()); - s.append(")"); - return s; + std::stringstream ss; + ss << f.raw_operator_name() << "(" << arg1_expr.to_raw_string() + << ", " << arg2_expr.to_raw_string() << ")"; + return ss.str(); } } /* end ns Wccl */ diff --git a/libwccl/ops/logicalpredicate.cpp b/libwccl/ops/logicalpredicate.cpp index 750f1445eb32e084d7a2d206d12858ac7be0b705..c00be52f9917551dedcdaff931119d4b63ff3e81 100644 --- a/libwccl/ops/logicalpredicate.cpp +++ b/libwccl/ops/logicalpredicate.cpp @@ -1,35 +1,37 @@ #include <libwccl/ops/logicalpredicate.h> +#include <sstream> + namespace Wccl { std::string LogicalPredicate::to_string(const Corpus2::Tagset& tagset) const { - std::string s(operator_name(tagset)); - s.append("("); + std::stringstream ss; + ss << operator_name(tagset) << "("; BoolFunctionPtrVector::const_iterator it = expressions_->begin(); while(it != expressions_->end()) { - s.append((*it)->to_string(tagset)); + ss << (*it)->to_string(tagset); if(++it != expressions_->end()) { - s.append(", "); + ss << ", "; } } - s.append(")"); - return s; + ss << ")"; + return ss.str(); } std::string LogicalPredicate::to_raw_string() const { - std::string s(raw_operator_name()); - s.append("("); + std::stringstream ss; + ss << raw_operator_name() << "("; BoolFunctionPtrVector::const_iterator it = expressions_->begin(); while(it != expressions_->end()) { - s.append((*it)->to_raw_string()); + ss << (*it)->to_raw_string(); if(++it != expressions_->end()) { - s.append(", "); + ss << ", "; } } - s.append(")"); - return s; + ss << ")"; + return ss.str(); } } /* end ns Wccl */