12
12
13
13
use core:: prelude:: * ;
14
14
use core:: { char, int, vec} ;
15
+ use core:: iterator:: IteratorUtil ;
15
16
16
17
#[ deriving( Eq ) ]
17
18
enum States {
@@ -60,15 +61,12 @@ impl Variables {
60
61
To be compatible with ncurses, `vars` should be the same between calls to `expand` for
61
62
multiple capabilities for the same terminal.
62
63
*/
63
- pub fn expand ( cap : & [ u8 ] , params : & mut [ Param ] , vars : & mut Variables )
64
+ pub fn expand( cap : & [ u8 ] , params : & [ Param ] , vars : & mut Variables )
64
65
-> Result < ~[ u8 ] , ~str > {
65
- assert ! ( cap. len( ) != 0 , "expanding an empty capability makes no sense" ) ;
66
- assert ! ( params. len( ) <= 9 , "only 9 parameters are supported by capability strings" ) ;
67
-
68
66
let mut state = Nothing ;
69
67
let mut i = 0 ;
70
68
71
- // expanded cap will only rarely be smaller than the cap itself
69
+ // expanded cap will only rarely be larger than the cap itself
72
70
let mut output = vec:: with_capacity ( cap. len ( ) ) ;
73
71
74
72
let mut cur;
@@ -77,6 +75,12 @@ pub fn expand(cap: &[u8], params: &mut [Param], vars: &mut Variables)
77
75
78
76
let mut intstate = ~[ ] ;
79
77
78
+ // Copy parameters into a local vector for mutability
79
+ let mut mparams = [ Number ( 0 ) , ..9 ] ;
80
+ for mparams. mut_iter( ) . zip( params. iter( ) ) . advance |( dst, & src) | {
81
+ * dst = src;
82
+ }
83
+
80
84
while i < cap. len ( ) {
81
85
cur = cap[ i] as char ;
82
86
let mut old_state = state;
@@ -163,7 +167,7 @@ pub fn expand(cap: &[u8], params: &mut [Param], vars: &mut Variables)
163
167
Number ( x) => stack. push ( Number ( !x) ) ,
164
168
_ => return Err ( ~"non-number on stack with %~")
165
169
} ,
166
- 'i' => match ( copy params [ 0 ] , copy params [ 1 ] ) {
170
+ 'i' => match ( copy mparams [ 0 ] , copy mparams [ 1 ] ) {
167
171
( Number ( ref mut x) , Number ( ref mut y) ) => {
168
172
* x += 1 ;
169
173
* y += 1 ;
@@ -176,7 +180,7 @@ pub fn expand(cap: &[u8], params: &mut [Param], vars: &mut Variables)
176
180
} ,
177
181
PushParam => {
178
182
// params are 1-indexed
179
- stack. push ( copy params [ char:: to_digit ( cur, 10 ) . expect ( "bad param number" ) - 1 ] ) ;
183
+ stack. push ( copy mparams [ char:: to_digit ( cur, 10 ) . expect ( "bad param number" ) - 1 ] ) ;
180
184
} ,
181
185
SetVar => {
182
186
if cur >= 'A' && cur <= 'Z' {
0 commit comments