-
Notifications
You must be signed in to change notification settings - Fork 2
/
regex.go
70 lines (60 loc) · 1.29 KB
/
regex.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package bson
import (
"bytes"
"errors"
"fmt"
"regexp"
)
// Regex represents BSON regular expression.
type Regex struct {
Pattern string
Options string
}
// String returns a hex string representation of the id.
func (re Regex) String() string {
return fmt.Sprintf(`Regex('%s', '%s')`, re.Pattern, re.Options)
}
// MarshalBSON implements [bson.Marshaler].
func (re *Regex) MarshalBSON() ([]byte, error) {
b := make([]byte, 0, len(re.Pattern)+1+len(re.Options)+1)
b = append(b, re.Pattern...)
b = append(b, 0)
b = append(b, re.Options...)
b = append(b, 0)
return b, nil
}
// UnmarshalBSON implements [bson.Unmarshaler].
func (re *Regex) UnmarshalBSON(b []byte) error {
idx := bytes.IndexByte(b, 0)
if idx == -1 {
return errors.New("malformed regex")
}
re.Pattern = string(b[0:idx])
b = b[idx+1:]
idx = bytes.IndexByte(b, 0)
if idx == -1 {
return errors.New("malformed regex")
}
re.Options = string(b[:idx])
return nil
}
// Compile returns [regexp.Regexp].
func (r Regex) Compile() (*regexp.Regexp, error) {
var opts string
for _, o := range r.Options {
switch o {
case 'i', 'm', 's':
opts += string(o)
default:
}
}
expr := r.Pattern
if opts != "" {
expr = "(?" + opts + ")" + expr
}
re, err := regexp.Compile(expr)
if err != nil {
return nil, err
}
return re, nil
}