Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libc/printf/nanoprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#define NANOPRINTF_IMPLEMENTATION
#define NANOPRINTF_VISIBILITY_STATIC
#define NANOPRINTF_STATIC_GLOBALS

/**
* @remarks don't set this above 40, or there is a chance that
Expand Down
11 changes: 11 additions & 0 deletions src/libc/printf/nanoprintf.h
Original file line number Diff line number Diff line change
Expand Up @@ -761,13 +761,22 @@ typedef struct npf_cnt_putc_ctx {
int n;
} npf_cnt_putc_ctx_t;

#ifdef NANOPRINTF_STATIC_GLOBALS
static npf_cnt_putc_ctx_t pc_cnt;
#endif

static void npf_putc_cnt(int c, void *ctx) {
npf_cnt_putc_ctx_t *pc_cnt = (npf_cnt_putc_ctx_t *)ctx;
++pc_cnt->n;
pc_cnt->pc(c, pc_cnt->ctx); // sibling-call optimization
}

#ifdef NANOPRINTF_STATIC_GLOBALS
static void NPF_PUTC_IMPL(int VAL) { npf_putc_cnt((int)(VAL), &pc_cnt); }
#define NPF_PUTC(VAL) NPF_PUTC_IMPL((int)(VAL))
#else
#define NPF_PUTC(VAL) do { npf_putc_cnt((int)(VAL), &pc_cnt); } while (0)
#endif

#define NPF_EXTRACT(MOD, CAST_TO, EXTRACT_AS) \
case NPF_FMT_SPEC_LEN_MOD_##MOD: val = (CAST_TO)va_arg(args, EXTRACT_AS); break
Expand All @@ -778,7 +787,9 @@ static void npf_putc_cnt(int c, void *ctx) {
int npf_vpprintf(npf_putc pc, void *pc_ctx, char const *format, va_list args) {
npf_format_spec_t fs;
char const *cur = format;
#ifndef NANOPRINTF_STATIC_GLOBALS
npf_cnt_putc_ctx_t pc_cnt;
#endif
pc_cnt.pc = pc;
pc_cnt.ctx = pc_ctx;
pc_cnt.n = 0;
Expand Down
Loading