Giter Site home page Giter Site logo

regexp2go's Introduction

regexp2go

regexp2go is an alternate backend for the regexp package that allows to perform ahead-of-time compilation of regular expressions to Go code.

This is similar in spirit to re2go, but aiming for compatibility with the Go regexp package.

⚠️ This is an experimental PoC that currently implements only a subset of the functionalities and optimizations present in the full regexp package. Rather importantly while it aims at offering the "run in time linear in the size of the input" guarantee that is offered by the regexp package, there may be cases in which this is not currently possible. It may also contain some correctness bugs since the test suite is not really extensive yet.

Using code generated by this package in production is not yet recommended.

Usage

For a quick demo you can use the online demo, or run it locally:

$ go get github.com/CAFxX/regexp2go

# Compile the provided regular expression using the default Go regexp 
# flags (212) and save the result in gen/main.go.
# This requires regexp2go to be on your PATH.
$ regexp2go '(?m)^INFO res=([0-9]+) msg=(.*)' > gen/main.go

# The generated file contains a main function that simply calls the
# generated `Match` function (the one that implements the regular expression)
# passing the input text provided as argument.
$ go run gen/main.go 'INFO res=42 msg=ok'
0: "INFO res=42 msg=ok"
1: "42"
2: "ok"

To generate a Match function that can be called from your program:

# Compile the provided regular expression using the default Go regexp 
# flags (212) and save the result in re/info_line.go.
# The pkg flag specifies the package name. The fn flag specifies the
# name of the generated function.
# This requires regexp2go to be on your PATH.
$ regexp2go -pkg re -fn MatchInfoLine '(?m)^INFO res=([0-9]+) msg=(.*)' > re/info_line.go

Examples

Examples of generated code are in examples/.

Each example contains the generated Go code, and the disassembly of the function that implements the regular expression.

For demo purposes you can also use the online demo to compile a custom regular expression.

Benchmarks

⚠️ These are preliminary results since not all features are implemented, and some edge cases are not handled yet. At the same time, there are many possible optimizations that haven't been implemented yet. See the TODOs in the code for details.

Benchmark results

regexp2go can be from slightly faster to over 5 times faster than the regexp package in the golang standard library, depending on the regexp and the input data.

regexp2go's People

Contributors

cafxx avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.