string.rst 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. TOLOWER
  110. """""""
  111. ::
  112. string(TOLOWER <string1> <output variable>)
  113. Convert string to lower characters.
  114. TOUPPER
  115. """""""
  116. ::
  117. string(TOUPPER <string1> <output variable>)
  118. Convert string to upper characters.
  119. LENGTH
  120. """"""
  121. ::
  122. string(LENGTH <string> <output variable>)
  123. Store in an output variable a given string's length.
  124. SUBSTRING
  125. """""""""
  126. ::
  127. string(SUBSTRING <string> <begin> <length> <output variable>)
  128. Store in an output variable a substring of a given string. If length is
  129. ``-1`` the remainder of the string starting at begin will be returned.
  130. If string is shorter than length then end of string is used instead.
  131. .. note::
  132. CMake 3.1 and below reported an error if length pointed past
  133. the end of string.
  134. STRIP
  135. """""
  136. ::
  137. string(STRIP <string> <output variable>)
  138. Store in an output variable a substring of a given string with leading and
  139. trailing spaces removed.
  140. GENEX_STRIP
  141. """""""""""
  142. ::
  143. string(GENEX_STRIP <input string> <output variable>)
  144. Strip any :manual:`generator expressions <cmake-generator-expressions(7)>`
  145. from the ``input string`` and store the result in the ``output variable``.
  146. Comparison
  147. ^^^^^^^^^^
  148. ::
  149. string(COMPARE LESS <string1> <string2> <output variable>)
  150. string(COMPARE GREATER <string1> <string2> <output variable>)
  151. string(COMPARE EQUAL <string1> <string2> <output variable>)
  152. string(COMPARE NOTEQUAL <string1> <string2> <output variable>)
  153. string(COMPARE LESS_EQUAL <string1> <string2> <output variable>)
  154. string(COMPARE GREATER_EQUAL <string1> <string2> <output variable>)
  155. Compare the strings and store true or false in the output variable.
  156. .. _`Supported Hash Algorithms`:
  157. Hashing
  158. ^^^^^^^
  159. ::
  160. string(<HASH> <output variable> <input>)
  161. Compute a cryptographic hash of the input string.
  162. The supported ``<HASH>`` algorithm names are:
  163. ``MD5``
  164. Message-Digest Algorithm 5, RFC 1321.
  165. ``SHA1``
  166. US Secure Hash Algorithm 1, RFC 3174.
  167. ``SHA224``
  168. US Secure Hash Algorithms, RFC 4634.
  169. ``SHA256``
  170. US Secure Hash Algorithms, RFC 4634.
  171. ``SHA384``
  172. US Secure Hash Algorithms, RFC 4634.
  173. ``SHA512``
  174. US Secure Hash Algorithms, RFC 4634.
  175. ``SHA3_224``
  176. Keccak SHA-3.
  177. ``SHA3_256``
  178. Keccak SHA-3.
  179. ``SHA3_384``
  180. Keccak SHA-3.
  181. ``SHA3_512``
  182. Keccak SHA-3.
  183. Generation
  184. ^^^^^^^^^^
  185. ASCII
  186. """""
  187. ::
  188. string(ASCII <number> [<number> ...] <output variable>)
  189. Convert all numbers into corresponding ASCII characters.
  190. CONFIGURE
  191. """""""""
  192. ::
  193. string(CONFIGURE <string1> <output variable>
  194. [@ONLY] [ESCAPE_QUOTES])
  195. Transform a string like :command:`configure_file` transforms a file.
  196. RANDOM
  197. """"""
  198. ::
  199. string(RANDOM [LENGTH <length>] [ALPHABET <alphabet>]
  200. [RANDOM_SEED <seed>] <output variable>)
  201. Return a random string of given length consisting of
  202. characters from the given alphabet. Default length is 5 characters
  203. and default alphabet is all numbers and upper and lower case letters.
  204. If an integer ``RANDOM_SEED`` is given, its value will be used to seed the
  205. random number generator.
  206. TIMESTAMP
  207. """""""""
  208. ::
  209. string(TIMESTAMP <output variable> [<format string>] [UTC])
  210. Write a string representation of the current date
  211. and/or time to the output variable.
  212. Should the command be unable to obtain a timestamp the output variable
  213. will be set to the empty string "".
  214. The optional ``UTC`` flag requests the current date/time representation to
  215. be in Coordinated Universal Time (UTC) rather than local time.
  216. The optional ``<format string>`` may contain the following format
  217. specifiers:
  218. ::
  219. %% A literal percent sign (%).
  220. %d The day of the current month (01-31).
  221. %H The hour on a 24-hour clock (00-23).
  222. %I The hour on a 12-hour clock (01-12).
  223. %j The day of the current year (001-366).
  224. %m The month of the current year (01-12).
  225. %b Abbreviated month name (e.g. Oct).
  226. %B Full month name (e.g. October).
  227. %M The minute of the current hour (00-59).
  228. %s Seconds since midnight (UTC) 1-Jan-1970 (UNIX time).
  229. %S The second of the current minute.
  230. 60 represents a leap second. (00-60)
  231. %U The week number of the current year (00-53).
  232. %w The day of the current week. 0 is Sunday. (0-6)
  233. %a Abbreviated weekday name (e.g. Fri).
  234. %A Full weekday name (e.g. Friday).
  235. %y The last two digits of the current year (00-99)
  236. %Y The current year.
  237. Unknown format specifiers will be ignored and copied to the output
  238. as-is.
  239. If no explicit ``<format string>`` is given it will default to:
  240. ::
  241. %Y-%m-%dT%H:%M:%S for local time.
  242. %Y-%m-%dT%H:%M:%SZ for UTC.
  243. ::
  244. string(MAKE_C_IDENTIFIER <input string> <output variable>)
  245. Write a string which can be used as an identifier in C.
  246. .. note::
  247. If the ``SOURCE_DATE_EPOCH`` environment variable is set,
  248. its value will be used instead of the current time.
  249. See https://reproducible-builds.org/specs/source-date-epoch/ for details.
  250. UUID
  251. """"
  252. ::
  253. string(UUID <output variable> NAMESPACE <namespace> NAME <name>
  254. TYPE <MD5|SHA1> [UPPER])
  255. Create a univerally unique identifier (aka GUID) as per RFC4122
  256. based on the hash of the combined values of ``<namespace>``
  257. (which itself has to be a valid UUID) and ``<name>``.
  258. The hash algorithm can be either ``MD5`` (Version 3 UUID) or
  259. ``SHA1`` (Version 5 UUID).
  260. A UUID has the format ``xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx``
  261. where each `x` represents a lower case hexadecimal character.
  262. Where required an uppercase representation can be requested
  263. with the optional ``UPPER`` flag.