Skip to content

Commit

Permalink
Implement ${name%word} and ${name%%word}
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Oct 23, 2024
1 parent 44c298d commit 03bbbfa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/elements/subword/braced_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,41 @@ impl BracedParam {

if self.remove_symbol.starts_with("#") {
let mut length = 0;
let mut max_length = 0;
let mut ans_length = 0;

for ch in self.text.chars() {
length += ch.len_utf8();
let s = self.text[0..length].to_string();

if glob::compare(&s, &pattern, extglob) {
max_length = length;
ans_length = length;
if self.remove_symbol == "#" {
break;
}
}
}

self.text = self.text[max_length..].to_string();
self.text = self.text[ans_length..].to_string();
return true;
}

if self.remove_symbol.starts_with("%") {
let mut length = self.text.len();
let mut ans_length = length;

for ch in self.text.chars().rev() {
length -= ch.len_utf8();
let s = self.text[length..].to_string();

if glob::compare(&s, &pattern, extglob) {
ans_length = length;
if self.remove_symbol == "%" {
break;
}
}
}

self.text = self.text[0..ans_length].to_string();
return true;
}

Expand Down
6 changes: 6 additions & 0 deletions test/test_others.bash
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ res=$($com <<< 'A=usr/local/bin/bash; echo ${A#*/}' )
res=$($com <<< 'A=usr/local/bin/bash; echo ${A##*/}' )
[ "$res" = "bash" ] || err $LINENO

res=$($com <<< 'A=usr/local/bin/bash; echo ${A%/*}' )
[ "$res" = "usr/local/bin" ] || err $LINENO

res=$($com <<< 'A=usr/local/bin/bash; echo ${A%%/*}' )
[ "$res" = "usr" ] || err $LINENO

res=$($com <<< 'A="あいう うえお"; echo ${A#*う う}' )
[ "$res" = "えお" ] || err $LINENO

Expand Down

0 comments on commit 03bbbfa

Please sign in to comment.