CMake:遍历字符串并获取前三个数字 [英] CMake: Looping through a string and getting the first three digits

查看:459
本文介绍了CMake:遍历字符串并获取前三个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个执行以下操作的CMake脚本:

I'm trying to write a CMake script that does the following:

使用execute_process指令读取命令的输出,并将其存储到名为"STRING_VARIABLE"的变量中.该命令返回名称中同时包含字符和数字的内容.像这样:RESULT-v1.2.8 -...

Using the execute_process instruction reads the output of a command and stores it into a variable named 'STRING_VARIABLE'. The command that returns something that has both characters and digits in it's name. Something like this: RESULT-v1.2.8-...

我已正确读取此值,并将其显示在终端上以确认. 现在,我要做的是存储此输出的前三位数字,并将它们存储在其他3个变量中:"FIRST_DIGIT","SECOND_DIGIT"和"THIRD_DIGIT".

I have read this value properly and displayed it on the terminal to confirm this. Now what I want to do is store the first three digits of this output and store them in 3 other variables: 'FIRST_DIGIT', 'SECOND_DIGIT' and 'THIRD_DIGIT'.

我的逻辑是这样的: 每次在变量名中遇到数字时,都使用计数器计数.每次遇到数字时,将数字存储在三个变量之一中,然后使计数器递增.因此,计数器从0到2进行计数,并且对于这3个值中的每一个进行存储.

My logic was this: Using a counter count each time a digit is encountered in a variable name. Each time a digit is encountered store the digit in one of the three variables then increment the counter. The counter counts therefore from 0 to 2 and for each of these 3 values does a store.

这是我写的脚本:

SET(COUNTER 0)
foreach(LETTER ${STRING_VARIABLE})
       if(LETTER EQUAL '0,1,2,3,4,5,6,7,8,9')

        if( COUNTER EQUAL 0 )                                 # if first digit is encountered
         list(GET STRING_VARIABLE LETTER FIRST_DIGIT)        # store it in FIRST_DIGIT
         SET(COUNTER 1) 
        elseif( COUNTER EQUAL 1 )                             # if second digit is encountered
         list(GET STRING_VARIABLE LETTER SECOND_DIGIT)        # store it in SECOND_DIGIT
         SET(COUNTER 2)
        else( COUNTER EQUAL 2 )                               # if second digit is encountered
         list(GET STRING_VARIABLE LETTER THIRD_DIGIT)        # store it in THIRD_DIGIT
        endif()

       endif()

endforeach()

# To check the variables

#message("*****${STRING_VARIABLE}") # OK!
message("*****${FIRST_DIGIT}")      # NOT OK :(

由于我是CMake的初学者,所以我想我的问题出在这两者(或两者兼有)中: -遍历"STRING_VARIABLE"时,我使用了foreach(LETTER),并且由于我的字符串也包含数字,因此程序可能看不到它们.如果那是错误的话,我还应该替换LETTER以获得字符串的每个字符吗? -在第一个中,如果我检查字母"是否为数字.我不确定这是正确的语法,但我不确定.基本上,我在这里正在尝试检查每个索引处的字母是否为数字.

As I'm a total beginner in CMake I suppose my problem is at either of the two(or both): - When looping through the 'STRING_VARIABLE' I used foreach(LETTER) and since my string also contains digits the program may not see them. If that is the mistake with what else should I replace LETTER in order to get each character of the string? - In the first if where I check if the 'LETTER' is a digit. I think that is the correct syntax altough I'm not sure. Basically what I'm doing there is trying to check if the letter at each index is a digit.

正如我所说,"STRING_VARIABLE"可以正常打印. 但是,当我尝试打印"FIRST_DIGIT"或其他3个(第二个和第三个)时,结果是一个空字符串. 请帮助我了解我的逻辑上有什么问题以及我做错了什么.

The 'STRING_VARIABLE' prints ok as I said. However when I try printing the 'FIRST_DIGIT' or any other of the 3(second and third) I get an empty string as a result. Please help me understand what is wrong in my logic and what I'm doing wrong.

请帮助我了解我在做什么错.谢谢.

Please help me understand what I'm doing wrong. Thank you.

推荐答案

如果知道格式,则可以使用字符串(REGEX REPLACE ...).

In case the format is know, you can use string(REGEX REPLACE ...).

功能:

function(get_versions versionstring libname major minor patch)
    string(REGEX REPLACE "([A-Za-z0-9_]*)-[vV].*" "\\1" locallibname ${versionstring} )
    set(libname ${locallibname} PARENT_SCOPE)
    string(REGEX REPLACE "^([A-Za-z0-9_]*-[vV])([0-9]*)([.][0-9]*[.][0-9]*-?.*)$" "\\2" numbers ${versionstring} )
    set(major ${numbers} PARENT_SCOPE)
    string(REGEX REPLACE "^([A-Za-z0-9_]*-[vV][0-9]*[.])([0-9]*)([.][0-9]*-?.*)$" "\\2" numbers ${versionstring} )
    set(minor ${numbers} PARENT_SCOPE)
    string(REGEX REPLACE "^([A-Za-z0-9_]*-[vV][0-9]*[.][0-9]*[.])([0-9]*)(-?.*)$" "\\2" numbers ${versionstring} )
    set(patch ${numbers} PARENT_SCOPE)
endfunction()

用法:

get_versions("MyLib-V11.222.034-remark" libname major minor patch)
status_ref(libname)
status_ref(major)
status_ref(minor)
status_ref(patch)

结果:

STATUS: libname = "MyLib"
STATUS: major = "11"
STATUS: minor = "222"
STATUS: patch = "034"

这篇关于CMake:遍历字符串并获取前三个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆