Skip to content

Commit 4bb40cd

Browse files
committed
began implementing public function
1 parent a58f040 commit 4bb40cd

File tree

4 files changed

+40
-16
lines changed

4 files changed

+40
-16
lines changed

libc/src/__support/wchar/wcsrtombs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ErrorOr<size_t> wcsrtombs(char *__restrict dst, const wchar_t **__restrict src,
2525
size_t len, mbstate *__restrict ps) {
2626
static_assert(sizeof(wchar_t) == 4);
2727

28-
if (dst == nullptr)
28+
if (src == nullptr || dst == nullptr)
2929
return Error(-1);
3030

3131
size_t bytes_written = 0;

libc/src/__support/wchar/wcsrtombs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace LIBC_NAMESPACE_DECL {
1919
namespace internal {
2020

2121
ErrorOr<size_t> wcsrtombs(char *__restrict dst, const wchar_t **__restrict src,
22-
size_t len, mbstate_t *__restrict ps);
22+
size_t len, mbstate *__restrict ps);
2323

2424
} // namespace internal
2525
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wcsrtombs.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1-
//===-- Implementation of wcrtomb -----------------------------------------===//
1+
//===-- Implementation of wcsrtombs ---------------------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "src/wchar/wcrtomb.h"
9+
#include "src/wchar/wcsrtombs.h"
1010

1111
#include "hdr/types/mbstate_t.h"
1212
#include "hdr/types/wchar_t.h"
1313
#include "src/__support/common.h"
1414
#include "src/__support/libc_errno.h"
1515
#include "src/__support/macros/config.h"
16+
#include "src/__support/macros/null_check.h"
1617
#include "src/__support/wchar/mbstate.h"
17-
#include "src/__support/wchar/wcrtomb.h"
18+
#include "src/__support/wchar/wcsrtombs.h"
1819

1920
namespace LIBC_NAMESPACE_DECL {
2021

21-
LLVM_LIBC_FUNCTION(size_t, wcrtomb,
22-
(char *__restrict s, wchar_t wc, mbstate_t *__restrict ps)) {
22+
LLVM_LIBC_FUNCTION(size_t, wcsrtombs,
23+
(char *__restrict dst, const wchar_t **__restrict src,
24+
size_t len, mbstate_t *__restrict ps)) {
2325
static internal::mbstate internal_mbstate;
2426

25-
// when s is nullptr, this is equivalent to wcrtomb(buf, L'\0', ps)
26-
char buf[sizeof(wchar_t) / sizeof(char)];
27-
if (s == nullptr) {
28-
s = buf;
29-
wc = L'\0';
30-
}
27+
LIBC_CRASH_ON_NULLPTR(src);
28+
char buf[len];
29+
if (dst == nullptr)
30+
dst = buf;
3131

32-
auto result = internal::wcrtomb(
33-
s, wc,
32+
auto result = internal::wcsrtombs(
33+
dst, src, len,
3434
ps == nullptr ? &internal_mbstate
35-
: reinterpret_cast<internal::mbstate *>(ps), 4);
35+
: reinterpret_cast<internal::mbstate *>(ps));
3636

3737
if (!result.has_value()) {
3838
libc_errno = EILSEQ;

libc/src/wchar/wcsrtombs.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- Implementation header for wcsrtombs ---------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_WCHAR_WCSRTOMBS_H
10+
#define LLVM_LIBC_SRC_WCHAR_WCSRTOMBS_H
11+
12+
#include "hdr/types/mbstate_t.h"
13+
#include "hdr/types/size_t.h"
14+
#include "hdr/types/wchar_t.h"
15+
#include "src/__support/macros/config.h"
16+
17+
namespace LIBC_NAMESPACE_DECL {
18+
19+
size_t wcsrtombs(char *__restrict dst, const wchar_t **__restrict src,
20+
size_t len, mbstate_t *__restrict ps);
21+
22+
} // namespace LIBC_NAMESPACE_DECL
23+
24+
#endif // LLVM_LIBC_SRC_WCHAR_WCSRTOMBS_H

0 commit comments

Comments
 (0)