Skip to content

Commit 4010bcd

Browse files
authored
[skip-changelog] Refactor string match util (arduino#1415)
* Simplified utils.Match function * match func doesn't need err anymore * Removed leftover * Removed unused mutex * Factored out all duplicated match helpers * fix i18n
1 parent 804b8d0 commit 4010bcd

File tree

8 files changed

+52
-132
lines changed

8 files changed

+52
-132
lines changed

arduino/utils/search.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,35 @@ func removeDiatrics(s string) (string, error) {
4747
// Both str and substrings are transforms to lower case and have their
4848
// accents and other unicode diatrics removed.
4949
// If strings transformation fails an error is returned.
50-
func Match(str string, substrings []string) (bool, error) {
51-
str, err := removeDiatrics(strings.ToLower(str))
52-
if err != nil {
53-
return false, err
50+
func Match(str string, substrings []string) bool {
51+
clean := func(s string) string {
52+
s = strings.ToLower(s)
53+
if s2, err := removeDiatrics(s); err == nil {
54+
return s2
55+
}
56+
return s
5457
}
5558

59+
str = clean(str)
5660
for _, sub := range substrings {
57-
cleanSub, err := removeDiatrics(strings.ToLower(sub))
58-
if err != nil {
59-
return false, err
61+
if !strings.Contains(str, clean(sub)) {
62+
return false
6063
}
61-
if !strings.Contains(str, cleanSub) {
62-
return false, nil
64+
}
65+
return true
66+
}
67+
68+
// MatchAny checks if query matches at least one of the
69+
// string in arrayToMatch using the utils.Match function.
70+
func MatchAny(query string, arrayToMatch []string) bool {
71+
queryArgs := strings.Split(strings.TrimSpace(query), " ")
72+
if len(queryArgs) == 0 {
73+
return true
74+
}
75+
for _, t := range arrayToMatch {
76+
if Match(t, queryArgs) {
77+
return true
6378
}
6479
}
65-
return true, nil
80+
return false
6681
}

commands/board/list.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"regexp"
2424
"sort"
2525
"strings"
26-
"sync"
2726
"time"
2827

2928
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
@@ -39,7 +38,6 @@ import (
3938
var (
4039
// ErrNotFound is returned when the API returns 404
4140
ErrNotFound = errors.New(tr("board not found"))
42-
m sync.Mutex
4341
vidPidURL = "https://builder.arduino.cc/v3/boards/byVidPid"
4442
validVidPid = regexp.MustCompile(`0[xX][a-fA-F\d]{4}`)
4543
)

commands/board/listall.go

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,14 @@ import (
2525
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2626
)
2727

28-
// maximumSearchDistance is the maximum Levenshtein distance accepted when using fuzzy search.
29-
// This value is completely arbitrary and picked randomly.
30-
const maximumSearchDistance = 20
31-
3228
// ListAll FIXMEDOC
3329
func ListAll(ctx context.Context, req *rpc.BoardListAllRequest) (*rpc.BoardListAllResponse, error) {
3430
pm := commands.GetPackageManager(req.GetInstance().GetId())
3531
if pm == nil {
3632
return nil, errors.New(tr("invalid instance"))
3733
}
3834

39-
searchArgs := []string{}
40-
for _, s := range req.SearchArgs {
41-
searchArgs = append(searchArgs, strings.Trim(s, " "))
42-
}
43-
44-
match := func(toTest []string) (bool, error) {
45-
if len(searchArgs) == 0 {
46-
return true, nil
47-
}
48-
49-
for _, t := range toTest {
50-
matches, err := utils.Match(t, searchArgs)
51-
if err != nil {
52-
return false, err
53-
}
54-
if matches {
55-
return matches, nil
56-
}
57-
}
58-
return false, nil
59-
}
35+
searchArgs := strings.Join(req.GetSearchArgs(), " ")
6036

6137
list := &rpc.BoardListAllResponse{Boards: []*rpc.BoardListItem{}}
6238
for _, targetPackage := range pm.Packages {
@@ -100,9 +76,7 @@ func ListAll(ctx context.Context, req *rpc.BoardListAllRequest) (*rpc.BoardListA
10076

10177
toTest := append(toTest, board.Name())
10278
toTest = append(toTest, board.FQBN())
103-
if ok, err := match(toTest); err != nil {
104-
return nil, err
105-
} else if !ok {
79+
if !utils.MatchAny(searchArgs, toTest) {
10680
continue
10781
}
10882

commands/board/search.go

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,6 @@ func Search(ctx context.Context, req *rpc.BoardSearchRequest) (*rpc.BoardSearchR
3636
return nil, errors.New(tr("invalid instance"))
3737
}
3838

39-
searchArgs := strings.Split(strings.Trim(req.SearchArgs, " "), " ")
40-
41-
match := func(toTest []string) (bool, error) {
42-
if len(searchArgs) == 0 {
43-
return true, nil
44-
}
45-
46-
for _, t := range toTest {
47-
matches, err := utils.Match(t, searchArgs)
48-
if err != nil {
49-
return false, err
50-
}
51-
if matches {
52-
return matches, nil
53-
}
54-
}
55-
return false, nil
56-
}
57-
5839
res := &rpc.BoardSearchResponse{Boards: []*rpc.BoardListItem{}}
5940
for _, targetPackage := range pm.Packages {
6041
for _, platform := range targetPackage.Platforms {
@@ -93,9 +74,7 @@ func Search(ctx context.Context, req *rpc.BoardSearchRequest) (*rpc.BoardSearchR
9374
}
9475

9576
toTest := append(strings.Split(board.Name(), " "), board.Name(), board.FQBN())
96-
if ok, err := match(toTest); err != nil {
97-
return nil, err
98-
} else if !ok {
77+
if !utils.MatchAny(req.GetSearchArgs(), toTest) {
9978
continue
10079
}
10180

@@ -109,9 +88,7 @@ func Search(ctx context.Context, req *rpc.BoardSearchRequest) (*rpc.BoardSearchR
10988
} else if latestPlatformRelease != nil {
11089
for _, board := range latestPlatformRelease.BoardsManifest {
11190
toTest := append(strings.Split(board.Name, " "), board.Name)
112-
if ok, err := match(toTest); err != nil {
113-
return nil, err
114-
} else if !ok {
91+
if !utils.MatchAny(req.GetSearchArgs(), toTest) {
11592
continue
11693
}
11794

commands/core/search.go

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const maximumSearchDistance = 20
3333

3434
// PlatformSearch FIXMEDOC
3535
func PlatformSearch(req *rpc.PlatformSearchRequest) (*rpc.PlatformSearchResponse, error) {
36-
searchArgs := strings.Trim(req.SearchArgs, " ")
36+
searchArgs := strings.TrimSpace(req.SearchArgs)
3737
allVersions := req.AllVersions
3838
pm := commands.GetPackageManager(req.Instance.Id)
3939
if pm == nil {
@@ -46,25 +46,6 @@ func PlatformSearch(req *rpc.PlatformSearchRequest) (*rpc.PlatformSearchResponse
4646
res = pm.FindPlatformReleaseProvidingBoardsWithVidPid(vid, pid)
4747
} else {
4848

49-
searchArgs := strings.Split(searchArgs, " ")
50-
51-
match := func(toTest []string) (bool, error) {
52-
if len(searchArgs) == 0 {
53-
return true, nil
54-
}
55-
56-
for _, t := range toTest {
57-
matches, err := utils.Match(t, searchArgs)
58-
if err != nil {
59-
return false, err
60-
}
61-
if matches {
62-
return matches, nil
63-
}
64-
}
65-
return false, nil
66-
}
67-
6849
for _, targetPackage := range pm.Packages {
6950
for _, platform := range targetPackage.Platforms {
7051
// discard invalid platforms
@@ -95,9 +76,7 @@ func PlatformSearch(req *rpc.PlatformSearchRequest) (*rpc.PlatformSearchResponse
9576
}
9677

9778
// Search
98-
if ok, err := match(toTest); err != nil {
99-
return nil, err
100-
} else if !ok {
79+
if !utils.MatchAny(searchArgs, toTest) {
10180
continue
10281
}
10382

commands/lib/search.go

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package lib
1818
import (
1919
"context"
2020
"errors"
21-
"strings"
2221

2322
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
2423
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
@@ -39,34 +38,12 @@ func LibrarySearch(ctx context.Context, req *rpc.LibrarySearchRequest) (*rpc.Lib
3938
}
4039

4140
func searchLibrary(req *rpc.LibrarySearchRequest, lm *librariesmanager.LibrariesManager) (*rpc.LibrarySearchResponse, error) {
42-
query := req.GetQuery()
4341
res := []*rpc.SearchedLibrary{}
4442
status := rpc.LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS
4543

46-
searchArgs := strings.Split(strings.Trim(query, " "), " ")
47-
48-
match := func(toTest []string) (bool, error) {
49-
if len(searchArgs) == 0 {
50-
return true, nil
51-
}
52-
53-
for _, t := range toTest {
54-
matches, err := utils.Match(t, searchArgs)
55-
if err != nil {
56-
return false, err
57-
}
58-
if matches {
59-
return matches, nil
60-
}
61-
}
62-
return false, nil
63-
}
64-
6544
for _, lib := range lm.Index.Libraries {
6645
toTest := []string{lib.Name, lib.Latest.Paragraph, lib.Latest.Sentence}
67-
if ok, err := match(toTest); err != nil {
68-
return nil, err
69-
} else if !ok {
46+
if !utils.MatchAny(req.GetQuery(), toTest) {
7047
continue
7148
}
7249
res = append(res, indexLibraryToRPCSearchLibrary(lib))

i18n/data/en.po

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,15 +1035,15 @@ msgstr "Invalid output format: %s"
10351035
msgid "Invalid parameter %s: version not allowed"
10361036
msgstr "Invalid parameter %s: version not allowed"
10371037

1038-
#: commands/board/list.go:53
1038+
#: commands/board/list.go:51
10391039
msgid "Invalid pid value: '%s'"
10401040
msgstr "Invalid pid value: '%s'"
10411041

10421042
#: legacy/builder/phases/sizer.go:150
10431043
msgid "Invalid size regexp: %s"
10441044
msgstr "Invalid size regexp: %s"
10451045

1046-
#: commands/board/list.go:50
1046+
#: commands/board/list.go:48
10471047
msgid "Invalid vid value: '%s'"
10481048
msgstr "Invalid vid value: '%s'"
10491049

@@ -2093,7 +2093,7 @@ msgstr "binary file not found in %s"
20932093
msgid "board %s:%s not found"
20942094
msgstr "board %s:%s not found"
20952095

2096-
#: commands/board/list.go:41
2096+
#: commands/board/list.go:40
20972097
msgid "board not found"
20982098
msgstr "board not found"
20992099

@@ -2358,7 +2358,7 @@ msgstr "downloading tool %[1]s: %[2]s"
23582358
msgid "encoding sketch metadata: %s"
23592359
msgstr "encoding sketch metadata: %s"
23602360

2361-
#: commands/board/list.go:142
2361+
#: commands/board/list.go:140
23622362
msgid "error getting board info from Arduino Cloud"
23632363
msgstr "error getting board info from Arduino Cloud"
23642364

@@ -2378,11 +2378,11 @@ msgstr "error parsing FQBN"
23782378
msgid "error parsing value: %v"
23792379
msgstr "error parsing value: %v"
23802380

2381-
#: commands/board/list.go:83
2381+
#: commands/board/list.go:81
23822382
msgid "error processing response from server"
23832383
msgstr "error processing response from server"
23842384

2385-
#: commands/board/list.go:98
2385+
#: commands/board/list.go:96
23862386
msgid "error querying Arduino Cloud Api"
23872387
msgstr "error querying Arduino Cloud Api"
23882388

@@ -2411,7 +2411,7 @@ msgstr "extracting archive: %w"
24112411
msgid "failed to compute hash of file \"%s\""
24122412
msgstr "failed to compute hash of file \"%s\""
24132413

2414-
#: commands/board/list.go:66
2414+
#: commands/board/list.go:64
24152415
msgid "failed to initialize http client"
24162416
msgstr "failed to initialize http client"
24172417

@@ -2626,8 +2626,8 @@ msgstr "invalid hash '%[1]s': %[2]s"
26262626

26272627
#: commands/board/attach.go:42
26282628
#: commands/board/details.go:33
2629-
#: commands/board/list.go:187
2630-
#: commands/board/listall.go:36
2629+
#: commands/board/list.go:185
2630+
#: commands/board/listall.go:32
26312631
#: commands/board/search.go:36
26322632
#: commands/compile/compile.go:93
26332633
#: commands/core/download.go:36
@@ -2640,7 +2640,7 @@ msgstr "invalid hash '%[1]s': %[2]s"
26402640
#: commands/instances.go:589
26412641
#: commands/lib/list.go:41
26422642
#: commands/lib/list.go:46
2643-
#: commands/lib/search.go:35
2643+
#: commands/lib/search.go:34
26442644
#: commands/upload/upload.go:51
26452645
msgid "invalid instance"
26462646
msgstr "invalid instance"
@@ -3203,7 +3203,7 @@ msgstr "start syncing discovery %[1]s: %[2]w"
32033203
msgid "starting discovery %[1]s: %[2]w"
32043204
msgstr "starting discovery %[1]s: %[2]w"
32053205

3206-
#: commands/board/list.go:279
3206+
#: commands/board/list.go:277
32073207
msgid "stopping discoveries: %s"
32083208
msgstr "stopping discoveries: %s"
32093209

@@ -3235,7 +3235,7 @@ msgstr "text section exceeds available space in board"
32353235
msgid "the library name is different from the set (%[1]s != %[2]s)"
32363236
msgstr "the library name is different from the set (%[1]s != %[2]s)"
32373237

3238-
#: commands/board/list.go:74
3238+
#: commands/board/list.go:72
32393239
msgid "the server responded with status %s"
32403240
msgstr "the server responded with status %s"
32413241

@@ -3397,7 +3397,7 @@ msgstr "writing library_index.json.sig"
33973397
msgid "writing sketch metadata %[1]s: %[2]s"
33983398
msgstr "writing sketch metadata %[1]s: %[2]s"
33993399

3400-
#: commands/board/list.go:90
3400+
#: commands/board/list.go:88
34013401
msgid "wrong format in server response"
34023402
msgstr "wrong format in server response"
34033403

i18n/rice-box.go

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy