From 3c807402b3491ccf94af5f4da8394bbd3ff91977 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Sun, 19 Sep 2021 12:48:04 +0200 Subject: [PATCH] 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. --- Userland/Libraries/LibC/wchar.cpp | 17 ++++++----------- Userland/Libraries/LibC/wchar.h | 1 + 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/Userland/Libraries/LibC/wchar.cpp b/Userland/Libraries/LibC/wchar.cpp index aff52acd84..eb8242fd53 100644 --- a/Userland/Libraries/LibC/wchar.cpp +++ b/Userland/Libraries/LibC/wchar.cpp @@ -9,11 +9,6 @@ #include #include -static void mbstate_reset(mbstate_t* state) -{ - *state = { 0 }; -} - static unsigned int mbstate_stored_bytes(mbstate_t* state) { 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) { - static mbstate_t _anonymous_state = { 0 }; + static mbstate_t _anonymous_state = {}; if (state == nullptr) { 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 == nullptr) { if (mbstate_expected_bytes(state) == mbstate_stored_bytes(state)) { - mbstate_reset(state); + *state = {}; return 0; } else { - mbstate_reset(state); + *state = {}; errno = EILSEQ; 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 if (expected_bytes == 0) { - mbstate_reset(state); + *state = {}; errno = EILSEQ; 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 if ((c & 0b11000000) != 0b10000000) { // Invalid multibyte character - mbstate_reset(state); + *state = {}; errno = EILSEQ; 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 - mbstate_reset(state); + *state = {}; if (codepoint == 0) { return 0; diff --git a/Userland/Libraries/LibC/wchar.h b/Userland/Libraries/LibC/wchar.h index 557217b454..cb15c3f3ff 100644 --- a/Userland/Libraries/LibC/wchar.h +++ b/Userland/Libraries/LibC/wchar.h @@ -18,6 +18,7 @@ __BEGIN_DECLS typedef __WINT_TYPE__ wint_t; typedef unsigned long int wctype_t; +// A zero-initialized mbstate_t struct must be a valid initial state. typedef struct { unsigned char bytes[4]; } mbstate_t;