1
0

string.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. string
  2. ------
  3. String operations.
  4. Synopsis
  5. ^^^^^^^^
  6. .. parsed-literal::
  7. `Search and Replace`_
  8. string(`FIND`_ <string> <substring> <out-var> [...])
  9. string(`REPLACE`_ <match-string> <replace-string> <out-var> <input>...)
  10. string(`REGEX MATCH`_ <match-regex> <out-var> <input>...)
  11. string(`REGEX MATCHALL`_ <match-regex> <out-var> <input>...)
  12. string(`REGEX REPLACE`_ <match-regex> <replace-expr> <out-var> <input>...)
  13. `Manipulation`_
  14. string(`APPEND`_ <string-var> [<input>...])
  15. string(`PREPEND`_ <string-var> [<input>...])
  16. string(`CONCAT`_ <out-var> [<input>...])
  17. string(`JOIN`_ <glue> <out-var> [<input>...])
  18. string(`TOLOWER`_ <string> <out-var>)
  19. string(`TOUPPER`_ <string> <out-var>)
  20. string(`LENGTH`_ <string> <out-var>)
  21. string(`SUBSTRING`_ <string> <begin> <length> <out-var>)
  22. string(`STRIP`_ <string> <out-var>)
  23. string(`GENEX_STRIP`_ <string> <out-var>)
  24. string(`REPEAT`_ <string> <count> <out-var>)
  25. `Comparison`_
  26. string(`COMPARE`_ <op> <string1> <string2> <out-var>)
  27. `Hashing`_
  28. string(`\<HASH\> <HASH_>`_ <out-var> <input>)
  29. `Generation`_
  30. string(`ASCII`_ <number>... <out-var>)
  31. string(`HEX`_ <string> <out-var>)
  32. string(`CONFIGURE`_ <string> <out-var> [...])
  33. string(`MAKE_C_IDENTIFIER`_ <string> <out-var>)
  34. string(`RANDOM`_ [<option>...] <out-var>)
  35. string(`TIMESTAMP`_ <out-var> [<format string>] [UTC])
  36. string(`UUID`_ <out-var> ...)
  37. Search and Replace
  38. ^^^^^^^^^^^^^^^^^^
  39. Search and Replace With Plain Strings
  40. """""""""""""""""""""""""""""""""""""
  41. .. _FIND:
  42. .. code-block:: cmake
  43. string(FIND <string> <substring> <output_variable> [REVERSE])
  44. Return the position where the given ``<substring>`` was found in
  45. the supplied ``<string>``. If the ``REVERSE`` flag was used, the command will
  46. search for the position of the last occurrence of the specified
  47. ``<substring>``. If the ``<substring>`` is not found, a position of -1 is
  48. returned.
  49. The ``string(FIND)`` subcommand treats all strings as ASCII-only characters.
  50. The index stored in ``<output_variable>`` will also be counted in bytes,
  51. so strings containing multi-byte characters may lead to unexpected results.
  52. .. _REPLACE:
  53. .. code-block:: cmake
  54. string(REPLACE <match_string>
  55. <replace_string> <output_variable>
  56. <input> [<input>...])
  57. Replace all occurrences of ``<match_string>`` in the ``<input>``
  58. with ``<replace_string>`` and store the result in the ``<output_variable>``.
  59. Search and Replace With Regular Expressions
  60. """""""""""""""""""""""""""""""""""""""""""
  61. .. _`REGEX MATCH`:
  62. .. code-block:: cmake
  63. string(REGEX MATCH <regular_expression>
  64. <output_variable> <input> [<input>...])
  65. Match the ``<regular_expression>`` once and store the match in the
  66. ``<output_variable>``.
  67. All ``<input>`` arguments are concatenated before matching.
  68. Regular expressions are specified in the subsection just below.
  69. .. _`REGEX MATCHALL`:
  70. .. code-block:: cmake
  71. string(REGEX MATCHALL <regular_expression>
  72. <output_variable> <input> [<input>...])
  73. Match the ``<regular_expression>`` as many times as possible and store the
  74. matches in the ``<output_variable>`` as a list.
  75. All ``<input>`` arguments are concatenated before matching.
  76. .. _`REGEX REPLACE`:
  77. .. code-block:: cmake
  78. string(REGEX REPLACE <regular_expression>
  79. <replacement_expression> <output_variable>
  80. <input> [<input>...])
  81. Match the ``<regular_expression>`` as many times as possible and substitute
  82. the ``<replacement_expression>`` for the match in the output.
  83. All ``<input>`` arguments are concatenated before matching.
  84. The ``<replacement_expression>`` may refer to parenthesis-delimited
  85. subexpressions of the match using ``\1``, ``\2``, ..., ``\9``. Note that
  86. two backslashes (``\\1``) are required in CMake code to get a backslash
  87. through argument parsing.
  88. .. _`Regex Specification`:
  89. Regex Specification
  90. """""""""""""""""""
  91. The following characters have special meaning in regular expressions:
  92. ``^``
  93. Matches at beginning of input
  94. ``$``
  95. Matches at end of input
  96. ``.``
  97. Matches any single character
  98. ``\<char>``
  99. Matches the single character specified by ``<char>``. Use this to
  100. match special regex characters, e.g. ``\.`` for a literal ``.``
  101. or ``\\`` for a literal backslash ``\``. Escaping a non-special
  102. character is unnecessary but allowed, e.g. ``\a`` matches ``a``.
  103. ``[ ]``
  104. Matches any character(s) inside the brackets
  105. ``[^ ]``
  106. Matches any character(s) not inside the brackets
  107. ``-``
  108. Inside brackets, specifies an inclusive range between
  109. characters on either side e.g. ``[a-f]`` is ``[abcdef]``
  110. To match a literal ``-`` using brackets, make it the first
  111. or the last character e.g. ``[+*/-]`` matches basic
  112. mathematical operators.
  113. ``*``
  114. Matches preceding pattern zero or more times
  115. ``+``
  116. Matches preceding pattern one or more times
  117. ``?``
  118. Matches preceding pattern zero or once only
  119. ``|``
  120. Matches a pattern on either side of the ``|``
  121. ``()``
  122. Saves a matched subexpression, which can be referenced
  123. in the ``REGEX REPLACE`` operation. Additionally it is saved
  124. by all regular expression-related commands, including
  125. e.g. :command:`if(MATCHES)`, in the variables
  126. :variable:`CMAKE_MATCH_<n>` for ``<n>`` 0..9.
  127. ``*``, ``+`` and ``?`` have higher precedence than concatenation. ``|``
  128. has lower precedence than concatenation. This means that the regular
  129. expression ``^ab+d$`` matches ``abbd`` but not ``ababd``, and the regular
  130. expression ``^(ab|cd)$`` matches ``ab`` but not ``abd``.
  131. CMake language :ref:`Escape Sequences` such as ``\t``, ``\r``, ``\n``,
  132. and ``\\`` may be used to construct literal tabs, carriage returns,
  133. newlines, and backslashes (respectively) to pass in a regex. For example:
  134. * The quoted argument ``"[ \t\r\n]"`` specifies a regex that matches
  135. any single whitespace character.
  136. * The quoted argument ``"[/\\]"`` specifies a regex that matches
  137. a single forward slash ``/`` or backslash ``\``.
  138. * The quoted argument ``"[A-Za-z0-9_]"`` specifies a regex that matches
  139. any single "word" character in the C locale.
  140. * The quoted argument ``"\\(\\a\\+b\\)"`` specifies a regex that matches
  141. the exact string ``(a+b)``. Each ``\\`` is parsed in a quoted argument
  142. as just ``\``, so the regex itself is actually ``\(\a\+\b\)``. This
  143. can alternatively be specified in a :ref:`bracket argument` without
  144. having to escape the backslashes, e.g. ``[[\(\a\+\b\)]]``.
  145. Manipulation
  146. ^^^^^^^^^^^^
  147. .. _APPEND:
  148. .. code-block:: cmake
  149. string(APPEND <string_variable> [<input>...])
  150. Append all the ``<input>`` arguments to the string.
  151. .. _PREPEND:
  152. .. code-block:: cmake
  153. string(PREPEND <string_variable> [<input>...])
  154. Prepend all the ``<input>`` arguments to the string.
  155. .. _CONCAT:
  156. .. code-block:: cmake
  157. string(CONCAT <output_variable> [<input>...])
  158. Concatenate all the ``<input>`` arguments together and store
  159. the result in the named ``<output_variable>``.
  160. .. _JOIN:
  161. .. code-block:: cmake
  162. string(JOIN <glue> <output_variable> [<input>...])
  163. Join all the ``<input>`` arguments together using the ``<glue>``
  164. string and store the result in the named ``<output_variable>``.
  165. To join a list's elements, prefer to use the ``JOIN`` operator
  166. from the :command:`list` command. This allows for the elements to have
  167. special characters like ``;`` in them.
  168. .. _TOLOWER:
  169. .. code-block:: cmake
  170. string(TOLOWER <string> <output_variable>)
  171. Convert ``<string>`` to lower characters.
  172. .. _TOUPPER:
  173. .. code-block:: cmake
  174. string(TOUPPER <string> <output_variable>)
  175. Convert ``<string>`` to upper characters.
  176. .. _LENGTH:
  177. .. code-block:: cmake
  178. string(LENGTH <string> <output_variable>)
  179. Store in an ``<output_variable>`` a given string's length in bytes.
  180. Note that this means if ``<string>`` contains multi-byte characters, the
  181. result stored in ``<output_variable>`` will *not* be the number of characters.
  182. .. _SUBSTRING:
  183. .. code-block:: cmake
  184. string(SUBSTRING <string> <begin> <length> <output_variable>)
  185. Store in an ``<output_variable>`` a substring of a given ``<string>``. If
  186. ``<length>`` is ``-1`` the remainder of the string starting at ``<begin>``
  187. will be returned. If ``<string>`` is shorter than ``<length>`` then the
  188. end of the string is used instead.
  189. Both ``<begin>`` and ``<length>`` are counted in bytes, so care must
  190. be exercised if ``<string>`` could contain multi-byte characters.
  191. .. note::
  192. CMake 3.1 and below reported an error if ``<length>`` pointed past
  193. the end of ``<string>``.
  194. .. _STRIP:
  195. .. code-block:: cmake
  196. string(STRIP <string> <output_variable>)
  197. Store in an ``<output_variable>`` a substring of a given ``<string>`` with
  198. leading and trailing spaces removed.
  199. .. _GENEX_STRIP:
  200. .. code-block:: cmake
  201. string(GENEX_STRIP <string> <output_variable>)
  202. Strip any :manual:`generator expressions <cmake-generator-expressions(7)>`
  203. from the input ``<string>`` and store the result in the ``<output_variable>``.
  204. .. _REPEAT:
  205. .. code-block:: cmake
  206. string(REPEAT <string> <count> <output_variable>)
  207. Produce the output string as the input ``<string>`` repeated ``<count>`` times.
  208. Comparison
  209. ^^^^^^^^^^
  210. .. _COMPARE:
  211. .. code-block:: cmake
  212. string(COMPARE LESS <string1> <string2> <output_variable>)
  213. string(COMPARE GREATER <string1> <string2> <output_variable>)
  214. string(COMPARE EQUAL <string1> <string2> <output_variable>)
  215. string(COMPARE NOTEQUAL <string1> <string2> <output_variable>)
  216. string(COMPARE LESS_EQUAL <string1> <string2> <output_variable>)
  217. string(COMPARE GREATER_EQUAL <string1> <string2> <output_variable>)
  218. Compare the strings and store true or false in the ``<output_variable>``.
  219. .. _`Supported Hash Algorithms`:
  220. Hashing
  221. ^^^^^^^
  222. .. _`HASH`:
  223. .. code-block:: cmake
  224. string(<HASH> <output_variable> <input>)
  225. Compute a cryptographic hash of the ``<input>`` string.
  226. The supported ``<HASH>`` algorithm names are:
  227. ``MD5``
  228. Message-Digest Algorithm 5, RFC 1321.
  229. ``SHA1``
  230. US Secure Hash Algorithm 1, RFC 3174.
  231. ``SHA224``
  232. US Secure Hash Algorithms, RFC 4634.
  233. ``SHA256``
  234. US Secure Hash Algorithms, RFC 4634.
  235. ``SHA384``
  236. US Secure Hash Algorithms, RFC 4634.
  237. ``SHA512``
  238. US Secure Hash Algorithms, RFC 4634.
  239. ``SHA3_224``
  240. Keccak SHA-3.
  241. ``SHA3_256``
  242. Keccak SHA-3.
  243. ``SHA3_384``
  244. Keccak SHA-3.
  245. ``SHA3_512``
  246. Keccak SHA-3.
  247. Generation
  248. ^^^^^^^^^^
  249. .. _ASCII:
  250. .. code-block:: cmake
  251. string(ASCII <number> [<number> ...] <output_variable>)
  252. Convert all numbers into corresponding ASCII characters.
  253. .. _HEX:
  254. .. code-block:: cmake
  255. string(HEX <string> <output_variable>)
  256. Convert each byte in the input ``<string>`` to its hexadecimal representation
  257. and store the concatenated hex digits in the ``<output_variable>``. Letters in
  258. the output (``a`` through ``f``) are in lowercase.
  259. .. _CONFIGURE:
  260. .. code-block:: cmake
  261. string(CONFIGURE <string> <output_variable>
  262. [@ONLY] [ESCAPE_QUOTES])
  263. Transform a ``<string>`` like :command:`configure_file` transforms a file.
  264. .. _MAKE_C_IDENTIFIER:
  265. .. code-block:: cmake
  266. string(MAKE_C_IDENTIFIER <string> <output_variable>)
  267. Convert each non-alphanumeric character in the input ``<string>`` to an
  268. underscore and store the result in the ``<output_variable>``. If the first
  269. character of the ``<string>`` is a digit, an underscore will also be prepended
  270. to the result.
  271. .. _RANDOM:
  272. .. code-block:: cmake
  273. string(RANDOM [LENGTH <length>] [ALPHABET <alphabet>]
  274. [RANDOM_SEED <seed>] <output_variable>)
  275. Return a random string of given ``<length>`` consisting of
  276. characters from the given ``<alphabet>``. Default length is 5 characters
  277. and default alphabet is all numbers and upper and lower case letters.
  278. If an integer ``RANDOM_SEED`` is given, its value will be used to seed the
  279. random number generator.
  280. .. _TIMESTAMP:
  281. .. code-block:: cmake
  282. string(TIMESTAMP <output_variable> [<format_string>] [UTC])
  283. Write a string representation of the current date
  284. and/or time to the ``<output_variable>``.
  285. If the command is unable to obtain a timestamp, the ``<output_variable>``
  286. will be set to the empty string ``""``.
  287. The optional ``UTC`` flag requests the current date/time representation to
  288. be in Coordinated Universal Time (UTC) rather than local time.
  289. The optional ``<format_string>`` may contain the following format
  290. specifiers:
  291. ::
  292. %% A literal percent sign (%).
  293. %d The day of the current month (01-31).
  294. %H The hour on a 24-hour clock (00-23).
  295. %I The hour on a 12-hour clock (01-12).
  296. %j The day of the current year (001-366).
  297. %m The month of the current year (01-12).
  298. %b Abbreviated month name (e.g. Oct).
  299. %B Full month name (e.g. October).
  300. %M The minute of the current hour (00-59).
  301. %s Seconds since midnight (UTC) 1-Jan-1970 (UNIX time).
  302. %S The second of the current minute.
  303. 60 represents a leap second. (00-60)
  304. %U The week number of the current year (00-53).
  305. %w The day of the current week. 0 is Sunday. (0-6)
  306. %a Abbreviated weekday name (e.g. Fri).
  307. %A Full weekday name (e.g. Friday).
  308. %y The last two digits of the current year (00-99)
  309. %Y The current year.
  310. Unknown format specifiers will be ignored and copied to the output
  311. as-is.
  312. If no explicit ``<format_string>`` is given, it will default to:
  313. ::
  314. %Y-%m-%dT%H:%M:%S for local time.
  315. %Y-%m-%dT%H:%M:%SZ for UTC.
  316. .. note::
  317. If the ``SOURCE_DATE_EPOCH`` environment variable is set,
  318. its value will be used instead of the current time.
  319. See https://reproducible-builds.org/specs/source-date-epoch/ for details.
  320. .. _UUID:
  321. .. code-block:: cmake
  322. string(UUID <output_variable> NAMESPACE <namespace> NAME <name>
  323. TYPE <MD5|SHA1> [UPPER])
  324. Create a universally unique identifier (aka GUID) as per RFC4122
  325. based on the hash of the combined values of ``<namespace>``
  326. (which itself has to be a valid UUID) and ``<name>``.
  327. The hash algorithm can be either ``MD5`` (Version 3 UUID) or
  328. ``SHA1`` (Version 5 UUID).
  329. A UUID has the format ``xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx``
  330. where each ``x`` represents a lower case hexadecimal character.
  331. Where required, an uppercase representation can be requested
  332. with the optional ``UPPER`` flag.