Does bash-like string expansion like ${foo:-bar}
Release Version | Release Notes |
---|---|
RELEASENOTES |
MulleBashStringExpansion does a subset of the bash string expansions. See Bash Manual - 3.5.3 Shell Parameter Expansion for a list of operations that are defined a string expansion and a description of what each operation does. If you are not familiar with it, its best to try out the bash expansion first, then this will become clearer:
Supported Expansion | Description |
---|---|
${parameter:-expr} |
Use expr, if parameter is empty. Expr can itself be a string expression. |
${parameter:=parameter2} |
Use parameter2 instead, if parameter is empty. |
${parameter:+expr} |
Use expr, if parameter is not empty. |
${parameter:offset} |
Returns substring from offset |
${parameter:offset:length} |
Returns substring of length from offset |
${ #parameter} |
Returns length of string |
${parameter#pattern} |
Removes pattern prefix |
${parameter##pattern} |
Removes all pattern prefixes |
${parameter%pattern} |
Removes pattern suffix |
${parameter%%pattern} |
Removes all pattern suffixes |
${parameter/pattern/word} |
Replaces occurrence of pattern with word |
${parameter//pattern/word} |
Replaces all occurrences of pattern with word |
${parameter^pattern} |
Uppercase first character matching pattern |
${parameter^^pattern} |
Uppercase all characters matching pattern |
${parameter,pattern} |
Lowercase first character matching pattern |
${parameter,,pattern} |
Lowercase all characters matching pattern |
MulleBashStringExpansion does NOT do $()
or $(())
or backticks.
There is no tilde expansion nor globbing. Double and singlequoted strings are
not recognized. Backslash escape sequences are honored though.
info = @{
@"a": @"vfL",
@"b": @"BOCHUM",
@"c": @(1949)
};
s = @"${a^} ${b,,[C-Z]} ${c//9/8}";
mulle_printf( "%@\n", [s mulleBashExpandWithDataSource:info]);
will print VfL Bochum 1848
.
Use of
${
#parameter}
is problematic in MulleScion scripts. It will be mis-understood as a comment token. You can use the pipe character as an alternative|
, so write${|parameter}
. This is not a problem for well-formed${parameter#pattern}
,${parameter##pattern}
,${parameter%pattern}
,${parameter%%pattern}
since an empty pattern is useless there.
The class named "S" provides with its instance methods, convenient shortcuts
to string replacement and bash like string expansion. For example -$:
does
bash like string expansion.
Method | Description |
---|---|
$:(NSString *) string |
Returns bash expanded string |
:(NSString *) string :(NSString *) pattern |
Does [string mulleRangeOfPattern:pattern] |
:(NSString *) string :(NSString *) pattern :(NSString *) replacement |
Does [string mulleStringByReplacingPattern:pattern withString:replacement] |
You can use :
for pure strings or $:
for bash expanded string expansion on
all parameters, @selector(:$::)
for example is defined.
You setup an instance of the class with a data-source, in this case a
NSDictionary
:
S *sInstance;
info = @{ @"a": @"vfL", @"b": @"BOCHUM", @"c": @(1949) };
sInstance = [S objectWithDataSource:info];
Now to expand a string from the data-source you use $:
:
expanded = [sInstance $:@"c//9/8"];
The S
class is most useful in MulleScion, to reduce the amount of type-work.
In MulleScion, the instance is actually also called S
and the class S
is
hidden. String replacement code in a template could look like this then:
Rename your executable file:
mv "{{ filename }}" "{{ [S :filename :".exe" :".bat"] }}"
Use mulle-sde to add MulleBashStringExpansion to your project:
mulle-sde add github:MulleWeb/MulleBashStringExpansion
Use mulle-sde to build and install MulleBashStringExpansion and all dependencies:
mulle-sde install --prefix /usr/local \
https://github.com//MulleBashStringExpansion/archive/latest.tar.gz
Install the requirements:
Requirements | Description |
---|---|
MulleFoundationBase | 🧱 MulleFoundationBase amalgamates Foundations projects |
mulle-objc-list | 📒 Lists mulle-objc runtime information contained in executables. |
Download the latest tar or zip archive and unpack it.
Install MulleBashStringExpansion into /usr/local
with cmake:
cmake -B build \
-DCMAKE_INSTALL_PREFIX=/usr/local \
-DCMAKE_PREFIX_PATH=/usr/local \
-DCMAKE_BUILD_TYPE=Release &&
cmake --build build --config Release &&
cmake --install build --config Release
All platforms and compilers supported by mulle-c11.
Nat! for Mulle kybernetiK