Last active
May 26, 2022 23:11
-
-
Save jtanx/96ded5e050d5ee5b19804195ee5cf5f9 to your computer and use it in GitHub Desktop.
Padding a string in cmake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(pad_string output str padchar length) | |
string(LENGTH "${str}" _strlen) | |
math(EXPR _strlen "${length} - ${_strlen}") | |
if(_strlen GREATER 0) | |
if(${CMAKE_VERSION} VERSION_LESS "3.14") | |
unset(_pad) | |
foreach(_i RANGE 1 ${_strlen}) # inclusive | |
string(APPEND _pad ${padchar}) | |
endforeach() | |
else() | |
string(REPEAT ${padchar} ${_strlen} _pad) | |
endif() | |
string(APPEND str ${_pad}) | |
endif() | |
set(${output} "${str}" PARENT_SCOPE) | |
endfunction() |
Just one little mistake! You need to replace ${_pad}
with _pad
on line 12
A not so generic, but much shorter solution:
string(REGEX REPLACE "^.*(....)\$" "\\1" unicode "0000${unicode}")
also thanks @jtanx!!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks me