Hacker News new | ask | show | jobs
by krinkatt 1162 days ago
I have something like this that serves me well:

  struct strbuf { size_t cap, len; char *str; };
  void sb_setf(struct allocator *a, struct strbuf *sb, const char *fmt, ...);
  void sb_appendf(struct allocator *a, struct strbuf *sb, const char *fmt, ...);
  // have other convenience functions for formatting fixed point values like "prefix AAA.BBB suffix" ("voltage: 7.23 V")
  // special helpers for dates, times, etc.
Just keep building that library up and you'll have growable buffer, strings, lists, hashmap (uintptr -> uintptr is all you need in 99% of cases I've found, maybe some helper functions for string key -> void * built ontop of uintptr->uintptr) + replace/rewrite the standard library to operate on these types instead and you're good to go.
2 comments

That works until you want to use code from somebody else who also has something like that that serves them well, but is slightly different.

If you’re extremely lucky, things will compile and work.

If you’re just lucky things won’t compile, and you’ll have to write conversion functions (or macros).

If you’re unlucky, there will be subtle differences, likely poorly documented, between the libraries, and code will compile but have subtle bugs.

Of course, other languages have that problem, too, but at the higher level of json parsers or graphics libraries, not at the basic level of strings, lists, or maps.

Could even use something like Gnome's GLib