Skip to content

Commit

Permalink
Expand special parameter $#
Browse files Browse the repository at this point in the history
  • Loading branch information
magicant committed Apr 3, 2022
1 parent ad40dc6 commit d90ea11
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion yash-semantics/src/expansion/initial/param/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ impl Lookup<'_> {
Lookup::Array(values) => Some(Value::Array(values.into_owned())),
}
}

/// Returns the "length" of the value.
///
/// For `Unset`, the length is 0.
/// For `Scalar`, the length is the number of characters.
/// For `Array`, the length is the number of strings.
pub fn len(&self) -> usize {
match self {
Lookup::Unset => 0,
Lookup::Scalar(value) => value.len(),
Lookup::Array(values) => values.len(),
}
}
}

/// Looks up for a special parameter.
Expand All @@ -109,7 +122,10 @@ pub fn look_up_special_parameter<'a>(env: &'a mut Env, name: &str) -> Option<Loo
}
match first {
'@' | '*' => Some((&env.variables.positional_params().value).into()),
'#' => todo!(),
'#' => {
let value = Lookup::from(&env.variables.positional_params().value);
Some(value.len().to_string().into())
}
'?' => Some(env.exit_status.to_string().into()),
'-' => todo!(),
'$' => Some(env.main_pid.to_string().into()),
Expand Down Expand Up @@ -205,6 +221,18 @@ mod tests {
if values.as_ref() == params);
}

#[test]
fn special_length() {
let mut env = yash_env::Env::new_virtual();
let result = look_up_special_parameter(&mut env, "#").unwrap();
assert_matches!(result, Lookup::Scalar(value) if value == "0");

let params = vec!["a".to_string(), "foo bar".to_string(), "9".to_string()];
env.variables.positional_params_mut().value = Value::Array(params);
let result = look_up_special_parameter(&mut env, "#").unwrap();
assert_matches!(result, Lookup::Scalar(value) if value == "3");
}

#[test]
fn special_exit_status() {
let mut env = yash_env::Env::new_virtual();
Expand Down

0 comments on commit d90ea11

Please sign in to comment.