Skip to content

xnacly/regexcc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

regexcc

Compile regular expressions to optimised C code

Usage

Executing

regexcc -name abc 'ab*c'

Results in abc.h and abc.c being generated:

#ifndef REGEXCC_ABC_H
#define REGEXCC_ABC_H
/* 
Generated by github.com/xnacly/regecc 
If a bug occurs, please do open a new issue or send an email to contact@xnacly.me 
*/

#include <stdbool.h>
#include <stddef.h>

// Returns true if `s` matches `ab*c`
bool abc_match(const char *s, size_t len);
#endif
#include "abc.h"
// Returns true if `s` matches `ab*c`
bool abc_match(const char *s, size_t len) {
  size_t i = 0;
  goto S0;
S0: {
  if (i >= len) { return false; }
  if (i < len && s[i] == 'a') {
    i++;
    goto S1;
  }
  return false;
}
S1: {
  if (i >= len) { return false; }
  if (i < len && s[i] == 'b') {
    i++;
    goto S1;
  }
  goto S2;
  return false;
}
S2: {
  if (i >= len) { return true; }
  if (i < len && s[i] == 'c') {
    i++;
    goto S3;
  }
  return false;
}
S3: {
  if (i >= len) { return true; }
  return false;
}
}

In C

These can be included as any other header source pair:

#include "abc.h"
#include <stdio.h>
#include <string.h>

int main(void) {
  const char *samples[] = {
      "abc",
      "abbbc",
      "ab",
  };

  for (size_t i = 0; i < sizeof(samples) / sizeof(samples[0]); i++) {
    const char *s = samples[i];
    bool ok = abc_match(s, strlen(s));
    printf("%s => %s\n", s, ok ? "matches" : "doesnt match");
  }
}

Compiling this with abc.c

$ gcc -O3 main.c abc.c -o abc; ./abc
abc => matches
abbbc => matches
ab => doesnt match

About

Compile regular expressions to optimised C code

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages