diff options
Diffstat (limited to 'src/u8c/u8.h.d')
-rw-r--r-- | src/u8c/u8.h.d/u8alloc.c | 14 | ||||
-rw-r--r-- | src/u8c/u8.h.d/u8dec.c | 41 | ||||
-rw-r--r-- | src/u8c/u8.h.d/u8enc.c | 50 | ||||
-rw-r--r-- | src/u8c/u8.h.d/u8free.c | 10 |
4 files changed, 64 insertions, 51 deletions
diff --git a/src/u8c/u8.h.d/u8alloc.c b/src/u8c/u8.h.d/u8alloc.c index ba02bc8..ba28243 100644 --- a/src/u8c/u8.h.d/u8alloc.c +++ b/src/u8c/u8.h.d/u8alloc.c @@ -17,12 +17,16 @@ # include <stdlib.h> # include <u8c/err.h> # include <u8c/u8.h> -bool u8c_u8alloc(unsigned char * * const _u8,size_t const _sz) { +struct u8c_u8alloc_tuple u8c_u8alloc(size_t const _sz) { + struct u8c_u8alloc_tuple ret = { + .stat = false, + }; unsigned char * arr = NULL; if((arr = calloc(sizeof *arr,_sz)) == NULL) { - u8c_seterr(U"u8c_u8alloc: Unable to allocate resources (not enough memory?).",u8c_errtyp_badalloc); - return false; + u8c_seterr(u8c_errtyp_badalloc,U"u8c_u8alloc: Unable to allocate resources (not enough memory?)."); + ret.stat = true; + return ret; } - *_u8 = arr; - return false; + ret.u8 = arr; + return ret; } diff --git a/src/u8c/u8.h.d/u8dec.c b/src/u8c/u8.h.d/u8dec.c index 365c81a..4cba14f 100644 --- a/src/u8c/u8.h.d/u8dec.c +++ b/src/u8c/u8.h.d/u8dec.c @@ -19,23 +19,24 @@ # include <stdint.h> # include <u8c/SIZE_C.h> # include <u8c/err.h> -# include <u8c/u32.h> +# include <u8c/str.h> # include <u8c/u8.h> # include <uchar.h> -bool u8c_u8dec(size_t * const _sz,char32_t const * * const _out,unsigned char const * const _in) { - assert(_out != NULL); - assert(_in != NULL); - register size_t insz = SIZE_C(0x0); - register size_t outsz = SIZE_C(0x1); - for(register size_t n = SIZE_C(0x0);n <= SIZE_MAX;outsz += SIZE_C(0x1)) { /* First pass: get size of input array and determine size of output array. */ +struct u8c_u8dec_tuple u8c_u8dec(unsigned char const * const restrict _in) { + struct u8c_u8dec_tuple ret = { + .stat = false, + }; + register size_t insz = SIZE_C(0x0); + for(register size_t n = SIZE_C(0x0);n <= SIZE_MAX;ret.strsz += SIZE_C(0x1)) { /* First pass: get size of input array and determine size of output array. */ register unsigned char const tmp = _in[n]; if(tmp == UINT8_C(0x0)) { /* Null-terminator: end of string has been reached. */ insz = n; goto nottoobig; } if(tmp >= UINT8_C(0b11111000)) { /* Too big. */ - u8c_seterr(U"u8c_u8dec: Character out of range (too big).",u8c_errtyp_u8oor); - return true; + u8c_seterr(u8c_errtyp_u8oor,U"u8c_u8dec: Character out of range (too big)."); + ret.stat = true; + return ret; } if(tmp >= UINT8_C(0b11110000)) { /* Four byte. */ n += SIZE_C(0x4); @@ -53,15 +54,18 @@ bool u8c_u8dec(size_t * const _sz,char32_t const * * const _out,unsigned char co n += SIZE_C(0x1); } /* Input is not null-terminated. */ - u8c_seterr(U"u8c_u8dec: Unterminated input.",u8c_errtyp_untermin); - return true; + u8c_seterr(u8c_errtyp_untermin,U"u8c_u8dec: Unterminated input."); + ret.stat = true; + return ret; nottoobig:; - if(_sz != NULL) { - *_sz = outsz; - } uint_least32_t * out = NULL; - if(u8c_u32alloc(&out,outsz + SIZE_C(0x1))) { - return false; + { + struct u8c_stralloc_tuple const tuple = u8c_stralloc(ret.strsz + SIZE_C(0x1)); + if(tuple.stat) { + ret.stat = true; + return ret; + } + out = tuple.str; } for(register size_t n = SIZE_C(0x0),outn = SIZE_C(0x0);n < insz;outn += SIZE_C(0x1)) { /* Second pass: decode UTF-8. */ if(_in[n] >= UINT8_C(0b11110000)) { /* Four bytes. */ @@ -99,7 +103,6 @@ nottoobig:; n += SIZE_C(0x1); continue; } - u8c_u32free(_out); - *_out = out; - return false; + ret.str = out; + return ret; } diff --git a/src/u8c/u8.h.d/u8enc.c b/src/u8c/u8.h.d/u8enc.c index f3f3570..2ac0007 100644 --- a/src/u8c/u8.h.d/u8enc.c +++ b/src/u8c/u8.h.d/u8enc.c @@ -19,48 +19,53 @@ # include <stdint.h> # include <u8c/SIZE_C.h> # include <u8c/err.h> -# include <u8c/u32.h> +# include <u8c/main.h> +# include <u8c/str.h> # include <u8c/u8.h> # include <uchar.h> -bool u8c_u8enc(size_t * const _sz,unsigned char const * * const _out,char32_t const * const _in) { - assert(_out != NULL); - assert(_in != NULL); - size_t insz = SIZE_C(0x0); /* Size of input array (bytes). */ - size_t outsz = SIZE_C(0x0); /* Size of output array /bytes). */ +struct u8c_u8enc_tuple u8c_u8enc(char32_t const * const restrict _in) { + struct u8c_u8enc_tuple ret = { + .stat = false, + }; + size_t insz = SIZE_C(0x0); /* Size of input array (bytes). */ for(register size_t n = SIZE_C(0x0);n <= SIZE_MAX;n += SIZE_C(0x1)) { /* First pass: get size of input array, and determine size of output array. */ register char32_t const tmp = _in[n]; - if(tmp > u8c_u32max) { /* Codepoint out of range. */ - u8c_seterr(U"u8c_u8enc: Codepoint out of range (too big).",u8c_errtyp_u32oor); - return true; + if(tmp > u8c_unimax) { /* Codepoint out of range. */ + u8c_seterr(u8c_errtyp_stroor,U"u8c_u8enc: Codepoint out of range (too big)."); + ret.stat = true; + return ret; } if(tmp >= UINT32_C(0x10000)) { /* 4 bytes. */ - outsz += SIZE_C(0x4); + ret.u8sz += SIZE_C(0x4); continue; } if(tmp >= UINT32_C(0x800)) { /* 3 bytes. */ - outsz += SIZE_C(0x3); + ret.u8sz += SIZE_C(0x3); continue; } if(tmp >= UINT32_C(0x80)) { /* 2 bytes. */ - outsz += SIZE_C(0x2); + ret.u8sz += SIZE_C(0x2); continue; } /* 1 byte. */ - outsz += SIZE_C(0x1); + ret.u8sz += SIZE_C(0x1); if(tmp == UINT32_C(0x0)) { insz = n + SIZE_C(0x1); goto nottoobig; } } - u8c_seterr(U"u8c_u8enc: Unterminated input.",u8c_errtyp_untermin); - return true; + u8c_seterr(u8c_errtyp_untermin,U"u8c_u8enc: Unterminated input."); + ret.stat = true; + return ret; nottoobig:; - if(_sz != NULL) { - *_sz = outsz; - } unsigned char * out = NULL; - if(u8c_u8alloc(&out,outsz + SIZE_C(0x1))) { - return true; + { + struct u8c_u8alloc_tuple const tuple = u8c_u8alloc(ret.u8sz + SIZE_C(0x1)); + if(tuple.stat) { + ret.stat = true; + return ret; + } + out = tuple.u8; } for(register size_t n = SIZE_C(0x0), outn = SIZE_C(0x0);n < insz;n += SIZE_C(0x1),outn += SIZE_C(0x1)) { /* Second pass: encode each codepoint into UTF-8. */ register char32_t const tmp = _in[n]; @@ -91,7 +96,6 @@ nottoobig:; /* One byte. */ out[outn] = (uint_least8_t)tmp; } - u8c_u8free(_out); - *_out = out; - return false; + ret.u8 = out; + return ret; } diff --git a/src/u8c/u8.h.d/u8free.c b/src/u8c/u8.h.d/u8free.c index af5a6bd..a0b61a8 100644 --- a/src/u8c/u8.h.d/u8free.c +++ b/src/u8c/u8.h.d/u8free.c @@ -17,8 +17,10 @@ # include <stdint.h> # include <stdlib.h> # include <u8c/u8.h> -bool u8c_u8free(unsigned char const * * const _u8) { - free((unsigned char *)*_u8); - *_u8 = NULL; - return false; +struct u8c_u8free_tuple u8c_u8free(unsigned char const * const restrict _u8) { + struct u8c_u8free_tuple ret = { + .stat = false, + }; + free((unsigned char *)_u8); + return ret; } |