Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,7 @@ if(LLVM_LIBC_FULL_BUILD)
# wchar.h entrypoints
libc.src.wchar.mblen
libc.src.wchar.mbrlen
libc.src.wchar.mbsinit
libc.src.wchar.mbrtowc
libc.src.wchar.mbtowc
libc.src.wchar.wcrtomb
Expand Down
6 changes: 6 additions & 0 deletions libc/include/wchar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ functions:
- type: wchar_t *__restrict
- type: const char *__restrict
- type: size_t
- name: mbsinit
standards:
- stdc
return_type: int
arguments:
- type: mbstate_t *
- name: mblen
standards:
- stdc
Expand Down
15 changes: 15 additions & 0 deletions libc/src/wchar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ add_entrypoint_object(
libc.src.__support.libc_errno
)

add_entrypoint_object(
mbsinit
SRCS
mbsinit.cpp
HDRS
mbsinit.h
DEPENDS
libc.hdr.types.wchar_t
libc.hdr.types.mbstate_t
libc.src.__support.common
libc.src.__support.macros.config
libc.src.__support.wchar.character_converter
libc.src.__support.wchar.mbstate
)

add_entrypoint_object(
mbrtowc
SRCS
Expand Down
26 changes: 26 additions & 0 deletions libc/src/wchar/mbsinit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===-- Implementation of mbsinit -----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/wchar/mbsinit.h"

#include "hdr/types/mbstate_t.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/wchar/character_converter.h"
#include "src/__support/wchar/mbstate.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, mbsinit, (mbstate_t * ps)) {
if (ps == nullptr)
return true;
internal::CharacterConverter cr(reinterpret_cast<internal::mbstate *>(ps));
return cr.isValidState() && cr.isEmpty();
}

} // namespace LIBC_NAMESPACE_DECL
22 changes: 22 additions & 0 deletions libc/src/wchar/mbsinit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Implementation header for mbsinit ---------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_WCHAR_MBSINIT_H
#define LLVM_LIBC_SRC_WCHAR_MBSINIT_H

#include "hdr/types/mbstate_t.h"
#include "hdr/types/size_t.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

int mbsinit(mbstate_t *ps);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_WCHAR_MBSINIT_H
15 changes: 15 additions & 0 deletions libc/test/src/wchar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,22 @@ add_libc_test(
libc.src.string.memset
libc.src.wchar.mbrlen
libc.hdr.types.mbstate_t
libc.hdr.types.wchar_t
libc.test.UnitTest.ErrnoCheckingTest
)

add_libc_test(
mbsinit_test
SUITE
libc_wchar_unittests
SRCS
mbsinit_test.cpp
DEPENDS
libc.src.string.memset
libc.src.wchar.mbsinit
libc.src.wchar.mbrtowc
libc.hdr.types.mbstate_t
libc.hdr.types.wchar_t
)

add_libc_test(
Expand Down
33 changes: 33 additions & 0 deletions libc/test/src/wchar/mbsinit_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===-- Unittests for mbsinit ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "hdr/types/wchar_t.h"
#include "src/string/memset.h"
#include "src/wchar/mbrtowc.h"
#include "src/wchar/mbsinit.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcMBSInitTest, EmptyState) {
mbstate_t ps;
LIBC_NAMESPACE::memset(&ps, 0, sizeof(mbstate_t));
ASSERT_NE(LIBC_NAMESPACE::mbsinit(&ps), 0);
ASSERT_NE(LIBC_NAMESPACE::mbsinit(nullptr), 0);
}

TEST(LlvmLibcMBSInitTest, ConversionTest) {
const char *src = "\xf0\x9f\xa4\xa3"; // 4 byte emoji
wchar_t dest[2];
mbstate_t ps;
LIBC_NAMESPACE::memset(&ps, 0, sizeof(mbstate_t));

ASSERT_NE(LIBC_NAMESPACE::mbsinit(&ps), 0);
LIBC_NAMESPACE::mbrtowc(dest, src, 2, &ps); // partial conversion
ASSERT_EQ(LIBC_NAMESPACE::mbsinit(&ps), 0);
LIBC_NAMESPACE::mbrtowc(dest, src + 2, 2, &ps); // complete conversion
ASSERT_NE(LIBC_NAMESPACE::mbsinit(&ps), 0); // state should be reset now
}
Loading