summaryrefslogtreecommitdiff
path: root/devdocs/c/string%2Fbyte%2Fstrtok.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/string%2Fbyte%2Fstrtok.html
new repository
Diffstat (limited to 'devdocs/c/string%2Fbyte%2Fstrtok.html')
-rw-r--r--devdocs/c/string%2Fbyte%2Fstrtok.html108
1 files changed, 108 insertions, 0 deletions
diff --git a/devdocs/c/string%2Fbyte%2Fstrtok.html b/devdocs/c/string%2Fbyte%2Fstrtok.html
new file mode 100644
index 00000000..09405556
--- /dev/null
+++ b/devdocs/c/string%2Fbyte%2Fstrtok.html
@@ -0,0 +1,108 @@
+ <h1 id="firstHeading" class="firstHeading">strtok, strtok_s</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;string.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl-rev-aux"> <td></td> <td rowspan="3">(1)</td> <td></td> </tr> <tr class="t-dcl t-until-c99"> <td><pre data-language="c">char *strtok( char *str, const char *delim );</pre></td> <td><span class="t-mark-rev t-until-c99">(until C99)</span></td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">char *strtok( char *restrict str, const char *restrict delim );</pre>
+</td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">char *strtok_s(char *restrict str, rsize_t *restrict strmax,
+ const char *restrict delim, char **restrict ptr);</pre>
+</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> </table> <div class="t-li1">
+<span class="t-li">1)</span> Finds the next token in a null-terminated byte string pointed to by <code>str</code>. The separator characters are identified by null-terminated byte string pointed to by <code>delim</code>.</div> <div class="t-li1">
+ This function is designed to be called multiple times to obtain successive tokens from the same string.</div> <ul>
+<li> If <code>str</code> is not a null pointer, the call is treated as the first call to <code>strtok</code> for this particular string. The function searches for the first character which is <i>not</i> contained in <code>delim</code>. </li>
+<li> If no such character was found, there are no tokens in <code>str</code> at all, and the function returns a null pointer. </li>
+<li> If such character was found, it is the <i>beginning of the token</i>. The function then searches from that point on for the first character that <i>is</i> contained in <code>delim</code>. </li>
+<ul>
+<li> If no such character was found, <code>str</code> has only one token, and future calls to <code>strtok</code> will return a null pointer </li>
+<li> If such character was found, it is <i>replaced</i> by the null character <code>'\0'</code> and the pointer to the following character is stored in a static location for subsequent invocations. </li>
+</ul>
+<li> The function then returns the pointer to the beginning of the token </li>
+<li> If <code>str</code> is a null pointer, the call is treated as a subsequent call to <code>strtok</code>: the function continues from where it left in previous invocation. The behavior is the same as if the previously stored pointer is passed as <code>str</code>. </li>
+</ul> <div class="t-li1">
+ The behavior is undefined if either <code>str</code> or <code>delim</code> is not a pointer to a null-terminated byte string.</div> <div class="t-li1">
+<span class="t-li">2)</span> Same as <span class="t-v">(1)</span>, except that on every step, writes the number of characters left to see in <code>str</code> into <code>*strmax</code> and writes the tokenizer's internal state to <code>*ptr</code>. Repeat calls (with null <code>str</code>) must pass <code>strmax</code> and <code>ptr</code> with the values stored by the previous call. Also, the following errors are detected at runtime and call the currently installed <a href="../../error/set_constraint_handler_s" title="c/error/set constraint handler s">constraint handler</a> function, without storing anything in the object pointed to by <code>ptr</code> <ul>
+<li> <code>strmax</code>, <code>delim</code>, or <code>ptr</code> is a null pointer </li>
+<li> on a non-initial call (with null <code>str</code>), <code>*ptr</code> is a null pointer </li>
+<li> on the first call, <code>*strmax</code> is zero or greater than <code>RSIZE_MAX</code> </li>
+<li> search for the end of a token reaches the end of the source string (as measured by the initial value of <code>*strmax</code>) without encountering the null terminator</li>
+</ul>
+</div> <div class="t-li1">
+ The behavior is undefined if both <code>str</code> points to a character array which lacks the null character and <code>strmax</code> points to a value which is greater than the size of that character array. As with all bounds-checked functions, <code>strtok_s</code> only guaranteed to be available if <code>__STDC_LIB_EXT1__</code> is defined by the implementation and if the user defines <code>__STDC_WANT_LIB_EXT1__</code> to the integer constant <code>1</code> before including <a href="../byte" title="c/string/byte"><code>&lt;string.h&gt;</code></a>.</div> <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> str </td> <td> - </td> <td> pointer to the null-terminated byte string to tokenize </td>
+</tr> <tr class="t-par"> <td> delim </td> <td> - </td> <td> pointer to the null-terminated byte string identifying delimiters </td>
+</tr> <tr class="t-par"> <td> strmax </td> <td> - </td> <td> pointer to an object which initially holds the size of <code>str</code>: strtok_s stores the number of characters that remain to be examined </td>
+</tr> <tr class="t-par"> <td> ptr </td> <td> - </td> <td> pointer to an object of type <code>char*</code>, which is used by strtok_s to store its internal state </td>
+</tr>
+</table> <h3 id="Return_value"> Return value</h3> <p>Returns pointer to the beginning of the next token or a null pointer if there are no more tokens.</p>
+<h3 id="Note"> Note</h3> <p>This function is destructive: it writes the <code>'\0'</code> characters in the elements of the string <code>str</code>. In particular, a string literal cannot be used as the first argument of <code>strtok</code>.</p>
+<p>Each call to <code>strtok</code> modifies a static variable: is not thread safe.</p>
+<p>Unlike most other tokenizers, the delimiters in <code>strtok</code> can be different for each subsequent token, and can even depend on the contents of the previous tokens.</p>
+<p>The <code>strtok_s</code> function differs from the POSIX <a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtok.html"><code>strtok_r</code></a> function by guarding against storing outside of the string being tokenized, and by checking runtime constraints. The Microsoft CRT <code>strtok_s</code> signature matches this POSIX <code>strtok_r</code> definition, not the C11 <code>strtok_s</code>.</p>
+<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#define __STDC_WANT_LIB_EXT1__ 1
+#include &lt;string.h&gt;
+#include &lt;stdio.h&gt;
+
+int main(void)
+{
+ char input[] = "A bird came down the walk";
+ printf("Parsing the input string '%s'\n", input);
+ char *token = strtok(input, " ");
+ while(token) {
+ puts(token);
+ token = strtok(NULL, " ");
+ }
+
+ printf("Contents of the input string now: '");
+ for(size_t n = 0; n &lt; sizeof input; ++n)
+ input[n] ? putchar(input[n]) : fputs("\\0", stdout);
+ puts("'");
+
+#ifdef __STDC_LIB_EXT1__
+ char str[] = "A bird came down the walk";
+ rsize_t strmax = sizeof str;
+ const char *delim = " ";
+ char *next_token;
+ printf("Parsing the input string '%s'\n", str);
+ token = strtok_s(str, &amp;strmax, delim, &amp;next_token);
+ while(token) {
+ puts(token);
+ token = strtok_s(NULL, &amp;strmax, delim, &amp;next_token);
+ }
+
+ printf("Contents of the input string now: '");
+ for(size_t n = 0; n &lt; sizeof str; ++n)
+ str[n] ? putchar(str[n]) : fputs("\\0", stdout);
+ puts("'");
+#endif
+}</pre></div> <p>Possible output:</p>
+<div class="text source-text"><pre data-language="c">Parsing the input string 'A bird came down the walk'
+A
+bird
+came
+down
+the
+walk
+Contents of the input string now: 'A\0bird\0came\0down\0the\0walk\0'
+Parsing the input string 'A bird came down the walk'
+A
+bird
+came
+down
+the
+walk
+Contents of the input string now: 'A\0bird\0came\0down\0the\0walk\0'</pre></div> </div> <h3 id="References"> References</h3> <ul>
+<li> C11 standard (ISO/IEC 9899:2011): </li>
+<ul>
+<li> 7.24.5.8 The strtok function (p: 369-370) </li>
+<li> K.3.7.3.1 The strtok_s function (p: 620-621) </li>
+</ul>
+<li> C99 standard (ISO/IEC 9899:1999): </li>
+<ul><li> 7.21.5.8 The strtok function (p: 332-333) </li></ul>
+<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
+<ul><li> 4.11.5.8 The strtok function </li></ul>
+</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="strpbrk" title="c/string/byte/strpbrk"> <span class="t-lines"><span>strpbrk</span></span></a></div> </td> <td> finds the first location of any character in one string, in another string <br> <span class="t-mark">(function)</span> </td>
+</tr> <tr class="t-dsc"> <td> <div><a href="strcspn" title="c/string/byte/strcspn"> <span class="t-lines"><span>strcspn</span></span></a></div> </td> <td> returns the length of the maximum initial segment that consists <br> of only the characters not found in another byte string <br> <span class="t-mark">(function)</span> </td>
+</tr> <tr class="t-dsc"> <td> <div><a href="strspn" title="c/string/byte/strspn"> <span class="t-lines"><span>strspn</span></span></a></div> </td> <td> returns the length of the maximum initial segment that consists <br> of only the characters found in another byte string <br> <span class="t-mark">(function)</span> </td>
+</tr> <tr class="t-dsc"> <td> <div><a href="../wide/wcstok" title="c/string/wide/wcstok"> <span class="t-lines"><span>wcstok</span><span>wcstok_s</span></span></a></div>
+<div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> finds the next token in a wide string <br> <span class="t-mark">(function)</span> </td>
+</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/string/byte/strtok" title="cpp/string/byte/strtok">C++ documentation</a></span> for <code>strtok</code> </td>
+</tr> </table> <div class="_attribution">
+ <p class="_attribution-p">
+ &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
+ <a href="https://en.cppreference.com/w/c/string/byte/strtok" class="_attribution-link">https://en.cppreference.com/w/c/string/byte/strtok</a>
+ </p>
+</div>