strings.subr (260678) | strings.subr (263980) |
---|---|
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: --- 10 unchanged lines hidden (view full) --- 19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25# SUCH DAMAGE. 26# | 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: --- 10 unchanged lines hidden (view full) --- 19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25# SUCH DAMAGE. 26# |
27# $FreeBSD: stable/10/usr.sbin/bsdconfig/share/strings.subr 260678 2014-01-15 07:49:17Z dteske $ | 27# $FreeBSD: stable/10/usr.sbin/bsdconfig/share/strings.subr 263980 2014-04-01 00:19:13Z dteske $ |
28# 29############################################################ INCLUDES 30 31BSDCFG_SHARE="/usr/share/bsdconfig" 32. $BSDCFG_SHARE/common.subr || exit 1 33 34############################################################ GLOBALS 35 36# | 28# 29############################################################ INCLUDES 30 31BSDCFG_SHARE="/usr/share/bsdconfig" 32. $BSDCFG_SHARE/common.subr || exit 1 33 34############################################################ GLOBALS 35 36# |
37# A Literal newline (for use with f_replace_all(), or IFS, or whatever) 38# 39NL=" 40" # END-QUOTE 41 42# |
|
37# Valid characters that can appear in an sh(1) variable name 38# 39# Please note that the character ranges A-Z and a-z should be avoided because 40# these can include accent characters (which are not valid in a variable name). 41# For example, A-Z matches any character that sorts after A but before Z, 42# including A and Z. Although ASCII order would make more sense, that is not 43# how it works. 44# --- 139 unchanged lines hidden (view full) --- 184 185# f_isinteger $arg 186# 187# Returns true if argument is a positive/negative whole integer. 188# 189f_isinteger() 190{ 191 local arg="${1#-}" | 43# Valid characters that can appear in an sh(1) variable name 44# 45# Please note that the character ranges A-Z and a-z should be avoided because 46# these can include accent characters (which are not valid in a variable name). 47# For example, A-Z matches any character that sorts after A but before Z, 48# including A and Z. Although ASCII order would make more sense, that is not 49# how it works. 50# --- 139 unchanged lines hidden (view full) --- 190 191# f_isinteger $arg 192# 193# Returns true if argument is a positive/negative whole integer. 194# 195f_isinteger() 196{ 197 local arg="${1#-}" |
192 [ "${arg:-x}" = "${arg#[!0-9]*}" ] | 198 [ "${arg:-x}" = "${arg%[!0-9]*}" ] |
193} 194 195# f_uriencode [$text] 196# 197# Encode $text for the purpose of embedding safely into a URL. Non-alphanumeric 198# characters are converted to `%XX' sequence where XX represents the hexa- 199# decimal ordinal of the non-alphanumeric character. If $text is missing, data 200# is instead read from standard input. --- 59 unchanged lines hidden (view full) --- 260 261# f_replaceall $string $find $replace [$var_to_set] 262# 263# Replace all occurrences of $find in $string with $replace. If $var_to_set is 264# either missing or NULL, the variable name is produced on standard out for 265# capturing in a sub-shell (which is less recommended due to performance 266# degradation). 267# | 199} 200 201# f_uriencode [$text] 202# 203# Encode $text for the purpose of embedding safely into a URL. Non-alphanumeric 204# characters are converted to `%XX' sequence where XX represents the hexa- 205# decimal ordinal of the non-alphanumeric character. If $text is missing, data 206# is instead read from standard input. --- 59 unchanged lines hidden (view full) --- 266 267# f_replaceall $string $find $replace [$var_to_set] 268# 269# Replace all occurrences of $find in $string with $replace. If $var_to_set is 270# either missing or NULL, the variable name is produced on standard out for 271# capturing in a sub-shell (which is less recommended due to performance 272# degradation). 273# |
274# To replace newlines or a sequence containing the newline character, use $NL 275# as `\n' is not supported. 276# |
|
268f_replaceall() 269{ 270 local __left="" __right="$1" 271 local __find="$2" __replace="$3" __var_to_set="$4" 272 while :; do 273 case "$__right" in *$__find*) 274 __left="$__left${__right%%$__find*}$__replace" 275 __right="${__right#*$__find}" --- 91 unchanged lines hidden (view full) --- 367# 3 Result too large to calculate 368# 369f_expand_number() 370{ 371 local __string="$1" __var_to_set="$2" 372 local __cp __num __bshift __maxinput 373 374 # Remove any leading non-digits | 277f_replaceall() 278{ 279 local __left="" __right="$1" 280 local __find="$2" __replace="$3" __var_to_set="$4" 281 while :; do 282 case "$__right" in *$__find*) 283 __left="$__left${__right%%$__find*}$__replace" 284 __right="${__right#*$__find}" --- 91 unchanged lines hidden (view full) --- 376# 3 Result too large to calculate 377# 378f_expand_number() 379{ 380 local __string="$1" __var_to_set="$2" 381 local __cp __num __bshift __maxinput 382 383 # Remove any leading non-digits |
375 while :; do 376 __cp="$__string" 377 __string="${__cp#[!0-9]}" 378 [ "$__string" = "$__cp" ] && break 379 done | 384 __string="${__string#${__string%%[0-9]*}}" |
380 | 385 |
386 # Store the numbers (no trailing suffix) 387 __num="${__string%%[!0-9]*}" 388 |
|
381 # Produce `-1' if string didn't contain any digits | 389 # Produce `-1' if string didn't contain any digits |
382 if [ ! "$__string" ]; then | 390 if [ ! "$__num" ]; then |
383 if [ "$__var_to_set" ]; then 384 setvar "$__var_to_set" -1 385 else 386 echo -1 387 fi 388 return 1 # 1 = "Given $string contains no digits" 389 fi 390 | 391 if [ "$__var_to_set" ]; then 392 setvar "$__var_to_set" -1 393 else 394 echo -1 395 fi 396 return 1 # 1 = "Given $string contains no digits" 397 fi 398 |
391 # Store the numbers 392 __num="${__string%%[!0-9]*}" 393 394 # Shortcut 395 if [ $__num -eq 0 ]; then 396 if [ "$__var_to_set" ]; then 397 setvar "$__var_to_set" 0 398 else 399 echo 0 400 fi 401 return $SUCCESS 402 fi 403 | |
404 # Remove all the leading numbers from the string to get at the prefix | 399 # Remove all the leading numbers from the string to get at the prefix |
405 while :; do 406 __cp="$__string" 407 __string="${__cp#[0-9]}" 408 [ "$__string" = "$__cp" ] && break 409 done | 400 __string="${__string#"$__num"}" |
410 411 # 412 # Test for invalid prefix (and determine bitshift length) 413 # 414 case "$__string" in 415 ""|[[:space:]]*) # Shortcut 416 if [ "$__var_to_set" ]; then 417 setvar "$__var_to_set" $__num --- 46 unchanged lines hidden --- | 401 402 # 403 # Test for invalid prefix (and determine bitshift length) 404 # 405 case "$__string" in 406 ""|[[:space:]]*) # Shortcut 407 if [ "$__var_to_set" ]; then 408 setvar "$__var_to_set" $__num --- 46 unchanged lines hidden --- |