Skip to content

hljeong/cfmt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cfmt

format strings in c. supports custom formatters. passes va_list by value, so probably not portable. works on my computer tho

str_view and str_builder included gratis

usage

single-header technique inspired by stb

#define CFMT_IMPL
#include "cfmt.h"

int main() {
  // drop-in replacement for the standard ones
  // except sprintf() and vsprintf() which are
  // worthless pieces of trash
  print("hello %s\n", "world");
}

custom formatters

static int fmt_custom(const sink s, va_list ap) {
  const int n = va_arg(ap, int);
  const int m = va_arg(ap, int);
  // see also: vemitf()
  return emitf(s, "n=%d, m=%d, n+m=%d", n, m, n + m);
}

int main() {
  add_formatter("custom", fmt_custom);
  print("result: {custom}\n", 67, 727);  // prints: result: n=67, m=727, n+m=794
}

escaped braces

print("braces: {{ and }}\n");  // prints: braces: { and }

str_view

non-owning string reference. just a pointer and a length, no heap allocation

str_view a = sv_create("hello", 5);
str_view b = sv_create("world", 5);

sv_cmp(a, b);          // compare two views
sv_cmp_s(a, "hello");  // compare view to c string
print("{sv}\n", a);    // formatter included

str_builder

growable string buffer. allocates on the heap

str_builder sb = sb_create(16);  // start with 16 bytes
sb_append(&sb, "hello ");
sb_append(&sb, "%s %d", "world", 42);
// sb.buf now contains "hello world 42"
sb_free(&sb);

shoutouts

About

format strings in c, amongst other string utilities

Resources

License

Stars

Watchers

Forks

Contributors