-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathwildcard.go
More file actions
54 lines (45 loc) · 1.37 KB
/
wildcard.go
File metadata and controls
54 lines (45 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* Copyright (c) 2023 Iglou.eu <contact@iglou.eu>
* Copyright (c) 2023 Adrien Kara <adrien@iglou.eu>
*
* Licensed under the BSD 3-Clause License,
* see LICENSE.md for more details.
*/
//go:generate go run cmd/build/build.go
package wildcard
import "bytes"
// Match returns true if the pattern matches the string s.
// It uses byte comparison rather than rune or grapheme cluster comparison.
// For matching complex Unicode, only the "*" wildcard or exact equality is supported.
func Match(pattern, s string) bool {
if pattern == "" {
return s == pattern
}
if pattern == "*" || s == pattern {
return true
}
return matchByString(pattern, s)
}
// MatchByRune returns true if the pattern matches the string s.
// It supports complex Unicode matching with wildcards such as "*", "?", and ".".
// Note that it incurs allocation and more CPU usage.
func MatchByRune(pattern, s string) bool {
if pattern == "" {
return s == pattern
}
if pattern == "*" || s == pattern {
return true
}
return matchByRunes([]rune(pattern), []rune(s))
}
// MatchFromByte returns true if the pattern matches the byte slice s.
// Similar to Match but operates on byte slices to avoid conversions/alloc.
func MatchFromByte(pattern, s []byte) bool {
if len(pattern) == 0 {
return len(s) == 0
}
if pattern[0] == '*' || bytes.Equal(pattern, s) {
return true
}
return matchByByte(pattern, s)
}