[libc-commits] [libc] [libc] add wmemchr, wcslen, wcschr, wcsrchr, wcspbrk (PR #121183)

Tristan Ross via libc-commits libc-commits at lists.llvm.org
Fri Dec 27 11:48:23 PST 2024


https://github.com/RossComputerGuy updated https://github.com/llvm/llvm-project/pull/121183

>From 58af7c8de18c65a0cd269db3e2d188bb7fed4221 Mon Sep 17 00:00:00 2001
From: Tristan Ross <tristan.ross at midstall.com>
Date: Thu, 26 Dec 2024 22:30:08 -0800
Subject: [PATCH 1/6] [libc] add wmemchr

---
 libc/config/gpu/entrypoints.txt           |  1 +
 libc/config/linux/aarch64/entrypoints.txt |  1 +
 libc/config/linux/riscv/entrypoints.txt   |  1 +
 libc/config/linux/x86_64/entrypoints.txt  |  1 +
 libc/hdrgen/yaml/wchar.yaml               |  8 +++++++
 libc/src/wchar/CMakeLists.txt             | 12 +++++++++++
 libc/src/wchar/wmemchr.cpp                | 26 +++++++++++++++++++++++
 libc/src/wchar/wmemchr.h                  | 22 +++++++++++++++++++
 8 files changed, 72 insertions(+)
 create mode 100644 libc/src/wchar/wmemchr.cpp
 create mode 100644 libc/src/wchar/wmemchr.h

diff --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt
index b008e0e6684fdd..a06db2e822bb45 100644
--- a/libc/config/gpu/entrypoints.txt
+++ b/libc/config/gpu/entrypoints.txt
@@ -261,6 +261,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.time.nanosleep
 
     # wchar.h entrypoints
+    libc.src.wchar.wmemchr
     libc.src.wchar.wctob
 
     # locale.h entrypoints
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 00f0c6a8bfb8e4..f4ceae9401cc6a 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -349,6 +349,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wmemchr
     libc.src.wchar.wctob
 )
 
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 49a8d61b938027..d0226a2fba673a 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -346,6 +346,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wmemchr
     libc.src.wchar.wctob
 )
 
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 7e549607716c02..404003d74fe0be 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -348,6 +348,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wmemchr
     libc.src.wchar.wctob
     libc.src.wchar.btowc
 )
diff --git a/libc/hdrgen/yaml/wchar.yaml b/libc/hdrgen/yaml/wchar.yaml
index bc824b21d8be17..5325e1ce4ede55 100644
--- a/libc/hdrgen/yaml/wchar.yaml
+++ b/libc/hdrgen/yaml/wchar.yaml
@@ -14,3 +14,11 @@ functions:
     return_type: int
     arguments:
       - type: wint_t
+  - name: wmemchr
+    standards:
+      - stdc
+    return_type: const wchar_t *
+    arguments:
+      - type: const wchar_t *
+      - type: wchar_t
+      - type: size_t
diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index d4c98ea527a8f9..7ae99f03ceecb0 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -22,3 +22,15 @@ add_entrypoint_object(
     libc.hdr.wchar_macros
     libc.src.__support.wctype_utils
 )
+
+add_entrypoint_object(
+  wmemchr
+  SRCS
+    wmemchr.cpp
+  HDRS
+    wmemchr.h
+  DEPENDS
+    libc.hdr.types.size_t
+    libc.hdr.types.wchar_t
+    libc.src.__support.wctype_utils
+)
diff --git a/libc/src/wchar/wmemchr.cpp b/libc/src/wchar/wmemchr.cpp
new file mode 100644
index 00000000000000..62191454343a8a
--- /dev/null
+++ b/libc/src/wchar/wmemchr.cpp
@@ -0,0 +1,26 @@
+//===-- Implementation of wmemchr -----------------------------------------===//
+//
+// 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/wmemchr.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(const wchar_t *, wmemchr,
+                   (const wchar_t *s, wchar_t c, size_t n)) {
+  for (size_t i = 0; i < n; i++) {
+    if (s[i] == c) {
+      return &s[i];
+    }
+  }
+
+  return nullptr;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/wchar/wmemchr.h b/libc/src/wchar/wmemchr.h
new file mode 100644
index 00000000000000..e47a0a75735939
--- /dev/null
+++ b/libc/src/wchar/wmemchr.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for wmemchr -----------------------*- 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_WMEMCHR_H
+#define LLVM_LIBC_SRC_WCHAR_WMEMCHR_H
+
+#include "hdr/types/size_t.h"
+#include "hdr/types/wchar_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+const wchar_t *wmemchr(const wchar_t *s, wchar_t c, size_t n);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_WCHAR_WMEMCHR_H

>From ed5869d0744ff3a0ae201a77bb96b41be9a0c9f9 Mon Sep 17 00:00:00 2001
From: Tristan Ross <tristan.ross at midstall.com>
Date: Thu, 26 Dec 2024 22:42:48 -0800
Subject: [PATCH 2/6] [libc] add wcslen

---
 libc/config/gpu/entrypoints.txt           |  1 +
 libc/config/linux/aarch64/entrypoints.txt |  1 +
 libc/config/linux/riscv/entrypoints.txt   |  1 +
 libc/config/linux/x86_64/entrypoints.txt  |  1 +
 libc/hdrgen/yaml/wchar.yaml               |  6 ++++++
 libc/src/wchar/wcslen.cpp                 | 22 ++++++++++++++++++++++
 libc/src/wchar/wcslen.h                   | 22 ++++++++++++++++++++++
 7 files changed, 54 insertions(+)
 create mode 100644 libc/src/wchar/wcslen.cpp
 create mode 100644 libc/src/wchar/wcslen.h

diff --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt
index a06db2e822bb45..4230c9adcea06e 100644
--- a/libc/config/gpu/entrypoints.txt
+++ b/libc/config/gpu/entrypoints.txt
@@ -261,6 +261,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.time.nanosleep
 
     # wchar.h entrypoints
+    libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
     libc.src.wchar.wctob
 
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index f4ceae9401cc6a..099dc93ad4a213 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -349,6 +349,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
     libc.src.wchar.wctob
 )
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index d0226a2fba673a..a5554cdba8a1a4 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -346,6 +346,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
     libc.src.wchar.wctob
 )
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 404003d74fe0be..9303c744623fb3 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -348,6 +348,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
     libc.src.wchar.wctob
     libc.src.wchar.btowc
diff --git a/libc/hdrgen/yaml/wchar.yaml b/libc/hdrgen/yaml/wchar.yaml
index 5325e1ce4ede55..ec773d84ff39ea 100644
--- a/libc/hdrgen/yaml/wchar.yaml
+++ b/libc/hdrgen/yaml/wchar.yaml
@@ -22,3 +22,9 @@ functions:
       - type: const wchar_t *
       - type: wchar_t
       - type: size_t
+  - name: wcslen
+    standards:
+      - stdc
+    return_type: size_t
+    arguments:
+      - type: const wchar_t *
diff --git a/libc/src/wchar/wcslen.cpp b/libc/src/wchar/wcslen.cpp
new file mode 100644
index 00000000000000..2e711af8b12bf7
--- /dev/null
+++ b/libc/src/wchar/wcslen.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of wcslen ------------------------------------------===//
+//
+// 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/wcslen.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(size_t, wcslen, (const wchar_t *s)) {
+  size_t length = 0;
+  while (s[length++])
+    ;
+  return length;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/wchar/wcslen.h b/libc/src/wchar/wcslen.h
new file mode 100644
index 00000000000000..f472a1d5e96315
--- /dev/null
+++ b/libc/src/wchar/wcslen.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for wcslen ------------------------*- 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_WCSLEN_H
+#define LLVM_LIBC_SRC_WCHAR_WCSLEN_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 wcslen(const wchar_t *s);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_WCHAR_WCSLEN_H

>From 36ec86166943589010a67444edf62d855fc2094e Mon Sep 17 00:00:00 2001
From: Tristan Ross <tristan.ross at midstall.com>
Date: Thu, 26 Dec 2024 22:49:52 -0800
Subject: [PATCH 3/6] [libc] add wcschr

---
 libc/config/gpu/entrypoints.txt           |  1 +
 libc/config/linux/aarch64/entrypoints.txt |  1 +
 libc/config/linux/riscv/entrypoints.txt   |  1 +
 libc/config/linux/x86_64/entrypoints.txt  |  1 +
 libc/hdrgen/yaml/wchar.yaml               |  7 +++++++
 libc/src/wchar/CMakeLists.txt             | 24 +++++++++++++++++++++++
 libc/src/wchar/wcschr.cpp                 | 21 ++++++++++++++++++++
 libc/src/wchar/wcschr.h                   | 21 ++++++++++++++++++++
 8 files changed, 77 insertions(+)
 create mode 100644 libc/src/wchar/wcschr.cpp
 create mode 100644 libc/src/wchar/wcschr.h

diff --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt
index 4230c9adcea06e..d861a7cdbc2273 100644
--- a/libc/config/gpu/entrypoints.txt
+++ b/libc/config/gpu/entrypoints.txt
@@ -261,6 +261,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.time.nanosleep
 
     # wchar.h entrypoints
+    libc.src.wchar.wcschr
     libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
     libc.src.wchar.wctob
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 099dc93ad4a213..ca46816991d52a 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -349,6 +349,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcschr
     libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
     libc.src.wchar.wctob
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index a5554cdba8a1a4..29a0456538bc64 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -346,6 +346,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcschr
     libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
     libc.src.wchar.wctob
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 9303c744623fb3..c2defa7ab7c3aa 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -348,6 +348,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcschr
     libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
     libc.src.wchar.wctob
diff --git a/libc/hdrgen/yaml/wchar.yaml b/libc/hdrgen/yaml/wchar.yaml
index ec773d84ff39ea..73e77f6438bc7a 100644
--- a/libc/hdrgen/yaml/wchar.yaml
+++ b/libc/hdrgen/yaml/wchar.yaml
@@ -28,3 +28,10 @@ functions:
     return_type: size_t
     arguments:
       - type: const wchar_t *
+  - 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 7ae99f03ceecb0..11a8ac0461464f 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -34,3 +34,27 @@ add_entrypoint_object(
     libc.hdr.types.wchar_t
     libc.src.__support.wctype_utils
 )
+
+add_entrypoint_object(
+  wcslen
+  SRCS
+    wcslen.cpp
+  HDRS
+    wcslen.h
+  DEPENDS
+    libc.hdr.types.size_t
+    libc.hdr.types.wchar_t
+    libc.src.__support.wctype_utils
+)
+
+add_entrypoint_object(
+  wcschr
+  SRCS
+    wcschr.cpp
+  HDRS
+    wcschr.h
+  DEPENDS
+    .wcslen
+    .wmemchr
+    libc.hdr.types.wchar_t
+)
diff --git a/libc/src/wchar/wcschr.cpp b/libc/src/wchar/wcschr.cpp
new file mode 100644
index 00000000000000..ebb24a64f639e1
--- /dev/null
+++ b/libc/src/wchar/wcschr.cpp
@@ -0,0 +1,21 @@
+//===-- 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 "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "wcslen.h"
+#include "wmemchr.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(const wchar_t *, wcschr, (const wchar_t *s, wchar_t c)) {
+  return wmemchr(s, c, wcslen(s));
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/wchar/wcschr.h b/libc/src/wchar/wcschr.h
new file mode 100644
index 00000000000000..6466d8e2ec2d62
--- /dev/null
+++ b/libc/src/wchar/wcschr.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for wmemchr -----------------------*- 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_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

>From 9dd063a3b84d9793f46741f86cdea3dfbefe971d Mon Sep 17 00:00:00 2001
From: Tristan Ross <tristan.ross at midstall.com>
Date: Thu, 26 Dec 2024 23:01:12 -0800
Subject: [PATCH 4/6] [libc] add wcsrchr

---
 libc/config/gpu/entrypoints.txt           |  1 +
 libc/config/linux/aarch64/entrypoints.txt |  1 +
 libc/config/linux/riscv/entrypoints.txt   |  1 +
 libc/config/linux/x86_64/entrypoints.txt  |  1 +
 libc/hdrgen/yaml/wchar.yaml               |  7 ++++++
 libc/src/wchar/CMakeLists.txt             | 11 ++++++++++
 libc/src/wchar/wcsrchr.cpp                | 26 +++++++++++++++++++++++
 libc/src/wchar/wcsrchr.h                  | 21 ++++++++++++++++++
 8 files changed, 69 insertions(+)
 create mode 100644 libc/src/wchar/wcsrchr.cpp
 create mode 100644 libc/src/wchar/wcsrchr.h

diff --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt
index d861a7cdbc2273..2720ecc2f9f772 100644
--- a/libc/config/gpu/entrypoints.txt
+++ b/libc/config/gpu/entrypoints.txt
@@ -261,6 +261,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.time.nanosleep
 
     # wchar.h entrypoints
+    libc.src.wchar.wcsrchr
     libc.src.wchar.wcschr
     libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index ca46816991d52a..bfc8224660dad0 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -349,6 +349,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcsrchr
     libc.src.wchar.wcschr
     libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 29a0456538bc64..50f1789bc60677 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -346,6 +346,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcsrchr
     libc.src.wchar.wcschr
     libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index c2defa7ab7c3aa..82e6fe688e619f 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -348,6 +348,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcsrchr
     libc.src.wchar.wcschr
     libc.src.wchar.wcslen
     libc.src.wchar.wmemchr
diff --git a/libc/hdrgen/yaml/wchar.yaml b/libc/hdrgen/yaml/wchar.yaml
index 73e77f6438bc7a..a1ef7c41e1492f 100644
--- a/libc/hdrgen/yaml/wchar.yaml
+++ b/libc/hdrgen/yaml/wchar.yaml
@@ -35,3 +35,10 @@ functions:
     arguments:
       - type: const wchar_t *
       - type: wchar_t
+  - name: wcsrchr
+    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 11a8ac0461464f..f5a9158d28e330 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -58,3 +58,14 @@ add_entrypoint_object(
     .wmemchr
     libc.hdr.types.wchar_t
 )
+
+add_entrypoint_object(
+  wcsrchr
+  SRCS
+    wcsrchr.cpp
+  HDRS
+    wcsrchr.h
+  DEPENDS
+    .wcslen
+    libc.hdr.types.wchar_t
+)
diff --git a/libc/src/wchar/wcsrchr.cpp b/libc/src/wchar/wcsrchr.cpp
new file mode 100644
index 00000000000000..a43ed30ce267b6
--- /dev/null
+++ b/libc/src/wchar/wcsrchr.cpp
@@ -0,0 +1,26 @@
+//===-- Implementation of wcsrchr
+//------------------------------------------===//
+//
+// 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/wcsrchr.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "wcslen.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(const wchar_t *, wcsrchr, (const wchar_t *s, wchar_t c)) {
+  size_t length = wcslen(s);
+  for (size_t i = 0; i < length; i++) {
+    if (s[length - i] == c)
+      return &s[length - i];
+  }
+  return nullptr;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/wchar/wcsrchr.h b/libc/src/wchar/wcsrchr.h
new file mode 100644
index 00000000000000..8b4a3ef11c6b49
--- /dev/null
+++ b/libc/src/wchar/wcsrchr.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for wcsrchr -----------------------*- 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_WCSRCHR_H
+#define LLVM_LIBC_SRC_WCHAR_WCSRCHR_H
+
+#include "hdr/types/wchar_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+const wchar_t *wcsrchr(const wchar_t *s, wchar_t c);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_WCHAR_WCSRCHR_H

>From 4fe47b1507a3527bff2be5a8e7d0833e5b0f6d9b Mon Sep 17 00:00:00 2001
From: Tristan Ross <tristan.ross at midstall.com>
Date: Thu, 26 Dec 2024 23:14:18 -0800
Subject: [PATCH 5/6] [libc] add wcspbrk

---
 libc/config/gpu/entrypoints.txt           |  1 +
 libc/config/linux/aarch64/entrypoints.txt |  1 +
 libc/config/linux/riscv/entrypoints.txt   |  1 +
 libc/config/linux/x86_64/entrypoints.txt  |  1 +
 libc/hdrgen/yaml/wchar.yaml               |  7 +++++
 libc/src/wchar/CMakeLists.txt             | 11 +++++++
 libc/src/wchar/wcspbrk.cpp                | 37 +++++++++++++++++++++++
 libc/src/wchar/wcspbrk.h                  | 21 +++++++++++++
 8 files changed, 80 insertions(+)
 create mode 100644 libc/src/wchar/wcspbrk.cpp
 create mode 100644 libc/src/wchar/wcspbrk.h

diff --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt
index 2720ecc2f9f772..2ef1c7954c69c8 100644
--- a/libc/config/gpu/entrypoints.txt
+++ b/libc/config/gpu/entrypoints.txt
@@ -261,6 +261,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.time.nanosleep
 
     # wchar.h entrypoints
+    libc.src.wchar.wcspbrk
     libc.src.wchar.wcsrchr
     libc.src.wchar.wcschr
     libc.src.wchar.wcslen
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index bfc8224660dad0..77d9ccde9f1d4c 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -349,6 +349,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcspbrk
     libc.src.wchar.wcsrchr
     libc.src.wchar.wcschr
     libc.src.wchar.wcslen
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 50f1789bc60677..2d1dfd1f9bf6cd 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -346,6 +346,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcspbrk
     libc.src.wchar.wcsrchr
     libc.src.wchar.wcschr
     libc.src.wchar.wcslen
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 82e6fe688e619f..bb3fd50b7c9f16 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -348,6 +348,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcspbrk
     libc.src.wchar.wcsrchr
     libc.src.wchar.wcschr
     libc.src.wchar.wcslen
diff --git a/libc/hdrgen/yaml/wchar.yaml b/libc/hdrgen/yaml/wchar.yaml
index a1ef7c41e1492f..be53305b4530d5 100644
--- a/libc/hdrgen/yaml/wchar.yaml
+++ b/libc/hdrgen/yaml/wchar.yaml
@@ -42,3 +42,10 @@ functions:
     arguments:
       - type: const wchar_t *
       - type: wchar_t
+  - name: wcspbrk
+    standards:
+      - stdc
+    return_type: const wchar_t *
+    arguments:
+      - type: const wchar_t *
+      - type: const wchar_t *
diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index f5a9158d28e330..647cb78b9f8b49 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -69,3 +69,14 @@ add_entrypoint_object(
     .wcslen
     libc.hdr.types.wchar_t
 )
+
+add_entrypoint_object(
+  wcspbrk
+  SRCS
+    wcspbrk.cpp
+  HDRS
+    wcspbrk.h
+  DEPENDS
+    .wcslen
+    libc.hdr.types.wchar_t
+)
diff --git a/libc/src/wchar/wcspbrk.cpp b/libc/src/wchar/wcspbrk.cpp
new file mode 100644
index 00000000000000..ffdc037f873962
--- /dev/null
+++ b/libc/src/wchar/wcspbrk.cpp
@@ -0,0 +1,37 @@
+//===-- Implementation of wcspbrk -----------------------------------------===//
+//
+// 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/wcspbrk.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "wcslen.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(const wchar_t *, wcspbrk,
+                   (const wchar_t *wcs, const wchar_t *accept)) {
+  size_t n_accept = wcslen(accept);
+
+  for (size_t i = 0; i < wcslen(wcs); i++) {
+    bool accepted = true;
+
+    for (size_t x = 0; x < n_accept; i++) {
+      if (wcs[i] != accept[x]) {
+        accepted = false;
+        break;
+      }
+    }
+
+    if (!accepted)
+      continue;
+    return &wcs[i];
+  }
+  return nullptr;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/wchar/wcspbrk.h b/libc/src/wchar/wcspbrk.h
new file mode 100644
index 00000000000000..883f904368c329
--- /dev/null
+++ b/libc/src/wchar/wcspbrk.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for wcspbrk -----------------------*- 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_WCSPBRK_H
+#define LLVM_LIBC_SRC_WCHAR_WCSPBRK_H
+
+#include "hdr/types/wchar_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+const wchar_t *wcspbrk(const wchar_t *wcs, const wchar_t *accept);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_WCHAR_WCSPBRK_H

>From 2b58f97fac97399bd78d6dd669bc9546dd709494 Mon Sep 17 00:00:00 2001
From: Tristan Ross <tristan.ross at midstall.com>
Date: Fri, 27 Dec 2024 11:48:05 -0800
Subject: [PATCH 6/6] [libc] add wcsstr

---
 libc/config/gpu/entrypoints.txt           |  1 +
 libc/config/linux/aarch64/entrypoints.txt |  1 +
 libc/config/linux/riscv/entrypoints.txt   |  1 +
 libc/config/linux/x86_64/entrypoints.txt  |  1 +
 libc/hdrgen/yaml/wchar.yaml               |  7 ++++
 libc/src/wchar/CMakeLists.txt             | 11 ++++++
 libc/src/wchar/wcsstr.cpp                 | 44 +++++++++++++++++++++++
 libc/src/wchar/wcsstr.h                   | 21 +++++++++++
 8 files changed, 87 insertions(+)
 create mode 100644 libc/src/wchar/wcsstr.cpp
 create mode 100644 libc/src/wchar/wcsstr.h

diff --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt
index 2ef1c7954c69c8..23acb2e95e4bb7 100644
--- a/libc/config/gpu/entrypoints.txt
+++ b/libc/config/gpu/entrypoints.txt
@@ -261,6 +261,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.time.nanosleep
 
     # wchar.h entrypoints
+    libc.src.wchar.wcsstr
     libc.src.wchar.wcspbrk
     libc.src.wchar.wcsrchr
     libc.src.wchar.wcschr
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 77d9ccde9f1d4c..1491993f8041cc 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -349,6 +349,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcsstr
     libc.src.wchar.wcspbrk
     libc.src.wchar.wcsrchr
     libc.src.wchar.wcschr
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 2d1dfd1f9bf6cd..354acec4cc8bd1 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -346,6 +346,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcsstr
     libc.src.wchar.wcspbrk
     libc.src.wchar.wcsrchr
     libc.src.wchar.wcschr
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index bb3fd50b7c9f16..ec095b493414cb 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -348,6 +348,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.write
 
     # wchar.h entrypoints
+    libc.src.wchar.wcsstr
     libc.src.wchar.wcspbrk
     libc.src.wchar.wcsrchr
     libc.src.wchar.wcschr
diff --git a/libc/hdrgen/yaml/wchar.yaml b/libc/hdrgen/yaml/wchar.yaml
index be53305b4530d5..2ffcc458771568 100644
--- a/libc/hdrgen/yaml/wchar.yaml
+++ b/libc/hdrgen/yaml/wchar.yaml
@@ -49,3 +49,10 @@ functions:
     arguments:
       - type: const wchar_t *
       - type: const wchar_t *
+  - name: wcsstr
+    standards:
+      - stdc
+    return_type: const wchar_t *
+    arguments:
+      - type: const wchar_t *
+      - type: const wchar_t *
diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index 647cb78b9f8b49..36609cd23a9491 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -80,3 +80,14 @@ add_entrypoint_object(
     .wcslen
     libc.hdr.types.wchar_t
 )
+
+add_entrypoint_object(
+  wcsstr
+  SRCS
+    wcsstr.cpp
+  HDRS
+    wcsstr.h
+  DEPENDS
+    .wcslen
+    libc.hdr.types.wchar_t
+)
diff --git a/libc/src/wchar/wcsstr.cpp b/libc/src/wchar/wcsstr.cpp
new file mode 100644
index 00000000000000..084f854ef23357
--- /dev/null
+++ b/libc/src/wchar/wcsstr.cpp
@@ -0,0 +1,44 @@
+//===-- Implementation of wcsstr ------------------------------------------===//
+//
+// 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/wcsstr.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "wcslen.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(const wchar_t *, wcsstr,
+                   (const wchar_t *s, const wchar_t *needle)) {
+  size_t s_len = wcslen(s);
+  size_t needle_len = wcslen(needle);
+
+  if (needle_len > s_len)
+    return nullptr;
+
+  for (size_t i = 0; i < s_len; i++) {
+    size_t end = needle_len + i;
+    if (end > s_len)
+      break;
+
+    bool found = true;
+    for (size_t x = 0; x < needle_len; x++) {
+      if (s[i + x] != needle[x]) {
+        found = false;
+        break;
+      }
+    }
+
+    if (!found)
+      continue;
+    return &s[i];
+  }
+  return nullptr;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/wchar/wcsstr.h b/libc/src/wchar/wcsstr.h
new file mode 100644
index 00000000000000..15ccbf01ac9af5
--- /dev/null
+++ b/libc/src/wchar/wcsstr.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for wcsstr ------------------------*- 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_WCSSTR_H
+#define LLVM_LIBC_SRC_WCHAR_WCSSTR_H
+
+#include "hdr/types/wchar_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+const wchar_t *wcsstr(const wchar_t *s, const wchar_t *needle);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_WCHAR_WCSSTR_H



More information about the libc-commits mailing list