Skip to content

Commit cfba771

Browse files
sribee8Sriya Pratipati
andauthored
WCSChr Implementation (#141690)
implemented wcschr and tests --------- Co-authored-by: Sriya Pratipati <[email protected]>
1 parent f9ae8aa commit cfba771

File tree

7 files changed

+136
-0
lines changed

7 files changed

+136
-0
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ set(TARGET_LIBC_ENTRYPOINTS
364364
libc.src.wchar.btowc
365365
libc.src.wchar.wcslen
366366
libc.src.wchar.wctob
367+
libc.src.wchar.wcschr
367368

368369
# sys/uio.h entrypoints
369370
libc.src.sys.uio.writev

libc/include/wchar.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,10 @@ functions:
2727
return_type: wint_t
2828
arguments:
2929
- type: int
30+
- name: wcschr
31+
standards:
32+
- stdc
33+
return_type: const wchar_t *
34+
arguments:
35+
- type: const wchar_t *
36+
- type: wchar_t

libc/src/wchar/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,14 @@ add_entrypoint_object(
3333
libc.hdr.wchar_macros
3434
libc.src.__support.wctype_utils
3535
)
36+
37+
add_entrypoint_object(
38+
wcschr
39+
SRCS
40+
wcschr.cpp
41+
HDRS
42+
wcschr.h
43+
DEPENDS
44+
libc.hdr.wchar_macros
45+
libc.src.__support.wctype_utils
46+
)

libc/src/wchar/wcschr.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===-- Implementation of wcschr ------------------------------------------===//
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/wcschr.h"
10+
11+
#include "hdr/types/wchar_t.h"
12+
#include "src/__support/common.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(const wchar_t *, wcschr, (const wchar_t *s, wchar_t c)) {
18+
for (; *s && *s != c; ++s)
19+
;
20+
if (*s == c)
21+
return s;
22+
return nullptr;
23+
}
24+
25+
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wcschr.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for wcschr ----------------------------------===//
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+
#ifndef LLVM_LIBC_SRC_WCHAR_WCSCHR_H
10+
#define LLVM_LIBC_SRC_WCHAR_WCSCHR_H
11+
12+
#include "hdr/types/wchar_t.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
const wchar_t *wcschr(const wchar_t *s, wchar_t c);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_WCHAR_WCSCHR_H

libc/test/src/wchar/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,13 @@ add_libc_test(
3232
DEPENDS
3333
libc.src.wchar.wctob
3434
)
35+
36+
add_libc_test(
37+
wcschr_test
38+
SUITE
39+
libc_wchar_unittests
40+
SRCS
41+
wcschr_test.cpp
42+
DEPENDS
43+
libc.src.wchar.wcschr
44+
)

libc/test/src/wchar/wcschr_test.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//===-- Unittests for wcschr ----------------------------------------------===//
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 "hdr/types/wchar_t.h"
10+
#include "src/wchar/wcschr.h"
11+
#include "test/UnitTest/Test.h"
12+
13+
TEST(LlvmLibcWCSChrTest, FindsFirstCharacter) {
14+
// Should return pointer to original string since 'a' is the first character.
15+
const wchar_t *src = L"abcde";
16+
ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'a'), src);
17+
}
18+
19+
TEST(LlvmLibcWCSChrTest, FindsMiddleCharacter) {
20+
// Should return pointer to 'c'.
21+
const wchar_t *src = L"abcde";
22+
ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'c'), (src + 2));
23+
}
24+
25+
TEST(LlvmLibcWCSChrTest, FindsLastCharacterThatIsNotNullTerminator) {
26+
// Should return pointer to 'e'.
27+
const wchar_t *src = L"abcde";
28+
ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'e'), (src + 4));
29+
}
30+
31+
TEST(LlvmLibcWCSChrTest, FindsNullTerminator) {
32+
// Should return pointer to null terminator.
33+
const wchar_t *src = L"abcde";
34+
ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'\0'), (src + 5));
35+
}
36+
37+
TEST(LlvmLibcWCSChrTest, CharacterNotWithinStringShouldReturnNullptr) {
38+
// Since 'z' is not within the string, should return nullptr.
39+
const wchar_t *src = L"abcde";
40+
ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'z'), nullptr);
41+
}
42+
43+
TEST(LlvmLibcWCSChrTest, ShouldFindFirstOfDuplicates) {
44+
// Should return pointer to the first '1'.
45+
const wchar_t *src = L"abc1def1ghi";
46+
ASSERT_EQ((int)(LIBC_NAMESPACE::wcschr(src, L'1') - src), 3);
47+
48+
// Should return original string since 'X' is the first character.
49+
const wchar_t *dups = L"XXXXX";
50+
ASSERT_EQ(LIBC_NAMESPACE::wcschr(dups, L'X'), dups);
51+
}
52+
53+
TEST(LlvmLibcWCSChrTest, EmptyStringShouldOnlyMatchNullTerminator) {
54+
// Null terminator should match
55+
const wchar_t *src = L"";
56+
ASSERT_EQ(src, LIBC_NAMESPACE::wcschr(src, L'\0'));
57+
// All other characters should not match
58+
ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'Z'), nullptr);
59+
ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'3'), nullptr);
60+
ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'*'), nullptr);
61+
}

0 commit comments

Comments
 (0)