Skip to content

WCSChr Implementation #141690

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 2 commits into from
May 28, 2025
Merged

WCSChr Implementation #141690

merged 2 commits into from
May 28, 2025

Conversation

sribee8
Copy link
Contributor

@sribee8 sribee8 commented May 27, 2025

implemented wcschr and tests

implemented wcschr and tests
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the libc label May 27, 2025
@llvmbot
Copy link
Member

llvmbot commented May 27, 2025

@llvm/pr-subscribers-libc

Author: None (sribee8)

Changes

implemented wcschr and tests


Full diff: https://github.com/llvm/llvm-project/pull/141690.diff

7 Files Affected:

  • (modified) libc/config/linux/x86_64/entrypoints.txt (+1)
  • (modified) libc/include/wchar.yaml (+7)
  • (modified) libc/src/wchar/CMakeLists.txt (+11)
  • (added) libc/src/wchar/wcschr.cpp (+23)
  • (added) libc/src/wchar/wcschr.h (+21)
  • (modified) libc/test/src/wchar/CMakeLists.txt (+10)
  • (added) libc/test/src/wchar/wcschr_test.cpp (+62)
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 9f447dd0d35d2..52e746e32a1cd 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -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
diff --git a/libc/include/wchar.yaml b/libc/include/wchar.yaml
index 0ac9aa29f0a18..987bdc0b806dc 100644
--- a/libc/include/wchar.yaml
+++ b/libc/include/wchar.yaml
@@ -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
diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index 703db75b5b194..41034cab16d4d 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -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
+)
diff --git a/libc/src/wchar/wcschr.cpp b/libc/src/wchar/wcschr.cpp
new file mode 100644
index 0000000000000..99723a0e54de6
--- /dev/null
+++ b/libc/src/wchar/wcschr.cpp
@@ -0,0 +1,23 @@
+//===-- 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
diff --git a/libc/src/wchar/wcschr.h b/libc/src/wchar/wcschr.h
new file mode 100644
index 0000000000000..70bf2839f2e9e
--- /dev/null
+++ b/libc/src/wchar/wcschr.h
@@ -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
diff --git a/libc/test/src/wchar/CMakeLists.txt b/libc/test/src/wchar/CMakeLists.txt
index d41e328fc9d90..7d64dfeb13b6e 100644
--- a/libc/test/src/wchar/CMakeLists.txt
+++ b/libc/test/src/wchar/CMakeLists.txt
@@ -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
+)
diff --git a/libc/test/src/wchar/wcschr_test.cpp b/libc/test/src/wchar/wcschr_test.cpp
new file mode 100644
index 0000000000000..8ceaf62390144
--- /dev/null
+++ b/libc/test/src/wchar/wcschr_test.cpp
@@ -0,0 +1,62 @@
+//===-- 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);
+}
+

@michaelrj-google michaelrj-google self-requested a review May 27, 2025 23:50
Copy link

github-actions bot commented May 27, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

fixed the formatting of some files
Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for writing this!

@michaelrj-google
Copy link
Contributor

Premerge failure seems to be unrelated. Merging.

@michaelrj-google michaelrj-google merged commit cfba771 into llvm:main May 28, 2025
15 of 16 checks passed
Copy link

@sribee8 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

google-yfyang pushed a commit to google-yfyang/llvm-project that referenced this pull request May 29, 2025
implemented wcschr and tests

---------

Co-authored-by: Sriya Pratipati <sriyap@google.com>
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
implemented wcschr and tests

---------

Co-authored-by: Sriya Pratipati <sriyap@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants