https://github.com/spacewander/little-lua-glob
A little implementation of glob in Lua
https://github.com/spacewander/little-lua-glob
glob wildcard
Last synced: about 1 year ago
JSON representation
A little implementation of glob in Lua
- Host: GitHub
- URL: https://github.com/spacewander/little-lua-glob
- Owner: spacewander
- License: mit
- Created: 2020-05-14T11:41:28.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-11-12T03:15:50.000Z (over 5 years ago)
- Last Synced: 2025-05-19T00:38:32.555Z (about 1 year ago)
- Topics: glob, wildcard
- Language: Lua
- Size: 7.81 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# little-lua-glob
A little implementation of glob in Lua.
This library is ported from https://github.com/ryanuber/go-glob.
Unlike other implementations which support all kinds of wildcard, this library only supports '\*'.
However, supporting '\*' is enough for most of cases while keeping the implementation simple.
Compared with other implementations, this library has several advantages:
* simple: you can put the single Lua file in your project directly.
* safe: it doesn't use backtrack to match the (possibly evil) user input, see [security](#security) section for the details.
As for the performance, this library is almost as fast as a PCRE regex base implementation. See the ./benchmark.lua for the details.
## Usage
```lua
local glob = require "glob"
local pat = "a.*b.com"
local matcher = glob.compile(pat)
print(matcher:match("a.aliyun.b.com"))
```
## Security
Like regex, if the greedy pattern is (incorrecly) used in the glob implementation, an evil user input may cause terrible performance problem. For example:
```go
package main
import (
"fmt"
"strings"
"time"
"github.com/gobwas/glob"
)
func main() {
s := strings.Repeat("12345", 25)
pat := strings.Repeat("*5", 25)
start = time.Now()
var g glob.Glob
g = glob.MustCompile(pat)
rb := g.Match(s)
fmt.Printf("%+v %v\n", time.Now().Sub(start), rb)
}
```
In the `./benchmark.lua`, if I use `.*` instead of `.*?` in the regex base implementation, there will have the same problem.