strings.subr (14d5c08ba87c8cc00b00829e47a108aaf52c5bc7) | strings.subr (0f913ed5f95a95df25f1125531f4f1c62d7144dd) |
---|---|
1if [ ! "$_STRINGS_SUBR" ]; then _STRINGS_SUBR=1 2# 3# Copyright (c) 2006-2013 Devin Teske 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions 8# are met: --- 56 unchanged lines hidden (view full) --- 65# f_snprintf $var_to_set $size $format [$arguments ...] 66# 67# Similar to snprintf(3), write at most $size number of bytes into $var_to_set 68# using printf(1) syntax (`$format [$arguments ...]'). The value of $var_to_set 69# is NULL unless at-least one byte is stored from the output. 70# 71f_snprintf() 72{ | 1if [ ! "$_STRINGS_SUBR" ]; then _STRINGS_SUBR=1 2# 3# Copyright (c) 2006-2013 Devin Teske 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions 8# are met: --- 56 unchanged lines hidden (view full) --- 65# f_snprintf $var_to_set $size $format [$arguments ...] 66# 67# Similar to snprintf(3), write at most $size number of bytes into $var_to_set 68# using printf(1) syntax (`$format [$arguments ...]'). The value of $var_to_set 69# is NULL unless at-least one byte is stored from the output. 70# 71f_snprintf() 72{ |
73 local __funcname=f_snprintf |
|
73 local __var_to_set="$1" __size="$2" 74 shift 2 # var_to_set size | 74 local __var_to_set="$1" __size="$2" 75 shift 2 # var_to_set size |
75 eval "$__var_to_set"=\$\( printf -- \"\$@\" \| \ 76 awk -v max=\"\$__size\" \'' 77 { 78 len = length($0) 79 max -= len 80 print substr($0,0,(max > 0 ? len : max + len)) 81 if ( max < 0 ) exit 82 max-- 83 }'\' \) | 76 77 if [ "$__size" -eq 0 ] 2> /dev/null; then 78 setvar "$__var_to_set" "" 79 return ${SUCCESS:-0} 80 elif [ $? -ge 2 ] || [ $__size -lt 0 ]; then 81 setvar "$__var_to_set" "" 82 echo "$__funcname: invalid size argument \`__size'" >&2 83 return ${FAILURE:-1} 84 fi 85 86 local __f_snprintf_tmp 87 f_sprintf __f_snprintf_tmp "$@" 88 89 local __tmp_size=${#__f_snprintf_tmp} 90 local __trim=$(( $__tmp_size - $__size )) __trimq 91 local __tbuf __tbuf_len 92 local __mask __mask_len 93 while [ $__trim -gt 0 ]; do 94 __tbuf="?" 95 __tbuf_len=1 96 if [ $__trim -le $__size ]; then 97 while [ $__tbuf_len -lt $(( $__trim / $__tbuf_len )) ] 98 do 99 __tbuf="$__tbuf?" 100 __tbuf_len=$(( $__tbuf_len + 1 )) 101 done 102 __trimq=$(( $__trim / $__tbuf_len )) 103 __trim=$(( $__trim - $__tbuf_len * $__trimq )) 104 while [ $__trimq -gt 0 ]; do 105 __f_snprintf_tmp="${__f_snprintf_tmp%$__tbuf}" 106 __trimq=$(( $__trimq - 1 )) 107 done 108 else 109 __mask="$__f_snprintf_tmp" 110 while [ $__tbuf_len -lt $(( $__size / $__tbuf_len )) ] 111 do 112 __tbuf="$__tbuf?" 113 __tbuf_len=$(( $__tbuf_len + 1 )) 114 done 115 __trimq=$(( $__size / $__tbuf_len )) 116 if [ $(( $__trimq * $__tbuf_len )) -ne $__size ]; then 117 __tbuf="$__tbuf?" 118 __tbuf_len=$(( $__tbuf_len + 1 )) 119 fi 120 __mask_len=$(( $__tmp_size - $__tbuf_len * $__trimq )) 121 __trim=$(( $__tmp_size - $__mask_len - $__size )) 122 while [ $__trimq -gt 0 ]; do 123 __mask="${__mask#$__tbuf}" 124 __trimq=$(( $__trimq - 1 )) 125 done 126 __f_snprintf_tmp="${__f_snprintf_tmp%"$__mask"}" 127 fi 128 done 129 setvar "$__var_to_set" "$__f_snprintf_tmp" |
84} 85 86# f_sprintf $var_to_set $format [$arguments ...] 87# 88# Similar to sprintf(3), write a string into $var_to_set using printf(1) syntax 89# (`$format [$arguments ...]'). 90# 91f_sprintf() --- 363 unchanged lines hidden --- | 130} 131 132# f_sprintf $var_to_set $format [$arguments ...] 133# 134# Similar to sprintf(3), write a string into $var_to_set using printf(1) syntax 135# (`$format [$arguments ...]'). 136# 137f_sprintf() --- 363 unchanged lines hidden --- |