wcstok.c (05db21b17b545bd5ffa38715b49e1247aa0c9679) wcstok.c (9ad391340ed786640390cea55667544e006f26fd)
1/*-
2 * Copyright (c) 1998 Softweyr LLC. All rights reserved.
3 *
4 * strtok_r, from Berkeley strtok
5 * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
6 *
7 * Copyright (c) 1988, 1993
8 * The Regents of the University of California. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notices, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notices, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
1/*-
2 * Copyright (c) 1998 Softweyr LLC. All rights reserved.
3 *
4 * strtok_r, from Berkeley strtok
5 * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
6 *
7 * Copyright (c) 1988, 1993
8 * The Regents of the University of California. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notices, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notices, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Softweyr LLC, the
21 * University of California, Berkeley, and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE

--- 10 unchanged lines hidden (view full) ---

36__FBSDID("$FreeBSD$");
37
38#include <wchar.h>
39
40wchar_t *
41wcstok(wchar_t * __restrict s, const wchar_t * __restrict delim,
42 wchar_t ** __restrict last)
43{
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
29 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE

--- 10 unchanged lines hidden (view full) ---

40__FBSDID("$FreeBSD$");
41
42#include <wchar.h>
43
44wchar_t *
45wcstok(wchar_t * __restrict s, const wchar_t * __restrict delim,
46 wchar_t ** __restrict last)
47{
44 const wchar_t *spanp;
45 wchar_t *tok;
48 wchar_t *spanp, *tok;
46 wchar_t c, sc;
47
48 if (s == NULL && (s = *last) == NULL)
49 return (NULL);
50
51 /*
52 * Skip (span) leading delimiters (s += wcsspn(s, delim), sort of).
53 */
54cont:
55 c = *s++;
49 wchar_t c, sc;
50
51 if (s == NULL && (s = *last) == NULL)
52 return (NULL);
53
54 /*
55 * Skip (span) leading delimiters (s += wcsspn(s, delim), sort of).
56 */
57cont:
58 c = *s++;
56 for (spanp = delim; (sc = *spanp++) != L'\0';) {
59 for (spanp = (wchar_t *)delim; (sc = *spanp++) != L'\0';) {
57 if (c == sc)
58 goto cont;
59 }
60
61 if (c == L'\0') { /* no non-delimiter characters */
62 *last = NULL;
63 return (NULL);
64 }
65 tok = s - 1;
66
67 /*
68 * Scan token (scan for delimiters: s += wcscspn(s, delim), sort of).
69 * Note that delim must have one NUL; we stop if we see that, too.
70 */
71 for (;;) {
72 c = *s++;
60 if (c == sc)
61 goto cont;
62 }
63
64 if (c == L'\0') { /* no non-delimiter characters */
65 *last = NULL;
66 return (NULL);
67 }
68 tok = s - 1;
69
70 /*
71 * Scan token (scan for delimiters: s += wcscspn(s, delim), sort of).
72 * Note that delim must have one NUL; we stop if we see that, too.
73 */
74 for (;;) {
75 c = *s++;
73 spanp = delim;
76 spanp = (wchar_t *)delim;
74 do {
75 if ((sc = *spanp++) == c) {
76 if (c == L'\0')
77 s = NULL;
78 else
79 s[-1] = L'\0';
80 *last = s;
81 return (tok);
82 }
83 } while (sc != L'\0');
84 }
85 /* NOTREACHED */
86}
77 do {
78 if ((sc = *spanp++) == c) {
79 if (c == L'\0')
80 s = NULL;
81 else
82 s[-1] = L'\0';
83 *last = s;
84 return (tok);
85 }
86 } while (sc != L'\0');
87 }
88 /* NOTREACHED */
89}