commit 72a393b284ce994e267d97e717b1a5d921f26942 parent 1b6c677696dc8f2c16696b038cb88f474ceee8ed Author: Michael Savage <mikejsavage@gmail.com> Date: Sat Mar 4 14:40:32 +0200 Don't use str in ggformat Diffstat:
ggformat.cc | | | 35 | +++++++++++++++++++++++++---------- |
diff --git a/ggformat.cc b/ggformat.cc @@ -2,12 +2,27 @@ #include "intrinsics.h" #include "ggformat.h" -#include "str.h" // TODO: implement a subset of str #include "strtonum.h" +#include "strlcat.h" #include "platform_io.h" +struct ShortString { + char buf[ 16 ]; + size_t length = 0; + + void operator+=( int x ) { + char num[ 16 ]; + snprintf( num, sizeof( num ), "%d", x ); + *this += num; + } + + void operator+=( const char * str ) { + strlcat( buf, str, sizeof( buf ) ); + } +}; + template< typename T > -static void format_helper( FormatBuffer * fb, const str< 16 > & fmt, const T & x ) { +static void format_helper( FormatBuffer * fb, const ShortString & fmt, const T & x ) { char * dst = fb->buf + fb->len; size_t len = fb->capacity - fb->len; @@ -16,12 +31,12 @@ static void format_helper( FormatBuffer * fb, const str< 16 > & fmt, const T & x len = 0; } - int printed = snprintf( dst, len, fmt.c_str(), x ); + int printed = snprintf( dst, len, fmt.buf, x ); fb->len += checked_cast< size_t >( printed ); } void format( FormatBuffer * fb, double x, const FormatOpts & opts ) { - str< 16 > fmt; + ShortString fmt; fmt += "%"; if( opts.left_align ) fmt += "-"; if( opts.width != -1 ) fmt += opts.width; @@ -34,7 +49,7 @@ void format( FormatBuffer * fb, double x, const FormatOpts & opts ) { } void format( FormatBuffer * fb, char x, const FormatOpts & opts ) { - str< 16 > fmt; + ShortString fmt; fmt += "%"; if( opts.left_align ) fmt += "-"; if( opts.width != -1 ) fmt += opts.width; @@ -43,7 +58,7 @@ void format( FormatBuffer * fb, char x, const FormatOpts & opts ) { } void format( FormatBuffer * fb, const char * x, const FormatOpts & opts ) { - str< 16 > fmt; + ShortString fmt; fmt += "%"; if( opts.left_align ) fmt += "-"; if( opts.width != -1 ) fmt += opts.width; @@ -56,8 +71,8 @@ void format( FormatBuffer * fb, bool x, const FormatOpts & opts ) { } template< typename T > -static void int_helper( FormatBuffer * fb, char fmt_decimal, const T & x, const FormatOpts & opts ) { - str< 16 > fmt; +static void int_helper( FormatBuffer * fb, const char * fmt_decimal, const T & x, const FormatOpts & opts ) { + ShortString fmt; fmt += "%"; if( opts.left_align ) fmt += "-"; if( opts.zero_pad ) fmt += "0"; @@ -75,11 +90,11 @@ static void int_helper( FormatBuffer * fb, char fmt_decimal, const T & x, const } void format( FormatBuffer * fb, long long x, const FormatOpts & opts ) { - int_helper( fb, 'd', x, opts ); + int_helper( fb, "d", x, opts ); } void format( FormatBuffer * fb, unsigned long long x, const FormatOpts & opts ) { - int_helper( fb, 'u', x, opts ); + int_helper( fb, "u", x, opts ); } #define INT_OVERLOADS( T ) \