Skip to content

Commit 40aa6ee

Browse files
author
Sriya Pratipati
committed
implemented mbsnrtowcs and tests
1 parent fbd6453 commit 40aa6ee

File tree

12 files changed

+491
-125
lines changed

12 files changed

+491
-125
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,7 @@ if(LLVM_LIBC_FULL_BUILD)
12631263
libc.src.wchar.mbtowc
12641264
libc.src.wchar.mbstowcs
12651265
libc.src.wchar.mbsrtowcs
1266+
libc.src.wchar.mbsnrtowcs
12661267
libc.src.wchar.wcrtomb
12671268
libc.src.wchar.wctomb
12681269
)

libc/include/wchar.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ functions:
7070
- type: const char **__restrict
7171
- type: size_t
7272
- type: mbstate_t *__restrict
73+
- name: mbsnrtowcs
74+
standards:
75+
- stdc
76+
return_type: size_t
77+
arguments:
78+
- type: wchar_t *__restrict
79+
- type: const char **__restrict
80+
- type: size_t
81+
- type: size_t
82+
- type: mbstate_t *__restrict
7383
- name: wmemset
7484
standards:
7585
- stdc

libc/src/__support/wchar/mbsnrtowcs.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,18 @@ ErrorOr<size_t> mbsnrtowcs(wchar_t *__restrict dst, const char **__restrict src,
3838
dst[dst_idx] = converted.value();
3939
// null terminator should not be counted in return value
4040
if (converted.value() == L'\0') {
41-
src = nullptr;
41+
if (dst != nullptr)
42+
*src = nullptr;
4243
return dst_idx;
4344
}
4445
dst_idx++;
4546
converted = str_conv.popUTF32();
4647
}
4748

48-
*src += str_conv.getSourceIndex();
49-
if (converted.error() == -1) // if we hit conversion limit
49+
if (converted.error() == -1) { // if we hit conversion limit
50+
*src += str_conv.getSourceIndex();
5051
return dst_idx;
52+
}
5153

5254
return Error(converted.error());
5355
}

libc/src/wchar/CMakeLists.txt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,25 @@ add_entrypoint_object(
187187
libc.src.__support.common
188188
libc.src.__support.macros.config
189189
libc.src.__support.libc_errno
190-
libc.src.__support.wchar.string_converter
190+
libc.src.__support.wchar.mbstate
191+
libc.src.__support.wchar.mbsnrtowcs
192+
libc.src.__support.wchar.mbstate
193+
)
194+
195+
add_entrypoint_object(
196+
mbsnrtowcs
197+
SRCS
198+
mbsnrtowcs.cpp
199+
HDRS
200+
mbsnrtowcs.h
201+
DEPENDS
202+
libc.hdr.types.size_t
203+
libc.hdr.types.wchar_t
204+
libc.src.__support.common
205+
libc.src.__support.macros.config
206+
libc.src.__support.libc_errno
207+
libc.src.__support.wchar.mbstate
208+
libc.src.__support.wchar.mbsnrtowcs
191209
libc.src.__support.wchar.mbstate
192210
)
193211

libc/src/wchar/mbsnrtowcs.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===-- Implementation of mbsnrtowcs --------------------------------------===//
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/mbsnrtowcs.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/mbsnrtowcs.h"
17+
#include "src/__support/wchar/mbstate.h"
18+
19+
namespace LIBC_NAMESPACE_DECL {
20+
21+
LLVM_LIBC_FUNCTION(size_t, mbsnrtowcs,
22+
(wchar_t *__restrict dst, const char **__restrict src,
23+
size_t nmc, size_t len, mbstate_t *__restrict ps)) {
24+
static internal::mbstate internal_mbstate;
25+
// If destination is null, ignore len
26+
len = dst == nullptr ? SIZE_MAX : len;
27+
auto ret = internal::mbsnrtowcs(
28+
dst, src, nmc, len,
29+
ps == nullptr ? &internal_mbstate
30+
: reinterpret_cast<internal::mbstate *>(ps));
31+
if (!ret.has_value()) {
32+
// Encoding failure
33+
libc_errno = ret.error();
34+
return -1;
35+
}
36+
return ret.value();
37+
}
38+
39+
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/mbsnrtowcs.h

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

libc/src/wchar/mbsrtowcs.cpp

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,27 @@
1313
#include "src/__support/common.h"
1414
#include "src/__support/libc_errno.h"
1515
#include "src/__support/macros/config.h"
16+
#include "src/__support/wchar/mbsnrtowcs.h"
1617
#include "src/__support/wchar/mbstate.h"
17-
#include "src/__support/wchar/string_converter.h"
1818

1919
namespace LIBC_NAMESPACE_DECL {
2020

2121
LLVM_LIBC_FUNCTION(size_t, mbsrtowcs,
2222
(wchar_t *__restrict dst, const char **__restrict src,
2323
size_t len, mbstate_t *__restrict ps)) {
2424
static internal::mbstate internal_mbstate;
25-
internal::StringConverter<char8_t> str_conv(
26-
reinterpret_cast<const char8_t *>(*src),
25+
// If destination is null, ignore len
26+
len = dst == nullptr ? SIZE_MAX : len;
27+
auto ret = internal::mbsnrtowcs(
28+
dst, src, SIZE_MAX, len,
2729
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-
// null terminator should not be counted in return value
36-
if (converted.value() == L'\0') {
37-
src = nullptr;
38-
return dst_idx;
39-
}
40-
dst_idx++;
41-
converted = str_conv.popUTF32();
30+
: reinterpret_cast<internal::mbstate *>(ps));
31+
if (!ret.has_value()) {
32+
// Encoding failure
33+
libc_errno = ret.error();
34+
return -1;
4235
}
43-
44-
*src += str_conv.getSourceIndex();
45-
if (converted.error() == -1) // if we hit conversion limit
46-
return dst_idx;
47-
48-
libc_errno = converted.error();
49-
return -1;
36+
return ret.value();
5037
}
5138

5239
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/mbstowcs.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace LIBC_NAMESPACE_DECL {
2121
LLVM_LIBC_FUNCTION(size_t, mbstowcs,
2222
(wchar_t *__restrict pwcs, const char *__restrict s,
2323
size_t n)) {
24+
// If destination is null, ignore n
2425
n = pwcs == nullptr ? SIZE_MAX : n;
2526
static internal::mbstate internal_mbstate;
2627
const char *temp = s;

libc/test/src/wchar/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,22 @@ add_libc_test(
9393
libc.test.UnitTest.ErrnoCheckingTest
9494
)
9595

96+
add_libc_test(
97+
mbsnrtowcs_test
98+
SUITE
99+
libc_wchar_unittests
100+
SRCS
101+
mbsnrtowcs_test.cpp
102+
DEPENDS
103+
libc.src.__support.libc_errno
104+
libc.src.__support.wchar.mbstate
105+
libc.src.string.memset
106+
libc.src.wchar.mbsnrtowcs
107+
libc.hdr.types.mbstate_t
108+
libc.hdr.types.wchar_t
109+
libc.test.UnitTest.ErrnoCheckingTest
110+
)
111+
96112
add_libc_test(
97113
wctob_test
98114
SUITE

0 commit comments

Comments
 (0)