summaryrefslogtreecommitdiff
path: root/libc/src
diff options
context:
space:
mode:
authorUzair Nawaz <uzairnawaz@google.com>2025-07-24 11:29:40 -0700
committerGitHub <noreply@github.com>2025-07-24 11:29:40 -0700
commitf26c0d00df97c3b3ffce1047e92acfcbc680845b (patch)
treedd7ed4f046cd703af50b12b1439ff924e73d1b0e /libc/src
parent4f2686e5a131094d985677591482508586f1f5c9 (diff)
[libc] Implemented wcsdup libc function (#150453)
Implemented wcsdup by templating internal strdup function
Diffstat (limited to 'libc/src')
-rw-r--r--libc/src/string/allocating_string_utils.h6
-rw-r--r--libc/src/wchar/CMakeLists.txt13
-rw-r--r--libc/src/wchar/wcsdup.cpp27
-rw-r--r--libc/src/wchar/wcsdup.h21
4 files changed, 64 insertions, 3 deletions
diff --git a/libc/src/string/allocating_string_utils.h b/libc/src/string/allocating_string_utils.h
index 1dece510e796..e2f61f77b0c7 100644
--- a/libc/src/string/allocating_string_utils.h
+++ b/libc/src/string/allocating_string_utils.h
@@ -20,15 +20,15 @@
namespace LIBC_NAMESPACE_DECL {
namespace internal {
-LIBC_INLINE cpp::optional<char *> strdup(const char *src) {
+template <typename T> LIBC_INLINE cpp::optional<T *> strdup(const T *src) {
if (src == nullptr)
return cpp::nullopt;
size_t len = string_length(src) + 1;
AllocChecker ac;
- char *newstr = new (ac) char[len];
+ T *newstr = new (ac) T[len];
if (!ac)
return cpp::nullopt;
- inline_memcpy(newstr, src, len);
+ inline_memcpy(newstr, src, len * sizeof(T));
return newstr;
}
diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index 2b95d94e4230..25319837bdc7 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -256,6 +256,19 @@ add_entrypoint_object(
)
add_entrypoint_object(
+ wcsdup
+ SRCS
+ wcsdup.cpp
+ HDRS
+ wcsdup.h
+ DEPENDS
+ libc.hdr.types.wchar_t
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
+ libc.src.string.allocating_string_utils
+)
+
+add_entrypoint_object(
wcspbrk
SRCS
wcspbrk.cpp
diff --git a/libc/src/wchar/wcsdup.cpp b/libc/src/wchar/wcsdup.cpp
new file mode 100644
index 000000000000..d4a13d35e4d5
--- /dev/null
+++ b/libc/src/wchar/wcsdup.cpp
@@ -0,0 +1,27 @@
+//===-- Implementation of wcsdup -----------------------------------------===//
+//
+// 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/wcsdup.h"
+#include "hdr/types/wchar_t.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+#include "src/string/allocating_string_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(wchar_t *, wcsdup, (const wchar_t *wcs)) {
+ auto dup = internal::strdup(wcs);
+ if (dup)
+ return *dup;
+ if (wcs != nullptr)
+ libc_errno = ENOMEM;
+ return nullptr;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/wchar/wcsdup.h b/libc/src/wchar/wcsdup.h
new file mode 100644
index 000000000000..80b3e52d71b9
--- /dev/null
+++ b/libc/src/wchar/wcsdup.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for wcsdup ----------------------------------===//
+//
+// 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_WCSDUP_H
+#define LLVM_LIBC_SRC_WCHAR_WCSDUP_H
+
+#include "hdr/types/wchar_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+wchar_t *wcsdup(const wchar_t *wcs);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_WCHAR_WCSDUP_H