wcstok.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*-
  2. * Copyright (c) 1998 Softweyr LLC. All rights reserved.
  3. *
  4. * strtok_r, from Berkeley strtok
  5. * Oct 13, 1998 by Wes Peters <[email protected]>
  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. * 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
  26. * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  28. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  31. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <sys/cdefs.h>
  35. __FBSDID("$FreeBSD$");
  36. #include <wchar.h>
  37. wchar_t *
  38. wcstok(wchar_t * __restrict s, const wchar_t * __restrict delim,
  39. wchar_t ** __restrict last)
  40. {
  41. const wchar_t *spanp;
  42. wchar_t *tok;
  43. wchar_t c, sc;
  44. if (s == NULL && (s = *last) == NULL)
  45. return (NULL);
  46. /*
  47. * Skip (span) leading delimiters (s += wcsspn(s, delim), sort of).
  48. */
  49. cont:
  50. c = *s++;
  51. for (spanp = delim; (sc = *spanp++) != L'\0';) {
  52. if (c == sc)
  53. goto cont;
  54. }
  55. if (c == L'\0') { /* no non-delimiter characters */
  56. *last = NULL;
  57. return (NULL);
  58. }
  59. tok = s - 1;
  60. /*
  61. * Scan token (scan for delimiters: s += wcscspn(s, delim), sort of).
  62. * Note that delim must have one NUL; we stop if we see that, too.
  63. */
  64. for (;;) {
  65. c = *s++;
  66. spanp = delim;
  67. do {
  68. if ((sc = *spanp++) == c) {
  69. if (c == L'\0')
  70. s = NULL;
  71. else
  72. s[-1] = L'\0';
  73. *last = s;
  74. return (tok);
  75. }
  76. } while (sc != L'\0');
  77. }
  78. /* NOTREACHED */
  79. }