Skip to content
This repository has been archived by the owner on Mar 4, 2018. It is now read-only.

Commit

Permalink
Option to specify what to trim
Browse files Browse the repository at this point in the history
`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
kopischke committed Apr 9, 2013
1 parent eb278b4 commit 30bda2e
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/lib/strings.sh
Expand Up @@ -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.