diff options
Diffstat (limited to 'libc/src/stdlib')
| -rw-r--r-- | libc/src/stdlib/CMakeLists.txt | 59 | ||||
| -rw-r--r-- | libc/src/stdlib/mbstowcs.cpp | 40 | ||||
| -rw-r--r-- | libc/src/stdlib/mbstowcs.h | 22 | ||||
| -rw-r--r-- | libc/src/stdlib/mbtowc.cpp | 37 | ||||
| -rw-r--r-- | libc/src/stdlib/mbtowc.h | 22 | ||||
| -rw-r--r-- | libc/src/stdlib/wcstombs.cpp | 38 | ||||
| -rw-r--r-- | libc/src/stdlib/wcstombs.h | 22 | ||||
| -rw-r--r-- | libc/src/stdlib/wctomb.cpp | 35 | ||||
| -rw-r--r-- | libc/src/stdlib/wctomb.h | 22 |
9 files changed, 297 insertions, 0 deletions
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt index 1ccdcc8bec14..62da469f0eb9 100644 --- a/libc/src/stdlib/CMakeLists.txt +++ b/libc/src/stdlib/CMakeLists.txt @@ -368,6 +368,65 @@ add_entrypoint_object( libc.hdr.types.size_t ) +add_entrypoint_object( + mbtowc + SRCS + mbtowc.cpp + HDRS + mbtowc.h + DEPENDS + libc.hdr.types.size_t + libc.hdr.types.wchar_t + libc.src.__support.common + libc.src.__support.macros.config + libc.src.__support.libc_errno + libc.src.__support.wchar.mbrtowc + libc.src.__support.wchar.mbstate +) + +add_entrypoint_object( + mbstowcs + SRCS + mbstowcs.cpp + HDRS + mbstowcs.h + DEPENDS + libc.hdr.types.size_t + libc.hdr.types.wchar_t + libc.src.__support.common + libc.src.__support.macros.config + libc.src.__support.macros.null_check + libc.src.__support.libc_errno + libc.src.__support.wchar.mbstate + libc.src.__support.wchar.mbsnrtowcs +) + +add_entrypoint_object( + wctomb + SRCS + wctomb.cpp + HDRS + wctomb.h + DEPENDS + libc.hdr.types.wchar_t + libc.src.__support.wchar.wcrtomb + libc.src.__support.wchar.mbstate + libc.src.__support.libc_errno +) + +add_entrypoint_object( + wcstombs + SRCS + wcstombs.cpp + HDRS + wcstombs.h + DEPENDS + libc.hdr.types.wchar_t + libc.src.__support.wchar.mbstate + libc.src.__support.wchar.wcsnrtombs + libc.src.__support.libc_errno +) + if(NOT LIBC_TARGET_OS_IS_BAREMETAL AND NOT LIBC_TARGET_OS_IS_GPU) if(LLVM_LIBC_INCLUDE_SCUDO) set(SCUDO_DEPS "") diff --git a/libc/src/stdlib/mbstowcs.cpp b/libc/src/stdlib/mbstowcs.cpp new file mode 100644 index 000000000000..6d283ea46e3b --- /dev/null +++ b/libc/src/stdlib/mbstowcs.cpp @@ -0,0 +1,40 @@ +//===-- Implementation of mbstowcs ----------------------------------------===// +// +// 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/stdlib/mbstowcs.h" + +#include "hdr/types/size_t.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/__support/macros/null_check.h" +#include "src/__support/wchar/mbsnrtowcs.h" +#include "src/__support/wchar/mbstate.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(size_t, mbstowcs, + (wchar_t *__restrict pwcs, const char *__restrict s, + size_t n)) { + LIBC_CRASH_ON_NULLPTR(s); + // If destination is null, ignore n + n = pwcs == nullptr ? SIZE_MAX : n; + static internal::mbstate internal_mbstate; + const char *temp = s; + auto ret = internal::mbsnrtowcs(pwcs, &temp, SIZE_MAX, n, &internal_mbstate); + + if (!ret.has_value()) { + // Encoding failure + libc_errno = ret.error(); + return -1; + } + return ret.value(); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/stdlib/mbstowcs.h b/libc/src/stdlib/mbstowcs.h new file mode 100644 index 000000000000..90f8195a39ec --- /dev/null +++ b/libc/src/stdlib/mbstowcs.h @@ -0,0 +1,22 @@ +//===-- Implementation header for mbstowcs --------------------------------===// +// +// 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_STDLIB_MBSTOWCS_H +#define LLVM_LIBC_SRC_STDLIB_MBSTOWCS_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 mbstowcs(wchar_t *__restrict pwcs, const char *__restrict s, size_t n); + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC_STDLIB_MBSTOWCS_H diff --git a/libc/src/stdlib/mbtowc.cpp b/libc/src/stdlib/mbtowc.cpp new file mode 100644 index 000000000000..5f482463f471 --- /dev/null +++ b/libc/src/stdlib/mbtowc.cpp @@ -0,0 +1,37 @@ +//===-- Implementation of mbtowc -----------------------------------------===// +// +// 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/stdlib/mbtowc.h" + +#include "hdr/types/size_t.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/__support/wchar/mbrtowc.h" +#include "src/__support/wchar/mbstate.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(int, mbtowc, + (wchar_t *__restrict pwc, const char *__restrict s, + size_t n)) { + // returns 0 since UTF-8 encoding is not state-dependent + if (s == nullptr) + return 0; + internal::mbstate internal_mbstate; + auto ret = internal::mbrtowc(pwc, s, n, &internal_mbstate); + if (!ret.has_value() || static_cast<int>(ret.value()) == -2) { + // Encoding failure + libc_errno = EILSEQ; + return -1; + } + return static_cast<int>(ret.value()); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/stdlib/mbtowc.h b/libc/src/stdlib/mbtowc.h new file mode 100644 index 000000000000..acd85cb77ba7 --- /dev/null +++ b/libc/src/stdlib/mbtowc.h @@ -0,0 +1,22 @@ +//===-- Implementation header for mbtowc ---------------------------------===// +// +// 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_STDLIB_MBTOWC_H +#define LLVM_LIBC_SRC_STDLIB_MBTOWC_H + +#include "hdr/types/size_t.h" +#include "hdr/types/wchar_t.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +int mbtowc(wchar_t *__restrict pwc, const char *__restrict s, size_t n); + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC_STDLIB_MBTOWC_H diff --git a/libc/src/stdlib/wcstombs.cpp b/libc/src/stdlib/wcstombs.cpp new file mode 100644 index 000000000000..712af958456d --- /dev/null +++ b/libc/src/stdlib/wcstombs.cpp @@ -0,0 +1,38 @@ +//===-- Implementation of wcstombs ----------------------------------------===// +// +// 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/stdlib/wcstombs.h" + +#include "hdr/types/char32_t.h" +#include "hdr/types/size_t.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/__support/wchar/mbstate.h" +#include "src/__support/wchar/wcsnrtombs.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(size_t, wcstombs, + (char *__restrict s, const wchar_t *__restrict wcs, + size_t n)) { + LIBC_CRASH_ON_NULLPTR(wcs); + static internal::mbstate internal_mbstate; + const wchar_t *wcs_ptr_copy = wcs; + auto result = + internal::wcsnrtombs(s, &wcs_ptr_copy, SIZE_MAX, n, &internal_mbstate); + if (!result.has_value()) { + libc_errno = result.error(); + return -1; + } + + return result.value(); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/stdlib/wcstombs.h b/libc/src/stdlib/wcstombs.h new file mode 100644 index 000000000000..39515431098c --- /dev/null +++ b/libc/src/stdlib/wcstombs.h @@ -0,0 +1,22 @@ +//===-- Implementation header for wcstombs --------------------------------===// +// +// 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_STDLIB_WCSTOMBS_H +#define LLVM_LIBC_SRC_STDLIB_WCSTOMBS_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 wcstombs(char *__restrict s, const wchar_t *__restrict pwcs, size_t n); + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC_STDLIB_WCSTOMBS_H diff --git a/libc/src/stdlib/wctomb.cpp b/libc/src/stdlib/wctomb.cpp new file mode 100644 index 000000000000..0ca1a84cd923 --- /dev/null +++ b/libc/src/stdlib/wctomb.cpp @@ -0,0 +1,35 @@ +//===-- Implementation of wctomb ------------------------------------------===// +// +// 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/stdlib/wctomb.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/__support/wchar/mbstate.h" +#include "src/__support/wchar/wcrtomb.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(int, wctomb, (char *s, wchar_t wc)) { + static internal::mbstate internal_mbstate; + if (s == nullptr) + return 0; + + auto result = internal::wcrtomb(s, wc, &internal_mbstate); + + if (!result.has_value()) { // invalid wide character + libc_errno = EILSEQ; + return -1; + } + + return static_cast<int>(result.value()); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/stdlib/wctomb.h b/libc/src/stdlib/wctomb.h new file mode 100644 index 000000000000..90afa31d9e70 --- /dev/null +++ b/libc/src/stdlib/wctomb.h @@ -0,0 +1,22 @@ +//===-- Implementation header for wctomb ------------------------*- 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_STDLIB_WCTOMB_H +#define LLVM_LIBC_SRC_STDLIB_WCTOMB_H + +#include "hdr/types/mbstate_t.h" +#include "hdr/types/wchar_t.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +int wctomb(char *s, wchar_t wc); + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC_STDLIB_WCTOMB_H |
