Advertisement
devinteske

replace awk with a more efficient built-ins-only algo

Jan 24th, 2016
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 2.20 KB | None | 0 0
  1. Index: strings.subr
  2. ===================================================================
  3. --- strings.subr        (revision 294669)
  4. +++ strings.subr        (working copy)
  5. @@ -70,17 +70,63 @@ f_substr()
  6.  #
  7.  f_snprintf()
  8.  {
  9. +   local __funcname=f_snprintf
  10.     local __var_to_set="$1" __size="$2"
  11.     shift 2 # var_to_set size
  12. -   eval "$__var_to_set"=\$\( printf -- \"\$@\" \| \
  13. -       awk -v max=\"\$__size\" \''
  14. -   {
  15. -       len = length($0)
  16. -       max -= len
  17. -       print substr($0,0,(max > 0 ? len : max + len))
  18. -       if ( max < 0 ) exit
  19. -       max--
  20. -   }'\' \)
  21. +
  22. +   if [ "$__size" -eq 0 ] 2> /dev/null; then
  23. +       setvar "$__var_to_set" ""
  24. +       return ${SUCCESS:-0}
  25. +   elif [ $? -ge 2 ] || [ $__size -lt 0 ]; then
  26. +       setvar "$__var_to_set" ""
  27. +       echo "$__funcname: invalid size argument \`__size'" >&2
  28. +       return ${FAILURE:-1}
  29. +   fi
  30. +
  31. +   local __f_snprintf_tmp
  32. +   f_sprintf __f_snprintf_tmp "$@"
  33. +
  34. +   local __tmp_size=${#__f_snprintf_tmp}
  35. +   local __trim=$(( $__tmp_size - $__size )) __trimq
  36. +   local __tbuf __tbuf_len
  37. +   local __mask __mask_len
  38. +   while [ $__trim -gt 0 ]; do
  39. +       __tbuf="?"
  40. +       __tbuf_len=1
  41. +       if [ $__trim -le $__size ]; then
  42. +           while [ $__tbuf_len -lt $(( $__trim / $__tbuf_len )) ]
  43. +           do
  44. +               __tbuf="$__tbuf?"
  45. +               __tbuf_len=$(( $__tbuf_len + 1 ))
  46. +           done
  47. +           __trimq=$(( $__trim / $__tbuf_len ))
  48. +           __trim=$(( $__trim - $__tbuf_len * $__trimq ))
  49. +           while [ $__trimq -gt 0 ]; do
  50. +               __f_snprintf_tmp="${__f_snprintf_tmp%$__tbuf}"
  51. +               __trimq=$(( $__trimq - 1 ))
  52. +           done
  53. +       else
  54. +           __mask="$__f_snprintf_tmp"
  55. +           while [ $__tbuf_len -lt $(( $__size / $__tbuf_len )) ]
  56. +           do
  57. +               __tbuf="$__tbuf?"
  58. +               __tbuf_len=$(( $__tbuf_len + 1 ))
  59. +           done
  60. +           __trimq=$(( $__size / $__tbuf_len ))
  61. +           if [ $(( $__trimq * $__tbuf_len )) -ne $__size ]; then
  62. +               __tbuf="$__tbuf?"
  63. +               __tbuf_len=$(( $__tbuf_len + 1 ))
  64. +           fi
  65. +           __mask_len=$(( $__tmp_size - $__tbuf_len * $__trimq ))
  66. +           __trim=$(( $__tmp_size - $__mask_len - $__size ))
  67. +           while [ $__trimq -gt 0 ]; do
  68. +               __mask="${__mask#$__tbuf}"
  69. +               __trimq=$(( $__trimq - 1 ))
  70. +           done
  71. +           __f_snprintf_tmp="${__f_snprintf_tmp%"$__mask"}"
  72. +       fi
  73. +   done
  74. +   setvar "$__var_to_set" "$__f_snprintf_tmp"
  75.  }
  76.  
  77.  # f_sprintf $var_to_set $format [$arguments ...]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement