Get the latest tech news

Giving C++ std:regex a C makeover


ullprogram.com/blog/2024/09/04/ Suppose you’re working in C using one of the major toolchains — that is, it’s mainly a C++ implementation — and you need regular expressions. You could integrate a library, but there’s a regex implementation in the C++ standard library included with your compiler, just within reach.

If matching lots of source strings, scope the arena to the loop and then the results, and any regex working memory, are automatically freed in O(1) at the end of each iteration: I can’t tell std::regex about the arena — it calls operator new the usual way, without extra arguments — so I have to smuggle it in through a thread-local variable: Interesting side note: In a rough benchmark these replacements made MSVC std::regex matching four times faster!

Get the Android app

Or read this on Hacker News

Read more on:

Photo of C++

C++

Photo of std

std

Photo of C makeover

C makeover

Related news:

News photo

Honey, I shrunk {fmt}: bringing binary size to 14k and ditching the C++ runtime

News photo

Scpptool – a tool to enforce a memory and data race safe subset of C++

News photo

Programming terrain from scratch using C++ and OpenGL by Shamus Young (2006)