SoDaFormat-1.0.0-main:f160581
1.0.0
|
A format object that may be "filled in" with integer, float, double, string, or character values.
The format string supplied here contains value placeholders of the form "%[0-9]*" That is, a percent sign "%" followed by one or more decimal digits. Any placeholder may occur at any place in the string. A placeholder may appear more than once. So this string
is just fine.
Values are numbered starting at zero, because that's the way we roll here. Values are supplied by invocations of the Format::addX methods, as in
Most of these provide for additional format directives that determine minimum field with and decimal precision.
As each .addX method is invoked, it fills in the associated placeholders. The format string, thus evolves as the placeholders are replaced with the formatted representation of the supplied values.
The format string, like any other string object, follows the "\" escape conventions. In addition, since "%" is used as a special character, the string "%%" may be used to insert a single "%" character.
Those in areas where the decimal radix point is not "." or those who simply wish for an alternative may set the radix separator by assigning to the static variable SoDa::Format::separator like this
The output stream operator "<<" is a friend of the SoDa::Format class.
The SoDa::Format::str() method will return a reference to the current state of the format string (with all the placeholders filled in, as far as the process has progressed.)
SoDa::Format methods don't return success/failure values to distinguish between good outcomes and errors. If something goes wrong that SoDa::Format feels bad about, it will throw a SoDa::Format::BadFormat exception. This inherits from "std::runtime_exception" so it is moderately well behaved.
SoDa::Format uses exceptions because, after years of reading my code and the code of others, it is a rare and disciplined individual who checks every return value for errors. And their code looks like crap.
More skillful programmers might have overloaded one method name for all five types. However, making a C++ compiler distinguish between add(int v) and add(unsigned int v) – though it seems obvious that this should work – proved beyond my ability, and may be outside of the C++11 definition. LLVM warned about it loudly.
Definition at line 269 of file Format.hxx.
#include <Format.hxx>
Classes | |
class | BadFormat |
Exception to announce that there was something wrong with the format string, or that too many values were supplied. More... | |
Public Member Functions | |
Format (const std::string &fmt_string) | |
create a format object with placeholders and all that stuff. More... | |
Format & | addI (int v, unsigned int width=0) |
insert a signed integer into the format string More... | |
Format & | addU (unsigned int v, unsigned int width=0) |
insert an unsigned integer into the format string More... | |
Format & | addF (double v, char fmt='f', unsigned int width=0, unsigned int frac_precision=3) |
insert a float or double into the format string More... | |
Format & | addS (const std::string &v, unsigned int width=0) |
insert a string into the format string More... | |
Format & | addC (char v) |
insert a character into the format string More... | |
Format & | reset () |
reset the format string to its original value, with all the placeholders restored. More... | |
const std::string & | str (bool check_for_filled_out=false) const |
provide a string representing the current state of the format string with all placeholders "filled in" as far as the last invocation of an add method. More... | |
Static Public Attributes | |
static char | separator = '.' |
the radix separator character. More... | |
SoDa::Format::Format | ( | const std::string & | fmt_string | ) |
create a format object with placeholders and all that stuff.
fmt_string | the format string with placeholders and stuff. |
Definition at line 41 of file Format.cxx.
Format & SoDa::Format::addC | ( | char | v | ) |
insert a character into the format string
v | the character value |
Not much to say here.
Definition at line 142 of file Format.cxx.
Referenced by main().
Format & SoDa::Format::addF | ( | double | v, |
char | fmt = 'f' , |
||
unsigned int | width = 0 , |
||
unsigned int | frac_precision = 3 |
||
) |
insert a float or double into the format string
v | the double precision value (floats will be promoted) |
fmt | a choice of representations (see below) |
width | the minimum width of the field. |
frac_precision | the number of decimal positions to the right of the radix point. |
The default value for the width parameter will take up only as much room as the value requires.
The value is converted to a string in the manner of the C++ stream "std::fixed" format. The width parameter sets the minimum field width, and the frac_precision parameter determines the number of places to the right of the radix point.
The default value for the width parameter will take up only as much room as the value requires.
The value is converted to a string in the manner of the C++ stream "std::scientific" format. The width parameter sets the minimum field width, and the frac_precision parameter determines the number of places to the right of the radix point. THe number will be represented as a floating string value in the range 1.0 to 1.999999999...999 followed by an exponent specifier like "e+" or "e-" followed by one or more digits.
The exponent may take on just about any value in the range of the machine's double precision datatype.
The default value for the width parameter will take up only as much room as the value requires.
If you just want to get the job done and don't care how your values appear, the 'g' format will use the default C++ stream conversion to float. It could be fixed. It could be scientific. The width parameter will set the minimum field width.
The default value for the width parameter will take up only as much room as the value requires.
This is similar to the 's' format, but the exponent (power of 10) will always be a multiple of 3. That makes the value useful to those who think in Peta, Tera, Giga, Mega, kilo, milli, micro, nano, and pico. Those left in the CGS world can write their own code.
The width parameter is ignored. The field width is determined by the fmt choice and the frac_precision. The field contain a leading sign, up to three decimal digits, a radix point, frac_precision digits, "e+" or "e-" and one or more exponent digits.
The leading (whole) part of the value will be padded with spaces to the left of the sign.
Definition at line 70 of file Format.cxx.
References separator.
Referenced by main().
Format & SoDa::Format::addI | ( | int | v, |
unsigned int | width = 0 |
||
) |
insert a signed integer into the format string
v | the signed integer value |
width | the minimum width of the field. |
The width is passed along to the setw I/O manipulator, so it is no more useful than that. If the value takes more room than the specified width, the width parameter will be ignored and the field will be as wide as necessary to accommodate the value.
Definition at line 50 of file Format.cxx.
Referenced by main().
Format & SoDa::Format::addS | ( | const std::string & | v, |
unsigned int | width = 0 |
||
) |
insert a string into the format string
v | the string value |
width | the minimum width of the field. |
The width is passed along to the setw I/O manipulator, so it is no more useful than that. If the string is wider than the width, the field will expand to fit the string. If the string is smaller than the width the field will be padded to the left with spaces.
The default value for the width parameter will take up only as much room as the value requires.
Definition at line 132 of file Format.cxx.
Referenced by main().
Format & SoDa::Format::addU | ( | unsigned int | v, |
unsigned int | width = 0 |
||
) |
insert an unsigned integer into the format string
v | the unsigned integer value |
width | the minimum width of the field. |
The width is passed along to the setw I/O manipulator, so it is no more useful than that. If the value takes more room than the specified width, the width parameter will be ignored and the field will be as wide as necessary to accommodate the value.
Definition at line 60 of file Format.cxx.
Format & SoDa::Format::reset | ( | ) |
reset the format string to its original value, with all the placeholders restored.
This method allows a format object to be re-used.
Definition at line 207 of file Format.cxx.
Referenced by main().
const std::string & SoDa::Format::str | ( | bool | check_for_filled_out = false | ) | const |
provide a string representing the current state of the format string with all placeholders "filled in" as far as the last invocation of an add method.
check_for_filled_out | If a placeholder has not yet been replaced, this method may throw a BadFormat exception. It doesn't yet. |
Definition at line 213 of file Format.cxx.
Referenced by operator<<().
|
static |
the radix separator character.
For those in locales where pi is 3,14159 or so, the separater may be replaced, like this:
Definition at line 464 of file Format.hxx.
Referenced by addF().