1
0

string.rst 9.4 KB

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