[CMake] FIND sub-command to the STRING command

Michael Wild themiwi at gmail.com
Fri Feb 4 10:02:19 EST 2011


Exactly. The single-character matching would then just be a special
case. If you want to get fancy, having a string(FIND REGEX) command
would also be nice ;-)

Michael

On 02/04/2011 03:31 PM, Tim Hütz wrote:
> Mhh, okay. With substring matching you mean that I can get the position of the first occurrence of a word or sub-sentence, am I right?
> 
> 
> Am 04.02.2011 um 13:32 schrieb Michael Wild:
> 
>> On 02/04/2011 12:49 PM, Tim Hütz wrote:
>>> I don't think that the speed-factor was important in this case. A complete CMake run takes about 25 seconds to run (in my case) and I see no influence in the processing time with my suggested patch or the regular expression sample provided by Michael. I thought that my solution is a bit more intuitive, but I don't know if this is really important for CMake. If you use the provided patch, you can do something like
>>>
>>> PROJECT( "STRING FIND command" )
>>> SET( TESTSTRING "This is a test" )
>>> STRING( FIND ${TESTSTRING} "a" APOSITION )
>>> MESSAGE( STATUS "Position of character 'a' is ${APOSITION}" )
>>>
>>> Regards,
>>> Tim
>>>
>>> Am 04.02.2011 um 12:00 schrieb SF Markus Elfring:
>>>
>>>>> mhh, yeah, thats a good idea. Ok, I'll use this for doing a "STRING( FIND ...)" :) It was just an idea to share this patch with you, but you're right. Its easier doing this with regular expressions.
>>>>
>>>> Can it be that your implementation was faster than a complete regex application for your use case?
>>>>
>>>> Are there any remarkable differences in the corresponding execution speed and performance characteristics?
>>>>
>>>> Regards,
>>
>> I just wanted to point out that this particular use-case can be handled
>> by a regular expression. I still think that a string(FIND) command would
>> be useful, although it would be even more so if it did substring matching.
>>
>> BTW, I just had an idea how to find substrings with regexes, but it is
>> even more tedious:
>>
>> set(str "Hello Tim. How are you Tim?")
>> set(pos -1)
>> if(str MATCHES "(Tim.*)")
>>  string(LENGTH "${str}" l1)
>>  string(LENGTH "${CMAKE_MATCH_1}" l2)
>>  math(EXPR pos "${l1}-${l2}")
>> endif()
>>
>> This finds the position of the first occurence of "Tim". Another
>> alternative, without math(EXPR) is:
>>
>> set(str "Hello Tim. How are you Tim?")
>> set(pos -1)
>> string(REGEX REPLACE "Ali.*" "" tmp "${str}")
>> if(NOT "${tmp}" STREQUAL "${str}")
>>  string(LENGTH "${tmp}" pos)
>> endif()
>>
>> Michael
>>


More information about the CMake mailing list