string.rst 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. string
  2. ------
  3. .. only:: html
  4. .. contents::
  5. String operations.
  6. Search and Replace
  7. ^^^^^^^^^^^^^^^^^^
  8. FIND
  9. """"
  10. ::
  11. string(FIND <string> <substring> <output variable> [REVERSE])
  12. Return the position where the given substring was found in
  13. the supplied string. If the ``REVERSE`` flag was used, the command will
  14. search for the position of the last occurrence of the specified
  15. substring. If the substring is not found, a position of -1 is returned.
  16. REPLACE
  17. """""""
  18. ::
  19. string(REPLACE <match_string>
  20. <replace_string> <output variable>
  21. <input> [<input>...])
  22. Replace all occurrences of ``match_string`` in the input
  23. with ``replace_string`` and store the result in the output.
  24. Regular Expressions
  25. ^^^^^^^^^^^^^^^^^^^
  26. REGEX MATCH
  27. """""""""""
  28. ::
  29. string(REGEX MATCH <regular_expression>
  30. <output variable> <input> [<input>...])
  31. Match the regular expression once and store the match in the output variable.
  32. REGEX MATCHALL
  33. """"""""""""""
  34. ::
  35. string(REGEX MATCHALL <regular_expression>
  36. <output variable> <input> [<input>...])
  37. Match the regular expression as many times as possible and store the matches
  38. in the output variable as a list.
  39. REGEX REPLACE
  40. """""""""""""
  41. ::
  42. string(REGEX REPLACE <regular_expression>
  43. <replace_expression> <output variable>
  44. <input> [<input>...])
  45. Match the regular expression as many times as possible and substitute the
  46. replacement expression for the match in the output.
  47. The replace expression may refer to paren-delimited subexpressions of the
  48. match using ``\1``, ``\2``, ..., ``\9``. Note that two backslashes (``\\1``)
  49. are required in CMake code to get a backslash through argument parsing.
  50. Regex Specification
  51. """""""""""""""""""
  52. The following characters have special meaning in regular expressions:
  53. ::
  54. ^ Matches at beginning of input
  55. $ Matches at end of input
  56. . Matches any single character
  57. [ ] Matches any character(s) inside the brackets
  58. [^ ] Matches any character(s) not inside the brackets
  59. - Inside brackets, specifies an inclusive range between
  60. characters on either side e.g. [a-f] is [abcdef]
  61. To match a literal - using brackets, make it the first
  62. or the last character e.g. [+*/-] matches basic
  63. mathematical operators.
  64. * Matches preceding pattern zero or more times
  65. + Matches preceding pattern one or more times
  66. ? Matches preceding pattern zero or once only
  67. | Matches a pattern on either side of the |
  68. () Saves a matched subexpression, which can be referenced
  69. in the REGEX REPLACE operation. Additionally it is saved
  70. by all regular expression-related commands, including
  71. e.g. if( MATCHES ), in the variables CMAKE_MATCH_(0..9).
  72. ``*``, ``+`` and ``?`` have higher precedence than concatenation. ``|``
  73. has lower precedence than concatenation. This means that the regular
  74. expression ``^ab+d$`` matches ``abbd`` but not ``ababd``, and the regular
  75. expression ``^(ab|cd)$`` matches ``ab`` but not ``abd``.
  76. Manipulation
  77. ^^^^^^^^^^^^
  78. APPEND
  79. """"""
  80. ::
  81. string(APPEND <string variable> [<input>...])
  82. Append all the input arguments to the string.
  83. CONCAT
  84. """"""
  85. ::
  86. string(CONCAT <output variable> [<input>...])
  87. Concatenate all the input arguments together and store
  88. the result in the named output variable.
  89. TOLOWER
  90. """""""
  91. ::
  92. string(TOLOWER <string1> <output variable>)
  93. Convert string to lower characters.
  94. TOUPPER
  95. """""""
  96. ::
  97. string(TOUPPER <string1> <output variable>)
  98. Convert string to upper characters.
  99. LENGTH
  100. """"""
  101. ::
  102. string(LENGTH <string> <output variable>)
  103. Store in an output variable a given string's length.
  104. SUBSTRING
  105. """""""""
  106. ::
  107. string(SUBSTRING <string> <begin> <length> <output variable>)
  108. Store in an output variable a substring of a given string. If length is
  109. ``-1`` the remainder of the string starting at begin will be returned.
  110. If string is shorter than length then end of string is used instead.
  111. .. note::
  112. CMake 3.1 and below reported an error if length pointed past
  113. the end of string.
  114. STRIP
  115. """""
  116. ::
  117. string(STRIP <string> <output variable>)
  118. Store in an output variable a substring of a given string with leading and
  119. trailing spaces removed.
  120. GENEX_STRIP
  121. """""""""""
  122. ::
  123. string(GENEX_STRIP <input string> <output variable>)
  124. Strip any :manual:`generator expressions <cmake-generator-expressions(7)>`
  125. from the ``input string`` and store the result in the ``output variable``.
  126. Comparison
  127. ^^^^^^^^^^
  128. ::
  129. string(COMPARE EQUAL <string1> <string2> <output variable>)
  130. string(COMPARE NOTEQUAL <string1> <string2> <output variable>)
  131. string(COMPARE LESS <string1> <string2> <output variable>)
  132. string(COMPARE GREATER <string1> <string2> <output variable>)
  133. Compare the strings and store true or false in the output variable.
  134. Hashing
  135. ^^^^^^^
  136. ::
  137. string(<MD5|SHA1|SHA224|SHA256|SHA384|SHA512>
  138. <output variable> <input>)
  139. Compute a cryptographic hash of the input string.
  140. Generation
  141. ^^^^^^^^^^
  142. ASCII
  143. """""
  144. ::
  145. string(ASCII <number> [<number> ...] <output variable>)
  146. Convert all numbers into corresponding ASCII characters.
  147. CONFIGURE
  148. """""""""
  149. ::
  150. string(CONFIGURE <string1> <output variable>
  151. [@ONLY] [ESCAPE_QUOTES])
  152. Transform a string like :command:`configure_file` transforms a file.
  153. RANDOM
  154. """"""
  155. ::
  156. string(RANDOM [LENGTH <length>] [ALPHABET <alphabet>]
  157. [RANDOM_SEED <seed>] <output variable>)
  158. Return a random string of given length consisting of
  159. characters from the given alphabet. Default length is 5 characters
  160. and default alphabet is all numbers and upper and lower case letters.
  161. If an integer ``RANDOM_SEED`` is given, its value will be used to seed the
  162. random number generator.
  163. TIMESTAMP
  164. """""""""
  165. ::
  166. string(TIMESTAMP <output variable> [<format string>] [UTC])
  167. Write a string representation of the current date
  168. and/or time to the output variable.
  169. Should the command be unable to obtain a timestamp the output variable
  170. will be set to the empty string "".
  171. The optional ``UTC`` flag requests the current date/time representation to
  172. be in Coordinated Universal Time (UTC) rather than local time.
  173. The optional ``<format string>`` may contain the following format
  174. specifiers:
  175. ::
  176. %d The day of the current month (01-31).
  177. %H The hour on a 24-hour clock (00-23).
  178. %I The hour on a 12-hour clock (01-12).
  179. %j The day of the current year (001-366).
  180. %m The month of the current year (01-12).
  181. %M The minute of the current hour (00-59).
  182. %S The second of the current minute.
  183. 60 represents a leap second. (00-60)
  184. %U The week number of the current year (00-53).
  185. %w The day of the current week. 0 is Sunday. (0-6)
  186. %y The last two digits of the current year (00-99)
  187. %Y The current year.
  188. Unknown format specifiers will be ignored and copied to the output
  189. as-is.
  190. If no explicit ``<format string>`` is given it will default to:
  191. ::
  192. %Y-%m-%dT%H:%M:%S for local time.
  193. %Y-%m-%dT%H:%M:%SZ for UTC.
  194. ::
  195. string(MAKE_C_IDENTIFIER <input string> <output variable>)
  196. Write a string which can be used as an identifier in C.
  197. UUID
  198. """"
  199. ::
  200. string(UUID <output variable> NAMESPACE <namespace> NAME <name>
  201. TYPE <MD5|SHA1> [UPPER])
  202. Create a univerally unique identifier (aka GUID) as per RFC4122
  203. based on the hash of the combined values of ``<namespace>``
  204. (which itself has to be a valid UUID) and ``<name>``.
  205. The hash algorithm can be either ``MD5`` (Version 3 UUID) or
  206. ``SHA1`` (Version 5 UUID).
  207. A UUID has the format ``xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx``
  208. where each `x` represents a lower case hexadecimal character.
  209. Where required an uppercase representation can be requested
  210. with the optional ``UPPER`` flag.