1//===-- Implementation of wcstok ------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "src/wchar/wcstok.h"
10
11#include "hdr/types/wchar_t.h"
12#include "src/__support/common.h"
13
14namespace LIBC_NAMESPACE_DECL {
15
16bool isADelimeter(wchar_t wc, const wchar_t *delimiters) {
17 for (const wchar_t *delim_ptr = delimiters; *delim_ptr != L'\0'; ++delim_ptr)
18 if (wc == *delim_ptr)
19 return true;
20 return false;
21}
22
23LLVM_LIBC_FUNCTION(wchar_t *, wcstok,
24 (wchar_t *__restrict str, const wchar_t *__restrict delim,
25 wchar_t **__restrict context)) {
26 if (str == nullptr) {
27 if (*context == nullptr)
28 return nullptr;
29
30 str = *context;
31 }
32
33 wchar_t *tok_start, *tok_end;
34 for (tok_start = str; *tok_start != L'\0' && isADelimeter(*tok_start, delim);
35 ++tok_start)
36 ;
37
38 for (tok_end = tok_start; *tok_end != L'\0' && !isADelimeter(*tok_end, delim);
39 ++tok_end)
40 ;
41
42 if (*tok_end != L'\0') {
43 *tok_end = L'\0';
44 ++tok_end;
45 }
46 *context = tok_end;
47 return *tok_start == L'\0' ? nullptr : tok_start;
48}
49
50} // namespace LIBC_NAMESPACE_DECL
51

source code of libc/src/wchar/wcstok.cpp