string.rst 9.6 KB

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