Unify code paths in herodotus_read_codepoint() - libgrapheme - unicode string library
 (HTM) git clone git://git.suckless.org/libgrapheme
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 28815433e3595cba51a40c4a5e291da3a8746d78
 (DIR) parent f70ea8c12ab5b7ad6f90f8860544779a43ce8a9e
 (HTM) Author: Laslo Hunhold <dev@frign.de>
       Date:   Mon,  3 Oct 2022 21:14:52 +0200
       
       Unify code paths in herodotus_read_codepoint()
       
       This saves redundancy.
       
       Signed-off-by: Laslo Hunhold <dev@frign.de>
       
       Diffstat:
         M src/util.c                          |      55 +++++++++++++++----------------
       
       1 file changed, 26 insertions(+), 29 deletions(-)
       ---
 (DIR) diff --git a/src/util.c b/src/util.c
       @@ -31,9 +31,9 @@ herodotus_reader_copy(const HERODOTUS_READER *src, HERODOTUS_READER *dest)
                size_t i;
        
                /*
       -         * we copy such that we have a "fresh" start and build
       -         * on the fact that src->soft_limit[i] for any i and src->srclen
       -         * are always larger or equal to src->off
       +         * we copy such that we have a "fresh" start and build on the
       +         * fact that src->soft_limit[i] for any i and src->srclen are
       +         * always larger or equal to src->off
                 */
                dest->type = src->type;
                if (src->type == HERODOTUS_TYPE_CODEPOINT) {
       @@ -129,40 +129,37 @@ herodotus_read_codepoint(HERODOTUS_READER *r, bool advance, uint_least32_t *cp)
        
                if (r->type == HERODOTUS_TYPE_CODEPOINT) {
                        *cp = ((const uint_least32_t *)(r->src))[r->off];
       -
       -                if (advance) {
       -                        r->off++;
       -                }
       +                ret = 1;
                } else { /* r->type == HERODOTUS_TYPE_UTF8 */
                        ret = grapheme_decode_utf8((const char *)r->src + r->off,
                                                   MIN(r->srclen, r->soft_limit[0]) -
                                                   r->off, cp);
       +        }
        
       -                if (unlikely(r->srclen == SIZE_MAX && *cp == 0)) {
       -                        /*
       -                         * We encountered a NUL-byte. Don't increment
       -                         * offset and return as if the buffer had ended
       -                         * here all along
       -                         */
       -                        r->terminated_by_null = true;
       -                        return HERODOTUS_STATUS_END_OF_BUFFER;
       -                }
       -
       -                if (r->off + ret > MIN(r->srclen, r->soft_limit[0])) {
       -                        /*
       -                         * we want more than we have; instead of
       -                         * returning garbage we terminate here.
       -                         */
       -                        return HERODOTUS_STATUS_END_OF_BUFFER;
       -                }
       +        if (unlikely(r->srclen == SIZE_MAX && *cp == 0)) {
       +                /*
       +                 * We encountered a null-codepoint. Don't increment
       +                 * offset and return as if the buffer had ended here all
       +                 * along
       +                 */
       +                r->terminated_by_null = true;
       +                return HERODOTUS_STATUS_END_OF_BUFFER;
       +        }
        
       +        if (r->off + ret > MIN(r->srclen, r->soft_limit[0])) {
                        /*
       -                 * Increase offset which we now know won't surpass
       -                 * the limits, unless we got told otherwise
       +                 * we want more than we have; instead of returning
       +                 * garbage we terminate here.
                         */
       -                if (advance) {
       -                        r->off += ret;
       -                }
       +                return HERODOTUS_STATUS_END_OF_BUFFER;
       +        }
       +
       +        /*
       +         * Increase offset which we now know won't surpass the limits,
       +         * unless we got told otherwise
       +         */
       +        if (advance) {
       +                r->off += ret;
                }
        
                return HERODOTUS_STATUS_SUCCESS;