Giter Club home page Giter Club logo

zig-regex's Introduction

An automaton-based regex implementation for zig.

Note: This is still a work in progress and many things still need to be done.

  • Capture group support
  • UTF-8 support
  • More tests (plus some automated tests/fuzzing)
  • Add a PikeVM implementation
  • Literal optimizations and just general performance improvements.

Usage

const debug = @import("std").debug;
const Regex = @import("regex").Regex;

test "example" {
    var re = try Regex.compile(debug.global_allocator, "\\w+");

    debug.assert(try re.match("hej") == true);
}

Api

Regex

fn compile(a: Allocator, re: []const u8) !Regex

Compiles a regex string, returning any errors during parsing/compiling.


pub fn match(re: *Regex, input: []const u8) !bool

Match a compiled regex against some input. The input must be matched in its entirety and from the first index.


pub fn partialMatch(re: *Regex, input: []const u8) !bool

Match a compiled regex against some input. Unlike match, this matches the leftmost and does not have to be anchored to the start of input.


pub fn captures(re: *Regex, input: []const u8) !?Captures

Match a compiled regex against some input. Returns a list of all matching slices in the regex with the first (0-index) being the entire regex.

If no match was found, null is returned.

Captures

pub fn sliceAt(captures: *const Captures, n: usize) ?[]const u8

Return the sub-slice for the numbered capture group. 0 refers to the entire match.

pub fn boundsAt(captures: *const Captures, n: usize) ?Span

Return the lower and upper byte positions for the specified capture group.

We can retrieve the sub-slice using this function:

const span = caps.boundsAt(0)
debug.assert(mem.eql(u8, caps.sliceAt(0), input[span.lower..span.upper]));

References

See the following useful sources:

zig-regex's People

Contributors

dgbuckley avatar drdeano avatar jjkh avatar joachimschmidt557 avatar kivikakk avatar lemonboy avatar rexicon226 avatar thechampagne avatar tiehuis 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.