-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathggformat.cpp
394 lines (337 loc) · 10.9 KB
/
ggformat.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
* ggformat v1.1
*
* Copyright (c) 2017 Michael Savage <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <ctype.h>
#include "ggformat.h"
static size_t ggformat_strlcat( char * dst, const char * src, size_t dsize );
static long long ggformat_strtonum( const char * numstr, long long minval, long long maxval, const char ** errstrp );
template< typename To, typename From >
inline To checked_cast( const From & from ) {
To result = To( from );
GGFORMAT_ASSERT( From( result ) == from );
return result;
}
namespace {
struct ShortString {
char buf[ 16 ];
ShortString() {
buf[ 0 ] = '\0';
}
void operator+=( int x ) {
char num[ 16 ];
snprintf( num, sizeof( num ), "%d", x );
*this += num;
}
void operator+=( const char * str ) {
ggformat_strlcat( buf, str, sizeof( buf ) );
}
};
}
template< typename T >
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;
if( fb->len >= fb->capacity ) {
dst = NULL;
len = 0;
}
#if GGFORMAT_COMPILER_MSVC
#pragma warning( disable : 4996 ) // '_snprintf': This function or variable may be unsafe.
int printed = _snprintf( NULL, 0, fmt.buf, x );
_snprintf( dst, len, fmt.buf, x );
#else
int printed = snprintf( dst, len, fmt.buf, x );
#endif
fb->len += checked_cast< size_t >( printed );
}
void format( FormatBuffer * fb, double x, const FormatOpts & opts ) {
::ShortString fmt;
fmt += "%";
int precision = opts.precision != -1 ? opts.precision : 5;
if( opts.plus_sign ) fmt += "+";
if( opts.left_align ) fmt += "-";
if( opts.zero_pad ) fmt += "0";
if( opts.width != -1 ) fmt += opts.width + 1 + precision;
fmt += ".";
fmt += precision;
fmt += "f";
format_helper( fb, fmt, x );
}
void format( FormatBuffer * fb, char x, const FormatOpts & opts ) {
::ShortString fmt;
fmt += "%";
if( opts.left_align ) fmt += "-";
if( opts.width != -1 ) fmt += opts.width;
fmt += "c";
format_helper( fb, fmt, x );
}
void format( FormatBuffer * fb, const char * x, const FormatOpts & opts ) {
::ShortString fmt;
fmt += "%";
if( opts.left_align ) fmt += "-";
if( opts.width != -1 ) fmt += opts.width;
fmt += "s";
format_helper( fb, fmt, x );
}
void format( FormatBuffer * fb, bool x, const FormatOpts & opts ) {
format( fb, x ? "true" : "false", opts );
}
template< typename T >
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 += "+";
if( opts.left_align ) fmt += "-";
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 ) {
fmt += "s";
char binary[ sizeof( x ) * 8 + 1 ];
binary[ sizeof( x ) * 8 ] = '\0';
for( size_t i = 0; i < sizeof( x ) * 8; i++ ) {
unsigned long long bit = x & ( ( unsigned long long ) 1 << ( sizeof( x ) * 8 - i - 1 ) );
binary[ i ] = bit == 0 ? '0' : '1';
}
format_helper( fb, fmt, binary );
return;
}
format_helper( fb, fmt, x );
}
#define INT_OVERLOADS( T, fmt_length ) \
void format( FormatBuffer * fb, signed T x, const FormatOpts & opts ) { \
int_helper( fb, fmt_length, "d", x, opts ); \
} \
void format( FormatBuffer * fb, unsigned T x, const FormatOpts & opts ) { \
int_helper( fb, fmt_length, "u", x, opts ); \
}
INT_OVERLOADS( char, "hh" )
INT_OVERLOADS( short, "h" )
INT_OVERLOADS( int, "" )
INT_OVERLOADS( long, "l" )
INT_OVERLOADS( long long, "ll" )
#undef INT_OVERLOADS
static const char * parse_format_bool( const char * p, const char * one_past_end, char x, bool * out ) {
if( p >= one_past_end ) return p;
if( *p != x ) return p;
*out = true;
return p + 1;
}
static const char * parse_format_int( const char * p, const char * one_past_end, int * out ) {
char num[ 16 ];
size_t num_len = 0;
while( p + num_len < one_past_end && isdigit( p[ num_len ] ) ) {
num[ num_len ] = p[ num_len ];
num_len++;
}
num[ num_len ] = '\0';
if( num_len == 0 ) return p;
*out = int( ggformat_strtonum( num, 1, 1024, NULL ) );
GGFORMAT_ASSERT( *out != 0 );
return p + num_len;
}
static const char * parse_format_precision( const char * p, const char * one_past_end, int * precision ) {
bool has_a_dot = false;
const char * after_dot = parse_format_bool( p, one_past_end, '.', &has_a_dot );
if( !has_a_dot ) return p;
return parse_format_int( after_dot, one_past_end, precision );
}
static const char * parse_format_number_format( const char * p, const char * one_past_end, FormatOpts::NumberFormat * number_format ) {
bool hex = false;
const char * after_hex = parse_format_bool( p, one_past_end, 'x', &hex );
if( hex ) {
*number_format = FormatOpts::HEX;
return after_hex;
}
bool bin = false;
const char * after_bin = parse_format_bool( p, one_past_end, 'b', &bin );
if( bin ) {
*number_format = FormatOpts::BINARY;
return after_bin;
}
return p;
}
FormatOpts parse_formatopts( const char * fmt, size_t len ) {
FormatOpts opts;
const char * start = fmt;
const char * one_past_end = start + len;
start = parse_format_bool( start, one_past_end, '+', &opts.plus_sign );
start = parse_format_bool( start, one_past_end, '-', &opts.left_align );
start = parse_format_bool( start, one_past_end, '0', &opts.zero_pad );
start = parse_format_int( start, one_past_end, &opts.width );
start = parse_format_precision( start, one_past_end, &opts.precision );
start = parse_format_number_format( start, one_past_end, &opts.number_format );
GGFORMAT_ASSERT( start == one_past_end );
return opts;
}
static bool strchridx( const char * haystack, char needle, size_t * idx, size_t skip = 0 ) {
*idx = skip;
while( haystack[ *idx ] != '\0' ) {
if( haystack[ *idx ] == needle ) {
return true;
}
( *idx )++;
}
return false;
}
bool ggformat_find( const char * str, size_t * start, size_t * one_past_end ) {
size_t open_idx;
bool has_open = strchridx( str, '{', &open_idx );
if( has_open && str[ open_idx + 1 ] == '{' ) {
has_open = false;
}
if( !has_open ) open_idx = 0;
size_t close_idx;
bool has_close = strchridx( str, '}', &close_idx, open_idx );
if( has_close && str[ close_idx + 1 ] == '}' ) {
has_close = false;
}
if( has_open ) {
GGFORMAT_ASSERT( has_close );
GGFORMAT_ASSERT( open_idx < close_idx );
*start = open_idx;
*one_past_end = close_idx;
return true;
}
GGFORMAT_ASSERT( !has_close );
return false;
}
void ggformat_literals( FormatBuffer * fb, const char * literals, size_t len ) {
size_t copied_len = 0;
for( size_t i = 0; i < len; i++ ) {
if( literals[ i ] == '{' || literals[ i ] == '}' ) {
i++;
}
if( fb->len + copied_len < fb->capacity ) {
fb->buf[ fb->len + copied_len ] = literals[ i ];
}
copied_len++;
}
fb->len += copied_len;
if( fb->capacity > 0 ) {
fb->buf[ fb->len < fb->capacity - 1 ? fb->len : fb->capacity - 1 ] = '\0';
}
}
void ggformat_impl( FormatBuffer * fb, const char * fmt ) {
size_t ignored;
GGFORMAT_ASSERT( !ggformat_find( fmt, &ignored, &ignored ) );
ggformat_literals( fb, fmt, strlen( fmt ) );
}
/*
* Copyright (c) 1998, 2015 Todd C. Miller <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
static size_t
ggformat_strlcat(char *dst, const char *src, size_t dsize)
{
const char *odst = dst;
const char *osrc = src;
size_t n = dsize;
size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end. */
while (n-- != 0 && *dst != '\0')
dst++;
dlen = dst - odst;
n = dsize - dlen;
if (n-- == 0)
return(dlen + strlen(src));
while (*src != '\0') {
if (n != 0) {
*dst++ = *src;
n--;
}
src++;
}
*dst = '\0';
return(dlen + (src - osrc)); /* count does not include NUL */
}
/*
* Copyright (c) 2004 Ted Unangst and Todd Miller
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#define INVALID 1
#define TOOSMALL 2
#define TOOLARGE 3
static long long
ggformat_strtonum(const char *numstr, long long minval, long long maxval, const char **errstrp)
{
long long ll = 0;
char *ep;
int error = 0;
struct errval {
const char *errstr;
int err;
} ev[4] = {
{ NULL, 0 },
{ "invalid", EINVAL },
{ "too small", ERANGE },
{ "too large", ERANGE },
};
ev[0].err = errno;
errno = 0;
if (minval > maxval)
error = INVALID;
else {
ll = strtoll(numstr, &ep, 10);
if (numstr == ep || *ep != '\0')
error = INVALID;
else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
error = TOOSMALL;
else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
error = TOOLARGE;
}
if (errstrp != NULL)
*errstrp = ev[error].errstr;
errno = ev[error].err;
if (error)
ll = 0;
return (ll);
}