[CMake] FIND sub-command to the STRING command

Michael Wild themiwi at gmail.com
Fri Feb 4 07:32:17 EST 2011


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