1<img src="assets/search-gopher-1.png" alt="gopher looking for stuff"> <img src="assets/search-gopher-2.png" alt="gopher found stuff"> 2 3# fuzzy 4[![Build Status](https://travis-ci.org/sahilm/fuzzy.svg?branch=master)](https://travis-ci.org/sahilm/fuzzy) 5[![Documentation](https://godoc.org/github.com/sahilm/fuzzy?status.svg)](https://godoc.org/github.com/sahilm/fuzzy) 6 7Go library that provides fuzzy string matching optimized for filenames and code symbols in the style of Sublime Text, 8VSCode, IntelliJ IDEA et al. This library is external dependency-free. It only depends on the Go standard library. 9 10## Features 11 12- Intuitive matching. Results are returned in descending order of match quality. Quality is determined by: 13 - The first character in the pattern matches the first character in the match string. 14 - The matched character is camel cased. 15 - The matched character follows a separator such as an underscore character. 16 - The matched character is adjacent to a previous match. 17 18- Speed. Matches are returned in milliseconds. It's perfect for interactive search boxes. 19 20- The positions of matches is returned. Allows you to highlight matching characters. 21 22- Unicode aware. 23 24## Demo 25 26Here is a [demo](_example/main.go) of matching various patterns against ~16K files from the Unreal Engine 4 codebase. 27 28![demo](assets/demo.gif) 29 30You can run the demo yourself like so: 31 32``` 33cd _example/ 34go get github.com/jroimartin/gocui 35go run main.go 36``` 37 38## Usage 39 40The following example prints out matches with the matched chars in bold. 41 42```go 43package main 44 45import ( 46 "fmt" 47 48 "github.com/sahilm/fuzzy" 49) 50 51func main() { 52 const bold = "\033[1m%s\033[0m" 53 pattern := "mnr" 54 data := []string{"game.cpp", "moduleNameResolver.ts", "my name is_Ramsey"} 55 56 matches := fuzzy.Find(pattern, data) 57 58 for _, match := range matches { 59 for i := 0; i < len(match.Str); i++ { 60 if contains(i, match.MatchedIndexes) { 61 fmt.Print(fmt.Sprintf(bold, string(match.Str[i]))) 62 } else { 63 fmt.Print(string(match.Str[i])) 64 } 65 } 66 fmt.Println() 67 } 68} 69 70func contains(needle int, haystack []int) bool { 71 for _, i := range haystack { 72 if needle == i { 73 return true 74 } 75 } 76 return false 77} 78``` 79If the data you want to match isn't a slice of strings, you can use `FindFromSource` by implementing 80the provided `Source` interface. Here's an example: 81 82```go 83package main 84 85import ( 86 "fmt" 87 88 "github.com/sahilm/fuzzy" 89) 90 91type employee struct { 92 name string 93 age int 94} 95 96type employees []employee 97 98func (e employees) String(i int) string { 99 return e[i].name 100} 101 102func (e employees) Len() int { 103 return len(e) 104} 105 106func main() { 107 emps := employees{ 108 { 109 name: "Alice", 110 age: 45, 111 }, 112 { 113 name: "Bob", 114 age: 35, 115 }, 116 { 117 name: "Allie", 118 age: 35, 119 }, 120 } 121 results := fuzzy.FindFrom("al", emps) 122 fmt.Println(results) 123} 124``` 125 126Check out the [godoc](https://godoc.org/github.com/sahilm/fuzzy) for detailed documentation. 127 128## Installation 129 130`go get github.com/sahilm/fuzzy` or use your favorite dependency management tool. 131 132## Speed 133 134Here are a few benchmark results on a normal laptop. 135 136``` 137BenchmarkFind/with_unreal_4_(~16K_files)-4 100 12915315 ns/op 138BenchmarkFind/with_linux_kernel_(~60K_files)-4 50 30885038 ns/op 139``` 140 141Matching a pattern against ~60K files from the Linux kernel takes about 30ms. 142 143## Contributing 144 145Everyone is welcome to contribute. Please send me a pull request or file an issue. I promise 146to respond promptly. 147 148## Credits 149 150* [@ericpauley](https://github.com/ericpauley) & [@lunixbochs](https://github.com/lunixbochs) contributed Unicode awareness and various performance optimisations. 151 152* The algorithm is based of the awesome work of [forrestthewoods](https://github.com/forrestthewoods/lib_fts/blob/master/code/fts_fuzzy_match.js). 153See [this](https://blog.forrestthewoods.com/reverse-engineering-sublime-text-s-fuzzy-match-4cffeed33fdb#.d05n81yjy) 154blog post for details of the algorithm. 155 156* The artwork is by my lovely wife Sanah. It's based on the Go Gopher. 157 158* The Go gopher was designed by Renee French (http://reneefrench.blogspot.com/). 159The design is licensed under the Creative Commons 3.0 Attributions license. 160 161## License 162 163The MIT License (MIT) 164 165Copyright (c) 2017 Sahil Muthoo 166 167Permission is hereby granted, free of charge, to any person obtaining a copy 168of this software and associated documentation files (the "Software"), to deal 169in the Software without restriction, including without limitation the rights 170to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 171copies of the Software, and to permit persons to whom the Software is 172furnished to do so, subject to the following conditions: 173 174The above copyright notice and this permission notice shall be included in all 175copies or substantial portions of the Software. 176 177THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 178IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 179FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 180AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 181LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 182OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 183SOFTWARE. 184 185