README.md

  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 are 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 `FindFrom` 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	for _, r := range results {
123		fmt.Println(emps[r.Index])
124	}
125}
126```
127
128Check out the [godoc](https://godoc.org/github.com/sahilm/fuzzy) for detailed documentation.
129
130## Installation
131
132`go get github.com/sahilm/fuzzy` or use your favorite dependency management tool.
133
134## Speed
135
136Here are a few benchmark results on a normal laptop.
137
138```
139BenchmarkFind/with_unreal_4_(~16K_files)-4         	     100	  12915315 ns/op
140BenchmarkFind/with_linux_kernel_(~60K_files)-4     	      50	  30885038 ns/op
141```
142
143Matching a pattern against ~60K files from the Linux kernel takes about 30ms.
144
145## Contributing
146
147Everyone is welcome to contribute. Please send me a pull request or file an issue. I promise
148to respond promptly.
149
150## Credits
151
152* [@ericpauley](https://github.com/ericpauley) & [@lunixbochs](https://github.com/lunixbochs) contributed Unicode awareness and various performance optimisations.
153
154* The algorithm is based of the awesome work of [forrestthewoods](https://github.com/forrestthewoods/lib_fts/blob/master/code/fts_fuzzy_match.js). 
155See [this](https://blog.forrestthewoods.com/reverse-engineering-sublime-text-s-fuzzy-match-4cffeed33fdb#.d05n81yjy)
156blog post for details of the algorithm.
157
158* The artwork is by my lovely wife Sanah. It's based on the Go Gopher.
159
160* The Go gopher was designed by Renee French (http://reneefrench.blogspot.com/). 
161The design is licensed under the Creative Commons 3.0 Attributions license.
162
163## License
164
165The MIT License (MIT)
166
167Copyright (c) 2017 Sahil Muthoo
168
169Permission is hereby granted, free of charge, to any person obtaining a copy
170of this software and associated documentation files (the "Software"), to deal
171in the Software without restriction, including without limitation the rights
172to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
173copies of the Software, and to permit persons to whom the Software is
174furnished to do so, subject to the following conditions:
175
176The above copyright notice and this permission notice shall be included in all
177copies or substantial portions of the Software.
178
179THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
180IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
181FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
182AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
183LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
184OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
185SOFTWARE.
186