cmakepp_lang/utilities/encode_special_chars

cmakepp_lang/utilities/encode_special_chars.cpp_encode_special_chars(argn return_argn)

Encodes special characters to protect them during function passes.

This function encodes special characters that need to be escaped in a CMake string to protect them while being passed as function parameters through multiple functions. It is assumed that this will be called on the arguments immediately after they are passed into the first function in a series of function calls, protecting the special characters until they are decoded at their destination.

This is necessary because CMake removes the backslashes escaping characters from strings as they are passed as a function or macro parameters. Encoding special characters removes the burden on the user to have to add backslashes based on the various function calls being performed in the background when an object method is called.

Specifically, the special characters handled are $;"\\.

Parameters:
  • argn (list) – The argument list. This should have at least one string in it, otherwise this function will have nothing to encode.

  • return_argn (list) – Return variable for the encoded argument list.

Returns:

The list of arguments with special characters encoded.

Return type:

list

Example Usage

This function is intended to be called near the top of a function call chain where arguments will be passed through multiple levels of function calls. This ensures that special characters are not altered and do not cause unintended side effects while being passed through functions and macros.

The special characters need to be decoded again upon reaching their destination.

include(cmakepp_lang/asserts/signature)
function(my_fxn a_str a_bool)
    cpp_encode_special_chars("${ARGN}" encoded_args) # Encode the arguments
    do_stuff(encoded_args)
endfunction()

function(do_stuff encoded_arg_list)
    cpp_decode_special_chars("${encoded_arg_list}" decoded_args)
    # Do stuff with decoded_args
endfunction()

The only argument to this function should always be "${ARGN}".