jnwhiteh/go-luapatterns
{ "createdAt": "2010-06-23T16:49:35Z", "defaultBranch": "master", "description": "A pure Go implementation of Lua pattern matching", "fullName": "jnwhiteh/go-luapatterns", "homepage": "", "language": "Go", "name": "go-luapatterns", "pushedAt": "2013-10-30T16:45:48Z", "stargazersCount": 23, "topics": [], "updatedAt": "2024-01-18T04:01:26Z", "url": "https://github.com/jnwhiteh/go-luapatterns"}A pure Go implementation of Lua pattern matching
Section titled “A pure Go implementation of Lua pattern matching”Introduction
Section titled “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
Section titled “Installing”You can install the package using [go install][4]
go install github.com/jnwhiteh/go-luapatterns/patternUpdating
Section titled “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/luapatternsOnce 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
Section titled “Reference”The documentation for Lua patterns can be found on the [Lua Reference Manual - Section 5.4.1][3].
Differences
Section titled “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
Replacefunction is the string-only version of gsub. There is currently no function/table lookup equivalent. - The
Gmatchfunction is not a translation of the Lua version, it is a simple naive implementation based on index tracking andFind.
Known Issues
Section titled “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
Section titled “Resources”- [Lua 5.1 Reference Manual][2]
- [Kahlua String Library][1]
[1] !: http://github.com/krka/kahlua2/blob/master/core/src/se/krka/kahlua/stdlib/StringLib.java [2] !: http://www.lua.org/manual/5.1/manual.html [3] !: http://www.lua.org/manual/5.1/manual.html#5.4.1 [4] !: http://golang.org/cmd/goinstall/