Skip to content
Myr

[Perf] `nvm_version_greater`, `nvm_version_greater_than_or_equal_to`: avoid `awk` where possible

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Cassidy Marble · committed by Jordan Harband · parent 9c8cac4 · Browse files · 2 files+101 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+101 added−14 removed

  • tests:+39 added−0 removed
  • other:+62 added−14 removed

2 files+101 added−14 removed

modified:test/fast/Unit tests/nvm_version_greater+39 added
4 unmodified lines
5: nvm.sh6\. ../../../nvm.sh78nvm_version_greater 0.10.0 0.2.12 || die '"nvm_version_greater 0.10.0 0.2.12" did not return true'9nvm_version_greater 0.20.12 0.10.0 || die '"nvm_version_greater 0.20.12 0.10.0" did not return true'10nvm_version_greater v18.20.0 v18.19.9 || die '"nvm_version_greater v18.20.0 v18.19.9" did not return true'1112if nvm_version_greater 0.10.0 0.20.12; then13 die '"nvm_version_greater 0.10.0 0.20.12" returned true'14fi1516if nvm_version_greater 0.10.0 0.10.0; then17 die '"nvm_version_greater" returned false for the same two versions'18fi1920if nvm_version_greater v18.20.0 v18.20.0; then21 die '"nvm_version_greater v18.20.0 v18.20.0" returned true for the same two versions'22fi2324if nvm_version_greater v18.19.9 v18.20.0; then25 die '"nvm_version_greater v18.19.9 v18.20.0" returned true'26fi2728# a partial version is not a plain `x.y.z`, so it keeps the awk comparison29nvm_version_greater 0.10 0.2.12 || die '"nvm_version_greater 0.10 0.2.12" did not return true'3031# the comparison must not write anything to stdout (zsh's `typeset` prints a32# local variable when it is redeclared)33OUTPUT="$(nvm_version_greater 0.10.0 0.2.12 2>&1)"34[ -z "${OUTPUT}" ] || die "nvm_version_greater 0.10.0 0.2.12 wrote output: '${OUTPUT}'"3536# `nvm_version_greater` must not fork a command for a plain `x.y.z` pair:37# mock `awk`, which the comparison used, and fail if it runs38TEST_DIR="$(pwd)/nvm_version_greater_tmp"39[ ! -e "${TEST_DIR}" ] && mkdir "${TEST_DIR}"40MOCK_BIN="${TEST_DIR}/bin"41mkdir -p "${MOCK_BIN}"42{43 echo '#!/bin/sh'44 echo 'echo "awk was called" >&2'45 echo 'exit 1'46} > "${MOCK_BIN}/awk"47chmod +x "${MOCK_BIN}/awk"4849PATH="${MOCK_BIN}:${PATH}" nvm_version_greater 0.10.0 0.2.12 \50 || die 'nvm_version_greater 0.10.0 0.2.12 forked a command'5152PATH="${MOCK_BIN}:${PATH}" nvm_version_greater v18.20.0 v18.19.9 \53 || die 'nvm_version_greater v18.20.0 v18.19.9 forked a command'5455rm -rf "${TEST_DIR}"
modified:nvm.sh+62 added-14 removed
794 unmodified lines
795nvm_curl_version() {796 command curl -V | command awk '{ if ($1 == "curl") print $2 }' | command sed 's/-.*$//g'797}798799# Try to do some shell magic to do semver greater-than maths.800# Fall through to the expensive test by calling `awk` if we can't be sure.801nvm_version_greater() {800 command awk 'BEGIN {802 local NVM_LEFT803 NVM_LEFT="${1#v}"804 local NVM_RIGHT805 NVM_RIGHT="${2#v}"806 local NVM_PLAIN807 # a third, more complex, argument of `1` (a flag) compares equal versions as true; `0` or808 # no argument compares them as false.809 local NVM_OR_EQUAL810 NVM_OR_EQUAL="${3:-0}"811 NVM_PLAIN='true'812813 # `x.y.z` operands are compared in shell arithmetic, which avoids a fork,814 # except when a group is long enough to overflow it; anything else - partial815 # versions, prereleases, and malformed operands - keeps the comparison this816 # function has always made817 case "${NVM_LEFT}" in818 '' | *[!0-9.]* | *..* | .* | *. | *.*.*.*) NVM_PLAIN='false' ;;819 *.*.*) ;;820 *) NVM_PLAIN='false' ;;821 esac822 case "${NVM_RIGHT}" in823 '' | *[!0-9.]* | *..* | .* | *. | *.*.*.*) NVM_PLAIN='false' ;;824 *.*.*) ;;825 *) NVM_PLAIN='false' ;;826 esac827828 if [ "${NVM_PLAIN}" = 'true' ] && [ "${#NVM_LEFT}" -lt 19 ] && [ "${#NVM_RIGHT}" -lt 19 ]; then829 local NVM_GROUP830 local NVM_LEFT_GROUP831 local NVM_RIGHT_GROUP832 NVM_GROUP=1833 while [ "${NVM_GROUP}" -le 3 ]; do834 NVM_LEFT_GROUP="${NVM_LEFT%%.*}"835 NVM_LEFT="${NVM_LEFT#*.}"836 NVM_RIGHT_GROUP="${NVM_RIGHT%%.*}"837 NVM_RIGHT="${NVM_RIGHT#*.}"838 if [ "${NVM_LEFT_GROUP}" -lt "${NVM_RIGHT_GROUP}" ]; then839 return 1840 elif [ "${NVM_LEFT_GROUP}" -gt "${NVM_RIGHT_GROUP}" ]; then841 return 0842 fi843 NVM_GROUP=$((NVM_GROUP + 1))844 done845 if [ "${NVM_OR_EQUAL}" = 1 ]; then846 return 0847 fi848 return 1849 fi850 command awk -v or_equal="${NVM_OR_EQUAL}" 'BEGIN {851 if (ARGV[1] == "" || ARGV[2] == "") exit(1)852 split(ARGV[1], a, /\./);853 split(ARGV[2], b, /\./);854 for (i=1; i<=3; i++) {855 if (a[i] && a[i] !~ /^[0-9]+$/) exit(2);806 if (b[i] && b[i] !~ /^[0-9]+$/) { exit(0); }856 if (b[i] && b[i] !~ /^[0-9]+$/) {857 # a non-numeric right-hand group (a prerelease): the left is greater,858 # unless equality counts, in which case the comparison below decides859 if (or_equal != 1) { exit(0); }860 }861 if (a[i] < b[i]) exit(3);862 else if (a[i] > b[i]) exit(0);863 }864 if (or_equal == 1) { exit(0); }865 exit(4)811 }' "${1#v}" "${2#v}"866 }' "${NVM_LEFT}" "${NVM_RIGHT}"867}868869# Try to do some shell magic to do semver greater-than or equal-to maths.870# Fall through to the expensive test by calling `awk` if we can't be sure.871# see `nvm_version_greater`872nvm_version_greater_than_or_equal_to() {815 command awk 'BEGIN {816 if (ARGV[1] == "" || ARGV[2] == "") exit(1)817 split(ARGV[1], a, /\./);818 split(ARGV[2], b, /\./);819 for (i=1; i<=3; i++) {820 if (a[i] && a[i] !~ /^[0-9]+$/) exit(2);821 if (a[i] < b[i]) exit(3);822 else if (a[i] > b[i]) exit(0);823 }824 exit(0)825 }' "${1#v}" "${2#v}"873 nvm_version_greater "${1-}" "${2-}" 1874}875876nvm_version_dir() {877 local NVM_WHICH_DIR
4735 unmodified lines