hash 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/env bash
  2. #
  3. # The idea is that we emulate a hash using two methods
  4. #
  5. # The first method is providing functions by sourcing this file
  6. #
  7. # The second method is where this script is called directly,
  8. # we then provide functionality of a file based hash
  9. #
  10. if [[ "$rvm_trace_flag" -eq 2 ]] ; then set -x ; export rvm_trace_flag ; fi
  11. [[ -z "${ZSH_VERSION:-}" ]] ; array_start=$?
  12. hash()
  13. {
  14. hash_name=$1 ; hash_key=$2 ; hash_value=$3
  15. eval "_hash_${hash_name}_keys=\${_hash_${hash_name}_keys:-()} ; _hash_${hash_name}_values=\${_hash_${hash_name}_values:-()}"
  16. if [[ -z "$hash_value" ]] ; then
  17. eval "length=\${#_hash_${hash_name}_keys[@]}"
  18. for (( index=$__array_start ; index < $length; index++ )) ; do
  19. eval "key=\"\${_hash_${hash_name}_keys[$index]}\""
  20. if [[ "$hash_key" == "$key" ]] ; then
  21. eval "echo -n \${_hash_${hash_name}_values[$index]}"
  22. break
  23. fi
  24. done
  25. else
  26. eval "index=\$((\${#_hash_${hash_name}_keys[*]} + $__array_start))"
  27. eval "_hash_${hash_name}_keys[$index]=\"$hash_key\""
  28. eval "_hash_${hash_name}_values[$index]=\"$hash_value\""
  29. fi
  30. }