Permalink
Browse files

Option to specify what to trim

`trim`, `rtrim` and `ltrim` now all accept a second argument: a regex
specifying what to trim. If no second arg is passed, they default to
`[:space:]` as before. Also got rid of superfluous subshelling.
  • Loading branch information...
1 parent eb278b4 commit 30bda2ea101bb7e71bcf98f233377a0f8801470d @kopischke committed Apr 9, 2013
Showing with 6 additions and 3 deletions.
  1. +6 −3 src/lib/strings.sh
View
@@ -38,13 +38,16 @@ function match {
}
function ltrim {
- echo "$(match '[^[:space:]].*' "$1")"
+ local to_trim="${2:-[:space:]}"
+ match "[^$to_trim].*$" "$1"
}
function rtrim {
- echo "$(match '[^[:space:]].*$' "$1")"
+ local to_trim="${2:-[:space:]}"
+ match "^.*[^$to_trim]" "$1"
}
function trim {
- echo "$(match '[^[:space:]](.*[^[:space:]])?' "$1")"
+ local to_trim="${2:-[:space:]}"
+ match "[^$to_trim](.*[^$to_trim])?" "$1"
}

0 comments on commit 30bda2e

Please sign in to comment.