From d72b35b7b894aec4f48566a3a7d9a30a8762319d Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sun, 5 Apr 2015 01:26:10 -0700 Subject: [PATCH] Remove external calls to `sed` using parameter filtering. POSIX-compliant per http://stackoverflow.com/a/25536935/632724 Relates to #709 --- nvm.sh | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/nvm.sh b/nvm.sh index 7b3efbc..caedaef 100755 --- a/nvm.sh +++ b/nvm.sh @@ -307,7 +307,7 @@ nvm_is_valid_version() { } nvm_normalize_version() { - echo "$1" | command sed -e 's/^v//' | command awk -F. '{ printf("%d%06d%06d\n", $1,$2,$3); }' + echo "${1#v*}" | command awk -F. '{ printf("%d%06d%06d\n", $1,$2,$3); }' } nvm_ensure_version_prefix() { @@ -324,8 +324,7 @@ nvm_format_version() { local VERSION VERSION="$(nvm_ensure_version_prefix "$1")" if [ "_$(nvm_num_version_groups "$VERSION")" != "_3" ]; then - VERSION="$(echo "$VERSION" | command sed -e 's/\.*$/.0/')" - nvm_format_version "$VERSION" + nvm_format_version "${VERSION%*\.}.0" else echo "$VERSION" fi @@ -338,10 +337,12 @@ nvm_num_version_groups() { echo "0" return fi + VERSION="${VERSION#v*}" + VERSION="${VERSION%\.}" local NVM_NUM_DOTS - NVM_NUM_DOTS=$(echo "$VERSION" | command sed -e 's/^v//' | command sed -e 's/\.$//' | command sed -e 's/[^\.]//g') + NVM_NUM_DOTS=$(echo "$VERSION" | command sed -e 's/[^\.]//g') local NVM_NUM_GROUPS - NVM_NUM_GROUPS=".$NVM_NUM_DOTS" + NVM_NUM_GROUPS=".$NVM_NUM_DOTS" # add extra dot, since it's (n - 1) dots at this point echo "${#NVM_NUM_GROUPS}" } @@ -509,7 +510,7 @@ nvm_strip_iojs_prefix() { if [ "_$1" = "_$NVM_IOJS_PREFIX" ]; then echo else - echo "$1" | command sed "s/^$NVM_IOJS_PREFIX-//" + echo "${1#"$NVM_IOJS_PREFIX"-*}" fi } @@ -564,7 +565,7 @@ nvm_ls() { local NUM_VERSION_GROUPS NUM_VERSION_GROUPS="$(nvm_num_version_groups "$PATTERN")" if [ "_$NUM_VERSION_GROUPS" = "_2" ] || [ "_$NUM_VERSION_GROUPS" = "_1" ]; then - PATTERN="$(echo "$PATTERN" | command sed -e 's/\.*$//g')." + PATTERN="${PATTERN%*\.}." fi ;; esac