Skip to content

Commit 3830500

Browse files
authored
Merge pull request #40 from koki-develop/multibyte-string
2 parents fb3c42b + 4d58e4a commit 3830500

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

β€Žmodel.goβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func (m *model) itemView(match Match, cursorLine bool) string {
239239
}
240240

241241
// write item
242-
for ci, c := range match.Str {
242+
for ci, c := range []rune(match.Str) {
243243
// matches
244244
if intContains(match.MatchedIndexes, ci) {
245245
if cursorLine {

β€Žsearch.goβ€Ž

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"sort"
55
"strings"
66
"sync"
7+
"unicode/utf8"
78
)
89

910
var (
@@ -143,19 +144,20 @@ func fuzzySearch(str, search string, option searchOption) (Match, bool) {
143144
}
144145

145146
// Create a slice to store the matched indexes.
146-
matchedIndexes := make([]int, 0, len(search))
147+
matchedIndexes := make([]int, 0, utf8.RuneCountInString(search))
147148
j := 0
148149

149150
// Check for matching between the item's characters and the search string.
150-
for i, r := range item {
151-
if j < len(search) && r == rune(search[j]) {
151+
searchRunes := []rune(search)
152+
for i, r := range []rune(item) {
153+
if j < len(searchRunes) && r == searchRunes[j] {
152154
matchedIndexes = append(matchedIndexes, i)
153155
j++
154156
}
155157
}
156158

157159
// Returns Match if all characters in the search string match.
158-
if j == len(search) {
160+
if j == len(searchRunes) {
159161
return Match{Str: str, MatchedIndexes: matchedIndexes}, true
160162
} else {
161163
return Match{}, false

β€Žsearch_test.goβ€Ž

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ func Test_fuzzySearch(t *testing.T) {
4242
{str: "abc", search: "d"},
4343
{str: "abc", search: "abcd"},
4444

45+
{str: "こんにけは", search: "", matchedIndexes: []int{}},
46+
{str: "こんにけは", search: "こ", matchedIndexes: []int{0}},
47+
{str: "こんにけは", search: "こん", matchedIndexes: []int{0, 1}},
48+
{str: "こんにけは", search: "こんに", matchedIndexes: []int{0, 1, 2}},
49+
{str: "こんにけは", search: "こんにけ", matchedIndexes: []int{0, 1, 2, 3}},
50+
{str: "こんにけは", search: "こんにけは", matchedIndexes: []int{0, 1, 2, 3, 4}},
51+
{str: "こんにけは", search: "γ‚“", matchedIndexes: []int{1}},
52+
{str: "こんにけは", search: "んに", matchedIndexes: []int{1, 2}},
53+
{str: "こんにけは", search: "んにけ", matchedIndexes: []int{1, 2, 3}},
54+
{str: "こんにけは", search: "んにけは", matchedIndexes: []int{1, 2, 3, 4}},
55+
{str: "こんにけは", search: "こには", matchedIndexes: []int{0, 2, 4}},
56+
4557
{str: "xaxbxc", search: "a", matchedIndexes: []int{1}},
4658
{str: "xaxbxc", search: "ab", matchedIndexes: []int{1, 3}},
4759
{str: "xaxbxc", search: "ac", matchedIndexes: []int{1, 5}},

0 commit comments

Comments
Β (0)