WCSChr Implementation (#141690)
implemented wcschr and tests --------- Co-authored-by: Sriya Pratipati <sriyap@google.com>
This commit is contained in:
@@ -364,6 +364,7 @@ set(TARGET_LIBC_ENTRYPOINTS
|
||||
libc.src.wchar.btowc
|
||||
libc.src.wchar.wcslen
|
||||
libc.src.wchar.wctob
|
||||
libc.src.wchar.wcschr
|
||||
|
||||
# sys/uio.h entrypoints
|
||||
libc.src.sys.uio.writev
|
||||
|
||||
@@ -27,3 +27,10 @@ functions:
|
||||
return_type: wint_t
|
||||
arguments:
|
||||
- type: int
|
||||
- name: wcschr
|
||||
standards:
|
||||
- stdc
|
||||
return_type: const wchar_t *
|
||||
arguments:
|
||||
- type: const wchar_t *
|
||||
- type: wchar_t
|
||||
|
||||
@@ -33,3 +33,14 @@ add_entrypoint_object(
|
||||
libc.hdr.wchar_macros
|
||||
libc.src.__support.wctype_utils
|
||||
)
|
||||
|
||||
add_entrypoint_object(
|
||||
wcschr
|
||||
SRCS
|
||||
wcschr.cpp
|
||||
HDRS
|
||||
wcschr.h
|
||||
DEPENDS
|
||||
libc.hdr.wchar_macros
|
||||
libc.src.__support.wctype_utils
|
||||
)
|
||||
|
||||
25
libc/src/wchar/wcschr.cpp
Normal file
25
libc/src/wchar/wcschr.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
//===-- Implementation of wcschr ------------------------------------------===//
|
||||
//
|
||||
// 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/wcschr.h"
|
||||
|
||||
#include "hdr/types/wchar_t.h"
|
||||
#include "src/__support/common.h"
|
||||
#include "src/__support/macros/config.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;
|
||||
}
|
||||
|
||||
} // namespace LIBC_NAMESPACE_DECL
|
||||
21
libc/src/wchar/wcschr.h
Normal file
21
libc/src/wchar/wcschr.h
Normal file
@@ -0,0 +1,21 @@
|
||||
//===-- Implementation header for wcschr ----------------------------------===//
|
||||
//
|
||||
// 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_WCSCHR_H
|
||||
#define LLVM_LIBC_SRC_WCHAR_WCSCHR_H
|
||||
|
||||
#include "hdr/types/wchar_t.h"
|
||||
#include "src/__support/macros/config.h"
|
||||
|
||||
namespace LIBC_NAMESPACE_DECL {
|
||||
|
||||
const wchar_t *wcschr(const wchar_t *s, wchar_t c);
|
||||
|
||||
} // namespace LIBC_NAMESPACE_DECL
|
||||
|
||||
#endif // LLVM_LIBC_SRC_WCHAR_WCSCHR_H
|
||||
@@ -32,3 +32,13 @@ add_libc_test(
|
||||
DEPENDS
|
||||
libc.src.wchar.wctob
|
||||
)
|
||||
|
||||
add_libc_test(
|
||||
wcschr_test
|
||||
SUITE
|
||||
libc_wchar_unittests
|
||||
SRCS
|
||||
wcschr_test.cpp
|
||||
DEPENDS
|
||||
libc.src.wchar.wcschr
|
||||
)
|
||||
|
||||
61
libc/test/src/wchar/wcschr_test.cpp
Normal file
61
libc/test/src/wchar/wcschr_test.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
//===-- Unittests for wcschr ----------------------------------------------===//
|
||||
//
|
||||
// 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/wchar_t.h"
|
||||
#include "src/wchar/wcschr.h"
|
||||
#include "test/UnitTest/Test.h"
|
||||
|
||||
TEST(LlvmLibcWCSChrTest, FindsFirstCharacter) {
|
||||
// Should return pointer to original string since 'a' is the first character.
|
||||
const wchar_t *src = L"abcde";
|
||||
ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'a'), src);
|
||||
}
|
||||
|
||||
TEST(LlvmLibcWCSChrTest, FindsMiddleCharacter) {
|
||||
// Should return pointer to 'c'.
|
||||
const wchar_t *src = L"abcde";
|
||||
ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'c'), (src + 2));
|
||||
}
|
||||
|
||||
TEST(LlvmLibcWCSChrTest, FindsLastCharacterThatIsNotNullTerminator) {
|
||||
// Should return pointer to 'e'.
|
||||
const wchar_t *src = L"abcde";
|
||||
ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'e'), (src + 4));
|
||||
}
|
||||
|
||||
TEST(LlvmLibcWCSChrTest, FindsNullTerminator) {
|
||||
// Should return pointer to null terminator.
|
||||
const wchar_t *src = L"abcde";
|
||||
ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'\0'), (src + 5));
|
||||
}
|
||||
|
||||
TEST(LlvmLibcWCSChrTest, CharacterNotWithinStringShouldReturnNullptr) {
|
||||
// Since 'z' is not within the string, should return nullptr.
|
||||
const wchar_t *src = L"abcde";
|
||||
ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'z'), nullptr);
|
||||
}
|
||||
|
||||
TEST(LlvmLibcWCSChrTest, ShouldFindFirstOfDuplicates) {
|
||||
// Should return pointer to the first '1'.
|
||||
const wchar_t *src = L"abc1def1ghi";
|
||||
ASSERT_EQ((int)(LIBC_NAMESPACE::wcschr(src, L'1') - src), 3);
|
||||
|
||||
// Should return original string since 'X' is the first character.
|
||||
const wchar_t *dups = L"XXXXX";
|
||||
ASSERT_EQ(LIBC_NAMESPACE::wcschr(dups, L'X'), dups);
|
||||
}
|
||||
|
||||
TEST(LlvmLibcWCSChrTest, EmptyStringShouldOnlyMatchNullTerminator) {
|
||||
// Null terminator should match
|
||||
const wchar_t *src = L"";
|
||||
ASSERT_EQ(src, LIBC_NAMESPACE::wcschr(src, L'\0'));
|
||||
// All other characters should not match
|
||||
ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'Z'), nullptr);
|
||||
ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'3'), nullptr);
|
||||
ASSERT_EQ(LIBC_NAMESPACE::wcschr(src, L'*'), nullptr);
|
||||
}
|
||||
Reference in New Issue
Block a user