string.rst 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. All ``<input>`` arguments are concatenated before matching.
  33. REGEX MATCHALL
  34. """"""""""""""
  35. ::
  36. string(REGEX MATCHALL <regular_expression>
  37. <output variable> <input> [<input>...])
  38. Match the regular expression as many times as possible and store the matches
  39. in the output variable as a list.
  40. All ``<input>`` arguments are concatenated before matching.
  41. REGEX REPLACE
  42. """""""""""""
  43. ::
  44. string(REGEX REPLACE <regular_expression>
  45. <replace_expression> <output variable>
  46. <input> [<input>...])
  47. Match the regular expression as many times as possible and substitute the
  48. replacement expression for the match in the output.
  49. All ``<input>`` arguments are concatenated before matching.
  50. The replace expression may refer to paren-delimited subexpressions of the
  51. match using ``\1``, ``\2``, ..., ``\9``. Note that two backslashes (``\\1``)
  52. are required in CMake code to get a backslash through argument parsing.
  53. Regex Specification
  54. """""""""""""""""""
  55. The following characters have special meaning in regular expressions:
  56. ``^``
  57. Matches at beginning of input
  58. ``$``
  59. Matches at end of input
  60. ``.``
  61. Matches any single character
  62. ``[ ]``
  63. Matches any character(s) inside the brackets
  64. ``[^ ]``
  65. Matches any character(s) not inside the brackets
  66. ``-``
  67. Inside brackets, specifies an inclusive range between
  68. characters on either side e.g. ``[a-f]`` is ``[abcdef]``
  69. To match a literal ``-`` using brackets, make it the first
  70. or the last character e.g. ``[+*/-]`` matches basic
  71. mathematical operators.
  72. ``*``
  73. Matches preceding pattern zero or more times
  74. ``+``
  75. Matches preceding pattern one or more times
  76. ``?``
  77. Matches preceding pattern zero or once only
  78. ``|``
  79. Matches a pattern on either side of the ``|``
  80. ``()``
  81. Saves a matched subexpression, which can be referenced
  82. in the ``REGEX REPLACE`` operation. Additionally it is saved
  83. by all regular expression-related commands, including
  84. e.g. :command:`if(MATCHES)`, in the variables
  85. ``CMAKE_MATCH_<n>`` for ``<n>`` 0..9.
  86. ``*``, ``+`` and ``?`` have higher precedence than concatenation. ``|``
  87. has lower precedence than concatenation. This means that the regular
  88. expression ``^ab+d$`` matches ``abbd`` but not ``ababd``, and the regular
  89. expression ``^(ab|cd)$`` matches ``ab`` but not ``abd``.
  90. Manipulation
  91. ^^^^^^^^^^^^
  92. APPEND
  93. """"""
  94. ::
  95. string(APPEND <string variable> [<input>...])
  96. Append all the input arguments to the string.
  97. CONCAT
  98. """"""
  99. ::
  100. string(CONCAT <output variable> [<input>...])
  101. Concatenate all the input arguments together and store
  102. the result in the named output variable.
  103. TOLOWER
  104. """""""
  105. ::
  106. string(TOLOWER <string1> <output variable>)
  107. Convert string to lower characters.
  108. TOUPPER
  109. """""""
  110. ::
  111. string(TOUPPER <string1> <output variable>)
  112. Convert string to upper characters.
  113. LENGTH
  114. """"""
  115. ::
  116. string(LENGTH <string> <output variable>)
  117. Store in an output variable a given string's length.
  118. SUBSTRING
  119. """""""""
  120. ::
  121. string(SUBSTRING <string> <begin> <length> <output variable>)
  122. Store in an output variable a substring of a given string. If length is
  123. ``-1`` the remainder of the string starting at begin will be returned.
  124. If string is shorter than length then end of string is used instead.
  125. .. note::
  126. CMake 3.1 and below reported an error if length pointed past
  127. the end of string.
  128. STRIP
  129. """""
  130. ::
  131. string(STRIP <string> <output variable>)
  132. Store in an output variable a substring of a given string with leading and
  133. trailing spaces removed.
  134. GENEX_STRIP
  135. """""""""""
  136. ::
  137. string(GENEX_STRIP <input string> <output variable>)
  138. Strip any :manual:`generator expressions <cmake-generator-expressions(7)>`
  139. from the ``input string`` and store the result in the ``output variable``.
  140. Comparison
  141. ^^^^^^^^^^
  142. ::
  143. string(COMPARE LESS <string1> <string2> <output variable>)
  144. string(COMPARE GREATER <string1> <string2> <output variable>)
  145. string(COMPARE EQUAL <string1> <string2> <output variable>)
  146. string(COMPARE NOTEQUAL <string1> <string2> <output variable>)
  147. string(COMPARE LESS_EQUAL <string1> <string2> <output variable>)
  148. string(COMPARE GREATER_EQUAL <string1> <string2> <output variable>)
  149. Compare the strings and store true or false in the output variable.
  150. .. _`Supported Hash Algorithms`:
  151. Hashing
  152. ^^^^^^^
  153. ::
  154. string(<HASH> <output variable> <input>)
  155. Compute a cryptographic hash of the input string.
  156. The supported ``<HASH>`` algorithm names are:
  157. ``MD5``
  158. Message-Digest Algorithm 5, RFC 1321.
  159. ``SHA1``
  160. US Secure Hash Algorithm 1, RFC 3174.
  161. ``SHA224``
  162. US Secure Hash Algorithms, RFC 4634.
  163. ``SHA256``
  164. US Secure Hash Algorithms, RFC 4634.
  165. ``SHA384``
  166. US Secure Hash Algorithms, RFC 4634.
  167. ``SHA512``
  168. US Secure Hash Algorithms, RFC 4634.
  169. ``SHA3_224``
  170. Keccak SHA-3.
  171. ``SHA3_256``
  172. Keccak SHA-3.
  173. ``SHA3_384``
  174. Keccak SHA-3.
  175. ``SHA3_512``
  176. Keccak SHA-3.
  177. Generation
  178. ^^^^^^^^^^
  179. ASCII
  180. """""
  181. ::
  182. string(ASCII <number> [<number> ...] <output variable>)
  183. Convert all numbers into corresponding ASCII characters.
  184. CONFIGURE
  185. """""""""
  186. ::
  187. string(CONFIGURE <string1> <output variable>
  188. [@ONLY] [ESCAPE_QUOTES])
  189. Transform a string like :command:`configure_file` transforms a file.
  190. RANDOM
  191. """"""
  192. ::
  193. string(RANDOM [LENGTH <length>] [ALPHABET <alphabet>]
  194. [RANDOM_SEED <seed>] <output variable>)
  195. Return a random string of given length consisting of
  196. characters from the given alphabet. Default length is 5 characters
  197. and default alphabet is all numbers and upper and lower case letters.
  198. If an integer ``RANDOM_SEED`` is given, its value will be used to seed the
  199. random number generator.
  200. TIMESTAMP
  201. """""""""
  202. ::
  203. string(TIMESTAMP <output variable> [<format string>] [UTC])
  204. Write a string representation of the current date
  205. and/or time to the output variable.
  206. Should the command be unable to obtain a timestamp the output variable
  207. will be set to the empty string "".
  208. The optional ``UTC`` flag requests the current date/time representation to
  209. be in Coordinated Universal Time (UTC) rather than local time.
  210. The optional ``<format string>`` may contain the following format
  211. specifiers:
  212. ::
  213. %% A literal percent sign (%).
  214. %d The day of the current month (01-31).
  215. %H The hour on a 24-hour clock (00-23).
  216. %I The hour on a 12-hour clock (01-12).
  217. %j The day of the current year (001-366).
  218. %m The month of the current year (01-12).
  219. %b Abbreviated month name (e.g. Oct).
  220. %M The minute of the current hour (00-59).
  221. %s Seconds since midnight (UTC) 1-Jan-1970 (UNIX time).
  222. %S The second of the current minute.
  223. 60 represents a leap second. (00-60)
  224. %U The week number of the current year (00-53).
  225. %w The day of the current week. 0 is Sunday. (0-6)
  226. %a Abbreviated weekday name (e.g. Fri).
  227. %y The last two digits of the current year (00-99)
  228. %Y The current year.
  229. Unknown format specifiers will be ignored and copied to the output
  230. as-is.
  231. If no explicit ``<format string>`` is given it will default to:
  232. ::
  233. %Y-%m-%dT%H:%M:%S for local time.
  234. %Y-%m-%dT%H:%M:%SZ for UTC.
  235. ::
  236. string(MAKE_C_IDENTIFIER <input string> <output variable>)
  237. Write a string which can be used as an identifier in C.
  238. .. note::
  239. If the ``SOURCE_DATE_EPOCH`` environment variable is set,
  240. its value will be used instead of the current time.
  241. See https://reproducible-builds.org/specs/source-date-epoch/ for details.
  242. UUID
  243. """"
  244. ::
  245. string(UUID <output variable> NAMESPACE <namespace> NAME <name>
  246. TYPE <MD5|SHA1> [UPPER])
  247. Create a univerally unique identifier (aka GUID) as per RFC4122
  248. based on the hash of the combined values of ``<namespace>``
  249. (which itself has to be a valid UUID) and ``<name>``.
  250. The hash algorithm can be either ``MD5`` (Version 3 UUID) or
  251. ``SHA1`` (Version 5 UUID).
  252. A UUID has the format ``xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx``
  253. where each `x` represents a lower case hexadecimal character.
  254. Where required an uppercase representation can be requested
  255. with the optional ``UPPER`` flag.