In C++20, the std::format library is introduced. However, it is not supported under restriction of ABACUS development that not later than C++11. Plus in ABACUS the formatting-output demands is not quite general but more specific, therefore, a simple alternative is proposed here. To use:
More...
#include <formatter.h>
|
| FmtCore (const std::string &fmt) |
|
| ~FmtCore () |
|
template<typename... Ts> |
std::string | format (const Ts &... args) |
| std::string overload of the varadic template function
|
|
void | reset (const std::string &fmt="") |
| reset the format string (std::string overloads)
|
|
const std::string & | fmt () |
| get the format string
|
|
|
template<typename... Ts> |
static std::string | format (const char *fmt, const Ts &... args) |
| static function to format data
|
|
static std::vector< std::string > | split (const std::string &in, const std::string &delim) |
| split a string with a delimiter, return uncollapse vector
|
|
static std::vector< std::string > | split (const std::string &in) |
| split a string with a delimiter, return only non-empty elements
|
|
static bool | startswith (const std::string &in, const std::string &prefix) |
|
static bool | endswith (const std::string &in, const std::string &suffix) |
|
static std::string | strip (const std::string &in, const std::string &chars=" ") |
|
static std::string | center (const std::string &in, const size_t &width, const char &fillchar=' ') |
|
static std::string | replace (const std::string &in, const std::string &old, const std::string &new_) |
|
static std::string | join (const std::string &delim, const std::vector< std::string > &src) |
|
static std::string | upper (const std::string &in) |
|
static std::string | lower (const std::string &in) |
|
|
template<typename T > |
static std::enable_if< std::is_same< T, std::string >::value, constchar * >::type | filter (const T &s) |
|
template<typename T > |
static std::enable_if<!std::is_same< T, std::string >::value, constT & >::type | filter (const T &s) |
|
In C++20, the std::format library is introduced. However, it is not supported under restriction of ABACUS development that not later than C++11. Plus in ABACUS the formatting-output demands is not quite general but more specific, therefore, a simple alternative is proposed here. To use:
- Use the static function format() to format data like
FmtCore::format("%d", 1);
- Use the class FmtCore to format data like
FmtCore fmt("%d"); fmt.format(1);
. The first way is more flexible while the second way is more efficient. The format string can be reset by reset() function. If empty, the format string is empty, otherwise it will be updated.
◆ FmtCore()
FmtCore::FmtCore |
( |
const std::string & |
fmt | ) |
|
|
inline |
◆ ~FmtCore()
◆ center()
static std::string FmtCore::center |
( |
const std::string & |
in, |
|
|
const size_t & |
width, |
|
|
const char & |
fillchar = ' ' |
|
) |
| |
|
inlinestatic |
◆ endswith()
static bool FmtCore::endswith |
( |
const std::string & |
in, |
|
|
const std::string & |
suffix |
|
) |
| |
|
inlinestatic |
◆ filter() [1/2]
static std::enable_if< std::is_same< T, std::string >::value, constchar * >::type FmtCore::filter |
( |
const T & |
s | ) |
|
|
inlinestaticprivate |
◆ filter() [2/2]
static std::enable_if<!std::is_same< T, std::string >::value, constT & >::type FmtCore::filter |
( |
const T & |
s | ) |
|
|
inlinestaticprivate |
◆ fmt()
const std::string & FmtCore::fmt |
( |
| ) |
|
|
inline |
get the format string
- Returns
- std::string
◆ format() [1/2]
template<typename... Ts>
static std::string FmtCore::format |
( |
const char * |
fmt, |
|
|
const Ts &... |
args |
|
) |
| |
|
inlinestatic |
static function to format data
- Template Parameters
-
- Parameters
-
fmt | format string |
args | data to format |
- Returns
- std::string
◆ format() [2/2]
template<typename... Ts>
std::string FmtCore::format |
( |
const Ts &... |
args | ) |
|
|
inline |
std::string overload of the varadic template function
- Parameters
-
- Returns
- std::string
◆ join()
static std::string FmtCore::join |
( |
const std::string & |
delim, |
|
|
const std::vector< std::string > & |
src |
|
) |
| |
|
inlinestatic |
◆ lower()
static std::string FmtCore::lower |
( |
const std::string & |
in | ) |
|
|
inlinestatic |
◆ replace()
static std::string FmtCore::replace |
( |
const std::string & |
in, |
|
|
const std::string & |
old, |
|
|
const std::string & |
new_ |
|
) |
| |
|
inlinestatic |
◆ reset()
void FmtCore::reset |
( |
const std::string & |
fmt = "" | ) |
|
|
inline |
reset the format string (std::string overloads)
- Parameters
-
◆ split() [1/2]
static std::vector< std::string > FmtCore::split |
( |
const std::string & |
in | ) |
|
|
inlinestatic |
split a string with a delimiter, return only non-empty elements
- Parameters
-
- Returns
- std::vector<std::string>
◆ split() [2/2]
static std::vector< std::string > FmtCore::split |
( |
const std::string & |
in, |
|
|
const std::string & |
delim |
|
) |
| |
|
inlinestatic |
split a string with a delimiter, return uncollapse vector
Python-style string functions will be implemented here as toolbox
- Parameters
-
in | string to split |
delim | delimiter |
- Returns
- std::vector<std::string>
◆ startswith()
static bool FmtCore::startswith |
( |
const std::string & |
in, |
|
|
const std::string & |
prefix |
|
) |
| |
|
inlinestatic |
◆ strip()
static std::string FmtCore::strip |
( |
const std::string & |
in, |
|
|
const std::string & |
chars = " " |
|
) |
| |
|
inlinestatic |
◆ upper()
static std::string FmtCore::upper |
( |
const std::string & |
in | ) |
|
|
inlinestatic |
◆ fmt_
std::string FmtCore::fmt_ |
|
private |
The documentation for this class was generated from the following file:
- /home/runner/work/abacus-develop/abacus-develop/source/source_base/formatter.h