A pure Go implementation of Lua pattern matching
Go
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
luapatterns
.gitignore
LICENSE.txt
README.md

README.md

A pure Go implementation of Lua pattern matching

Introduction

The package implements a subset of Lua patterns in Go. It is not an idiomatic port, but rather uses as close to a line-by-line translation of the original C source. This means this package incurs quite a bit of overhead by implementing a string pointer type. I have still found the speed to be rather acceptable for most pattern matching needs.

Currently the following function equivalents are implemented:

  • string.match as Match/MatchBytes
  • string.gmatch as Gmatch/GmatchBytes
  • string.find as Find/FindBytes
  • string.gsub as Replace/ReplaceBytes

Installing

You can install the package using go install

go install github.com/jnwhiteh/go-luapatterns/pattern

Updating

If you would like to update to the latest version of luapatterns, simply use the goinstall flag to accomplish this:

go install github.com/jnwhiteh/go-luapatterns/luapatterns

Using

Once you have installed the package using goinstall, you can use the package in the following manner:

    package main

    import "fmt"
    import pattern "github.com/jnwhiteh/go-luapatterns/luapatterns"
    
    func main() {
    	str := "aaaaab"
    	pat := "(.-)(b)"
    	succ, caps := pattern.Match(str, pat)
    
    	fmt.Printf("Match('%s', '%s') => %t\n", str, pat, succ)
    	for idx, capture := range caps {
    		fmt.Printf("capture[%d] = '%s'\n", idx, capture)
    	}
    }

Reference

The documentation for Lua patterns can be found on the Lua Reference Manual - Section 5.4.1.

Differences

  • This implementation drops the use of the 'init' argument to the find function, since if you would like to start finding a pattern at a point in the string, you can take a sub-slice in order to do this.
  • The indices returns from Find will not match the returns from the equivalent Lua program due to differences in array indexing (start will be -1) and slices. In order to get the substring of a pattern match, you can take str[startIndex:endIndex].
  • The Replace function is the string-only version of gsub. There is currently no function/table lookup equivalent.
  • The Gmatch function is not a translation of the Lua version, it is a simple naive implementation based on index tracking and Find.

Known Issues

  • Position captures are not currently implemented, as I am unsure how to return those values to the caller.
  • The frontier pattern %f is not working properly

Resources