MantisBT - CMake
View Issue Details
0011795CMakeCMakepublic2011-02-04 11:262011-06-17 18:25
Tim Hütz 
Brad King 
normalminorN/A
closedfixed 
 
CMake 2.8.5CMake 2.8.5 
0011795: Extending the STRING command with a FIND sub-command
Recently I was searching for a CMake option to get the position of a single character (first appearance) out of a string. As I found nothing which could provide me an easy solution, I pulled the latest development version and added a FIND sub-command to the STRING command.

The attached patch supports a forward search (finds the first occurrence of the substring) and a reverse search (which finds the last occurrence of a substring).

Here is a sample how the sub-commands works:

-----

project( "STRING FIND command" )
cmake_minimum_required(VERSION 2.8)

set( TESTSTRING "This is a test and here is another test." )
string( FIND ${TESTSTRING} "a" APOSITION )
string( FIND REVERSE ${TESTSTRING} "a" APOSITIONREV )
message( STATUS "Firt position of character 'a' (8) is ${APOSITION}" )
message( STATUS "Last position of character 'a' (27) is ${APOSITIONREV}" )

-----

I've already discussed this sub-command on the CMake mailing list (http://www.cmake.org/pipermail/cmake/2011-February/042521.html [^]).

Maybe, this would be a nice feature for one of the next CMake releases.

Best regards,
Tim
No tags attached.
patch 0002-Added-tests-for-the-FIND-sub-command-of-STRING.patch (4,051) 2011-02-15 05:27
https://public.kitware.com/Bug/file/3695/0002-Added-tests-for-the-FIND-sub-command-of-STRING.patch
patch 0001-Added-a-new-STRING-sub-command-called-FIND.patch (4,694) 2011-02-15 09:15
https://public.kitware.com/Bug/file/3697/0001-Added-a-new-STRING-sub-command-called-FIND.patch
patch 0003-Updated-the-tests-for-the-new-signature-of-STRING-FI.patch (4,281) 2011-02-15 11:52
https://public.kitware.com/Bug/file/3701/0003-Updated-the-tests-for-the-new-signature-of-STRING-FI.patch
patch 0004-Changed-the-signature-of-the-FIND-sub-command.patch (2,921) 2011-02-15 11:52
https://public.kitware.com/Bug/file/3702/0004-Changed-the-signature-of-the-FIND-sub-command.patch
Issue History
2011-02-04 11:26Tim HützNew Issue
2011-02-04 11:26Tim HützFile Added: 0001-Added-a-new-STRING-sub-command-called-FIND.patch
2011-02-04 12:52Tim HützNote Added: 0025286
2011-02-04 12:52Tim HützNote Edited: 0025286bug_revision_view_page.php?bugnote_id=25286#r188
2011-02-14 16:46Brad KingNote Added: 0025426
2011-02-15 05:27Tim HützFile Added: 0002-Added-tests-for-the-FIND-sub-command-of-STRING.patch
2011-02-15 05:30Tim HützNote Added: 0025428
2011-02-15 07:43Brad KingFile Deleted: 0001-Added-a-new-STRING-sub-command-called-FIND.patch
2011-02-15 07:44Brad KingNote Added: 0025430
2011-02-15 09:15Tim HützFile Added: 0001-Added-a-new-STRING-sub-command-called-FIND.patch
2011-02-15 09:16Tim HützNote Added: 0025433
2011-02-15 09:29Brad KingAssigned To => Brad King
2011-02-15 09:29Brad KingStatusnew => assigned
2011-02-15 09:35Brad KingNote Added: 0025435
2011-02-15 11:52Tim HützFile Added: 0003-Updated-the-tests-for-the-new-signature-of-STRING-FI.patch
2011-02-15 11:52Tim HützFile Added: 0004-Changed-the-signature-of-the-FIND-sub-command.patch
2011-02-15 11:53Tim HützNote Added: 0025439
2011-02-15 11:55Tim HützNote Edited: 0025439bug_revision_view_page.php?bugnote_id=25439#r201
2011-02-15 13:51Brad KingNote Added: 0025444
2011-02-15 13:51Brad KingStatusassigned => closed
2011-02-15 13:51Brad KingResolutionopen => fixed
2011-02-16 11:40David ColeTarget Version => CMake 2.8.5
2011-06-17 18:25David ColeFixed in Version => CMake 2.8.5

Notes
(0025286)
Tim Hütz   
2011-02-04 12:52   
Sorry, I've posted the wrong link. The correct one is http://www.cmake.org/pipermail/cmake/2011-February/042523.html [^]

(0025426)
Brad King   
2011-02-14 16:46   
Please add a test for this feature. You can probably modify one of these two files:

  Tests/CMakeTests/StringTest.cmake.in
  Tests/StringFileTest/CMakeLists.txt

Thanks.
(0025428)
Tim Hütz   
2011-02-15 05:30   
Hey Brad,

I've created several test cases and attached a patch for them. While I was writing those tests, I found some bugs in my code and I fixed them. Unfortunately I cannot delete the old patch (0001) to submit the new one. Am I not allowed to delete my own attachments or am I just blind to finds such a function? As soon as the old patch is gone, I'll upload the fixed one.

Best regards,
Tim
(0025430)
Brad King   
2011-02-15 07:44   
I deleted the old patch for you. I think the issue tracker is configured to only allow developers to delete attachments. I don't think it cares who created them. Typically folks just rename the file with -2 at the end or something to upload a new one.
(0025433)
Tim Hütz   
2011-02-15 09:16   
Thanks for deleting the old patch. I've uploaded the new one (with the included bug fixes).
(0025435)
Brad King   
2011-02-15 09:35   
One of the problems with CMake's ad-hoc keyword-based arguments is ambiguity so care needs to be taken in new command signatures. What happens if I want to search for the string "REVERSE"? I can imagine people in the future wanting "find starting at position N" and other things that come from the std::string interface. While string processing is not a goal of the CMake language I'd like to leave the interface open to such extensions unambiguously.

Perhaps the signature should be

  string(FIND <str> <substr> <outvar> [REVERSE] [future options here...])
(0025439)
Tim Hütz   
2011-02-15 11:53   
(edited on: 2011-02-15 11:55)
Thanks for your suggestion. I updated the tests and the sub-command implementation itself. The two new patches base on the previous two. Now the command signature is as you suggested:

string(FIND <str> <substr> <outvar> [REVERSE])

(0025444)
Brad King   
2011-02-15 13:51   
Applied with minor edits:

  http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=1462561a [^]

Thanks for your work!