Skip to content

Instantly share code, notes, and snippets.

@jtanx
Last active May 26, 2022 23:11
Show Gist options
  • 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()
@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