@@ -64,6 +64,19 @@ macro_rules! impl_new {
64
64
} ;
65
65
}
66
66
67
+ macro_rules! impl_process_token {
68
+ ( $elem_ty: ty, $inner_elem_ty: ty, $numbers_fun: expr, $letters_fun: expr) => {
69
+ fn process_token( input: $elem_ty) -> ( $inner_elem_ty, $elem_ty) {
70
+ if let Some ( pos_end) = ( * NUMBERS ) . find( & input) . map( |m| m. end( ) ) {
71
+ $numbers_fun( input, pos_end)
72
+ } else {
73
+ let pos_end = ( * LETTERS ) . find( & input) . unwrap( ) . end( ) ;
74
+ $letters_fun( input, pos_end)
75
+ }
76
+ }
77
+ } ;
78
+ }
79
+
67
80
macro_rules! impl_partial_ord {
68
81
( $elem_ident: ident, $struct: ident) => {
69
82
#[ inline]
@@ -114,18 +127,15 @@ pub struct HumanStr<'a> {
114
127
115
128
impl < ' a > HumanStr < ' a > {
116
129
impl_new ! ( & ' a str , HumanStr , HumanStr <' a>) ;
117
-
118
- fn process_token ( input : & ' a str ) -> ( StrElem < ' a > , & str ) {
119
- if let Some ( pos_end) = ( * NUMBERS ) . find ( input) . map ( |m| m. end ( ) ) {
120
- (
121
- StrElem :: Number ( BigInt :: from_str ( & input[ ..pos_end] ) . unwrap ( ) ) ,
122
- & input[ pos_end..] ,
123
- )
124
- } else {
125
- let pos_end = ( * LETTERS ) . find ( input) . unwrap ( ) . end ( ) ;
126
- ( StrElem :: Letters ( & input[ ..pos_end] ) , & input[ pos_end..] )
127
- }
128
- }
130
+ impl_process_token ! (
131
+ & ' a str ,
132
+ StrElem <' a>,
133
+ |input: & ' a str , pos_end| (
134
+ StrElem :: Number ( BigInt :: from_str( & input[ ..pos_end] ) . unwrap( ) ) ,
135
+ & input[ pos_end..] ,
136
+ ) ,
137
+ |input: & ' a str , pos_end| ( StrElem :: Letters ( & input[ ..pos_end] ) , & input[ pos_end..] )
138
+ ) ;
129
139
}
130
140
131
141
/// A utility function for sorting a list of strings using human sorting.
@@ -167,23 +177,19 @@ pub struct HumanString {
167
177
168
178
impl HumanString {
169
179
impl_new ! ( String , HumanString , HumanString ) ;
170
-
171
- fn process_token ( mut input : String ) -> ( StringElem , String ) {
172
- lazy_static ! {
173
- static ref NUMBERS : Regex = Regex :: new( "^[0-9]+" ) . unwrap( ) ;
174
- static ref LETTERS : Regex = Regex :: new( "^[^0-9]+" ) . unwrap( ) ;
175
- }
176
-
177
- if let Some ( pos_end) = ( * NUMBERS ) . find ( & input) . map ( |m| m. end ( ) ) {
180
+ impl_process_token ! (
181
+ String ,
182
+ StringElem ,
183
+ |mut input: String , pos_end| {
178
184
let number = StringElem :: Number ( BigInt :: from_str( & input[ ..pos_end] ) . unwrap( ) ) ;
179
185
input. drain( ..pos_end) . count( ) ;
180
186
( number, input)
181
- } else {
182
- let pos_end = ( * LETTERS ) . find ( & input ) . unwrap ( ) . end ( ) ;
187
+ } ,
188
+ | mut input : String , pos_end| {
183
189
let rest = input. split_off( pos_end - 1 ) ;
184
190
( StringElem :: Letters ( input) , rest)
185
191
}
186
- }
192
+ ) ;
187
193
}
188
194
189
195
impl PartialOrd for HumanString {
0 commit comments