14.10 Miscellaneous functions

Function: mbfl_string_replace STRING PATTERN
Function: mbfl_string_replace STRING PATTERN SUBST
Function: mbfl_string_replace_var _RV STRING PATTERN
Function: mbfl_string_replace_var _RV STRING PATTERN SUBST

Replace all the occurrences of PATTERN in STRING with SUBST, then print the result. If not used, SUBST defaults to the empty string.

The function variant _var stores the result in the variable _RV, rather than print it; Result variables.

mbfl_string_replace 'abcdefg' 'cde' '123'
-| 'ab123fg'

local _RV
mbfl_string_replace_var _RV 'abcdefg' 'cde' '123'
"$_RV" ⇒ ab123fg
Function: mbfl_sprintf VARNAME FORMAT ...

Make use of printf to format the string FORMAT with the additional arguments, then store the result in VARNAME: If this name is local in the scope of the caller, this has the effect of filling the variable in that scope.

NOTE We should use the built–in printf -v VARNAME construct, rather than this function.

Function: mbfl_string_skip STRING VARNAME CHAR

Skip all the characters in a string equal to CHAR. VARNAME is the name of a variable in the scope of the caller: Its value is the offset of the first character to test in STRING. The offset is incremented until a char different from CHAR is found, then the value of VARNAME is updated to the position of the different char. If the initial value of the offset corresponds to a char equal to CHAR, the variable is left untouched. Return true.

local -i i

# The char "z" is not present in the string: leave "i" untouched.
i=3
;;               0123456
mbfl_string_skip abcdefg i z
$i ⇒ 3

# The whole string  is filled with "a": increment "i" to
# the length of the string itself.
i=3
;;               0123456
mbfl_string_skip aaaaaaa i a
$i ⇒ 7

# Skip until the index of the "d" character.
i=3
;;               01234567890
mbfl_string_skip abcccccdefg i c
$i ⇒ 7

This document describes version 3.0.0-devel.9 of Marcos Bash Functions Library.