@@ -34,26 +34,37 @@ pub enum Param {
34
34
Number ( int )
35
35
}
36
36
37
+ /// Container for static and dynamic variable arrays
38
+ pub struct Variables {
39
+ /// Static variables A-Z
40
+ sta : [ Param , ..26 ] ,
41
+ /// Dynamic variables a-z
42
+ dyn : [ Param , ..26 ]
43
+ }
44
+
45
+ impl Variables {
46
+ /// Return a new zero-initialized Variables
47
+ pub fn new ( ) -> Variables {
48
+ Variables { sta : [ Number ( 0 ) , ..26 ] , dyn : [ Number ( 0 ) , ..26 ] }
49
+ }
50
+ }
51
+
37
52
/**
38
53
Expand a parameterized capability
39
54
40
55
# Arguments
41
56
* `cap` - string to expand
42
57
* `params` - vector of params for %p1 etc
43
- * `sta` - vector of params corresponding to static variables
44
- * `dyn` - vector of params corresponding to stativ variables
58
+ * `vars` - Variables struct for %Pa etc
45
59
46
- To be compatible with ncurses, `sta` and `dyn ` should be the same between calls to `expand` for
60
+ To be compatible with ncurses, `vars ` should be the same between calls to `expand` for
47
61
multiple capabilities for the same terminal.
48
62
*/
49
- pub fn expand ( cap : & [ u8 ] , params : & mut [ Param ] , sta : & mut [ Param ] , dyn : & mut [ Param ] )
63
+ pub fn expand ( cap : & [ u8 ] , params : & mut [ Param ] , vars : & mut Variables )
50
64
-> Result < ~[ u8 ] , ~str > {
51
65
assert ! ( cap. len( ) != 0 , "expanding an empty capability makes no sense" ) ;
52
66
assert ! ( params. len( ) <= 9 , "only 9 parameters are supported by capability strings" ) ;
53
67
54
- assert ! ( sta. len( ) <= 26 , "only 26 static vars are able to be used by capability strings" ) ;
55
- assert ! ( dyn. len( ) <= 26 , "only 26 dynamic vars are able to be used by capability strings" ) ;
56
-
57
68
let mut state = Nothing ;
58
69
let mut i = 0 ;
59
70
@@ -170,21 +181,21 @@ pub fn expand(cap: &[u8], params: &mut [Param], sta: &mut [Param], dyn: &mut [Pa
170
181
SetVar => {
171
182
if cur >= 'A' && cur <= 'Z' {
172
183
let idx = ( cur as u8 ) - ( 'A' as u8 ) ;
173
- sta[ idx] = stack. pop ( ) ;
184
+ vars . sta [ idx] = stack. pop ( ) ;
174
185
} else if cur >= 'a' && cur <= 'z' {
175
186
let idx = ( cur as u8 ) - ( 'a' as u8 ) ;
176
- dyn[ idx] = stack. pop ( ) ;
187
+ vars . dyn [ idx] = stack. pop ( ) ;
177
188
} else {
178
189
return Err ( ~"bad variable name in %P ") ;
179
190
}
180
191
} ,
181
192
GetVar => {
182
193
if cur >= 'A' && cur <= 'Z' {
183
194
let idx = ( cur as u8 ) - ( 'A' as u8 ) ;
184
- stack. push ( copy sta[ idx] ) ;
195
+ stack. push ( copy vars . sta [ idx] ) ;
185
196
} else if cur >= 'a' && cur <= 'z' {
186
197
let idx = ( cur as u8 ) - ( 'a' as u8 ) ;
187
- stack. push ( copy dyn[ idx] ) ;
198
+ stack. push ( copy vars . dyn [ idx] ) ;
188
199
} else {
189
200
return Err ( ~"bad variable name in %g") ;
190
201
}
@@ -222,6 +233,6 @@ mod test {
222
233
#[ test]
223
234
fn test_basic_setabf ( ) {
224
235
let s = bytes ! ( "\\ E[48;5;%p1%dm" ) ;
225
- assert_eq ! ( expand( s, [ Number ( 1 ) ] , [ ] , [ ] ) . unwrap( ) , bytes!( "\\ E[48;5;1m" ) . to_owned( ) ) ;
236
+ assert_eq ! ( expand( s, [ Number ( 1 ) ] , & mut Variables :: new ( ) ) . unwrap( ) , bytes!( "\\ E[48;5;1m" ) . to_owned( ) ) ;
226
237
}
227
238
}
0 commit comments