1
1
// Copyright (C) 2020 Quentin M. Kniep <[email protected] >
2
2
// Distributed under terms of the MIT license.
3
3
4
- enum ESVersion {
4
+ use std:: collections:: HashSet ;
5
+
6
+ #[ derive( Debug , Ord , PartialOrd , Eq , PartialEq ) ]
7
+ enum EcmaVersion {
5
8
ES5 ,
6
9
ES2015 ,
7
10
ES2016 ,
@@ -12,18 +15,56 @@ enum ESVersion {
12
15
ES2021 ,
13
16
}
14
17
15
- pub struct ESRegexValidator {
16
- es_version : ESVersion ,
18
+ #[ derive( Debug ) ]
19
+ pub struct EcmaRegexValidator {
20
+ ecma_version : EcmaVersion ,
17
21
}
18
22
19
- impl ESRegexValidator {
20
- fn new ( es_version : ESVersion ) -> Self {
21
- ESRegexValidator {
22
- es_version
23
+ impl EcmaRegexValidator {
24
+ fn new ( ecma_version : EcmaVersion ) -> Self {
25
+ EcmaRegexValidator {
26
+ ecma_version
23
27
}
24
28
}
25
29
26
- fn validate ( & self , regex : & str ) {
30
+ fn validatePattern ( & self , pattern : & str , ) -> bool {
31
+ return true ;
32
+ }
33
+
34
+ ///
35
+ fn validateFlags ( & self , flags : & str ) -> bool {
36
+ let mut existing_flags = HashSet :: < char > :: new ( ) ;
37
+ let mut global = false ;
38
+ let mut ignore_case = false ;
39
+ let mut multiline = false ;
40
+ let mut sticky = false ;
41
+ let mut unicode = false ;
42
+ let mut dot_all = false ;
43
+
44
+ for flag in flags. chars ( ) {
45
+ if existing_flags. contains ( & flag) {
46
+ return false ; // duplicate flag
47
+ }
48
+ existing_flags. insert ( flag) ;
49
+
50
+ if flag == 'g' {
51
+ global = true ;
52
+ } else if flag == 'i' {
53
+ ignore_case = true ;
54
+ } else if flag == 'm' {
55
+ multiline = true ;
56
+ } else if flag == 'u' && self . ecma_version >= EcmaVersion :: ES2015 {
57
+ unicode = true ;
58
+ } else if flag == 'y' && self . ecma_version >= EcmaVersion :: ES2015 {
59
+ sticky = true ;
60
+ } else if flag == 's' && self . ecma_version >= EcmaVersion :: ES2018 {
61
+ dot_all = true ;
62
+ } else {
63
+ return false ; // invalid flag
64
+ }
65
+ }
66
+
67
+ return true ;
27
68
}
28
69
}
29
70
@@ -33,6 +74,10 @@ mod tests {
33
74
use super :: * ;
34
75
35
76
#[ test]
36
- fn it_works ( ) {
77
+ fn validate_flags ( ) {
78
+ let validator = EcmaRegexValidator :: new ( EcmaVersion :: ES2018 ) ;
79
+ assert_eq ! ( validator. validateFlags( "gimuys" ) , true ) ;
80
+ assert_eq ! ( validator. validateFlags( "gimgu" ) , false ) ;
81
+ assert_eq ! ( validator. validateFlags( "gimuf" ) , false ) ;
37
82
}
38
83
}
0 commit comments