welibc
A clear, secure, and well documented standard C library
strtok.c
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright (c) 2021 Walt Elam
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7 
8  1. Redistributions of source code must retain the above copyright notice,
9  this list of conditions and the following disclaimer.
10 
11  2. Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in the
13  documentation and/or other materials provided with the distribution.
14 
15  3. Neither the name of the copyright holder nor the names of its
16  contributors may be used to endorse or promote products derived from
17  this software without specific prior written permission.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  POSSIBILITY OF SUCH DAMAGE.
30 *******************************************************************************/
31 /***************************************************************************//**
32  @file strtok.c
33 
34  @brief Defines the strtok function
35 *******************************************************************************/
36 #include <string.h>
37 
38 /*******************************************************************************
39  strtok
40 *//**
41  @brief Breaks a string into a sequence of tokens split on a given delimiter
42  @param *s1 String to be tokenized
43  @param *s2 Separator string used to delimit tokens
44 
45  A sequence of calls to the strtok functions breaks the string pointed to by
46  s1 into a sequence of tokens, each of which is delimited by a character from
47  the string pointed to by s2. The first call in the sequence has s1 as its
48  first argument, and is followed by calls with a null pointer as their first
49  argument. The separator string pointed to by s2 may be different from call
50  to call.
51 
52  The first call in the sequence searches the string pointed to by s1 for the
53  first character that is not contained in the current separator string
54  pointed to by s2. If no such character is found, then there are no tokens in
55  the string pointed to by s1 and the strtok function returns a null pointer.
56  If such a character is found, it is the start of the first token.
57 
58  The strtok function then searches from there for a character that is
59  contained in the current separator string. If no such character is found,
60  the current token extends to the end of the string pointed to by s1, and
61  subsequent searches for a token will return a null pointer. If such a
62  character is found, it is overwritten by a null character, which terminates
63  the current token. The strtok function saves a pointer to the following
64  character, from which the next search for a token will start.
65 
66  Each subsequent call, with a null pointer as the value of the first
67  argument, starts searching from the saved pointer and behaves as described
68  above.
69 
70  The implementation shall behave as if no library function calls the strtok
71  function.
72 
73  @return A pointer to the first character of a token
74  @retval NULL No token was found
75 *******************************************************************************/
76 char *
77 strtok(char *s1, const char *s2)
78 {
79  static char *save = NULL;
80 
81  if (!s2)
82  {
83  return NULL;
84  }
85 
86  if (!s1)
87  {
88  if (!save)
89  {
90  return NULL;
91  }
92 
93  s1 = save;
94  }
95 
96  /* Skip initial delimiters */
97  s1 += strspn(s1, s2);
98  if ('\0' == *s1)
99  {
100  return NULL;
101  }
102 
103  /* Find next delimiter */
104  save = s1 + strcspn(s1, s2);
105  if (save && *save)
106  {
107  *save++ = '\0';
108  }
109 
110  return s1;
111 }
112 
NULL
#define NULL
Definition: stddef.h:57
string.h
Declares types, functions, and macros for manipulating arrays of character type and other objects tre...
strcspn
size_t strcspn(const char *s1, const char *s2)
Find the length of the span of complementary characters to s2 in s1.
Definition: strcspn.c:52
strspn
size_t strspn(const char *s1, const char *s2)
Find the length of the span of characters in s2 in s1.
Definition: strspn.c:52
strtok
char * strtok(char *s1, const char *s2)
Breaks a string into a sequence of tokens split on a given delimiter.
Definition: strtok.c:77