@@ -34,26 +34,37 @@ pub enum Param {
3434 Number ( int )
3535}
3636
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+
3752/**
3853 Expand a parameterized capability
3954
4055 # Arguments
4156 * `cap` - string to expand
4257 * `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
4559
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
4761 multiple capabilities for the same terminal.
4862 */
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 )
5064 -> Result < ~[ u8 ] , ~str > {
5165 assert ! ( cap. len( ) != 0 , "expanding an empty capability makes no sense" ) ;
5266 assert ! ( params. len( ) <= 9 , "only 9 parameters are supported by capability strings" ) ;
5367
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-
5768 let mut state = Nothing ;
5869 let mut i = 0 ;
5970
@@ -170,21 +181,21 @@ pub fn expand(cap: &[u8], params: &mut [Param], sta: &mut [Param], dyn: &mut [Pa
170181 SetVar => {
171182 if cur >= 'A' && cur <= 'Z' {
172183 let idx = ( cur as u8 ) - ( 'A' as u8 ) ;
173- sta[ idx] = stack. pop ( ) ;
184+ vars . sta [ idx] = stack. pop ( ) ;
174185 } else if cur >= 'a' && cur <= 'z' {
175186 let idx = ( cur as u8 ) - ( 'a' as u8 ) ;
176- dyn[ idx] = stack. pop ( ) ;
187+ vars . dyn [ idx] = stack. pop ( ) ;
177188 } else {
178189 return Err ( ~"bad variable name in %P ") ;
179190 }
180191 } ,
181192 GetVar => {
182193 if cur >= 'A' && cur <= 'Z' {
183194 let idx = ( cur as u8 ) - ( 'A' as u8 ) ;
184- stack. push ( copy sta[ idx] ) ;
195+ stack. push ( copy vars . sta [ idx] ) ;
185196 } else if cur >= 'a' && cur <= 'z' {
186197 let idx = ( cur as u8 ) - ( 'a' as u8 ) ;
187- stack. push ( copy dyn[ idx] ) ;
198+ stack. push ( copy vars . dyn [ idx] ) ;
188199 } else {
189200 return Err ( ~"bad variable name in %g") ;
190201 }
@@ -222,6 +233,6 @@ mod test {
222233 #[ test]
223234 fn test_basic_setabf ( ) {
224235 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( ) ) ;
226237 }
227238}
0 commit comments