Commit 207ebc02 by morgue

Extended the readme + the example

parent 5ca31951
......@@ -11,7 +11,7 @@ Keywords: string, format, C++11
fmt11 is an open source **header-only** library that implements **string formatting** using **C++11** features.
It is **type safe**, and can be extended to work with **custom types**. It does not use any macro-magic, instead relies on C++ templates and overloading.
An appender and a replacer class is provide for convenient usage. Besides ASCII strings it supports **wide character strings** too.
An appender and a replacer class is provided for convenient usage. Besides ASCII strings it supports **wide character strings** too.
It relies on std::to_string for conversion to string, which allows for a simple implementation and extensibility,
but imposes some limitations, e.g. there is no special formatting for types.
......@@ -24,23 +24,30 @@ but imposes some limitations, e.g. there is no special formatting for types.
#include <fmt11.h>
using namespace std;
try {
// fmt11::Replace takes a format string a replaces the placeholders with the provided values (of different types)
cout<< fmt11::Replace("%s %s, %s %s!") % "hello" % "world" % 42 % 3.14 <<endl;
// operator << can be used too, but it has different precedence
cout<< (fmt11::Replace("%s %s, %s %s!") << "hello" << "world" << 42 << 3.14) <<endl;
// it also supports wide strings
wcout<< fmt11::ReplaceW(L"%s %s, %s %s!") % L"hello" % L"world" % 42 % 3.14 <<endl;
// the placeholder can be changed passing a second parameter to Replace
cout<< fmt11::Replace("{} {}, {} {}!", "{}") % "hello" % "world" % 42 % 3.14 <<endl;
}
catch(std::exception& e)
int main()
{
cout << "exception: " << e.what() << endl;
try {
// fmt11::Replace takes a format string and replaces the placeholders with the provided values (of different types)
cout<< fmt11::Replace("%s %s, %s %s!") % "hello" % "world" % 42 % 3.14 <<endl;
// operator << can be used too, but it has different precedence
cout<< (fmt11::Replace("%s %s, %s %s!") << "hello" << "world" << 42 << 3.14) <<endl;
// it also supports wide strings
wcout<< fmt11::ReplaceW(L"%s %s, %s %s!") % L"hello" % L"world" % 42 % 3.14 <<endl;
// the placeholder can be changed passing a second parameter to Replace
cout<< fmt11::Replace("{} {}, {} {}!", "{}") % "hello" % "world" % 42 % 3.14 <<endl;
// besides piping into streams, Append and Replace can be converted to std::string implicitly, or explicitly by calling the .str() method
string greetings = fmt11::Replace("{} {}", "{}") % "hello" % "world";
cout<< greetings <<endl;
}
catch(std::exception& e)
{
cout << "exception: " << e.what() << endl;
}
return 0;
}
```
......@@ -52,19 +59,23 @@ catch(std::exception& e)
using namespace std;
try {
// fmt11::Append can concatenate values of different types into a string
cout<< fmt11::Append("hello ") + "world " + "asdf " + 42 + ' ' + 3.14 <<endl;
// operator << can be used too, but it has different precedence
cout<< (fmt11::Append("hello ") << "world " << "asdf " << 42 << ' ' << 3.14) <<endl;
// it also supports wide strings
wcout<< fmt11::AppendW(L"hello ") + L"world " + L"asdf " + 42 + L' ' + 3.14 <<endl;
}
catch(std::exception& e)
int main()
{
cout << "exception: " << e.what() << endl;
try {
// fmt11::Append can concatenate values of different types into a string
cout<< fmt11::Append("hello ") + "world " + "asdf " + 42 + ' ' + 3.14 <<endl;
// operator << can be used too, but it has different precedence
cout<< (fmt11::Append("hello ") << "world " << "asdf " << 42 << ' ' << 3.14) <<endl;
// it also supports wide strings
wcout<< fmt11::AppendW(L"hello ") + L"world " + L"asdf " + 42 + L' ' + 3.14 <<endl;
}
catch(std::exception& e)
{
cout << "exception: " << e.what() << endl;
}
return 0;
}
```
......@@ -83,18 +94,22 @@ struct CustomType {
: str(str) {}
};
// by overloading to_string (or to_wstring for the wide string versions) to accept the CustomType
// by overloading to_string (or to_wstring for the wide string versions) to accept the CustomType,
// it becomes usable in Append and Replace
inline const std::string to_string(const CustomType& ct) { return std::string(ct.str); }
try {
// custom types can be used by implementing to_string for it
cout<< fmt11::Append("hello ") + CustomType("world") <<endl;
cout<< fmt11::Replace("hello %s") % CustomType("world") <<endl;
}
catch(std::exception& e)
int main()
{
cout << "exception: " << e.what() << endl;
try {
// custom types can be used by implementing to_string for it
cout<< fmt11::Append("hello ") + CustomType("world") <<endl;
cout<< fmt11::Replace("hello %s") % CustomType("world") <<endl;
}
catch(std::exception& e)
{
cout << "exception: " << e.what() << endl;
}
return 0;
}
```
......
......@@ -46,6 +46,10 @@ int main()
// the placeholder can be freely chosen
cout<< fmt11::Replace("{} {}, {} {}!", "{}") % "hello" % "world" % 42 % 3.14 <<endl;
// besides piping into streams, Append and Replace can be converted to std::string implicitly,
// or explicitly by calling the .str() method
string greetings = fmt11::Replace("{} {}", "{}") % "hello" % "world";
cout<< greetings <<endl;
// custom types can be used by implementing to_string for it
cout<< fmt11::Append("hello ") + CustomType("world") <<endl;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment