ggformat

A string formatting library for C++
Log | Files | Refs

commit 8da412b7d3790234cf007a61fae77c78e88b038d
parent bba4fa44d3f089b70e68ca3651b2bd50cfb11a6b
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Fri, 25 Aug 2017 08:44:07 +0100

Use the char/short/long/long long printf prefixes

Diffstat:
basic_examples.cc | 12++++++++++++
ggformat.cc | 20+++++++++++---------
2 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/basic_examples.cc b/basic_examples.cc @@ -48,6 +48,18 @@ int main() { ggprint( "bools: {} {}\n", true, false ); ggprint( "strings: {-10} {} {{ }}\n", "hello", "world" ); + ggprint( "mins : {} {} {} {}\n", int64_t( INT64_MIN ), int32_t( INT32_MIN ), int16_t( INT16_MIN ), int8_t( INT8_MIN ) ); + ggprint( "maxs : {} {} {} {}\n", int64_t( INT64_MAX ), int32_t( INT32_MAX ), int16_t( INT16_MAX ), int8_t( INT8_MAX ) ); + ggprint( "umaxs: {} {} {} {}\n", uint64_t( UINT64_MAX ), uint32_t( UINT32_MAX ), uint16_t( UINT16_MAX ), uint8_t( UINT8_MAX ) ); + + ggprint( "mins : {x} {x} {x} {x}\n", int64_t( INT64_MIN ), int32_t( INT32_MIN ), int16_t( INT16_MIN ), int8_t( INT8_MIN ) ); + ggprint( "maxs : {x} {x} {x} {x}\n", int64_t( INT64_MAX ), int32_t( INT32_MAX ), int16_t( INT16_MAX ), int8_t( INT8_MAX ) ); + ggprint( "umaxs: {x} {x} {x} {x}\n", uint64_t( UINT64_MAX ), uint32_t( UINT32_MAX ), uint16_t( UINT16_MAX ), uint8_t( UINT8_MAX ) ); + + ggprint( "mins : {b} {b} {b} {b}\n", int64_t( INT64_MIN ), int32_t( INT32_MIN ), int16_t( INT16_MIN ), int8_t( INT8_MIN ) ); + ggprint( "maxs : {b} {b} {b} {b}\n", int64_t( INT64_MAX ), int32_t( INT32_MAX ), int16_t( INT16_MAX ), int8_t( INT8_MAX ) ); + ggprint( "umaxs: {b} {b} {b} {b}\n", uint64_t( UINT64_MAX ), uint32_t( UINT32_MAX ), uint16_t( UINT16_MAX ), uint8_t( UINT8_MAX ) ); + // user defined type v3 a = v3( 1, 2, 3 ); v3 b = v3( 4, 5, 6 ); diff --git a/ggformat.cc b/ggformat.cc @@ -108,7 +108,7 @@ void format( FormatBuffer * fb, bool x, const FormatOpts & opts ) { } template< typename T > -static void int_helper( FormatBuffer * fb, const char * fmt_decimal, const T & x, const FormatOpts & opts ) { +static void int_helper( FormatBuffer * fb, const char * fmt_length, const char * fmt_decimal, const T & x, const FormatOpts & opts ) { ShortString fmt; fmt += "%"; if( opts.plus_sign ) fmt += "+"; @@ -116,9 +116,11 @@ static void int_helper( FormatBuffer * fb, const char * fmt_decimal, const T & x if( opts.zero_pad ) fmt += "0"; if( opts.width != -1 ) fmt += opts.width; if( opts.number_format == FormatOpts::DECIMAL ) { + fmt += fmt_length; fmt += fmt_decimal; } else if( opts.number_format == FormatOpts::HEX ) { + fmt += fmt_length; fmt += "x"; } else if( opts.number_format == FormatOpts::BINARY ) { @@ -138,19 +140,19 @@ static void int_helper( FormatBuffer * fb, const char * fmt_decimal, const T & x format_helper( fb, fmt, x ); } -#define INT_OVERLOADS( T ) \ +#define INT_OVERLOADS( T, fmt_length ) \ void format( FormatBuffer * fb, signed T x, const FormatOpts & opts ) { \ - int_helper( fb, "d", x, opts ); \ + int_helper( fb, fmt_length, "d", x, opts ); \ } \ void format( FormatBuffer * fb, unsigned T x, const FormatOpts & opts ) { \ - int_helper( fb, "u", x, opts ); \ + int_helper( fb, fmt_length, "u", x, opts ); \ } -INT_OVERLOADS( char ) -INT_OVERLOADS( short ) -INT_OVERLOADS( int ) -INT_OVERLOADS( long ) -INT_OVERLOADS( long long ) +INT_OVERLOADS( char, "hh" ) +INT_OVERLOADS( short, "h" ) +INT_OVERLOADS( int, "" ) +INT_OVERLOADS( long, "l" ) +INT_OVERLOADS( long long, "ll" ) #undef INT_OVERLOADS