28.4 Parsing semantic version specifications

Function: mbfl_semver_parse SEMVER_SPEC SEMVER_PARSER PARSER_INPUT

Parse the semantic version specification in PARSER_INPUT using the configuration in SEMVER_PARSER and store the result in SEMVER_SPEC. When successful return true; otherwise return false.

If an error occurs while parsing: an error message string is stored in SEMVER_PARSER.

In the following example we parse a correct semantic version specification; with prerelease version; with build metadata.

mbfl_default_object_declare(SEMVER_SPEC)
mbfl_default_object_declare(SEMVER_PARSER)
mbfl_default_object_declare(PARSER_INPUT)
declare -i START_INDEX=0

mbfl_semver_parser_input_make    _(PARSER_INPUT) '1.2.3-alpha.1+x86-64' $START_INDEX
mbfl_semver_parser_make_default  _(SEMVER_PARSER)
mbfl_semver_parse _(SEMVER_SPEC) _(SEMVER_PARSER) _(PARSER_INPUT)

mbfl_declare_varref(MAJOR_NUMBER)
mbfl_declare_varref(BUILD_METADATA)

mbfl_semver_spec_major_number_var   _(MAJOR_NUMBER)   _(SEMVER_PARSER)
mbfl_semver_spec_build_metadata_var _(BUILD_METADATA) _(SEMVER_PARSER)

"$MAJOR_NUMBER"         ⇒ 1
"$BUILD_METADATA"       ⇒ x86-64
Function: mbfl_semver_split_prerelease_version _RV PRERELEASE_VERSION_SPEC

Split a PRERELEASE_VERSION_SPEC into its component identifiers and store them in the indexed array whose name is _RV. If successful return 0; otherwise return 1.

The string PRERELEASE_VERSION_SPEC must be a correct prerelease version specification, without the leading hyphen character, like the one produced by calling mbfl_semver_parse(). If the string has an incorrect format: the behaviour of this function is undefined.

In the following exmample we split a prerelease version specification:

mbfl_declare_index_array_varref(RV)

mbfl_semver_split_prerelease_version mbfl_datavar(RV) 'alpha.12.beta.34.gamma.56'

$?                      ⇒ 0
mbfl_slots_number(RV)   ⇒ 6
mbfl_slot_qref(RV, 0)   ⇒ alpha
mbfl_slot_qref(RV, 1)   ⇒ 12
mbfl_slot_qref(RV, 2)   ⇒ beta
mbfl_slot_qref(RV, 3)   ⇒ 34
mbfl_slot_qref(RV, 4)   ⇒ gamma
mbfl_slot_qref(RV, 5)   ⇒ 56

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