@@ -18,8 +18,12 @@ import (
18
18
"github.com/dlclark/regexp2/syntax"
19
19
)
20
20
21
- // Default timeout used when running regexp matches -- "forever"
22
- var DefaultMatchTimeout = time .Duration (math .MaxInt64 )
21
+ var (
22
+ // DefaultMatchTimeout used when running regexp matches -- "forever"
23
+ DefaultMatchTimeout = time .Duration (math .MaxInt64 )
24
+ // DefaultUnmarshalOptions used when unmarshaling a regex from text
25
+ DefaultUnmarshalOptions = None
26
+ )
23
27
24
28
// Regexp is the representation of a compiled regular expression.
25
29
// A Regexp is safe for concurrent use by multiple goroutines.
@@ -43,7 +47,7 @@ type Regexp struct {
43
47
code * syntax.Code // compiled program
44
48
45
49
// cache of machines for running regexp
46
- muRun sync.Mutex
50
+ muRun * sync.Mutex
47
51
runner []* runner
48
52
}
49
53
@@ -72,6 +76,7 @@ func Compile(expr string, opt RegexOptions) (*Regexp, error) {
72
76
capsize : code .Capsize ,
73
77
code : code ,
74
78
MatchTimeout : DefaultMatchTimeout ,
79
+ muRun : & sync.Mutex {},
75
80
}, nil
76
81
}
77
82
@@ -371,3 +376,20 @@ func (re *Regexp) GroupNumberFromName(name string) int {
371
376
372
377
return - 1
373
378
}
379
+
380
+ // MarshalText implements [encoding.TextMarshaler]. The output
381
+ // matches that of calling the [Regexp.String] method.
382
+ func (re * Regexp ) MarshalText () ([]byte , error ) {
383
+ return []byte (re .String ()), nil
384
+ }
385
+
386
+ // UnmarshalText implements [encoding.TextUnmarshaler] by calling
387
+ // [Compile] on the encoded value.
388
+ func (re * Regexp ) UnmarshalText (text []byte ) error {
389
+ newRE , err := Compile (string (text ), DefaultUnmarshalOptions )
390
+ if err != nil {
391
+ return err
392
+ }
393
+ * re = * newRE
394
+ return nil
395
+ }
0 commit comments