Qual è il modo migliore per unire una lista in CMake in una stringa?Il modo migliore/più breve per unire un elenco in CMake
Unendomi intendo convertire SET (somelist "a" "b" "c \; c") a "a: b: c; c" dove è possibile selezionare la stringa di colla (":"). Il seguente codice funziona ma è VERAMENTE lungo, c'è un modo migliore?
FUNCTION(JOIN LISTNAME GLUE OUTPUT)
SET(_TMP_STR "")
FOREACH(VAL ${${LISTNAME}})
SET(_TMP_STR "${_TMP_STR}${GLUE}${VAL}")
ENDFOREACH(VAL ${${LISTNAME}})
STRING(LENGTH "${GLUE}" GLUE_LEN)
STRING(LENGTH "${_TMP_STR}" OUT_LEN)
MATH(EXPR OUT_LEN ${OUT_LEN}-${GLUE_LEN})
STRING(SUBSTRING "${_TMP_STR}" ${GLUE_LEN} ${OUT_LEN} _TMP_STR)
SET(${OUTPUT} "${_TMP_STR}" PARENT_SCOPE)
ENDFUNCTION()
#USAGE:
SET(somelist "a" "b" "c\;c")
JOIN(somelist ":" output)
MESSAGE("${output}") # will output "a:b:c;c"
Purtroppo utilizzando STRING (REPLACE ...) non funziona:
function(JOINSTRREPLACE VALUES GLUE OUTPUT)
string (REPLACE ";" "${GLUE}" _TMP_STR "${VALUES}")
set (${OUTPUT} "${_TMP_STR}" PARENT_SCOPE)
endfunction()
JOINSTRREPLACE("${somelist}" ":" output)
MESSAGE(${output}) # will output "a:b:c\:c"
Oh sì, grazie, questo funziona. Ma questo significa che non posso avere una lista in cui un elemento contiene un punto e virgola? – Geli
Ho aggiornato la domanda per includere un elenco con elementi che contengono il punto e virgola. – Geli
La funzione originale JOIN non gestisce nemmeno il punto e virgola. Emette "a: b: cc" sotto Windows con cmake 2.8.5. – sakra