Skip to content

[libc] wcsspn implementation #142034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.wchar.wctob
libc.src.wchar.wmemset
libc.src.wchar.wcschr
libc.src.wchar.wcsspn
libc.src.wchar.wmemcmp

# sys/uio.h entrypoints
Expand Down
7 changes: 7 additions & 0 deletions libc/include/wchar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ functions:
arguments:
- type: const wchar_t *
- type: wchar_t
- name: wcsspn
standards:
- stdc
return_type: size_t
arguments:
- type: const wchar_t *
- type: const wchar_t *
- name: wmemcmp
standards:
- stdc
Expand Down
12 changes: 12 additions & 0 deletions libc/src/wchar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ add_entrypoint_object(
libc.src.__support.wctype_utils
)

add_entrypoint_object(
wcsspn
SRCS
wcsspn.cpp
HDRS
wcsspn.h
DEPENDS
libc.hdr.wchar_macros
libc.hdr.types.size_t
libc.src.__support.wctype_utils
)

add_entrypoint_object(
wmemcmp
SRCS
Expand Down
34 changes: 34 additions & 0 deletions libc/src/wchar/wcsspn.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//===-- Implementation of wcsspn ------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "src/wchar/wcsspn.h"

#include "hdr/types/size_t.h"
#include "hdr/types/wchar_t.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.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;
}

} // namespace LIBC_NAMESPACE_DECL
22 changes: 22 additions & 0 deletions libc/src/wchar/wcsspn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Implementation header for wcsspn ----------------------------------===//
//
// 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_WCSSPN_H
#define LLVM_LIBC_SRC_WCHAR_WCSSPN_H

#include "hdr/types/size_t.h"
#include "hdr/types/wchar_t.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

size_t wcsspn(const wchar_t *s1, const wchar_t *s2);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_WCHAR_WCSCHR_H
10 changes: 10 additions & 0 deletions libc/test/src/wchar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ add_libc_test(
libc.src.wchar.wcschr
)

add_libc_test(
wcsspn_test
SUITE
libc_wchar_unittests
SRCS
wcsspn_test.cpp
DEPENDS
libc.src.wchar.wcsspn
)

add_libc_test(
wmemcmp_test
SUITE
Expand Down
86 changes: 86 additions & 0 deletions libc/test/src/wchar/wcsspn_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//===-- Unittests for wcsspn ----------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "hdr/types/size_t.h"
#include "hdr/types/wchar_t.h"
#include "src/wchar/wcsspn.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcWCSSpnTest, EmptyStringShouldReturnZeroLengthSpan) {
// The search should not include the null terminator.
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(L"", L""), size_t{0});
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(L"_", L""), size_t{0});
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(L"", L"_"), size_t{0});
}

TEST(LlvmLibcWCSSpnTest, ShouldNotSpanAnythingAfterNullTerminator) {
const wchar_t src[4] = {'a', 'b', '\0', 'c'};
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(src, L"ab"), size_t{2});
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(src, L"c"), size_t{0});

// Same goes for the segment to be searched for.
const wchar_t segment[4] = {'1', '2', '\0', '3'};
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(L"123", segment), size_t{2});
}

TEST(LlvmLibcWCSSpnTest, SpanEachIndividualCharacter) {
const wchar_t *src = L"12345";
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(src, L"1"), size_t{1});
// Since '1' is not within the segment, the span
// size should remain zero.
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(src, L"2"), size_t{0});
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(src, L"3"), size_t{0});
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(src, L"4"), size_t{0});
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(src, L"5"), size_t{0});
}

TEST(LlvmLibcWCSSpnTest, UnmatchedCharacterShouldNotBeCountedInSpan) {
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(L"a", L"b"), size_t{0});
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(L"abcdef", L"1"), size_t{0});
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(L"123", L"4"), size_t{0});
}

TEST(LlvmLibcWCSSpnTest, SequentialCharactersShouldSpan) {
const wchar_t *src = L"abcde";
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(src, L"a"), size_t{1});
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(src, L"ab"), size_t{2});
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(src, L"abc"), size_t{3});
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(src, L"abcd"), size_t{4});
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(src, L"abcde"), size_t{5});
// Same thing for when the roles are reversed.
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(L"abcde", src), size_t{5});
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(L"abcd", src), size_t{4});
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(L"abc", src), size_t{3});
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(L"ab", src), size_t{2});
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(L"a", src), size_t{1});
}

TEST(LlvmLibcWCSSpnTest, NonSequentialCharactersShouldNotSpan) {
const wchar_t *src = L"123456789";
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(src, L"_1_abc_2_def_3_"), size_t{3});
// Only spans 4 since '5' is not within the span.
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(src, L"67__34abc12"), size_t{4});
}

TEST(LlvmLibcWCSSpnTest, ReverseCharacters) {
// Since these are still sequential, this should span.
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(L"12345", L"54321"), size_t{5});
// Does not span any since '1' is not within the span.
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(L"12345", L"432"), size_t{0});
// Only spans 1 since '2' is not within the span.
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(L"12345", L"51"), size_t{1});
}

TEST(LlvmLibcWCSSpnTest, DuplicatedCharactersToBeSearchedForShouldStillMatch) {
// Only a single character, so only spans 1.
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(L"a", L"aa"), size_t{1});
// This should count once for each 'a' in the source string.
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(L"aa", L"aa"), size_t{2});
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(L"aaa", L"aa"), size_t{3});
EXPECT_EQ(LIBC_NAMESPACE::wcsspn(L"aaaa", L"aa"), size_t{4});
}
Loading