Get the latest tech news

A string formatting library in 65 lines of C++


In this write-up, I will walk you through an implementation of a string formatting library for C++ I came up with for my video game. The end result came out really compact, at only 65 lines of code—providing a skeleton that can be supplemented with additional functionality at low cost.

Naturally, this cannot work in memory-constrained environments, such as embedded devices—where you would want to write to a small buffer and flush it in a loop to reduce memory usage—but this does not apply in the context of a desktop video game. There exist plenty of string formatting libraries for C++, such as{fmt}, or even the recently introduced std::print, but they suffer from gigantic compile-time complexity through their heavy use of template metaprogramming. Instead, I want something lean and small, using basic features of the language, and easy enough to drop into your own project, then extend and modify according to your needs—in spirit of rxi’s simple serialisation system.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of string

string

Photo of lines

lines

Photo of C++

C++

Related news:

News photo

FakeIt: C++ Mocking Made Easy

News photo

SkiftOS: A hobby OS built from scratch using C/C++ for ARM, x86, and RISC-V

News photo

No more waiting for lines: New Windows keyboard shortcuts output em and en dashes with ease