Home | History | Annotate | Line # | Download | only in string
wcstok.c revision 1.3.56.1
      1  1.3.56.1      yamt /* $NetBSD: wcstok.c,v 1.3.56.1 2012/10/30 18:59:02 yamt Exp $ */
      2       1.1  tshiozak 
      3       1.1  tshiozak /*-
      4       1.1  tshiozak  * Copyright (c) 1998 Softweyr LLC.  All rights reserved.
      5       1.1  tshiozak  *
      6       1.1  tshiozak  * strtok_r, from Berkeley strtok
      7       1.1  tshiozak  * Oct 13, 1998 by Wes Peters <wes (at) softweyr.com>
      8       1.1  tshiozak  *
      9       1.1  tshiozak  * Copyright (c) 1988, 1993
     10       1.1  tshiozak  *	The Regents of the University of California.  All rights reserved.
     11       1.1  tshiozak  *
     12       1.1  tshiozak  * Redistribution and use in source and binary forms, with or without
     13       1.1  tshiozak  * modification, are permitted provided that the following conditions
     14       1.1  tshiozak  * are met:
     15       1.1  tshiozak  * 1. Redistributions of source code must retain the above copyright
     16       1.1  tshiozak  *    notices, this list of conditions and the following disclaimer.
     17       1.1  tshiozak  * 2. Redistributions in binary form must reproduce the above copyright
     18       1.1  tshiozak  *    notices, this list of conditions and the following disclaimer in the
     19       1.1  tshiozak  *    documentation and/or other materials provided with the distribution.
     20       1.1  tshiozak  * 3. All advertising materials mentioning features or use of this software
     21       1.1  tshiozak  *    must display the following acknowledgement:
     22       1.1  tshiozak  *	This product includes software developed by Softweyr LLC, the
     23       1.1  tshiozak  *      University of California, Berkeley, and its contributors.
     24       1.1  tshiozak  * 4. Neither the name of the University nor the names of its contributors
     25       1.1  tshiozak  *    may be used to endorse or promote products derived from this software
     26       1.1  tshiozak  *    without specific prior written permission.
     27       1.1  tshiozak  *
     28       1.1  tshiozak  * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
     29       1.1  tshiozak  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     30       1.1  tshiozak  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     31       1.1  tshiozak  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL SOFTWEYR LLC, THE
     32       1.1  tshiozak  * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     33       1.1  tshiozak  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     34       1.1  tshiozak  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     35       1.1  tshiozak  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     36       1.1  tshiozak  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     37       1.1  tshiozak  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     38       1.1  tshiozak  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     39       1.2  tshiozak  *
     40       1.2  tshiozak  * Original version ID:
     41       1.2  tshiozak  * FreeBSD: src/lib/libc/string/wcstok.c,v 1.1 2002/09/07 08:16:57 tjr Exp
     42       1.1  tshiozak  */
     43       1.1  tshiozak 
     44       1.1  tshiozak #include <sys/cdefs.h>
     45       1.1  tshiozak #if defined(LIBC_SCCS) && !defined(lint)
     46  1.3.56.1      yamt __RCSID("$NetBSD: wcstok.c,v 1.3.56.1 2012/10/30 18:59:02 yamt Exp $");
     47       1.1  tshiozak #endif
     48       1.1  tshiozak 
     49       1.1  tshiozak #include <assert.h>
     50       1.1  tshiozak #include <wchar.h>
     51       1.1  tshiozak 
     52       1.1  tshiozak wchar_t *
     53  1.3.56.1      yamt wcstok(wchar_t * __restrict s, const wchar_t * __restrict delim,
     54  1.3.56.1      yamt     wchar_t ** __restrict last)
     55       1.1  tshiozak {
     56       1.1  tshiozak 	const wchar_t *spanp;
     57       1.1  tshiozak 	wchar_t c, sc;
     58       1.1  tshiozak 	wchar_t *tok;
     59       1.1  tshiozak 
     60       1.1  tshiozak 	/* s may be NULL */
     61       1.3  tshiozak 	_DIAGASSERT(delim != NULL);
     62       1.1  tshiozak 	_DIAGASSERT(last != NULL);
     63       1.1  tshiozak 
     64       1.1  tshiozak 	if (s == NULL && (s = *last) == NULL)
     65       1.1  tshiozak 		return (NULL);
     66       1.1  tshiozak 
     67       1.1  tshiozak 	/*
     68       1.1  tshiozak 	 * Skip (span) leading delimiters (s += wcsspn(s, delim), sort of).
     69       1.1  tshiozak 	 */
     70       1.1  tshiozak cont:
     71       1.1  tshiozak 	c = *s++;
     72       1.1  tshiozak 	for (spanp = delim; (sc = *spanp++) != L'\0';) {
     73       1.1  tshiozak 		if (c == sc)
     74       1.1  tshiozak 			goto cont;
     75       1.1  tshiozak 	}
     76       1.1  tshiozak 
     77       1.1  tshiozak 	if (c == L'\0') {	/* no non-delimiter characters */
     78       1.1  tshiozak 		*last = NULL;
     79       1.1  tshiozak 		return (NULL);
     80       1.1  tshiozak 	}
     81       1.1  tshiozak 	tok = s - 1;
     82       1.1  tshiozak 
     83       1.1  tshiozak 	/*
     84       1.1  tshiozak 	 * Scan token (scan for delimiters: s += wcscspn(s, delim), sort of).
     85       1.1  tshiozak 	 * Note that delim must have one NUL; we stop if we see that, too.
     86       1.1  tshiozak 	 */
     87       1.1  tshiozak 	for (;;) {
     88       1.1  tshiozak 		c = *s++;
     89       1.1  tshiozak 		spanp = delim;
     90       1.1  tshiozak 		do {
     91       1.1  tshiozak 			if ((sc = *spanp++) == c) {
     92       1.1  tshiozak 				if (c == L'\0')
     93       1.1  tshiozak 					s = NULL;
     94       1.1  tshiozak 				else
     95       1.1  tshiozak 					s[-1] = L'\0';
     96       1.1  tshiozak 				*last = s;
     97       1.1  tshiozak 				return (tok);
     98       1.1  tshiozak 			}
     99       1.1  tshiozak 		} while (sc != L'\0');
    100       1.1  tshiozak 	}
    101       1.1  tshiozak 	/* NOTREACHED */
    102       1.1  tshiozak }
    103