Skip to content

Commit 26a6741

Browse files
author
Sriya Pratipati
committed
implemented mbsrtowcs
1 parent c9d06fc commit 26a6741

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

libc/src/wchar/mbsrtowcs.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===-- Implementation of mbsrtowcs ---------------------------------------===//
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+
#include "src/wchar/mbsrtowcs.h"
10+
11+
#include "hdr/types/size_t.h"
12+
#include "hdr/types/wchar_t.h"
13+
#include "src/__support/common.h"
14+
#include "src/__support/libc_errno.h"
15+
#include "src/__support/macros/config.h"
16+
#include "src/__support/wchar/mbstate.h"
17+
#include "src/__support/wchar/string_converter.h"
18+
19+
namespace LIBC_NAMESPACE_DECL {
20+
21+
LLVM_LIBC_FUNCTION(size_t, mbsrtowcs,
22+
(wchar_t *__restrict dst, const char **__restrict src,
23+
size_t len, mbstate_t *__restrict ps)) {
24+
static internal::mbstate internal_mbstate;
25+
internal::StringConverter<char8_t> str_conv(
26+
reinterpret_cast<const char8_t *>(src),
27+
ps == nullptr ? &internal_mbstate
28+
: reinterpret_cast<internal::mbstate *>(ps),
29+
len);
30+
31+
int dst_idx = 0;
32+
ErrorOr<char32_t> converted = str_conv.popUTF32();
33+
while (converted.has_value()) {
34+
dst[dst_idx] = converted.value();
35+
dst_idx++;
36+
converted = str_conv.popUTF32();
37+
}
38+
39+
src += str_conv.getSourceIndex();
40+
if (converted.error() == -1) // if we hit conversion limit
41+
return dst_idx;
42+
43+
libc_errno = converted.error();
44+
return -1;
45+
}
46+
47+
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/mbsrtowcs.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- Implementation header for mbsrtowcs -------------------------------===//
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_MBSRTOWCS_H
10+
#define LLVM_LIBC_SRC_WCHAR_MBSRTOWCS_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 mbsrtowcs(wchar_t *__restrict dst, const char **__restrict src,
20+
size_t len, mbstate_t *__restrict ps);
21+
22+
} // namespace LIBC_NAMESPACE_DECL
23+
24+
#endif // LLVM_LIBC_SRC_WCHAR_MBSRTOWCS_H

libc/src/wchar/mbstowcs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
namespace LIBC_NAMESPACE_DECL {
2020

21-
LLVM_LIBC_FUNCTION(int, mbstowcs,
21+
LLVM_LIBC_FUNCTION(size_t, mbstowcs,
2222
(wchar_t *__restrict pwcs, const char *__restrict s,
2323
size_t n)) {
2424
static internal::mbstate internal_mbstate;

libc/src/wchar/mbstowcs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace LIBC_NAMESPACE_DECL {
1717

18-
int mbstowcs(wchar_t *__restrict pwcs, const char *__restrict s, size_t n);
18+
size_t mbstowcs(wchar_t *__restrict pwcs, const char *__restrict s, size_t n);
1919

2020
} // namespace LIBC_NAMESPACE_DECL
2121

0 commit comments

Comments
 (0)