Created
December 12, 2013 16:33
-
-
Save nbergont/7930920 to your computer and use it in GitHub Desktop.
Formateur de chaine de caratère std::string à la manière Qt (voir QString)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "strformat.h" | |
#include <iomanip> | |
#include <cassert> | |
strformat::strformat(const std::string &str) : _str(str), _level(1) | |
{ | |
} | |
strformat &strformat::arg(const std::string &str1) | |
{ | |
_replace_arg(str1); | |
return *this; | |
} | |
strformat &strformat::arg(const std::string &str1, const std::string &str2) | |
{ | |
_replace_arg(str1); | |
_replace_arg(str2); | |
return *this; | |
} | |
strformat &strformat::arg(const std::string &str1, const std::string &str2, const std::string &str3) | |
{ | |
_replace_arg(str1); | |
_replace_arg(str2); | |
_replace_arg(str3); | |
return *this; | |
} | |
strformat &strformat::arg(const std::string &str1, const std::string &str2, const std::string &str3, const std::string &str4) | |
{ | |
_replace_arg(str1); | |
_replace_arg(str2); | |
_replace_arg(str3); | |
_replace_arg(str4); | |
return *this; | |
} | |
strformat &strformat::arg(const char *str) | |
{ | |
if(str) | |
_replace_arg(std::string(str)); | |
else | |
_replace_arg(std::string()); | |
return *this; | |
} | |
strformat &strformat::arg(int value, int base) | |
{ | |
_replace_arg(to_string(value, base)); | |
return *this; | |
} | |
strformat &strformat::arg(unsigned int value, int base) | |
{ | |
_replace_arg(to_string(value, base)); | |
return *this; | |
} | |
strformat &strformat::arg(long value, int base) | |
{ | |
_replace_arg(to_string(value, base)); | |
return *this; | |
} | |
strformat &strformat::arg(unsigned long value, int base) | |
{ | |
_replace_arg(to_string(value, base)); | |
return *this; | |
} | |
strformat &strformat::arg(long long value, int base) | |
{ | |
_replace_arg(to_string(value, base)); | |
return *this; | |
} | |
strformat &strformat::arg(unsigned long long value, int base) | |
{ | |
_replace_arg(to_string(value, base)); | |
return *this; | |
} | |
strformat &strformat::arg(double value, int precision) | |
{ | |
std::ostringstream ostr; | |
ostr<<std::setprecision(precision)<<value; | |
_replace_arg(ostr.str()); | |
return *this; | |
} | |
strformat::operator std::string() const | |
{ | |
return _str; | |
} | |
void strformat::_replace_arg(const std::string &text) | |
{ | |
const std::string num = '%' + to_string(_level++); | |
size_t pos; | |
while((pos = _str.find(num)) != std::string::npos) | |
_str.replace(pos, num.size(), text); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef STRINGTOOLS_H | |
#define STRINGTOOLS_H | |
#include <string> | |
#include <sstream> | |
/**\brief | |
* Permet de formater une chaine de caractère | |
* Exemple : std::string str = strformat("valeur: %1 info : %2").arg(12.0).arg("test"); | |
*/ | |
class strformat | |
{ | |
public: | |
strformat(const std::string &str); | |
strformat &arg(const std::string &str1); | |
strformat &arg(const std::string &str1, const std::string &str2); | |
strformat &arg(const std::string &str1, const std::string &str2, const std::string &str3); | |
strformat &arg(const std::string &str1, const std::string &str2, const std::string &str3, const std::string &str4); | |
strformat &arg(const char *str); | |
strformat &arg(int value, int base = 10); | |
strformat &arg(unsigned int value, int base = 10); | |
strformat &arg(long value, int base = 10); | |
strformat &arg(unsigned long value, int base = 10); | |
strformat &arg(long long value, int base = 10); | |
strformat &arg(unsigned long long value, int base = 10); | |
strformat &arg(double value, int precision=15); | |
operator std::string() const; | |
template <typename T> | |
static T from_string(const std::string &text) | |
{ | |
T value; | |
std::istringstream istr(text); | |
if(text.size() > 2 && text.at(0) == '0' && (text.at(1) == 'x' || text.at(1) == 'X')) | |
istr>>std::hex>>value; | |
else | |
istr>>value; | |
return value; | |
} | |
template <typename T> | |
static T from_cstring(const char *text) | |
{ | |
if(text) | |
return from_string<T>(text); | |
return 0; | |
} | |
template <typename T> | |
static std::string to_string(T value, int base=10) | |
{ | |
std::ostringstream ostr; | |
switch(base) | |
{ | |
case 8: | |
ostr<<std::oct<<value; | |
break; | |
case 10: | |
ostr<<std::dec<<value; | |
break; | |
case 16: | |
ostr<<std::hex<<value; | |
break; | |
default: | |
ostr<<value; | |
} | |
return ostr.str(); | |
} | |
private: | |
void _replace_arg(const std::string &text); | |
std::string _str; | |
int _level; | |
}; | |
#endif // STRINGTOOLS_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment