r/cprogramming • • 28d ago

MicroRegExp : tiny (50kb) regexp engine (with 3 API)

Hello,

As a part of a larger project I made a regexp engine for microcontrollers. It was tested on ESP32 / FreeRTOS, and also in Windows/CYGWIN. Didn't test it in Linux but it should work.

It is built on top of modified SLRE (Simple Lightweight Regular Expressions);

It is just 2 files (regexp.c and regexp.h) which can be include'd directly to your source file. Or it may be compiled as a separate unit.

It has 3 different API:

  1. The original one, slre_match(); no memory allocations, fastest
  2. PCRE-like functions and structures starts with pl_... (PCRE-Lite); for those familiar with PCRE
  3. PHP preg API: an implementation of PHP regular expressions; for PHP fans; functions and structures start with preg_...

There are some limitations: UTF8 is processed as stream of bytes, so [а-я] will not be understood. No lookarounds. Probably there are bugs. Hopefully - no memory leaks.

More limitations and bugs are yet to be discovered :).

There are 3 test files (AI-generated) to test the library: preg_test.c, pl_test.c (fuzzing) and slre_test.c

There is NO documentation, but preg_test.c can be used as a reference (or direct copy/paste).

Primary API is PHP's preg_-style API, which was the goal of this project.

Source code is here: GitHub - vvb333007/MicroRegExp: A C library which implements regular expression engine. PHP preg_ , PCRE and SLRE interfaces are supported · GitHub , around 50KiB of code. It can be compiled with GCC compiler from -std=c99 and up. No GCC-specific magic.

Have a good day!

6 Upvotes

2 comments sorted by

1

u/AmusedBolt 28d ago

Absence of full UTF8 support reduces potential application by 90%

1

u/Low_Lawyer_5684 28d ago edited 28d ago

Well, remaining 10% could find it useful. It is for microcontrollers mainly, i tried to keep .text and .data small. It is a component of a PHP engine for MCU's. Adding full UTF8 support means alot of overhead (memory-wise). But we are living in an AI era. I bet claude can add it if you ask him :)