1
+ use once_cell:: sync:: Lazy ;
1
2
use regex:: Regex ;
2
3
3
4
#[ derive( Debug , PartialEq ) ]
@@ -47,11 +48,12 @@ pub struct ParserError {
47
48
pub message : String ,
48
49
}
49
50
51
+ static PAT_COMMAND : Lazy < Regex > =
52
+ Lazy :: new ( || Regex :: new ( r"^\{(?:(\d+):)?(|\{|[A-Z]+[A-Z0-9_]*)(?:\.(\w+))?\}$" ) . unwrap ( ) ) ;
53
+
50
54
impl StringCommand {
51
55
fn parse ( string : & str ) -> Option < StringCommand > {
52
- let pat_command =
53
- Regex :: new ( r"^\{(?:(\d+):)?(|\{|[A-Z]+[A-Z0-9_]*)(?:\.(\w+))?\}$" ) . unwrap ( ) ;
54
- let caps = pat_command. captures ( string) ?;
56
+ let caps = PAT_COMMAND . captures ( string) ?;
55
57
Some ( StringCommand {
56
58
index : caps. get ( 1 ) . and_then ( |v| v. as_str ( ) . parse ( ) . ok ( ) ) ,
57
59
name : String :: from ( & caps[ 2 ] ) ,
@@ -73,10 +75,11 @@ impl StringCommand {
73
75
}
74
76
}
75
77
78
+ static PAT_GENDER : Lazy < Regex > = Lazy :: new ( || Regex :: new ( r"^\{G\s*=\s*(\w+)\}$" ) . unwrap ( ) ) ;
79
+
76
80
impl GenderDefinition {
77
81
fn parse ( string : & str ) -> Option < GenderDefinition > {
78
- let pat_gender = Regex :: new ( r"^\{G\s*=\s*(\w+)\}$" ) . unwrap ( ) ;
79
- let caps = pat_gender. captures ( string) ?;
82
+ let caps = PAT_GENDER . captures ( string) ?;
80
83
Some ( GenderDefinition {
81
84
gender : String :: from ( & caps[ 1 ] ) ,
82
85
} )
@@ -87,12 +90,14 @@ impl GenderDefinition {
87
90
}
88
91
}
89
92
93
+ static PAT_CHOICE : Lazy < Regex > =
94
+ Lazy :: new ( || Regex :: new ( r"^\{([PG])(?:\s+(\d+)(?::(\d+))?)?(\s+[^\s0-9].*?)\s*\}$" ) . unwrap ( ) ) ;
95
+ static PAT_ITEM : Lazy < Regex > =
96
+ Lazy :: new ( || Regex :: new ( r##"^\s+(?:([^\s"]+)|"([^"]*)")"## ) . unwrap ( ) ) ;
97
+
90
98
impl ChoiceList {
91
99
fn parse ( string : & str ) -> Option < ChoiceList > {
92
- let pat_choice =
93
- Regex :: new ( r"^\{([PG])(?:\s+(\d+)(?::(\d+))?)?(\s+[^\s0-9].*?)\s*\}$" ) . unwrap ( ) ;
94
- let pat_item = Regex :: new ( r##"^\s+(?:([^\s"]+)|"([^"]*)")"## ) . unwrap ( ) ;
95
- let caps = pat_choice. captures ( string) ?;
100
+ let caps = PAT_CHOICE . captures ( string) ?;
96
101
let mut result = ChoiceList {
97
102
name : String :: from ( & caps[ 1 ] ) ,
98
103
indexref : caps. get ( 2 ) . and_then ( |v| v. as_str ( ) . parse ( ) . ok ( ) ) ,
@@ -101,7 +106,7 @@ impl ChoiceList {
101
106
} ;
102
107
let mut rest = & caps[ 4 ] ;
103
108
while !rest. is_empty ( ) {
104
- let m = pat_item . captures ( rest) ?;
109
+ let m = PAT_ITEM . captures ( rest) ?;
105
110
result
106
111
. choices
107
112
. push ( String :: from ( m. get ( 1 ) . or ( m. get ( 2 ) ) . unwrap ( ) . as_str ( ) ) ) ;
0 commit comments