Skip to content

Instantly share code, notes, and snippets.

@jtanx
Last active May 26, 2022 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtanx/96ded5e050d5ee5b19804195ee5cf5f9 to your computer and use it in GitHub Desktop.
Save jtanx/96ded5e050d5ee5b19804195ee5cf5f9 to your computer and use it in GitHub Desktop.
Padding a string in cmake
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()
@jtanx
Copy link
Author

jtanx commented Feb 14, 2021

thanks me

@a-shahba
Copy link

a-shahba commented Mar 7, 2021

Just one little mistake! You need to replace ${_pad} with _pad on line 12

@hasselmm
Copy link

A not so generic, but much shorter solution:

string(REGEX REPLACE "^.*(....)\$" "\\1" unicode "0000${unicode}")

@jamiecook
Copy link

also thanks @jtanx!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment