string.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 <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\>`_ <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. `JSON`_
  38. string(JSON <out-var> [ERROR_VARIABLE <error-var>]
  39. {`GET <JSON GET_>`_ | `TYPE <JSON TYPE_>`_ | `LENGTH <JSON LENGTH_>`_ | `REMOVE <JSON REMOVE_>`_}
  40. <json-string> <member|index> [<member|index> ...])
  41. string(JSON <out-var> [ERROR_VARIABLE <error-var>]
  42. `MEMBER <JSON MEMBER_>`_ <json-string>
  43. [<member|index> ...] <index>)
  44. string(JSON <out-var> [ERROR_VARIABLE <error-var>]
  45. `SET <JSON SET_>`_ <json-string>
  46. <member|index> [<member|index> ...] <value>)
  47. string(JSON <out-var> [ERROR_VARIABLE <error-var>]
  48. `EQUAL <JSON EQUAL_>`_ <json-string1> <json-string2>)
  49. Search and Replace
  50. ^^^^^^^^^^^^^^^^^^
  51. Search and Replace With Plain Strings
  52. """""""""""""""""""""""""""""""""""""
  53. .. signature::
  54. string(FIND <string> <substring> <output_variable> [REVERSE])
  55. Return the position where the given ``<substring>`` was found in
  56. the supplied ``<string>``. If the ``REVERSE`` flag was used, the command
  57. will search for the position of the last occurrence of the specified
  58. ``<substring>``. If the ``<substring>`` is not found, a position of -1 is
  59. returned.
  60. The ``string(FIND)`` subcommand treats all strings as ASCII-only characters.
  61. The index stored in ``<output_variable>`` will also be counted in bytes,
  62. so strings containing multi-byte characters may lead to unexpected results.
  63. .. signature::
  64. string(REPLACE <match_string>
  65. <replace_string> <output_variable>
  66. <input> [<input>...])
  67. Replace all occurrences of ``<match_string>`` in the ``<input>``
  68. with ``<replace_string>`` and store the result in the ``<output_variable>``.
  69. Search and Replace With Regular Expressions
  70. """""""""""""""""""""""""""""""""""""""""""
  71. .. signature::
  72. string(REGEX MATCH <regular_expression>
  73. <output_variable> <input> [<input>...])
  74. Match the ``<regular_expression>`` once and store the match in the
  75. ``<output_variable>``.
  76. All ``<input>`` arguments are concatenated before matching.
  77. Regular expressions are specified in the subsection just below.
  78. .. signature::
  79. string(REGEX MATCHALL <regular_expression>
  80. <output_variable> <input> [<input>...])
  81. Match the ``<regular_expression>`` as many times as possible and store the
  82. matches in the ``<output_variable>`` as a list.
  83. All ``<input>`` arguments are concatenated before matching.
  84. .. signature::
  85. string(REGEX REPLACE <regular_expression>
  86. <replacement_expression> <output_variable>
  87. <input> [<input>...])
  88. Match the ``<regular_expression>`` as many times as possible and substitute
  89. the ``<replacement_expression>`` for the match in the output.
  90. All ``<input>`` arguments are concatenated before matching.
  91. The ``<replacement_expression>`` may refer to parenthesis-delimited
  92. subexpressions of the match using ``\1``, ``\2``, ..., ``\9``. Note that
  93. two backslashes (``\\1``) are required in CMake code to get a backslash
  94. through argument parsing.
  95. .. _`Regex Specification`:
  96. Regex Specification
  97. """""""""""""""""""
  98. The following characters have special meaning in regular expressions:
  99. ``^``
  100. Matches at beginning of input
  101. ``$``
  102. Matches at end of input
  103. ``.``
  104. Matches any single character
  105. ``\<char>``
  106. Matches the single character specified by ``<char>``. Use this to
  107. match special regex characters, e.g. ``\.`` for a literal ``.``
  108. or ``\\`` for a literal backslash ``\``. Escaping a non-special
  109. character is unnecessary but allowed, e.g. ``\a`` matches ``a``.
  110. ``[ ]``
  111. Matches any character(s) inside the brackets.
  112. To match a literal ``]``, make it the first character, e.g., ``[]ab]``.
  113. ``[^ ]``
  114. Matches any character(s) not inside the brackets.
  115. To not match a literal ``]``, make it the first character, e.g., ``[^]ab]``.
  116. ``-``
  117. Inside brackets, specifies an inclusive range between characters on
  118. either side, e.g., ``[a-f]`` is ``[abcdef]``.
  119. To match a literal ``-`` using brackets, make it the first or the last
  120. character, e.g., ``[+*/-]`` matches basic mathematical operators.
  121. ``*``
  122. Matches preceding pattern zero or more times
  123. ``+``
  124. Matches preceding pattern one or more times
  125. ``?``
  126. Matches preceding pattern zero or once only
  127. ``|``
  128. Matches a pattern on either side of the ``|``
  129. ``()``
  130. Saves a matched subexpression, which can be referenced
  131. in the ``REGEX REPLACE`` operation.
  132. .. versionadded:: 3.9
  133. All regular expression-related commands, including e.g.
  134. :command:`if(MATCHES)`, save subgroup matches in the variables
  135. :variable:`CMAKE_MATCH_<n>` for ``<n>`` 0..9.
  136. ``*``, ``+`` and ``?`` have higher precedence than concatenation. ``|``
  137. has lower precedence than concatenation. This means that the regular
  138. expression ``^ab+d$`` matches ``abbd`` but not ``ababd``, and the regular
  139. expression ``^(ab|cd)$`` matches ``ab`` but not ``abd``.
  140. CMake language :ref:`Escape Sequences` such as ``\t``, ``\r``, ``\n``,
  141. and ``\\`` may be used to construct literal tabs, carriage returns,
  142. newlines, and backslashes (respectively) to pass in a regex. For example:
  143. * The quoted argument ``"[ \t\r\n]"`` specifies a regex that matches
  144. any single whitespace character.
  145. * The quoted argument ``"[/\\]"`` specifies a regex that matches
  146. a single forward slash ``/`` or backslash ``\``.
  147. * The quoted argument ``"[A-Za-z0-9_]"`` specifies a regex that matches
  148. any single "word" character in the C locale.
  149. * The quoted argument ``"\\(\\a\\+b\\)"`` specifies a regex that matches
  150. the exact string ``(a+b)``. Each ``\\`` is parsed in a quoted argument
  151. as just ``\``, so the regex itself is actually ``\(\a\+\b\)``. This
  152. can alternatively be specified in a :ref:`bracket argument` without
  153. having to escape the backslashes, e.g. ``[[\(\a\+\b\)]]``.
  154. Manipulation
  155. ^^^^^^^^^^^^
  156. .. signature::
  157. string(APPEND <string_variable> [<input>...])
  158. .. versionadded:: 3.4
  159. Append all the ``<input>`` arguments to the string.
  160. .. signature::
  161. string(PREPEND <string_variable> [<input>...])
  162. .. versionadded:: 3.10
  163. Prepend all the ``<input>`` arguments to the string.
  164. .. signature::
  165. string(CONCAT <output_variable> [<input>...])
  166. Concatenate all the ``<input>`` arguments together and store
  167. the result in the named ``<output_variable>``.
  168. .. signature::
  169. string(JOIN <glue> <output_variable> [<input>...])
  170. .. versionadded:: 3.12
  171. Join all the ``<input>`` arguments together using the ``<glue>``
  172. string and store the result in the named ``<output_variable>``.
  173. To join a list's elements, prefer to use the ``JOIN`` operator
  174. from the :command:`list` command. This allows for the elements to have
  175. special characters like ``;`` in them.
  176. .. signature::
  177. string(TOLOWER <string> <output_variable>)
  178. Convert ``<string>`` to lower characters.
  179. .. signature::
  180. string(TOUPPER <string> <output_variable>)
  181. Convert ``<string>`` to upper characters.
  182. .. signature::
  183. string(LENGTH <string> <output_variable>)
  184. Store in an ``<output_variable>`` a given string's length in bytes.
  185. Note that this means if ``<string>`` contains multi-byte characters,
  186. the result stored in ``<output_variable>`` will *not* be
  187. the number of characters.
  188. .. signature::
  189. string(SUBSTRING <string> <begin> <length> <output_variable>)
  190. Store in an ``<output_variable>`` a substring of a given ``<string>``. If
  191. ``<length>`` is ``-1`` the remainder of the string starting at ``<begin>``
  192. will be returned.
  193. .. versionchanged:: 3.2
  194. If ``<string>`` is shorter than ``<length>``
  195. then the end of the string is used instead.
  196. Previous versions of CMake reported an error in this case.
  197. Both ``<begin>`` and ``<length>`` are counted in bytes, so care must
  198. be exercised if ``<string>`` could contain multi-byte characters.
  199. .. signature::
  200. string(STRIP <string> <output_variable>)
  201. Store in an ``<output_variable>`` a substring of a given ``<string>``
  202. with leading and trailing spaces removed.
  203. .. signature::
  204. string(GENEX_STRIP <string> <output_variable>)
  205. .. versionadded:: 3.1
  206. Strip any :manual:`generator expressions <cmake-generator-expressions(7)>`
  207. from the input ``<string>`` and store the result
  208. in the ``<output_variable>``.
  209. .. signature::
  210. string(REPEAT <string> <count> <output_variable>)
  211. .. versionadded:: 3.15
  212. Produce the output string as the input ``<string>``
  213. repeated ``<count>`` times.
  214. Comparison
  215. ^^^^^^^^^^
  216. .. _COMPARE:
  217. .. signature::
  218. string(COMPARE LESS <string1> <string2> <output_variable>)
  219. string(COMPARE GREATER <string1> <string2> <output_variable>)
  220. string(COMPARE EQUAL <string1> <string2> <output_variable>)
  221. string(COMPARE NOTEQUAL <string1> <string2> <output_variable>)
  222. string(COMPARE LESS_EQUAL <string1> <string2> <output_variable>)
  223. string(COMPARE GREATER_EQUAL <string1> <string2> <output_variable>)
  224. Compare the strings and store true or false in the ``<output_variable>``.
  225. .. versionadded:: 3.7
  226. Added the ``LESS_EQUAL`` and ``GREATER_EQUAL`` options.
  227. .. _`Supported Hash Algorithms`:
  228. Hashing
  229. ^^^^^^^
  230. .. signature::
  231. string(<HASH> <output_variable> <input>)
  232. :target: <HASH>
  233. Compute a cryptographic hash of the ``<input>`` string.
  234. The supported ``<HASH>`` algorithm names are:
  235. ``MD5``
  236. Message-Digest Algorithm 5, RFC 1321.
  237. ``SHA1``
  238. US Secure Hash Algorithm 1, RFC 3174.
  239. ``SHA224``
  240. US Secure Hash Algorithms, RFC 4634.
  241. ``SHA256``
  242. US Secure Hash Algorithms, RFC 4634.
  243. ``SHA384``
  244. US Secure Hash Algorithms, RFC 4634.
  245. ``SHA512``
  246. US Secure Hash Algorithms, RFC 4634.
  247. ``SHA3_224``
  248. Keccak SHA-3.
  249. ``SHA3_256``
  250. Keccak SHA-3.
  251. ``SHA3_384``
  252. Keccak SHA-3.
  253. ``SHA3_512``
  254. Keccak SHA-3.
  255. .. versionadded:: 3.8
  256. Added the ``SHA3_*`` hash algorithms.
  257. Generation
  258. ^^^^^^^^^^
  259. .. signature::
  260. string(ASCII <number> [<number> ...] <output_variable>)
  261. Convert all numbers into corresponding ASCII characters.
  262. .. signature::
  263. string(HEX <string> <output_variable>)
  264. .. versionadded:: 3.18
  265. Convert each byte in the input ``<string>`` to its hexadecimal representation
  266. and store the concatenated hex digits in the ``<output_variable>``.
  267. Letters in the output (``a`` through ``f``) are in lowercase.
  268. .. signature::
  269. string(CONFIGURE <string> <output_variable>
  270. [@ONLY] [ESCAPE_QUOTES])
  271. Transform a ``<string>`` like :command:`configure_file` transforms a file.
  272. .. signature::
  273. string(MAKE_C_IDENTIFIER <string> <output_variable>)
  274. Convert each non-alphanumeric character in the input ``<string>`` to an
  275. underscore and store the result in the ``<output_variable>``. If the first
  276. character of the ``<string>`` is a digit, an underscore will also be
  277. prepended to the result.
  278. .. signature::
  279. string(RANDOM [LENGTH <length>] [ALPHABET <alphabet>]
  280. [RANDOM_SEED <seed>] <output_variable>)
  281. Return a random string of given ``<length>`` consisting of
  282. characters from the given ``<alphabet>``. Default length is 5 characters
  283. and default alphabet is all numbers and upper and lower case letters.
  284. If an integer ``RANDOM_SEED`` is given, its value will be used to seed the
  285. random number generator.
  286. .. signature::
  287. string(TIMESTAMP <output_variable> [<format_string>] [UTC])
  288. Write a string representation of the current date
  289. and/or time to the ``<output_variable>``.
  290. If the command is unable to obtain a timestamp, the ``<output_variable>``
  291. will be set to the empty string ``""``.
  292. The optional ``UTC`` flag requests the current date/time representation to
  293. be in Coordinated Universal Time (UTC) rather than local time.
  294. The optional ``<format_string>`` may contain the following format
  295. specifiers:
  296. ``%%``
  297. .. versionadded:: 3.8
  298. A literal percent sign (%).
  299. ``%d``
  300. The day of the current month (01-31).
  301. ``%H``
  302. The hour on a 24-hour clock (00-23).
  303. ``%I``
  304. The hour on a 12-hour clock (01-12).
  305. ``%j``
  306. The day of the current year (001-366).
  307. ``%m``
  308. The month of the current year (01-12).
  309. ``%b``
  310. .. versionadded:: 3.7
  311. Abbreviated month name (e.g. Oct).
  312. ``%B``
  313. .. versionadded:: 3.10
  314. Full month name (e.g. October).
  315. ``%M``
  316. The minute of the current hour (00-59).
  317. ``%s``
  318. .. versionadded:: 3.6
  319. Seconds since midnight (UTC) 1-Jan-1970 (UNIX time).
  320. ``%S``
  321. The second of the current minute. 60 represents a leap second. (00-60)
  322. ``%f``
  323. .. versionadded:: 3.23
  324. The microsecond of the current second (000000-999999).
  325. ``%U``
  326. The week number of the current year (00-53).
  327. ``%V``
  328. .. versionadded:: 3.22
  329. The ISO 8601 week number of the current year (01-53).
  330. ``%w``
  331. The day of the current week. 0 is Sunday. (0-6)
  332. ``%a``
  333. .. versionadded:: 3.7
  334. Abbreviated weekday name (e.g. Fri).
  335. ``%A``
  336. .. versionadded:: 3.10
  337. Full weekday name (e.g. Friday).
  338. ``%y``
  339. The last two digits of the current year (00-99).
  340. ``%Y``
  341. The current year.
  342. ``%z``
  343. .. versionadded:: 3.26
  344. The offset of the time zone from UTC, in hours and minutes,
  345. with format ``+hhmm`` or ``-hhmm``.
  346. ``%Z``
  347. .. versionadded:: 3.26
  348. The time zone name.
  349. Unknown format specifiers will be ignored and copied to the output
  350. as-is.
  351. If no explicit ``<format_string>`` is given, it will default to:
  352. ::
  353. %Y-%m-%dT%H:%M:%S for local time.
  354. %Y-%m-%dT%H:%M:%SZ for UTC.
  355. .. versionadded:: 3.8
  356. If the ``SOURCE_DATE_EPOCH`` environment variable is set,
  357. its value will be used instead of the current time.
  358. See https://reproducible-builds.org/specs/source-date-epoch/ for details.
  359. .. signature::
  360. string(UUID <output_variable> NAMESPACE <namespace> NAME <name>
  361. TYPE <MD5|SHA1> [UPPER])
  362. .. versionadded:: 3.1
  363. Create a universally unique identifier (aka GUID) as per RFC4122
  364. based on the hash of the combined values of ``<namespace>``
  365. (which itself has to be a valid UUID) and ``<name>``.
  366. The hash algorithm can be either ``MD5`` (Version 3 UUID) or
  367. ``SHA1`` (Version 5 UUID).
  368. A UUID has the format ``xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx``
  369. where each ``x`` represents a lower case hexadecimal character.
  370. Where required, an uppercase representation can be requested
  371. with the optional ``UPPER`` flag.
  372. .. _JSON:
  373. JSON
  374. ^^^^
  375. .. versionadded:: 3.19
  376. Functionality for querying a JSON string.
  377. .. note::
  378. In each of the following JSON-related subcommands, if the optional
  379. ``ERROR_VARIABLE`` argument is given, errors will be reported in
  380. ``<error-variable>`` and the ``<out-var>`` will be set to
  381. ``<member|index>-[<member|index>...]-NOTFOUND`` with the path elements
  382. up to the point where the error occurred, or just ``NOTFOUND`` if there
  383. is no relevant path. If an error occurs but the ``ERROR_VARIABLE``
  384. option is not present, a fatal error message is generated. If no error
  385. occurs, the ``<error-variable>`` will be set to ``NOTFOUND``.
  386. .. signature::
  387. string(JSON <out-var> [ERROR_VARIABLE <error-variable>]
  388. GET <json-string> <member|index> [<member|index> ...])
  389. :target: JSON GET
  390. Get an element from ``<json-string>`` at the location given
  391. by the list of ``<member|index>`` arguments.
  392. Array and object elements will be returned as a JSON string.
  393. Boolean elements will be returned as ``ON`` or ``OFF``.
  394. Null elements will be returned as an empty string.
  395. Number and string types will be returned as strings.
  396. .. signature::
  397. string(JSON <out-var> [ERROR_VARIABLE <error-variable>]
  398. TYPE <json-string> <member|index> [<member|index> ...])
  399. :target: JSON TYPE
  400. Get the type of an element in ``<json-string>`` at the location
  401. given by the list of ``<member|index>`` arguments. The ``<out-var>``
  402. will be set to one of ``NULL``, ``NUMBER``, ``STRING``, ``BOOLEAN``,
  403. ``ARRAY``, or ``OBJECT``.
  404. .. signature::
  405. string(JSON <out-var> [ERROR_VARIABLE <error-var>]
  406. MEMBER <json-string>
  407. [<member|index> ...] <index>)
  408. :target: JSON MEMBER
  409. Get the name of the ``<index>``-th member in ``<json-string>``
  410. at the location given by the list of ``<member|index>`` arguments.
  411. Requires an element of object type.
  412. .. signature::
  413. string(JSON <out-var> [ERROR_VARIABLE <error-variable>]
  414. LENGTH <json-string> [<member|index> ...])
  415. :target: JSON LENGTH
  416. Get the length of an element in ``<json-string>`` at the location
  417. given by the list of ``<member|index>`` arguments.
  418. Requires an element of array or object type.
  419. .. signature::
  420. string(JSON <out-var> [ERROR_VARIABLE <error-variable>]
  421. REMOVE <json-string> <member|index> [<member|index> ...])
  422. :target: JSON REMOVE
  423. Remove an element from ``<json-string>`` at the location
  424. given by the list of ``<member|index>`` arguments. The JSON string
  425. without the removed element will be stored in ``<out-var>``.
  426. .. signature::
  427. string(JSON <out-var> [ERROR_VARIABLE <error-variable>]
  428. SET <json-string> <member|index> [<member|index> ...] <value>)
  429. :target: JSON SET
  430. Set an element in ``<json-string>`` at the location
  431. given by the list of ``<member|index>`` arguments to ``<value>``.
  432. The contents of ``<value>`` should be valid JSON.
  433. If ``<json-string>`` is an array, ``<value>`` can be appended to the end of
  434. the array by using a number greater or equal to the array length as the
  435. ``<member|index>`` argument.
  436. .. signature::
  437. string(JSON <out-var> [ERROR_VARIABLE <error-var>]
  438. EQUAL <json-string1> <json-string2>)
  439. :target: JSON EQUAL
  440. Compare the two JSON objects given by ``<json-string1>``
  441. and ``<json-string2>`` for equality. The contents of ``<json-string1>``
  442. and ``<json-string2>`` should be valid JSON. The ``<out-var>``
  443. will be set to a true value if the JSON objects are considered equal,
  444. or a false value otherwise.