Skip to content

Commit

Permalink
Merge branch 'pr/10' (close #10)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazuho committed Dec 3, 2014
2 parents 3cf4fc8 + 1314b2f commit e2ce1fe
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 33 deletions.
18 changes: 15 additions & 3 deletions misc/gen-qrintf.h.pl
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ sub build_x {
my ($check, $type, $suffix, $with_width) = @_;
return build_mt(template => << 'EOT', escape_func => undef)->($check, $type, $suffix, $with_width ? '_width' : '');
? my ($check, $type, $suffix, $width) = @_;
? my $push = $check eq 'chk' ? sub { "do { int ch = $_[0]; if (ctx.off < ctx.size) ctx.str[ctx.off] = ch; ++ctx.off; } while (0)" } : sub { "ctx.str[ctx.off++] = $_[0]" };
static inline qrintf_<?= $check ?>_t _qrintf_<?= $check ?><?= $width ?>_<?= $suffix ?>(qrintf_<?= $check ?>_t ctx<?= $width ? ", int fill_ch, int width" : "" ?>, <?= $type ?> v, const char *chars)
{
int len;
size_t len;
? if ($check eq 'chk') {
size_t rest = 0;
? }
if (v != 0) {
int bits;
if (sizeof(<?= $type ?>) == sizeof(unsigned long long))
Expand All @@ -106,12 +108,22 @@ sub build_x {
}
? if ($width) {
ctx = _qrintf_<?= $check ?>_fill(ctx, fill_ch, len, width);
? }
? if ($check eq 'chk') {
if (ctx.off + len > ctx.size) {
rest = ctx.off + len - ctx.size;
len -= rest;
v >>= rest * 4;
}
? }
len *= 4;
do {
len -= 4;
<?= $push->(q{chars[(v >> len) & 0xf]}) ?>;
ctx.str[ctx.off++] = chars[(v >> len) & 0xf];
} while (len != 0);
? if ($check eq 'chk') {
ctx.off += rest;
? }
return ctx;
}
EOT
Expand Down
Loading

0 comments on commit e2ce1fe

Please sign in to comment.