Previous: , Up: strings   [Contents][Index]


14.7 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
Function: mbfl_string_strip_carriage_return_var _RV LINE

When exchanging strings with a remote process through a network socket we, usually, need to end a line with a carriage return plus line feed sequence. If we use the command read to read a line from a socket: we can instruct it to discard the ending line feed, yielding us a line ending with a carriage return.

This function strips the ending carriage return from LINE, if any, and stores the result in the result variable _RV. It is fine if LINE is the empty string.


Previous: , Up: strings   [Contents][Index]

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