From 03bbbfaecd6af607f3a128607c2ea4263ffeb99e Mon Sep 17 00:00:00 2001 From: Ryuichi Ueda Date: Wed, 23 Oct 2024 15:08:56 +0900 Subject: [PATCH] Implement ${name%word} and ${name%%word} --- src/elements/subword/braced_param.rs | 26 +++++++++++++++++++++++--- test/test_others.bash | 6 ++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/elements/subword/braced_param.rs b/src/elements/subword/braced_param.rs index eeb6713f..05da65fc 100644 --- a/src/elements/subword/braced_param.rs +++ b/src/elements/subword/braced_param.rs @@ -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; } diff --git a/test/test_others.bash b/test/test_others.bash index d53f41e2..806a1770 100755 --- a/test/test_others.bash +++ b/test/test_others.bash @@ -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