diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt index 7ace1a6ca66ba..159778df6acca 100644 --- a/libc/src/wchar/CMakeLists.txt +++ b/libc/src/wchar/CMakeLists.txt @@ -1,3 +1,13 @@ +add_header_library( + wchar_utils + HDRS + wchar_utils.h + DEPENDS + libc.hdr.types.size_t + libc.hdr.types.wchar_t + libc.src.__support.common +) + add_entrypoint_object( wcslen SRCS @@ -255,6 +265,8 @@ add_entrypoint_object( DEPENDS libc.hdr.wchar_macros libc.hdr.types.size_t + libc.src.wchar.wchar_utils + libc.src.__support.macros.null_check ) add_entrypoint_object( @@ -266,6 +278,8 @@ add_entrypoint_object( DEPENDS libc.hdr.wchar_macros libc.hdr.types.size_t + libc.src.wchar.wchar_utils + libc.src.__support.macros.null_check ) add_entrypoint_object( diff --git a/libc/src/wchar/wchar_utils.h b/libc/src/wchar/wchar_utils.h new file mode 100644 index 0000000000000..e0218c7d89b1f --- /dev/null +++ b/libc/src/wchar/wchar_utils.h @@ -0,0 +1,45 @@ +//===-- wchar utils ---------------------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_WCHAR_WCHAR_UTILS_H +#define LLVM_LIBC_SRC_WCHAR_WCHAR_UTILS_H + +#include "hdr/types/size_t.h" +#include "hdr/types/wchar_t.h" +#include "src/__support/common.h" +#include "src/__support/macros/attributes.h" // LIBC_INLINE + +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; +} + +// bool should be true for wcscspn for complimentary span +// should be false for wcsspn since we want it to span +LIBC_INLINE static size_t wcsspn(const wchar_t *s1, const wchar_t *s2, + bool not_match_set) { + size_t i = 0; + for (; s1[i]; ++i) { + bool in_set = wcschr(s1[i], s2); + if (in_set == not_match_set) + return i; + } + return i; +} + +} // namespace internal +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC_WCHAR_WCHAR_UTILS_H diff --git a/libc/src/wchar/wcscspn.cpp b/libc/src/wchar/wcscspn.cpp index 8869d84cdfdee..34f3451b19c30 100644 --- a/libc/src/wchar/wcscspn.cpp +++ b/libc/src/wchar/wcscspn.cpp @@ -12,23 +12,15 @@ #include "hdr/types/wchar_t.h" #include "src/__support/common.h" #include "src/__support/macros/config.h" +#include "src/__support/macros/null_check.h" +#include "wchar_utils.h" namespace LIBC_NAMESPACE_DECL { -bool check(wchar_t c, const wchar_t *s2) { - for (int n = 0; s2[n]; ++n) { - if (s2[n] == c) - return false; - } - return true; -} LLVM_LIBC_FUNCTION(size_t, wcscspn, (const wchar_t *s1, const wchar_t *s2)) { - size_t i = 0; - for (; s1[i]; ++i) { - if (!check(s1[i], s2)) - return i; - } - return i; + LIBC_CRASH_ON_NULLPTR(s1); + LIBC_CRASH_ON_NULLPTR(s2); + return internal::wcsspn(s1, s2, /*not_match_set=*/true); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/wchar/wcsspn.cpp b/libc/src/wchar/wcsspn.cpp index 23de381a2d954..ae2cf5a41ae41 100644 --- a/libc/src/wchar/wcsspn.cpp +++ b/libc/src/wchar/wcsspn.cpp @@ -12,23 +12,15 @@ #include "hdr/types/wchar_t.h" #include "src/__support/common.h" #include "src/__support/macros/config.h" +#include "src/__support/macros/null_check.h" +#include "wchar_utils.h" namespace LIBC_NAMESPACE_DECL { -bool check(wchar_t c, const wchar_t *s2) { - for (int n = 0; s2[n]; ++n) { - if (s2[n] == c) - return true; - } - return false; -} LLVM_LIBC_FUNCTION(size_t, wcsspn, (const wchar_t *s1, const wchar_t *s2)) { - size_t i = 0; - for (; s1[i]; ++i) { - if (!check(s1[i], s2)) - return i; - } - return i; + LIBC_CRASH_ON_NULLPTR(s1); + LIBC_CRASH_ON_NULLPTR(s2); + return internal::wcsspn(s1, s2, /*not_match_set=*/false); } } // namespace LIBC_NAMESPACE_DECL diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel index 053d15869246b..5676c454c1120 100644 --- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel @@ -6059,6 +6059,17 @@ libc_function( ############################## wchar targets ############################### +libc_support_library( + name = "wchar_utils", + hdrs = ["src/wchar/wchar_utils.h"], + deps = [ + ":__support_common", + ":__support_macros_attributes", + ":types_size_t", + ":types_wchar_t", + ], +) + libc_function( name = "btowc", srcs = ["src/wchar/btowc.cpp"], @@ -6156,6 +6167,7 @@ libc_function( ":__support_macros_config", ":types_size_t", ":types_wchar_t", + ":wchar_utils", ], ) @@ -6267,6 +6279,7 @@ libc_function( ":__support_macros_config", ":types_size_t", ":types_wchar_t", + ":wchar_utils", ], )