[hackers] [libgrapheme] Change lg_grapheme_isbreak() return type from int to bool || Laslo Hunhold

From: <git_AT_suckless.org>
Date: Sun, 12 Dec 2021 16:15:22 +0100 (CET)

commit f128a915ba3140dc0b755d2f77d599cdbc2df7be
Author: Laslo Hunhold <dev_AT_frign.de>
AuthorDate: Sun Dec 12 16:03:29 2021 +0100
Commit: Laslo Hunhold <dev_AT_frign.de>
CommitDate: Sun Dec 12 16:03:29 2021 +0100

    Change lg_grapheme_isbreak() return type from int to bool
    
    Just as a disclaimer, I still really like int as a return type for
    functions to indicate errors (return 0 means success and anything >0
    means failure). For a library though one would most certainly use
    an enum to name those errors, too, which would then turn the function
    signature to "enum foo_errors foo(...)".
    
    lg_grapheme_isbreak() cannot fail, however, and as in the previous
    commit on reducing implicit assumptions on char, reducing assumptions
    and making clearer what a return value can entail is good. This is
    why the return type is changed to bool.
    
    The bool type is C99 and even the more "independent" compilers like scc
    support it. If it is internally expanded to an int, so be it, but it
    helps readability.
    
    Signed-off-by: Laslo Hunhold <dev_AT_frign.de>

diff --git a/grapheme.h b/grapheme.h
index 7f6726b..bd5244b 100644
--- a/grapheme.h
+++ b/grapheme.h
_AT_@ -2,6 +2,7 @@
 #ifndef GRAPHEME_H
 #define GRAPHEME_H
 
+#include <stdbool.h>
 #include <stddef.h>
 #include <stdint.h>
 
_AT_@ -20,7 +21,7 @@ typedef struct lg_internal_segmentation_state {
 
 size_t lg_grapheme_nextbreak(const uint8_t *);
 
-int lg_grapheme_isbreak(uint_least32_t, uint_least32_t, LG_SEGMENTATION_STATE *);
+bool lg_grapheme_isbreak(uint_least32_t, uint_least32_t, LG_SEGMENTATION_STATE *);
 
 size_t lg_utf8_decode(const uint8_t *, size_t, uint_least32_t *);
 size_t lg_utf8_encode(uint_least32_t, uint8_t *, size_t);
diff --git a/src/grapheme.c b/src/grapheme.c
index 7eee5ef..1cc5ab1 100644
--- a/src/grapheme.c
+++ b/src/grapheme.c
_AT_@ -1,4 +1,5 @@
 /* See LICENSE file for copyright and license details. */
+#include <stdbool.h>
 #include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
_AT_@ -12,11 +13,12 @@ enum {
         GRAPHEME_FLAG_EMOJI = 1 << 1, /* within emoji modifier or zwj sequence */
 };
 
-int
+bool
 lg_grapheme_isbreak(uint_least32_t a, uint_least32_t b, LG_SEGMENTATION_STATE *state)
 {
         struct lg_internal_heisenstate *p[2] = { 0 };
- int ret = 1, flags = 0;
+ int flags = 0;
+ bool isbreak = true;
 
         /* set state depending on state pointer */
         if (state != NULL) {
_AT_@ -160,7 +162,7 @@ lg_grapheme_isbreak(uint_least32_t a, uint_least32_t b, LG_SEGMENTATION_STATE *s
         /* GB999 */
         goto hasbreak;
 nobreak:
- ret = 0;
+ isbreak = false;
 hasbreak:
         if (state != NULL) {
                 /* move b-state to a-state, discard b-state */
_AT_@ -168,12 +170,12 @@ hasbreak:
                 memset(&(state->b), 0, sizeof(state->b));
 
                 /* reset flags */
- if (ret == 1) {
+ if (isbreak) {
                         state->flags = 0;
                 }
         }
 
- return ret;
+ return isbreak;
 }
 
 size_t
Received on Sun Dec 12 2021 - 16:15:22 CET

This archive was generated by hypermail 2.3.0 : Sun Dec 12 2021 - 16:24:31 CET