1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Parameterized string expansion
14 use std::mem::replace;
16 #[deriving(PartialEq)]
26 FormatPattern(Flags, FormatState),
28 SeekIfElsePercent(int),
33 #[deriving(PartialEq)]
40 /// Types of parameters a capability can use
48 /// Container for static and dynamic variable arrays
49 pub struct Variables {
50 /// Static variables A-Z
52 /// Dynamic variables a-z
57 /// Return a new zero-initialized Variables
58 pub fn new() -> Variables {
61 Number(0), Number(0), Number(0), Number(0), Number(0),
62 Number(0), Number(0), Number(0), Number(0), Number(0),
63 Number(0), Number(0), Number(0), Number(0), Number(0),
64 Number(0), Number(0), Number(0), Number(0), Number(0),
65 Number(0), Number(0), Number(0), Number(0), Number(0),
69 Number(0), Number(0), Number(0), Number(0), Number(0),
70 Number(0), Number(0), Number(0), Number(0), Number(0),
71 Number(0), Number(0), Number(0), Number(0), Number(0),
72 Number(0), Number(0), Number(0), Number(0), Number(0),
73 Number(0), Number(0), Number(0), Number(0), Number(0),
81 Expand a parameterized capability
84 * `cap` - string to expand
85 * `params` - vector of params for %p1 etc
86 * `vars` - Variables struct for %Pa etc
88 To be compatible with ncurses, `vars` should be the same between calls to `expand` for
89 multiple capabilities for the same terminal.
91 pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables)
92 -> Result<Vec<u8> , String> {
93 let mut state = Nothing;
95 // expanded cap will only rarely be larger than the cap itself
96 let mut output = Vec::with_capacity(cap.len());
98 let mut stack: Vec<Param> = Vec::new();
100 // Copy parameters into a local vector for mutability
102 Number(0), Number(0), Number(0), Number(0), Number(0),
103 Number(0), Number(0), Number(0), Number(0),
105 for (dst, src) in mparams.mut_iter().zip(params.iter()) {
106 *dst = (*src).clone();
109 for &c in cap.iter() {
111 let mut old_state = state;
122 '%' => { output.push(c); state = Nothing },
123 'c' => if stack.len() > 0 {
124 match stack.pop().unwrap() {
125 // if c is 0, use 0200 (128) for ncurses compatibility
126 Number(c) => output.push(if c == 0 { 128 } else { c } as u8),
127 _ => return Err("a non-char was used with %c".to_string())
129 } else { return Err("stack is empty".to_string()) },
130 'p' => state = PushParam,
131 'P' => state = SetVar,
132 'g' => state = GetVar,
133 '\'' => state = CharConstant,
134 '{' => state = IntConstant(0),
135 'l' => if stack.len() > 0 {
136 match stack.pop().unwrap() {
137 String(s) => stack.push(Number(s.len() as int)),
138 _ => return Err("a non-str was used with %l".to_string())
140 } else { return Err("stack is empty".to_string()) },
141 '+' => if stack.len() > 1 {
142 match (stack.pop().unwrap(), stack.pop().unwrap()) {
143 (Number(y), Number(x)) => stack.push(Number(x + y)),
144 _ => return Err("non-numbers on stack with +".to_string())
146 } else { return Err("stack is empty".to_string()) },
147 '-' => if stack.len() > 1 {
148 match (stack.pop().unwrap(), stack.pop().unwrap()) {
149 (Number(y), Number(x)) => stack.push(Number(x - y)),
150 _ => return Err("non-numbers on stack with -".to_string())
152 } else { return Err("stack is empty".to_string()) },
153 '*' => if stack.len() > 1 {
154 match (stack.pop().unwrap(), stack.pop().unwrap()) {
155 (Number(y), Number(x)) => stack.push(Number(x * y)),
156 _ => return Err("non-numbers on stack with *".to_string())
158 } else { return Err("stack is empty".to_string()) },
159 '/' => if stack.len() > 1 {
160 match (stack.pop().unwrap(), stack.pop().unwrap()) {
161 (Number(y), Number(x)) => stack.push(Number(x / y)),
162 _ => return Err("non-numbers on stack with /".to_string())
164 } else { return Err("stack is empty".to_string()) },
165 'm' => if stack.len() > 1 {
166 match (stack.pop().unwrap(), stack.pop().unwrap()) {
167 (Number(y), Number(x)) => stack.push(Number(x % y)),
168 _ => return Err("non-numbers on stack with %".to_string())
170 } else { return Err("stack is empty".to_string()) },
171 '&' => if stack.len() > 1 {
172 match (stack.pop().unwrap(), stack.pop().unwrap()) {
173 (Number(y), Number(x)) => stack.push(Number(x & y)),
174 _ => return Err("non-numbers on stack with &".to_string())
176 } else { return Err("stack is empty".to_string()) },
177 '|' => if stack.len() > 1 {
178 match (stack.pop().unwrap(), stack.pop().unwrap()) {
179 (Number(y), Number(x)) => stack.push(Number(x | y)),
180 _ => return Err("non-numbers on stack with |".to_string())
182 } else { return Err("stack is empty".to_string()) },
183 '^' => if stack.len() > 1 {
184 match (stack.pop().unwrap(), stack.pop().unwrap()) {
185 (Number(y), Number(x)) => stack.push(Number(x ^ y)),
186 _ => return Err("non-numbers on stack with ^".to_string())
188 } else { return Err("stack is empty".to_string()) },
189 '=' => if stack.len() > 1 {
190 match (stack.pop().unwrap(), stack.pop().unwrap()) {
191 (Number(y), Number(x)) => stack.push(Number(if x == y { 1 }
193 _ => return Err("non-numbers on stack with =".to_string())
195 } else { return Err("stack is empty".to_string()) },
196 '>' => if stack.len() > 1 {
197 match (stack.pop().unwrap(), stack.pop().unwrap()) {
198 (Number(y), Number(x)) => stack.push(Number(if x > y { 1 }
200 _ => return Err("non-numbers on stack with >".to_string())
202 } else { return Err("stack is empty".to_string()) },
203 '<' => if stack.len() > 1 {
204 match (stack.pop().unwrap(), stack.pop().unwrap()) {
205 (Number(y), Number(x)) => stack.push(Number(if x < y { 1 }
207 _ => return Err("non-numbers on stack with <".to_string())
209 } else { return Err("stack is empty".to_string()) },
210 'A' => if stack.len() > 1 {
211 match (stack.pop().unwrap(), stack.pop().unwrap()) {
212 (Number(0), Number(_)) => stack.push(Number(0)),
213 (Number(_), Number(0)) => stack.push(Number(0)),
214 (Number(_), Number(_)) => stack.push(Number(1)),
215 _ => return Err("non-numbers on stack with logical and".to_string())
217 } else { return Err("stack is empty".to_string()) },
218 'O' => if stack.len() > 1 {
219 match (stack.pop().unwrap(), stack.pop().unwrap()) {
220 (Number(0), Number(0)) => stack.push(Number(0)),
221 (Number(_), Number(_)) => stack.push(Number(1)),
222 _ => return Err("non-numbers on stack with logical or".to_string())
224 } else { return Err("stack is empty".to_string()) },
225 '!' => if stack.len() > 0 {
226 match stack.pop().unwrap() {
227 Number(0) => stack.push(Number(1)),
228 Number(_) => stack.push(Number(0)),
229 _ => return Err("non-number on stack with logical not".to_string())
231 } else { return Err("stack is empty".to_string()) },
232 '~' => if stack.len() > 0 {
233 match stack.pop().unwrap() {
234 Number(x) => stack.push(Number(!x)),
235 _ => return Err("non-number on stack with %~".to_string())
237 } else { return Err("stack is empty".to_string()) },
238 'i' => match (mparams[0].clone(), mparams[1].clone()) {
239 (Number(x), Number(y)) => {
240 mparams[0] = Number(x+1);
241 mparams[1] = Number(y+1);
243 (_, _) => return Err("first two params not numbers with %i".to_string())
246 // printf-style support for %doxXs
247 'd'|'o'|'x'|'X'|'s' => if stack.len() > 0 {
248 let flags = Flags::new();
249 let res = format(stack.pop().unwrap(), FormatOp::from_char(cur), flags);
250 if res.is_err() { return res }
251 output.push_all(res.unwrap().as_slice())
252 } else { return Err("stack is empty".to_string()) },
253 ':'|'#'|' '|'.'|'0'..'9' => {
254 let mut flags = Flags::new();
255 let mut fstate = FormatStateFlags;
258 '#' => flags.alternate = true,
259 ' ' => flags.space = true,
260 '.' => fstate = FormatStatePrecision,
262 flags.width = cur as uint - '0' as uint;
263 fstate = FormatStateWidth;
267 state = FormatPattern(flags, fstate);
272 't' => if stack.len() > 0 {
273 match stack.pop().unwrap() {
274 Number(0) => state = SeekIfElse(0),
276 _ => return Err("non-number on stack \
277 with conditional".to_string())
279 } else { return Err("stack is empty".to_string()) },
280 'e' => state = SeekIfEnd(0),
284 return Err(format!("unrecognized format option {}", cur))
289 // params are 1-indexed
290 stack.push(mparams[match char::to_digit(cur, 10) {
292 None => return Err("bad param number".to_string())
296 if cur >= 'A' && cur <= 'Z' {
298 let idx = (cur as u8) - ('A' as u8);
299 vars.sta[idx as uint] = stack.pop().unwrap();
300 } else { return Err("stack is empty".to_string()) }
301 } else if cur >= 'a' && cur <= 'z' {
303 let idx = (cur as u8) - ('a' as u8);
304 vars.dyn[idx as uint] = stack.pop().unwrap();
305 } else { return Err("stack is empty".to_string()) }
307 return Err("bad variable name in %P".to_string());
311 if cur >= 'A' && cur <= 'Z' {
312 let idx = (cur as u8) - ('A' as u8);
313 stack.push(vars.sta[idx as uint].clone());
314 } else if cur >= 'a' && cur <= 'z' {
315 let idx = (cur as u8) - ('a' as u8);
316 stack.push(vars.dyn[idx as uint].clone());
318 return Err("bad variable name in %g".to_string());
322 stack.push(Number(c as int));
327 return Err("malformed character constant".to_string());
333 stack.push(Number(i));
337 state = IntConstant(i*10 + (cur as int - '0' as int));
340 _ => return Err("bad int constant".to_string())
343 FormatPattern(ref mut flags, ref mut fstate) => {
345 match (*fstate, cur) {
346 (_,'d')|(_,'o')|(_,'x')|(_,'X')|(_,'s') => if stack.len() > 0 {
347 let res = format(stack.pop().unwrap(), FormatOp::from_char(cur), *flags);
348 if res.is_err() { return res }
349 output.push_all(res.unwrap().as_slice());
350 // will cause state to go to Nothing
351 old_state = FormatPattern(*flags, *fstate);
352 } else { return Err("stack is empty".to_string()) },
353 (FormatStateFlags,'#') => {
354 flags.alternate = true;
356 (FormatStateFlags,'-') => {
359 (FormatStateFlags,'+') => {
362 (FormatStateFlags,' ') => {
365 (FormatStateFlags,'0'..'9') => {
366 flags.width = cur as uint - '0' as uint;
367 *fstate = FormatStateWidth;
369 (FormatStateFlags,'.') => {
370 *fstate = FormatStatePrecision;
372 (FormatStateWidth,'0'..'9') => {
373 let old = flags.width;
374 flags.width = flags.width * 10 + (cur as uint - '0' as uint);
375 if flags.width < old { return Err("format width overflow".to_string()) }
377 (FormatStateWidth,'.') => {
378 *fstate = FormatStatePrecision;
380 (FormatStatePrecision,'0'..'9') => {
381 let old = flags.precision;
382 flags.precision = flags.precision * 10 + (cur as uint - '0' as uint);
383 if flags.precision < old {
384 return Err("format precision overflow".to_string())
387 _ => return Err("invalid format specifier".to_string())
390 SeekIfElse(level) => {
392 state = SeekIfElsePercent(level);
396 SeekIfElsePercent(level) => {
401 state = SeekIfElse(level-1);
403 } else if cur == 'e' && level == 0 {
405 } else if cur == '?' {
406 state = SeekIfElse(level+1);
408 state = SeekIfElse(level);
411 SeekIfEnd(level) => {
413 state = SeekIfEndPercent(level);
417 SeekIfEndPercent(level) => {
422 state = SeekIfEnd(level-1);
424 } else if cur == '?' {
425 state = SeekIfEnd(level+1);
427 state = SeekIfEnd(level);
431 if state == old_state {
438 #[deriving(PartialEq)]
450 Flags{ width: 0, precision: 0, alternate: false,
451 left: false, sign: false, space: false }
464 fn from_char(c: char) -> FormatOp {
471 _ => fail!("bad FormatOp char")
474 fn to_char(self) -> char {
485 fn format(val: Param, op: FormatOp, flags: Flags) -> Result<Vec<u8> ,String> {
486 let mut s = match val {
488 let s = match (op, flags.sign) {
489 (FormatDigit, true) => format!("{:+d}", d).into_bytes(),
490 (FormatDigit, false) => format!("{:d}", d).into_bytes(),
491 (FormatOctal, _) => format!("{:o}", d).into_bytes(),
492 (FormatHex, _) => format!("{:x}", d).into_bytes(),
493 (FormatHEX, _) => format!("{:X}", d).into_bytes(),
494 (FormatString, _) => {
495 return Err("non-number on stack with %s".to_string())
498 let mut s: Vec<u8> = s.move_iter().collect();
499 if flags.precision > s.len() {
500 let mut s_ = Vec::with_capacity(flags.precision);
501 let n = flags.precision - s.len();
502 s_.grow(n, &('0' as u8));
506 assert!(!s.is_empty(), "string conversion produced empty result");
509 if flags.space && !(*s.get(0) == '-' as u8 ||
510 *s.get(0) == '+' as u8) {
511 s.unshift(' ' as u8);
515 if flags.alternate && *s.get(0) != '0' as u8 {
516 s.unshift('0' as u8);
521 let s_ = replace(&mut s, vec!('0' as u8, 'x' as u8));
533 let s_ = replace(&mut s, vec!('0' as u8, 'X' as u8));
537 FormatString => unreachable!()
544 let mut s = Vec::from_slice(s.as_bytes());
545 if flags.precision > 0 && flags.precision < s.len() {
546 s.truncate(flags.precision);
551 return Err(format!("non-string on stack with %{}",
557 if flags.width > s.len() {
558 let n = flags.width - s.len();
560 s.grow(n, &(' ' as u8));
562 let mut s_ = Vec::with_capacity(flags.width);
563 s_.grow(n, &(' ' as u8));
573 use super::{expand,String,Variables,Number};
577 fn test_basic_setabf() {
578 let s = b"\\E[48;5;%p1%dm";
579 assert_eq!(expand(s, [Number(1)], &mut Variables::new()).unwrap(),
580 "\\E[48;5;1m".bytes().collect());
584 fn test_multiple_int_constants() {
585 assert_eq!(expand(b"%{1}%{2}%d%d", [], &mut Variables::new()).unwrap(),
586 "21".bytes().collect());
591 let mut vars = Variables::new();
592 assert_eq!(expand(b"%p1%d%p2%d%p3%d%i%p1%d%p2%d%p3%d",
593 [Number(1),Number(2),Number(3)], &mut vars),
594 Ok("123233".bytes().collect()));
595 assert_eq!(expand(b"%p1%d%p2%d%i%p1%d%p2%d", [], &mut vars),
596 Ok("0011".bytes().collect()));
600 fn test_param_stack_failure_conditions() {
601 let mut varstruct = Variables::new();
602 let vars = &mut varstruct;
603 let caps = ["%d", "%c", "%s", "%Pa", "%l", "%!", "%~"];
604 for cap in caps.iter() {
605 let res = expand(cap.as_bytes(), [], vars);
606 assert!(res.is_err(),
607 "Op {} succeeded incorrectly with 0 stack entries", *cap);
608 let p = if *cap == "%s" || *cap == "%l" {
609 String("foo".to_string())
613 let res = expand("%p1".bytes().collect::<Vec<_>>()
614 .append(cap.as_bytes()).as_slice(),
618 "Op {} failed with 1 stack entry: {}", *cap, res.unwrap_err());
620 let caps = ["%+", "%-", "%*", "%/", "%m", "%&", "%|", "%A", "%O"];
621 for cap in caps.iter() {
622 let res = expand(cap.as_bytes(), [], vars);
623 assert!(res.is_err(),
624 "Binop {} succeeded incorrectly with 0 stack entries", *cap);
625 let res = expand("%{1}".bytes().collect::<Vec<_>>()
626 .append(cap.as_bytes()).as_slice(),
629 assert!(res.is_err(),
630 "Binop {} succeeded incorrectly with 1 stack entry", *cap);
631 let res = expand("%{1}%{2}".bytes().collect::<Vec<_>>()
632 .append(cap.as_bytes()).as_slice(),
636 "Binop {} failed with 2 stack entries: {}", *cap, res.unwrap_err());
641 fn test_push_bad_param() {
642 assert!(expand(b"%pa", [], &mut Variables::new()).is_err());
646 fn test_comparison_ops() {
647 let v = [('<', [1u8, 0u8, 0u8]), ('=', [0u8, 1u8, 0u8]), ('>', [0u8, 0u8, 1u8])];
648 for &(op, bs) in v.iter() {
649 let s = format!("%{{1}}%{{2}}%{}%d", op);
650 let res = expand(s.as_bytes(), [], &mut Variables::new());
651 assert!(res.is_ok(), res.unwrap_err());
652 assert_eq!(res.unwrap(), vec!('0' as u8 + bs[0]));
653 let s = format!("%{{1}}%{{1}}%{}%d", op);
654 let res = expand(s.as_bytes(), [], &mut Variables::new());
655 assert!(res.is_ok(), res.unwrap_err());
656 assert_eq!(res.unwrap(), vec!('0' as u8 + bs[1]));
657 let s = format!("%{{2}}%{{1}}%{}%d", op);
658 let res = expand(s.as_bytes(), [], &mut Variables::new());
659 assert!(res.is_ok(), res.unwrap_err());
660 assert_eq!(res.unwrap(), vec!('0' as u8 + bs[2]));
665 fn test_conditionals() {
666 let mut vars = Variables::new();
667 let s = b"\\E[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m";
668 let res = expand(s, [Number(1)], &mut vars);
669 assert!(res.is_ok(), res.unwrap_err());
670 assert_eq!(res.unwrap(),
671 "\\E[31m".bytes().collect());
672 let res = expand(s, [Number(8)], &mut vars);
673 assert!(res.is_ok(), res.unwrap_err());
674 assert_eq!(res.unwrap(),
675 "\\E[90m".bytes().collect());
676 let res = expand(s, [Number(42)], &mut vars);
677 assert!(res.is_ok(), res.unwrap_err());
678 assert_eq!(res.unwrap(),
679 "\\E[38;5;42m".bytes().collect());
684 let mut varstruct = Variables::new();
685 let vars = &mut varstruct;
686 assert_eq!(expand(b"%p1%s%p2%2s%p3%2s%p4%.2s",
687 [String("foo".to_string()),
688 String("foo".to_string()),
689 String("f".to_string()),
690 String("foo".to_string())], vars),
691 Ok("foofoo ffo".bytes().collect()));
692 assert_eq!(expand(b"%p1%:-4.2s", [String("foo".to_string())], vars),
693 Ok("fo ".bytes().collect()));
695 assert_eq!(expand(b"%p1%d%p1%.3d%p1%5d%p1%:+d", [Number(1)], vars),
696 Ok("1001 1+1".bytes().collect()));
697 assert_eq!(expand(b"%p1%o%p1%#o%p2%6.4x%p2%#6.4X", [Number(15), Number(27)], vars),
698 Ok("17017 001b0X001B".bytes().collect()));