Skip to content

Commit 3611417

Browse files
committed
descriptive names; alphabetized yaml; used memset in tests
1 parent efb396a commit 3611417

File tree

4 files changed

+50
-60
lines changed

4 files changed

+50
-60
lines changed

libc/include/wchar.yaml

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,25 @@ functions:
189189
- type: wchar_t *__restrict
190190
- type: const wchar_t *__restrict
191191
- type: size_t
192+
- name: wcsnrtombs
193+
standards:
194+
- stdc
195+
return_type: size_t
196+
arguments:
197+
- type: char *__restrict
198+
- type: const wchar_t **__restrict
199+
- type: size_t
200+
- type: size_t
201+
- type: mbstate_t
202+
- name: wcsrtombs
203+
standards:
204+
- stdc
205+
return_type: size_t
206+
arguments:
207+
- type: char *__restrict
208+
- type: const wchar_t **__restrict
209+
- type: size_t
210+
- type: mbstate_t
192211
- name: wcrtomb
193212
standards:
194213
- stdc
@@ -258,6 +277,14 @@ functions:
258277
- type: const wchar_t *__restrict
259278
- type: wchar_t **__restrict
260279
- type: int
280+
- name: wcstombs
281+
standards:
282+
- stdc
283+
return_type: size_t
284+
arguments:
285+
- type: char *__restrict
286+
- type: const wchar_t *__restrict
287+
- type: size_t
261288
- name: wcstoul
262289
standards:
263290
- stdc
@@ -274,31 +301,3 @@ functions:
274301
- type: const wchar_t *__restrict
275302
- type: wchar_t **__restrict
276303
- type: int
277-
- name: wcstombs
278-
standards:
279-
- stdc
280-
return_type: size_t
281-
arguments:
282-
- type: char *__restrict
283-
- type: const wchar_t *__restrict
284-
- type: size_t
285-
- name: wcsrtombs
286-
standards:
287-
- stdc
288-
return_type: size_t
289-
arguments:
290-
- type: char *__restrict
291-
- type: const wchar_t **__restrict
292-
- type: size_t
293-
- type: mbstate_t
294-
- name: wcsnrtombs
295-
standards:
296-
- stdc
297-
return_type: size_t
298-
arguments:
299-
- type: char *__restrict
300-
- type: const wchar_t **__restrict
301-
- type: size_t
302-
- type: size_t
303-
- type: mbstate_t
304-

libc/src/__support/wchar/wcsnrtombs.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,40 @@
2222
namespace LIBC_NAMESPACE_DECL {
2323
namespace internal {
2424

25-
LIBC_INLINE static ErrorOr<size_t> wcsnrtombs(char *__restrict s,
26-
const wchar_t **__restrict pwcs,
27-
size_t nwc, size_t len,
25+
LIBC_INLINE static ErrorOr<size_t> wcsnrtombs(char *__restrict dest,
26+
const wchar_t **__restrict ptr_to_src,
27+
size_t num_src_widechars, size_t dest_len,
2828
mbstate *ps) {
29-
LIBC_CRASH_ON_NULLPTR(pwcs);
29+
LIBC_CRASH_ON_NULLPTR(ptr_to_src);
3030
LIBC_CRASH_ON_NULLPTR(ps);
3131

3232
CharacterConverter cr(ps);
3333
if (!cr.isValidState())
3434
return Error(EINVAL);
3535

36-
if (s == nullptr)
37-
len = SIZE_MAX;
36+
if (dest == nullptr)
37+
dest_len = SIZE_MAX;
3838

39-
StringConverter<char32_t> str_conv(reinterpret_cast<const char32_t *>(*pwcs),
40-
ps, len, nwc);
39+
StringConverter<char32_t> str_conv(reinterpret_cast<const char32_t *>(*ptr_to_src),
40+
ps, dest_len, num_src_widechars);
4141
size_t dst_idx = 0;
4242
ErrorOr<char8_t> converted = str_conv.popUTF8();
4343
while (converted.has_value()) {
44-
if (s != nullptr)
45-
s[dst_idx] = converted.value();
44+
if (dest != nullptr)
45+
dest[dst_idx] = converted.value();
4646

4747
if (converted.value() == '\0') {
48-
if (s != nullptr)
49-
*pwcs = nullptr;
48+
if (dest != nullptr)
49+
*ptr_to_src = nullptr;
5050
return dst_idx;
5151
}
5252

5353
dst_idx++;
5454
converted = str_conv.popUTF8();
5555
}
5656

57-
if (s != nullptr)
58-
*pwcs += str_conv.getSourceIndex();
57+
if (dest != nullptr)
58+
*ptr_to_src += str_conv.getSourceIndex();
5959

6060
if (converted.error() == -1) // if we hit conversion limit
6161
return dst_idx;

libc/test/src/wchar/wcsnrtombs_test.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ TEST_F(LlvmLibcWcsnrtombs, DestLimit) {
5959
const wchar_t *cur = src;
6060

6161
char mbs[11];
62-
for (int i = 0; i < 11; ++i)
63-
mbs[i] = '\x01'; // dummy initial values
62+
LIBC_NAMESPACE::memset(mbs, '\x01', 11); // dummy initial values
6463

6564
ASSERT_EQ(LIBC_NAMESPACE::wcsnrtombs(mbs, &cur, 5, 4, &state),
6665
static_cast<size_t>(4));
@@ -72,8 +71,7 @@ TEST_F(LlvmLibcWcsnrtombs, DestLimit) {
7271
ASSERT_EQ(mbs[3], '\xA1');
7372
ASSERT_EQ(mbs[4], '\x01'); // didn't write more than 4 bytes
7473

75-
for (int i = 0; i < 11; ++i)
76-
mbs[i] = '\x01'; // dummy initial values
74+
LIBC_NAMESPACE::memset(mbs, '\x01', 11); // dummy initial values
7775
LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t));
7876
cur = src;
7977

@@ -101,8 +99,7 @@ TEST(LlvmLibcWcsnrtombs, SrcLimit) {
10199
const wchar_t *cur = src;
102100

103101
char mbs[11];
104-
for (int i = 0; i < 11; ++i)
105-
mbs[i] = '\x01'; // dummy initial values
102+
LIBC_NAMESPACE::memset(mbs, '\x01', 11); // dummy initial values
106103

107104
auto res = LIBC_NAMESPACE::wcsnrtombs(mbs, &cur, 2, 11, &state);
108105
ASSERT_ERRNO_SUCCESS();
@@ -167,8 +164,7 @@ TEST_F(LlvmLibcWcsnrtombs, NullState) {
167164
const wchar_t *cur = src;
168165

169166
char mbs[11];
170-
for (int i = 0; i < 11; ++i)
171-
mbs[i] = '\x01'; // dummy initial values
167+
LIBC_NAMESPACE::memset(mbs, '\x01', 11); // dummy initial values
172168

173169
ASSERT_EQ(LIBC_NAMESPACE::wcsnrtombs(mbs, &cur, 5, 4, nullptr),
174170
static_cast<size_t>(4));
@@ -180,8 +176,7 @@ TEST_F(LlvmLibcWcsnrtombs, NullState) {
180176
ASSERT_EQ(mbs[3], '\xA1');
181177
ASSERT_EQ(mbs[4], '\x01'); // didn't write more than 4 bytes
182178

183-
for (int i = 0; i < 11; ++i)
184-
mbs[i] = '\x01'; // dummy initial values
179+
LIBC_NAMESPACE::memset(mbs, '\x01', 11); // dummy initial values
185180

186181
// not enough bytes to convert the second character, so only converts one
187182
cur = src;

libc/test/src/wchar/wcsrtombs_test.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ TEST_F(LlvmLibcWcsrtombs, DestLimit) {
5959
const wchar_t *cur = src;
6060

6161
char mbs[11];
62-
for (int i = 0; i < 11; ++i)
63-
mbs[i] = '\x01'; // dummy initial values
62+
LIBC_NAMESPACE::memset(mbs, '\x01', 11); // dummy initial values
6463

6564
ASSERT_EQ(LIBC_NAMESPACE::wcsrtombs(mbs, &cur, 4, &state),
6665
static_cast<size_t>(4));
@@ -72,8 +71,7 @@ TEST_F(LlvmLibcWcsrtombs, DestLimit) {
7271
ASSERT_EQ(mbs[3], '\xA1');
7372
ASSERT_EQ(mbs[4], '\x01'); // didn't write more than 4 bytes
7473

75-
for (int i = 0; i < 11; ++i)
76-
mbs[i] = '\x01'; // dummy initial values
74+
LIBC_NAMESPACE::memset(mbs, '\x01', 11); // dummy initial values
7775
LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t));
7876

7977
// not enough bytes to convert the second character, so only converts one
@@ -124,8 +122,7 @@ TEST_F(LlvmLibcWcsrtombs, NullState) {
124122
const wchar_t *cur = src;
125123

126124
char mbs[11];
127-
for (int i = 0; i < 11; ++i)
128-
mbs[i] = '\x01'; // dummy initial values
125+
LIBC_NAMESPACE::memset(mbs, '\x01', 11); // dummy initial values
129126

130127
ASSERT_EQ(LIBC_NAMESPACE::wcsrtombs(mbs, &cur, 4, nullptr),
131128
static_cast<size_t>(4));
@@ -137,8 +134,7 @@ TEST_F(LlvmLibcWcsrtombs, NullState) {
137134
ASSERT_EQ(mbs[3], '\xA1');
138135
ASSERT_EQ(mbs[4], '\x01'); // didn't write more than 4 bytes
139136

140-
for (int i = 0; i < 11; ++i)
141-
mbs[i] = '\x01'; // dummy initial values
137+
LIBC_NAMESPACE::memset(mbs, '\x01', 11); // dummy initial values
142138

143139
// not enough bytes to convert the second character, so only converts one
144140
cur = src;

0 commit comments

Comments
 (0)