diff options
-rw-r--r-- | Help/command/string.rst | 11 | ||||
-rw-r--r-- | Help/release/dev/string-hex.rst | 5 | ||||
-rw-r--r-- | Source/cmStringCommand.cxx | 22 | ||||
-rw-r--r-- | Tests/RunCMake/string/Hex.cmake | 20 | ||||
-rw-r--r-- | Tests/RunCMake/string/HexNotEnoughArgs-result.txt | 1 | ||||
-rw-r--r-- | Tests/RunCMake/string/HexNotEnoughArgs-stderr.txt | 4 | ||||
-rw-r--r-- | Tests/RunCMake/string/HexNotEnoughArgs.cmake | 1 | ||||
-rw-r--r-- | Tests/RunCMake/string/HexTooManyArgs-result.txt | 1 | ||||
-rw-r--r-- | Tests/RunCMake/string/HexTooManyArgs-stderr.txt | 4 | ||||
-rw-r--r-- | Tests/RunCMake/string/HexTooManyArgs.cmake | 1 | ||||
-rw-r--r-- | Tests/RunCMake/string/RunCMakeTest.cmake | 4 |
11 files changed, 74 insertions, 0 deletions
diff --git a/Help/command/string.rst b/Help/command/string.rst index ba7c656..cfcf914 100644 --- a/Help/command/string.rst +++ b/Help/command/string.rst @@ -36,6 +36,7 @@ Synopsis `Generation`_ string(`ASCII`_ <number>... <out-var>) + string(`HEX`_ <string> <out-var>) string(`CONFIGURE`_ <string> <out-var> [...]) string(`MAKE_C_IDENTIFIER`_ <string> <out-var>) string(`RANDOM`_ [<option>...] <out-var>) @@ -355,6 +356,16 @@ Generation Convert all numbers into corresponding ASCII characters. +.. _HEX: + +.. code-block:: cmake + + string(HEX <string> <output_variable>) + +Convert each byte in the input ``<string>`` to its hexadecimal representation +and store the concatenated hex digits in the ``<output_variable>``. Letters in +the output (``a`` through ``f``) are in lowercase. + .. _CONFIGURE: .. code-block:: cmake diff --git a/Help/release/dev/string-hex.rst b/Help/release/dev/string-hex.rst new file mode 100644 index 0000000..f220aca --- /dev/null +++ b/Help/release/dev/string-hex.rst @@ -0,0 +1,5 @@ +string-hex +---------- + +* The :command:`string` command learned a new ``HEX`` sub-command, which + converts strings into their hexadecimal representation. diff --git a/Source/cmStringCommand.cxx b/Source/cmStringCommand.cxx index 9212195..7662204 100644 --- a/Source/cmStringCommand.cxx +++ b/Source/cmStringCommand.cxx @@ -124,6 +124,27 @@ bool HandleAsciiCommand(std::vector<std::string> const& args, return true; } +bool HandleHexCommand(std::vector<std::string> const& args, + cmExecutionStatus& status) +{ + if (args.size() != 3) { + status.SetError("Incorrect number of arguments"); + return false; + } + auto const& instr = args[1]; + auto const& outvar = args[2]; + std::string output(instr.size() * 2, ' '); + + std::string::size_type hexIndex = 0; + for (auto const& c : instr) { + sprintf(&output[hexIndex], "%.2x", static_cast<unsigned char>(c) & 0xFF); + hexIndex += 2; + } + + status.GetMakefile().AddDefinition(outvar, output); + return true; +} + bool HandleConfigureCommand(std::vector<std::string> const& args, cmExecutionStatus& status) { @@ -936,6 +957,7 @@ bool cmStringCommand(std::vector<std::string> const& args, { "TOUPPER"_s, HandleToUpperCommand }, { "COMPARE"_s, HandleCompareCommand }, { "ASCII"_s, HandleAsciiCommand }, + { "HEX"_s, HandleHexCommand }, { "CONFIGURE"_s, HandleConfigureCommand }, { "LENGTH"_s, HandleLengthCommand }, { "APPEND"_s, HandleAppendCommand }, diff --git a/Tests/RunCMake/string/Hex.cmake b/Tests/RunCMake/string/Hex.cmake new file mode 100644 index 0000000..1c5aaa1 --- /dev/null +++ b/Tests/RunCMake/string/Hex.cmake @@ -0,0 +1,20 @@ +function(assert_strequal input actual expected) + if(NOT expected STREQUAL actual) + message(SEND_ERROR "Output did not match expected\nInput string:\n ${input}\nExpected:\n ${expected}\nActual:\n ${actual}") + endif() +endfunction() + +set(_input1 "The quick brown fox jumps over the lazy dog.") +string(HEX "${_input1}" _result1) +assert_strequal("${_input1}" "${_result1}" "54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f672e") + +set(_input2 "Hello world!") +string(HEX "${_input2}" _result2) +assert_strequal("${_input2}" "${_result2}" "48656c6c6f20776f726c6421") + +set(_input3 "Ash nazg durbatulûk\nAsh nazg gimbatul\nAsh nazg thrakatulûk\nAgh burzum-ishi krimpatul") +string(HEX "${_input3}" _result3) +assert_strequal("${_input3}" "${_result3}" "417368206e617a6720647572626174756cc3bb6b0a417368206e617a672067696d626174756c0a417368206e617a6720746872616b6174756cc3bb6b0a416768206275727a756d2d69736869206b72696d706174756c") + +string(HEX "" _result_empty) +assert_strequal("" "${_result_empty}" "") diff --git a/Tests/RunCMake/string/HexNotEnoughArgs-result.txt b/Tests/RunCMake/string/HexNotEnoughArgs-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/string/HexNotEnoughArgs-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/string/HexNotEnoughArgs-stderr.txt b/Tests/RunCMake/string/HexNotEnoughArgs-stderr.txt new file mode 100644 index 0000000..444f79d --- /dev/null +++ b/Tests/RunCMake/string/HexNotEnoughArgs-stderr.txt @@ -0,0 +1,4 @@ +^CMake Error at HexNotEnoughArgs\.cmake:[0-9]+ \(string\): + string Incorrect number of arguments +Call Stack \(most recent call first\): + CMakeLists\.txt:3 \(include\)$ diff --git a/Tests/RunCMake/string/HexNotEnoughArgs.cmake b/Tests/RunCMake/string/HexNotEnoughArgs.cmake new file mode 100644 index 0000000..7002af0 --- /dev/null +++ b/Tests/RunCMake/string/HexNotEnoughArgs.cmake @@ -0,0 +1 @@ +string(HEX "Hello world!") diff --git a/Tests/RunCMake/string/HexTooManyArgs-result.txt b/Tests/RunCMake/string/HexTooManyArgs-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/string/HexTooManyArgs-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/string/HexTooManyArgs-stderr.txt b/Tests/RunCMake/string/HexTooManyArgs-stderr.txt new file mode 100644 index 0000000..91b40ed --- /dev/null +++ b/Tests/RunCMake/string/HexTooManyArgs-stderr.txt @@ -0,0 +1,4 @@ +^CMake Error at HexTooManyArgs\.cmake:[0-9]+ \(string\): + string Incorrect number of arguments +Call Stack \(most recent call first\): + CMakeLists\.txt:3 \(include\)$ diff --git a/Tests/RunCMake/string/HexTooManyArgs.cmake b/Tests/RunCMake/string/HexTooManyArgs.cmake new file mode 100644 index 0000000..8986cf8 --- /dev/null +++ b/Tests/RunCMake/string/HexTooManyArgs.cmake @@ -0,0 +1 @@ +string(HEX "Hello world!" _output bad) diff --git a/Tests/RunCMake/string/RunCMakeTest.cmake b/Tests/RunCMake/string/RunCMakeTest.cmake index c432b4e..bb7cb17 100644 --- a/Tests/RunCMake/string/RunCMakeTest.cmake +++ b/Tests/RunCMake/string/RunCMakeTest.cmake @@ -37,3 +37,7 @@ run_cmake(UTF-32LE) run_cmake(Repeat) run_cmake(RepeatNoArgs) run_cmake(RepeatNegativeCount) + +run_cmake(Hex) +run_cmake(HexTooManyArgs) +run_cmake(HexNotEnoughArgs) |