Remove deprecated cstdbool from utf8proc

This commit is contained in:
Phillip Stephens 2025-05-01 19:26:15 -07:00
parent 4f3531dd1f
commit b37c3d3dcb

View File

@ -20,7 +20,6 @@
* DEALINGS IN THE SOFTWARE. * DEALINGS IN THE SOFTWARE.
*/ */
/** /**
* @mainpage * @mainpage
* *
@ -36,9 +35,11 @@
* The features of utf8proc include: * The features of utf8proc include:
* *
* - Transformation of strings (@ref utf8proc_map) to: * - Transformation of strings (@ref utf8proc_map) to:
* - decompose (@ref UTF8PROC_DECOMPOSE) or compose (@ref UTF8PROC_COMPOSE) Unicode combining characters (http://en.wikipedia.org/wiki/Combining_character) * - decompose (@ref UTF8PROC_DECOMPOSE) or compose (@ref UTF8PROC_COMPOSE) Unicode combining characters
* (http://en.wikipedia.org/wiki/Combining_character)
* - canonicalize Unicode compatibility characters (@ref UTF8PROC_COMPAT) * - canonicalize Unicode compatibility characters (@ref UTF8PROC_COMPAT)
* - strip "ignorable" (@ref UTF8PROC_IGNORE) characters, control characters (@ref UTF8PROC_STRIPCC), or combining characters such as accents (@ref UTF8PROC_STRIPMARK) * - strip "ignorable" (@ref UTF8PROC_IGNORE) characters, control characters (@ref UTF8PROC_STRIPCC), or combining
* characters such as accents (@ref UTF8PROC_STRIPMARK)
* - case-folding (@ref UTF8PROC_CASEFOLD) * - case-folding (@ref UTF8PROC_CASEFOLD)
* - Unicode normalization: @ref utf8proc_NFD, @ref utf8proc_NFC, @ref utf8proc_NFKD, @ref utf8proc_NFKC * - Unicode normalization: @ref utf8proc_NFD, @ref utf8proc_NFC, @ref utf8proc_NFKD, @ref utf8proc_NFKC
* - Detecting grapheme boundaries (@ref utf8proc_grapheme_break and @ref UTF8PROC_CHARBOUND) * - Detecting grapheme boundaries (@ref utf8proc_grapheme_break and @ref UTF8PROC_CHARBOUND)
@ -98,7 +99,6 @@ typedef bool utf8proc_bool;
#endif #endif
#else #else
#ifdef __cplusplus #ifdef __cplusplus
# include <cstdbool>
#include <cinttypes> #include <cinttypes>
#else #else
#include <stdbool.h> #include <stdbool.h>
@ -148,14 +148,14 @@ typedef bool utf8proc_bool;
* In case of success, the number of bytes read is returned; otherwise, a * In case of success, the number of bytes read is returned; otherwise, a
* negative error code is returned. * negative error code is returned.
*/ */
static inline utf8proc_ssize_t utf8proc_iterate( static inline utf8proc_ssize_t utf8proc_iterate(const utf8proc_uint8_t* str, utf8proc_ssize_t strlen,
const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_int32_t *dst utf8proc_int32_t* dst) {
) {
utf8proc_uint32_t uc; utf8proc_uint32_t uc;
const utf8proc_uint8_t* end; const utf8proc_uint8_t* end;
*dst = -1; *dst = -1;
if (!strlen) return 0; if (!strlen)
return 0;
end = str + ((strlen < 0) ? 4 : strlen); end = str + ((strlen < 0) ? 4 : strlen);
uc = *str++; uc = *str++;
if (uc < 0x80) { if (uc < 0x80) {
@ -163,10 +163,12 @@ static inline utf8proc_ssize_t utf8proc_iterate(
return 1; return 1;
} }
// Must be between 0xc2 and 0xf4 inclusive to be valid // Must be between 0xc2 and 0xf4 inclusive to be valid
if ((uc - 0xc2) > (0xf4-0xc2)) return UTF8PROC_ERROR_INVALIDUTF8; if ((uc - 0xc2) > (0xf4 - 0xc2))
return UTF8PROC_ERROR_INVALIDUTF8;
if (uc < 0xe0) { // 2-byte sequence if (uc < 0xe0) { // 2-byte sequence
// Must have valid continuation character // Must have valid continuation character
if (!UTF8PROC_cont(*str)) return UTF8PROC_ERROR_INVALIDUTF8; if (!UTF8PROC_cont(*str))
return UTF8PROC_ERROR_INVALIDUTF8;
*dst = ((uc & 0x1f) << 6) | (*str & 0x3f); *dst = ((uc & 0x1f) << 6) | (*str & 0x3f);
return 2; return 2;
} }
@ -188,9 +190,11 @@ static inline utf8proc_ssize_t utf8proc_iterate(
return UTF8PROC_ERROR_INVALIDUTF8; return UTF8PROC_ERROR_INVALIDUTF8;
// Make sure in correct range (0x10000 - 0x10ffff) // Make sure in correct range (0x10000 - 0x10ffff)
if (uc == 0xf0) { if (uc == 0xf0) {
if (*str < 0x90) return UTF8PROC_ERROR_INVALIDUTF8; if (*str < 0x90)
return UTF8PROC_ERROR_INVALIDUTF8;
} else if (uc == 0xf4) { } else if (uc == 0xf4) {
if (*str > 0x8f) return UTF8PROC_ERROR_INVALIDUTF8; if (*str > 0x8f)
return UTF8PROC_ERROR_INVALIDUTF8;
} }
*dst = ((uc & 7) << 18) | ((*str & 0x3f) << 12) | ((str[1] & 0x3f) << 6) | (str[2] & 0x3f); *dst = ((uc & 7) << 18) | ((*str & 0x3f) << 12) | ((str[1] & 0x3f) << 6) | (str[2] & 0x3f);
return 4; return 4;
@ -228,16 +232,17 @@ static inline utf8proc_ssize_t utf8proc_encode_char(utf8proc_int32_t uc, utf8pro
dst[2] = 0x80 + ((uc >> 6) & 0x3F); dst[2] = 0x80 + ((uc >> 6) & 0x3F);
dst[3] = 0x80 + (uc & 0x3F); dst[3] = 0x80 + (uc & 0x3F);
return 4; return 4;
} else return 0; } else
return 0;
} }
#ifdef __cplusplus #ifdef __cplusplus
#include <iterator> #include <iterator>
#include <string> #include <string>
class UTF8Iterator class UTF8Iterator {
{
std::string_view::const_iterator m_it; std::string_view::const_iterator m_it;
public: public:
using iterator_category = std::forward_iterator_tag; using iterator_category = std::forward_iterator_tag;
using value_type = uint32_t; using value_type = uint32_t;
@ -246,22 +251,17 @@ public:
using reference = uint32_t&; using reference = uint32_t&;
UTF8Iterator(const std::string_view::const_iterator& it) : m_it(it) {} UTF8Iterator(const std::string_view::const_iterator& it) : m_it(it) {}
UTF8Iterator& operator+=(size_t v) UTF8Iterator& operator+=(size_t v) {
{ for (size_t i = 0; i < v; ++i) {
for (size_t i=0 ; i<v ; ++i)
{
utf8proc_int32_t dummy; utf8proc_int32_t dummy;
utf8proc_ssize_t sz = utf8proc_iterate(reinterpret_cast<const utf8proc_uint8_t*>(&*m_it), -1, &dummy); utf8proc_ssize_t sz = utf8proc_iterate(reinterpret_cast<const utf8proc_uint8_t*>(&*m_it), -1, &dummy);
#ifndef NDEBUG #ifndef NDEBUG
if (*m_it == '\0') if (*m_it == '\0') {
{
fprintf(stderr, "ERROR! UTF8-iterator null-term fail\n"); fprintf(stderr, "ERROR! UTF8-iterator null-term fail\n");
abort(); abort();
} } else if (sz > 0)
else if (sz > 0)
m_it += sz; m_it += sz;
else else {
{
fprintf(stderr, "ERROR! UTF8Iterator character fail"); fprintf(stderr, "ERROR! UTF8Iterator character fail");
abort(); abort();
} }
@ -272,29 +272,22 @@ public:
} }
return *this; return *this;
} }
UTF8Iterator& operator++() UTF8Iterator& operator++() { return this->operator+=(1); }
{ UTF8Iterator operator+(size_t v) const {
return this->operator+=(1);
}
UTF8Iterator operator+(size_t v) const
{
UTF8Iterator ret(m_it); UTF8Iterator ret(m_it);
ret += v; ret += v;
return ret; return ret;
} }
uint32_t operator*() const uint32_t operator*() const {
{
utf8proc_int32_t ret; utf8proc_int32_t ret;
utf8proc_iterate(reinterpret_cast<const utf8proc_uint8_t*>(&*m_it), -1, &ret); utf8proc_iterate(reinterpret_cast<const utf8proc_uint8_t*>(&*m_it), -1, &ret);
return ret; return ret;
} }
std::string_view::const_iterator iter() const { return m_it; } std::string_view::const_iterator iter() const { return m_it; }
size_t countTo(std::string_view::const_iterator end) const size_t countTo(std::string_view::const_iterator end) const {
{
UTF8Iterator it(m_it); UTF8Iterator it(m_it);
size_t ret = 0; size_t ret = 0;
while (it.iter() < end && *it != '\0') while (it.iter() < end && *it != '\0') {
{
++ret; ++ret;
++it; ++it;
} }
@ -305,4 +298,3 @@ public:
#endif #endif
#endif #endif