mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:17:44 +00:00
LibC: Remove the mbstate_reset helper
A zero-initialized mbstate_t struct has to be a valid initial state, so we can just zero-initialize it whenever we need to reset. Having a helper function for resetting the struct might imply that you can add additional setup operations afterwards, which is not the case.
This commit is contained in:
parent
ab46864674
commit
3c807402b3
2 changed files with 7 additions and 11 deletions
|
@ -9,11 +9,6 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <wchar.h>
|
#include <wchar.h>
|
||||||
|
|
||||||
static void mbstate_reset(mbstate_t* state)
|
|
||||||
{
|
|
||||||
*state = { 0 };
|
|
||||||
}
|
|
||||||
|
|
||||||
static unsigned int mbstate_stored_bytes(mbstate_t* state)
|
static unsigned int mbstate_stored_bytes(mbstate_t* state)
|
||||||
{
|
{
|
||||||
for (unsigned int i = 0; i < sizeof(state->bytes); i++) {
|
for (unsigned int i = 0; i < sizeof(state->bytes); i++) {
|
||||||
|
@ -215,7 +210,7 @@ wint_t btowc(int c)
|
||||||
|
|
||||||
size_t mbrtowc(wchar_t* pwc, const char* s, size_t n, mbstate_t* state)
|
size_t mbrtowc(wchar_t* pwc, const char* s, size_t n, mbstate_t* state)
|
||||||
{
|
{
|
||||||
static mbstate_t _anonymous_state = { 0 };
|
static mbstate_t _anonymous_state = {};
|
||||||
|
|
||||||
if (state == nullptr) {
|
if (state == nullptr) {
|
||||||
state = &_anonymous_state;
|
state = &_anonymous_state;
|
||||||
|
@ -224,10 +219,10 @@ size_t mbrtowc(wchar_t* pwc, const char* s, size_t n, mbstate_t* state)
|
||||||
// If s is nullptr, check if the state contains a complete multibyte character
|
// If s is nullptr, check if the state contains a complete multibyte character
|
||||||
if (s == nullptr) {
|
if (s == nullptr) {
|
||||||
if (mbstate_expected_bytes(state) == mbstate_stored_bytes(state)) {
|
if (mbstate_expected_bytes(state) == mbstate_stored_bytes(state)) {
|
||||||
mbstate_reset(state);
|
*state = {};
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
mbstate_reset(state);
|
*state = {};
|
||||||
errno = EILSEQ;
|
errno = EILSEQ;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -251,7 +246,7 @@ size_t mbrtowc(wchar_t* pwc, const char* s, size_t n, mbstate_t* state)
|
||||||
|
|
||||||
// Check if the first byte is invalid
|
// Check if the first byte is invalid
|
||||||
if (expected_bytes == 0) {
|
if (expected_bytes == 0) {
|
||||||
mbstate_reset(state);
|
*state = {};
|
||||||
errno = EILSEQ;
|
errno = EILSEQ;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -269,7 +264,7 @@ size_t mbrtowc(wchar_t* pwc, const char* s, size_t n, mbstate_t* state)
|
||||||
// Continuation bytes have to start with 0b10xxxxxx
|
// Continuation bytes have to start with 0b10xxxxxx
|
||||||
if ((c & 0b11000000) != 0b10000000) {
|
if ((c & 0b11000000) != 0b10000000) {
|
||||||
// Invalid multibyte character
|
// Invalid multibyte character
|
||||||
mbstate_reset(state);
|
*state = {};
|
||||||
errno = EILSEQ;
|
errno = EILSEQ;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -296,7 +291,7 @@ size_t mbrtowc(wchar_t* pwc, const char* s, size_t n, mbstate_t* state)
|
||||||
}
|
}
|
||||||
|
|
||||||
// We don't have a shift state that we need to keep, so just clear the entire state
|
// We don't have a shift state that we need to keep, so just clear the entire state
|
||||||
mbstate_reset(state);
|
*state = {};
|
||||||
|
|
||||||
if (codepoint == 0) {
|
if (codepoint == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -18,6 +18,7 @@ __BEGIN_DECLS
|
||||||
typedef __WINT_TYPE__ wint_t;
|
typedef __WINT_TYPE__ wint_t;
|
||||||
typedef unsigned long int wctype_t;
|
typedef unsigned long int wctype_t;
|
||||||
|
|
||||||
|
// A zero-initialized mbstate_t struct must be a valid initial state.
|
||||||
typedef struct {
|
typedef struct {
|
||||||
unsigned char bytes[4];
|
unsigned char bytes[4];
|
||||||
} mbstate_t;
|
} mbstate_t;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue