Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions libc/src/wchar/wchar_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@
namespace LIBC_NAMESPACE_DECL {
namespace internal {

// returns true if the character exists in the string
LIBC_INLINE static bool wcschr(wchar_t c, const wchar_t *str) {
for (int n = 0; str[n]; ++n) {
if (str[n] == c)
return true;
}
return false;
LIBC_INLINE static wchar_t *wcschr(const wchar_t *s, wchar_t c) {
for (; *s && *s != c; ++s)
;
return (*s == c) ? s : nullptr;
}

// bool should be true for wcscspn for complimentary span
Expand Down
7 changes: 2 additions & 5 deletions libc/src/wchar/wcschr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@
#include "hdr/types/wchar_t.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "wchar_utils.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(const wchar_t *, wcschr, (const wchar_t *s, wchar_t c)) {
for (; *s && *s != c; ++s)
;
if (*s == c)
return s;
return nullptr;
return internal::wcschr(s, c);
}

} // namespace LIBC_NAMESPACE_DECL
10 changes: 1 addition & 9 deletions libc/src/wchar/wcspbrk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,14 @@

namespace LIBC_NAMESPACE_DECL {

bool contains_char(const wchar_t *str, wchar_t target) {
for (; *str != L'\0'; str++)
if (*str == target)
return true;

return false;
}

LLVM_LIBC_FUNCTION(const wchar_t *, wcspbrk,
(const wchar_t *src, const wchar_t *breakset)) {
LIBC_CRASH_ON_NULLPTR(src);
LIBC_CRASH_ON_NULLPTR(breakset);

// currently O(n * m), can be further optimized to O(n + m) with a hash set
for (int src_idx = 0; src[src_idx] != 0; src_idx++)
if (contains_char(breakset, src[src_idx]))
if (internal::wcschr(breakset, src[src_idx]))
return src + src_idx;

return nullptr;
Expand Down
13 changes: 3 additions & 10 deletions libc/src/wchar/wcstok.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,8 @@

namespace LIBC_NAMESPACE_DECL {

bool isADelimeter(wchar_t wc, const wchar_t *delimiters) {
for (const wchar_t *delim_ptr = delimiters; *delim_ptr != L'\0'; ++delim_ptr)
if (wc == *delim_ptr)
return true;
return false;
}

LLVM_LIBC_FUNCTION(wchar_t *, wcstok,
(wchar_t *__restrict str, const wchar_t *__restrict delim,
(wchar_t *__restrict str, const wchar_t *__restrict delims,
wchar_t **__restrict context)) {
if (str == nullptr) {
if (*context == nullptr)
Expand All @@ -31,11 +24,11 @@ LLVM_LIBC_FUNCTION(wchar_t *, wcstok,
}

wchar_t *tok_start, *tok_end;
for (tok_start = str; *tok_start != L'\0' && isADelimeter(*tok_start, delim);
for (tok_start = str; *tok_start != L'\0' && wcschr(delims, *tok_start);
++tok_start)
;

for (tok_end = tok_start; *tok_end != L'\0' && !isADelimeter(*tok_end, delim);
for (tok_end = tok_start; *tok_end != L'\0' && !wcschr(delims, *tok_end);
++tok_end)
;

Expand Down
Loading