Skip to content

Commit

Permalink
fix(completion): correct behavior of slice past end of array
Browse files Browse the repository at this point in the history
  • Loading branch information
reubeno committed Oct 25, 2024
1 parent 1efdbf8 commit 6c17c42
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 1 addition & 3 deletions brush-core/src/expansion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,9 +853,7 @@ impl<'a> WordExpander<'a> {
let expanded_offset = usize::try_from(expanded_offset)?;

let expanded_parameter_len = expanded_parameter.polymorphic_len();
if expanded_offset >= expanded_parameter_len {
return Ok(Expansion::from(String::new()));
}
let expanded_offset = min(expanded_offset, expanded_parameter_len);

let end_offset = if let Some(length) = length {
let mut expanded_length = length.eval(self.shell, false).await?;
Expand Down
10 changes: 10 additions & 0 deletions brush-shell/tests/cases/word_expansion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,16 @@ cases:
echo "\${myarray[@]:2:2}: ${myarray[@]:2:2}"
echo "\${myarray[@]:2}: ${myarray[@]:2}"
- name: "Substring operator past end of array"
stdin: |
set a b c
declare -a result1=("${@:3}")
declare -p result1
myarray=(a b c)
declare -a result2=("${myarray[@]:3}")
declare -p result2
- name: "Substring with length (with nested expressions)"
stdin: |
var="Hello, world!"
Expand Down

0 comments on commit 6c17c42

Please sign in to comment.