X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=internals%2F_PDCLIB_encoding.h;h=f53d77ab9e9bc67193833bac076766004794481b;hb=310c24160b52d6f09e5a9a6d935a2f60b5826e2a;hp=b29fe93d0a8acba5d715c1326cdb24740a14c60b;hpb=f3d811f877df14a15abccb5d6f11b2800a727db0;p=pdclib.old diff --git a/internals/_PDCLIB_encoding.h b/internals/_PDCLIB_encoding.h index b29fe93..f53d77a 100644 --- a/internals/_PDCLIB_encoding.h +++ b/internals/_PDCLIB_encoding.h @@ -4,36 +4,27 @@ Permission is granted to use, modify, and / or redistribute at will. */ -#ifndef _PDCLIB_ENCODING_H -#define _PDCLIB_ENCODING_H _PDCLIB_ENCODING_H +#ifndef __PDCLIB_ENCODING_H +#define __PDCLIB_ENCODING_H __PDCLIB_ENCODING_H #include "_PDCLIB_int.h" -#ifndef __cplusplus -typedef _PDCLIB_int_least16_t _PDCLIB_char16_t; -typedef _PDCLIB_int_least32_t _PDCLIB_char32_t; -#else -typedef char16_t _PDCLIB_char16_t; -typedef char32_t _PDCLIB_char32_t; -#endif - -#ifdef _PDCLIB_WCHAR_IS_UCS2 /* Must be cauued with bufsize >= 1, in != NULL, out != NULL, ps != NULL * - * Converts a wchar to a UCS4 (char32_t) value. Returns + * Converts a UTF-16 (char16_t) to a UCS4 (char32_t) value. Returns * 1, 2 : Valid character (converted to UCS-4) * -1 : Encoding error * -2 : Partial character (only lead surrogate in buffer) */ -static inline int _PDCLIB_wcrtoc32( +static inline int _PDCLIB_c16rtoc32( _PDCLIB_char32_t *_PDCLIB_restrict out, - const _PDCLIB_wchar_t *_PDCLIB_restrict in, - _PDCLIB__PDCLIB_size_t bufsize, + const _PDCLIB_char16_t *_PDCLIB_restrict in, + _PDCLIB_size_t bufsize, _PDCLIB_mbstate_t *_PDCLIB_restrict ps ) { if(ps->_Surrogate) { // We already have a lead surrogate - if(*in & ~0x3FF != 0xDC00) { + if((*in & ~0x3FF) != 0xDC00) { // Encoding error return -1; } else { @@ -42,11 +33,11 @@ static inline int _PDCLIB_wcrtoc32( ps->_Surrogate = 0; return 1; } - } if(*in & ~0x3FF == 0xD800) { + } if((*in & ~0x3FF) == 0xD800) { // Lead surrogate if(bufsize >= 2) { // Buffer big enough - if(in[1] & ~0x3FF != 0xDC00) { + if((in[1] & ~0x3FF) != 0xDC00) { // Encoding error return -1; } else { @@ -65,10 +56,10 @@ static inline int _PDCLIB_wcrtoc32( } } -static inline _PDCLIB_size_t _PDCLIB_c32rtowc( +static inline _PDCLIB_size_t _PDCLIB_c32rtoc16( _PDCLIB_wchar_t *_PDCLIB_restrict out, const _PDCLIB_char32_t *_PDCLIB_restrict in, - _PDCLIB__PDCLIB_size_t bufsize, + _PDCLIB_size_t bufsize, _PDCLIB_mbstate_t *_PDCLIB_restrict ps ) { @@ -94,55 +85,59 @@ static inline _PDCLIB_size_t _PDCLIB_c32rtowc( } } } -#else -/* Dummy implementation for when wc == c32 */ -static inline _PDCLIB_size_t _PDCLIB_wcrtoc32( - _PDCLIB_char32_t *_PDCLIB_restrict out, - const _PDCLIB_wchar_t *_PDCLIB_restrict in, - _PDCLIB__PDCLIB_size_t bufsize, - _PDCLIB_mbstate_t *_PDCLIB_restrict ps -) -{ - *out = *in; - return 1; -} -static inline _PDCLIB_size_t _PDCLIB_c32rtowc( - _PDCLIB_wchar_t *_PDCLIB_restrict out, - const _PDCLIB_char32_t *_PDCLIB_restrict in, - _PDCLIB__PDCLIB_size_t bufsize, - _PDCLIB_mbstate_t *_PDCLIB_restrict ps -) -{ - *out = *in; - return 1; -} -#endif - -typedef struct { - /* Reads at most *_P_insz bytes from *_P_inbuf and writes the result into - * *_P_outbuf, writing at most *_P_outsz characters. Updates *_P_outbuf, - * *_P_outsz, *_P_inbuf, *_P_outsz with the resulting state +struct _PDCLIB_charcodec { + /* Reads at most *_P_insz code units from *_P_inbuf and writes the result + * into *_P_outbuf, writing at most *_P_outsz code units. Updates + * *_P_outbuf, *_P_outsz, *_P_inbuf, *_P_outsz with the resulting state + * + * If _P_outbuf is NULL, then the input must be processed but no output + * generated. _P_outsz may be processed as normal. * * Returns true if the conversion completed successfully (i.e. one of * _P_outsize or _P_insize reached zero and no coding errors were * encountered), else return false. */ - _PDCLIB_bool_t (*__mbtoc32)( - _PDCLIB_char32_t **_PDCLIB_restrict _P_outbuf, - _PDCLIB_size_t *_PDCLIB_restrict _P_outsz, - const char **_PDCLIB_restrict _P_inbuf, - _PDCLIB_size_t *_PDCLIB_restrict _P_insz, - _PDCLIB_mbstate_t *_PDCLIB_restrict _P_ps + + /* UCS-4 variants. Mandatory. */ + + _PDCLIB_bool (*__mbstoc32s)( + _PDCLIB_char32_t *_PDCLIB_restrict *_PDCLIB_restrict _P_outbuf, + _PDCLIB_size_t *_PDCLIB_restrict _P_outsz, + const char *_PDCLIB_restrict *_PDCLIB_restrict _P_inbuf, + _PDCLIB_size_t *_PDCLIB_restrict _P_insz, + _PDCLIB_mbstate_t *_PDCLIB_restrict _P_ps + ); + + _PDCLIB_bool (*__c32stombs)( + char *_PDCLIB_restrict *_PDCLIB_restrict _P_outbuf, + _PDCLIB_size_t *_PDCLIB_restrict _P_outsz, + const _PDCLIB_char32_t *_PDCLIB_restrict *_PDCLIB_restrict _P_inbuf, + _PDCLIB_size_t *_PDCLIB_restrict _P_insz, + _PDCLIB_mbstate_t *_PDCLIB_restrict _P_ps + ); + + /* UTF-16 variants; same as above except optional. + * + * If not provided, _PDCLib will internally synthesize on top of the UCS-4 + * variants above, albeit at a performance cost. + */ + + _PDCLIB_bool (*__mbstoc16s)( + _PDCLIB_char16_t *_PDCLIB_restrict *_PDCLIB_restrict _P_outbuf, + _PDCLIB_size_t *_PDCLIB_restrict _P_outsz, + const char *_PDCLIB_restrict *_PDCLIB_restrict _P_inbuf, + _PDCLIB_size_t *_PDCLIB_restrict _P_insz, + _PDCLIB_mbstate_t *_PDCLIB_restrict _P_ps ); - _PDCLIB_bool_t (*__c32tomb)( - char **_PDCLIB_restrict _P_outbuf, - _PDCLIB_size_t *_PDCLIB_restrict _P_outsz, - const _PDCLIB_char32_t **_PDCLIB_restrict _P_inbuf, - _PDCLIB_size_t *_PDCLIB_restrict _P_insz, - _PDCLIB_mbstate_t *_PDCLIB_restrict _P_ps + _PDCLIB_bool (*__c16stombs)( + char *_PDCLIB_restrict *_PDCLIB_restrict _P_outbuf, + _PDCLIB_size_t *_PDCLIB_restrict _P_outsz, + const _PDCLIB_char16_t *_PDCLIB_restrict *_PDCLIB_restrict _P_inbuf, + _PDCLIB_size_t *_PDCLIB_restrict _P_insz, + _PDCLIB_mbstate_t *_PDCLIB_restrict _P_ps ); -} _PDCLIB_charcodec; +}; #endif