From: Mikko Rasa Date: Sat, 28 May 2011 07:52:49 +0000 (+0300) Subject: Rename formatter.* to format.* X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=3a965c87750250c8facc9dbff02aeb3a88e19f05;p=libs%2Fcore.git Rename formatter.* to format.* --- diff --git a/source/core/variant.cpp b/source/core/variant.cpp index 1cdc0a5..652999f 100644 --- a/source/core/variant.cpp +++ b/source/core/variant.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include "variant.h" using namespace std; diff --git a/source/strings/format.cpp b/source/strings/format.cpp new file mode 100644 index 0000000..dfd3800 --- /dev/null +++ b/source/strings/format.cpp @@ -0,0 +1,79 @@ +/* $Id$ + +This file is part of libmspstrings +Copyright © 2006-2007 Mikko Rasa +Distributed under the LGPL +*/ + +#include "format.h" + +using namespace std; + +namespace Msp { + +Formatter::Formatter(const string &f): + fmt(f), + pos(fmt.begin()) +{ + advance(); +} + +/** +Returns the result of the formatting operation. Will throw if not enough +values have been fed to the formatter. +*/ +const string &Formatter::str() const +{ + if(pos!=fmt.end()) + throw Exception("Too few arguments for format"); + + return result; +} + +/** +Advances the pos iterator to the next conversion, adding literal characters to +the result. The iterator is left at the second character of the conversion +(i.e. after the %). +*/ +void Formatter::advance() +{ + for(; pos!=fmt.end(); ++pos) + { + if(*pos=='%') + { + ++pos; + if(pos==fmt.end()) + throw Exception("Malformed format string"); + if(*pos!='%') + break; + } + + result += *pos; + } +} + +/** +Reads the next conversion from the format string and returns a corresponding +Fmt object. +*/ +Fmt Formatter::get_conversion() +{ + if(pos==fmt.end()) + throw Exception("Too many arguments for format"); + + string::iterator i = pos; + for(; i!=fmt.end(); ++i) + if(isalpha(*i)) + break; + + if(i==fmt.end()) + throw Exception("Malformed format string"); + + ++i; + string c(pos, i); + pos = i; + + return Fmt(c); +} + +} // namespace Msp diff --git a/source/strings/format.h b/source/strings/format.h new file mode 100644 index 0000000..db74bf9 --- /dev/null +++ b/source/strings/format.h @@ -0,0 +1,70 @@ +/* $Id$ + +This file is part of libmspstrings +Copyright © 2006-2007 Mikko Rasa +Distributed under the LGPL +*/ + +#ifndef MSP_STRINGS_FORMAT_H_ +#define MSP_STRINGS_FORMAT_H_ + +#include +#include "lexicalcast.h" + +namespace Msp { + +/** +Printf-like string formatter class. +*/ +class Formatter +{ +private: + std::string fmt; + std::string::iterator pos; + std::string result; + +public: + Formatter(const std::string &); + + /** Extracts the next conversion from the format string and formats the + given value with it. Will throw if no more conversions are found. */ + template + Formatter &operator()(const T &a) + { + result += lexical_cast(a, get_conversion()); + advance(); + return *this; + } + + const std::string &str() const; +private: + void advance(); + Fmt get_conversion(); +}; + +inline Formatter format(const std::string &f) +{ return Formatter(f); } + +template +inline std::string format(const std::string &f, const A1 &a1) +{ return Formatter(f)(a1).str(); } + +template +inline std::string format(const std::string &f, const A1 &a1, const A2 &a2) +{ return Formatter(f)(a1)(a2).str(); } + +template +inline std::string format(const std::string &f, const A1 &a1, const A2 &a2, const A3 &a3) +{ return Formatter(f)(a1)(a2)(a3).str(); } + +template +inline std::string format(const std::string &f, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4) +{ return Formatter(f)(a1)(a2)(a3)(a4).str(); } + +template +inline std::string format(const std::string &f, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5) +{ return Formatter(f)(a1)(a2)(a3)(a4)(a5).str(); } + +} // namespace Msp + +#endif diff --git a/source/strings/formatter.cpp b/source/strings/formatter.cpp deleted file mode 100644 index d9d311d..0000000 --- a/source/strings/formatter.cpp +++ /dev/null @@ -1,79 +0,0 @@ -/* $Id$ - -This file is part of libmspstrings -Copyright © 2006-2007 Mikko Rasa -Distributed under the LGPL -*/ - -#include "formatter.h" - -using namespace std; - -namespace Msp { - -Formatter::Formatter(const string &f): - fmt(f), - pos(fmt.begin()) -{ - advance(); -} - -/** -Returns the result of the formatting operation. Will throw if not enough -values have been fed to the formatter. -*/ -const string &Formatter::str() const -{ - if(pos!=fmt.end()) - throw Exception("Too few arguments for format"); - - return result; -} - -/** -Advances the pos iterator to the next conversion, adding literal characters to -the result. The iterator is left at the second character of the conversion -(i.e. after the %). -*/ -void Formatter::advance() -{ - for(; pos!=fmt.end(); ++pos) - { - if(*pos=='%') - { - ++pos; - if(pos==fmt.end()) - throw Exception("Malformed format string"); - if(*pos!='%') - break; - } - - result += *pos; - } -} - -/** -Reads the next conversion from the format string and returns a corresponding -Fmt object. -*/ -Fmt Formatter::get_conversion() -{ - if(pos==fmt.end()) - throw Exception("Too many arguments for format"); - - string::iterator i = pos; - for(; i!=fmt.end(); ++i) - if(isalpha(*i)) - break; - - if(i==fmt.end()) - throw Exception("Malformed format string"); - - ++i; - string c(pos, i); - pos = i; - - return Fmt(c); -} - -} // namespace Msp diff --git a/source/strings/formatter.h b/source/strings/formatter.h deleted file mode 100644 index cde508b..0000000 --- a/source/strings/formatter.h +++ /dev/null @@ -1,70 +0,0 @@ -/* $Id$ - -This file is part of libmspstrings -Copyright © 2006-2007 Mikko Rasa -Distributed under the LGPL -*/ - -#ifndef MSP_STRINGS_FORMATTER_H_ -#define MSP_STRINGS_FORMATTER_H_ - -#include -#include "lexicalcast.h" - -namespace Msp { - -/** -Printf-like string formatter class. -*/ -class Formatter -{ -private: - std::string fmt; - std::string::iterator pos; - std::string result; - -public: - Formatter(const std::string &); - - /** Extracts the next conversion from the format string and formats the - given value with it. Will throw if no more conversions are found. */ - template - Formatter &operator()(const T &a) - { - result += lexical_cast(a, get_conversion()); - advance(); - return *this; - } - - const std::string &str() const; -private: - void advance(); - Fmt get_conversion(); -}; - -inline Formatter format(const std::string &f) -{ return Formatter(f); } - -template -inline std::string format(const std::string &f, const A1 &a1) -{ return Formatter(f)(a1).str(); } - -template -inline std::string format(const std::string &f, const A1 &a1, const A2 &a2) -{ return Formatter(f)(a1)(a2).str(); } - -template -inline std::string format(const std::string &f, const A1 &a1, const A2 &a2, const A3 &a3) -{ return Formatter(f)(a1)(a2)(a3).str(); } - -template -inline std::string format(const std::string &f, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4) -{ return Formatter(f)(a1)(a2)(a3)(a4).str(); } - -template -inline std::string format(const std::string &f, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5) -{ return Formatter(f)(a1)(a2)(a3)(a4)(a5).str(); } - -} // namespace Msp - -#endif diff --git a/source/strings/regex.cpp b/source/strings/regex.cpp index f45c820..ef99d80 100644 --- a/source/strings/regex.cpp +++ b/source/strings/regex.cpp @@ -8,7 +8,7 @@ Distributed under the LGPL #include #include #include -#include "formatter.h" +#include "format.h" #include "regex.h" using namespace std;