File tree 3 files changed +33
-11
lines changed
3 files changed +33
-11
lines changed Original file line number Diff line number Diff line change
1
+ //go:build go1.21
2
+
3
+ package validator
4
+
5
+ import (
6
+ "regexp"
7
+ "sync"
8
+ )
9
+
10
+ func lazyRegexCompile (str string ) func () * regexp.Regexp {
11
+ return sync .OnceValue (func () * regexp.Regexp {
12
+ return regexp .MustCompile (str )
13
+ })
14
+ }
Original file line number Diff line number Diff line change
1
+ //go:build !go1.21
2
+
3
+ package validator
4
+
5
+ import (
6
+ "regexp"
7
+ "sync"
8
+ )
9
+
10
+ func lazyRegexCompile (str string ) func () * regexp.Regexp {
11
+ var regex * regexp.Regexp
12
+ var once sync.Once
13
+ return func () * regexp.Regexp {
14
+ once .Do (func () {
15
+ regex = regexp .MustCompile (str )
16
+ })
17
+ return regex
18
+ }
19
+ }
Original file line number Diff line number Diff line change 1
1
package validator
2
2
3
- import (
4
- "regexp"
5
- "sync"
6
- )
7
-
8
3
const (
9
4
alphaRegexString = "^[a-zA-Z]+$"
10
5
alphaNumericRegexString = "^[a-zA-Z0-9]+$"
@@ -79,12 +74,6 @@ const (
79
74
spicedbTypeRegexString = "^([a-z][a-z0-9_]{1,61}[a-z0-9]/)?[a-z][a-z0-9_]{1,62}[a-z0-9]$"
80
75
)
81
76
82
- func lazyRegexCompile (str string ) func () * regexp.Regexp {
83
- return sync .OnceValue (func () * regexp.Regexp {
84
- return regexp .MustCompile (str )
85
- })
86
- }
87
-
88
77
var (
89
78
alphaRegex = lazyRegexCompile (alphaRegexString )
90
79
alphaNumericRegex = lazyRegexCompile (alphaNumericRegexString )
You can’t perform that action at this time.
0 commit comments