Skip to content

Commit b7ae0ad

Browse files
uzairnawazmahesh-attarde
authored andcommitted
[libc] Implement mbsinit (llvm#150654)
Implemented public libc function to check if an mbstate describes an empty state
1 parent 2b2f697 commit b7ae0ad

File tree

7 files changed

+118
-0
lines changed

7 files changed

+118
-0
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,7 @@ if(LLVM_LIBC_FULL_BUILD)
12641264
# wchar.h entrypoints
12651265
libc.src.wchar.mblen
12661266
libc.src.wchar.mbrlen
1267+
libc.src.wchar.mbsinit
12671268
libc.src.wchar.mbrtowc
12681269
libc.src.wchar.mbtowc
12691270
libc.src.wchar.wcrtomb

libc/include/wchar.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ functions:
5353
- type: wchar_t *__restrict
5454
- type: const char *__restrict
5555
- type: size_t
56+
- name: mbsinit
57+
standards:
58+
- stdc
59+
return_type: int
60+
arguments:
61+
- type: mbstate_t *
5662
- name: mblen
5763
standards:
5864
- stdc

libc/src/wchar/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,21 @@ add_entrypoint_object(
136136
libc.src.__support.libc_errno
137137
)
138138

139+
add_entrypoint_object(
140+
mbsinit
141+
SRCS
142+
mbsinit.cpp
143+
HDRS
144+
mbsinit.h
145+
DEPENDS
146+
libc.hdr.types.wchar_t
147+
libc.hdr.types.mbstate_t
148+
libc.src.__support.common
149+
libc.src.__support.macros.config
150+
libc.src.__support.wchar.character_converter
151+
libc.src.__support.wchar.mbstate
152+
)
153+
139154
add_entrypoint_object(
140155
mbrtowc
141156
SRCS

libc/src/wchar/mbsinit.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- Implementation of mbsinit -----------------------------------------===//
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/mbsinit.h"
10+
11+
#include "hdr/types/mbstate_t.h"
12+
#include "src/__support/common.h"
13+
#include "src/__support/macros/config.h"
14+
#include "src/__support/wchar/character_converter.h"
15+
#include "src/__support/wchar/mbstate.h"
16+
17+
namespace LIBC_NAMESPACE_DECL {
18+
19+
LLVM_LIBC_FUNCTION(int, mbsinit, (mbstate_t * ps)) {
20+
if (ps == nullptr)
21+
return true;
22+
internal::CharacterConverter cr(reinterpret_cast<internal::mbstate *>(ps));
23+
return cr.isValidState() && cr.isEmpty();
24+
}
25+
26+
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/mbsinit.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Implementation header for mbsinit ---------------------------------===//
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_MBSINIT_H
10+
#define LLVM_LIBC_SRC_WCHAR_MBSINIT_H
11+
12+
#include "hdr/types/mbstate_t.h"
13+
#include "hdr/types/size_t.h"
14+
#include "src/__support/macros/config.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
int mbsinit(mbstate_t *ps);
19+
20+
} // namespace LIBC_NAMESPACE_DECL
21+
22+
#endif // LLVM_LIBC_SRC_WCHAR_MBSINIT_H

libc/test/src/wchar/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,22 @@ add_libc_test(
9999
libc.src.string.memset
100100
libc.src.wchar.mbrlen
101101
libc.hdr.types.mbstate_t
102+
libc.hdr.types.wchar_t
102103
libc.test.UnitTest.ErrnoCheckingTest
104+
)
105+
106+
add_libc_test(
107+
mbsinit_test
108+
SUITE
109+
libc_wchar_unittests
110+
SRCS
111+
mbsinit_test.cpp
112+
DEPENDS
113+
libc.src.string.memset
114+
libc.src.wchar.mbsinit
115+
libc.src.wchar.mbrtowc
116+
libc.hdr.types.mbstate_t
117+
libc.hdr.types.wchar_t
103118
)
104119

105120
add_libc_test(

libc/test/src/wchar/mbsinit_test.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===-- Unittests for mbsinit ---------------------------------------------===//
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 "hdr/types/wchar_t.h"
10+
#include "src/string/memset.h"
11+
#include "src/wchar/mbrtowc.h"
12+
#include "src/wchar/mbsinit.h"
13+
#include "test/UnitTest/Test.h"
14+
15+
TEST(LlvmLibcMBSInitTest, EmptyState) {
16+
mbstate_t ps;
17+
LIBC_NAMESPACE::memset(&ps, 0, sizeof(mbstate_t));
18+
ASSERT_NE(LIBC_NAMESPACE::mbsinit(&ps), 0);
19+
ASSERT_NE(LIBC_NAMESPACE::mbsinit(nullptr), 0);
20+
}
21+
22+
TEST(LlvmLibcMBSInitTest, ConversionTest) {
23+
const char *src = "\xf0\x9f\xa4\xa3"; // 4 byte emoji
24+
wchar_t dest[2];
25+
mbstate_t ps;
26+
LIBC_NAMESPACE::memset(&ps, 0, sizeof(mbstate_t));
27+
28+
ASSERT_NE(LIBC_NAMESPACE::mbsinit(&ps), 0);
29+
LIBC_NAMESPACE::mbrtowc(dest, src, 2, &ps); // partial conversion
30+
ASSERT_EQ(LIBC_NAMESPACE::mbsinit(&ps), 0);
31+
LIBC_NAMESPACE::mbrtowc(dest, src + 2, 2, &ps); // complete conversion
32+
ASSERT_NE(LIBC_NAMESPACE::mbsinit(&ps), 0); // state should be reset now
33+
}

0 commit comments

Comments
 (0)