]> git.neil.brown.name Git - edlib.git/commitdiff
Introduce strstarts()
authorNeilBrown <neil@brown.name>
Sun, 18 Jun 2023 00:44:39 +0000 (10:44 +1000)
committerNeilBrown <neil@brown.name>
Wed, 28 Jun 2023 07:51:41 +0000 (17:51 +1000)
It is a common pattern to check if a string starts with a fixed string.
So match this a #defined thing.

Signed-off-by: NeilBrown <neil@brown.name>
14 files changed:
core-doc.c
core-editor.c
display-ncurses.c
display-x11-xcb.c
doc-email.c
doc-multipart.c
lang-python.c
lib-config.c
lib-markup.c
lib-renderline.c
misc.h
mode-emacs.c
render-lines.c
rexel.c

index e9656c1e63e9ad13a4247af227b1ef66bf32c94a..2ee2eb9113f0c2d61c20a115cccd036d6f150c22 100644 (file)
@@ -1396,10 +1396,10 @@ static void simplify_path(const char *path safe, char *buf safe)
                if (len == 1)
                        /* Extra '/' at end or in the middle, ignore */
                        continue;
-               if (len == 2 && strncmp(p, "/.", 2) == 0 )
+               if (len == 2 && strstarts(p, "/.") )
                        /* Ignore the dot */
                        continue;
-               if (len == 3 && strncmp(p, "/..", 3) == 0) {
+               if (len == 3 && strstarts(p, "/..")) {
                        /* strip last component of buf */
                        while (b > buf && b[-1] != '/')
                                b -= 1;
index a4fc8b820a426624eedd488294183e84fd364641..4013e74d83040f8bab9301bb8ce0c1a106f1cff6 100644 (file)
@@ -200,10 +200,10 @@ DEF_CMD(editor_auto_load)
        if (key_lookup_cmd(map, ci->key))
                return Efallthrough;
 
-       if (strncmp(mod, "doc-", 4) == 0 ||
-           strncmp(mod, "render-", 7) == 0 ||
-           strncmp(mod, "mode-", 5) == 0 ||
-           strncmp(mod, "display-", 8) == 0)
+       if (strstarts(mod, "doc-") ||
+           strstarts(mod, "render-") ||
+           strstarts(mod, "mode-") ||
+           strstarts(mod, "display-"))
                ;
        else {
                const char *m = strrchr(ci->key, '-');
index 62d3009559514ee9cf7e9c0449a3eb08966d91c3..fd4d48d587d7b5b81c5ce176ad978eb5711b168d 100644 (file)
@@ -280,11 +280,6 @@ static void record_screen(struct pane *p safe)
        }
 }
 
-static inline int match(char *line safe, char *w safe)
-{
-       return strncmp(line, w, strlen(w)) == 0;
-}
-
 static char *copy_quote(char *line safe, char *buf safe)
 {
        char q;
@@ -342,11 +337,11 @@ static bool parse_event(struct pane *p safe)
        if (!dd->input ||
            fgets(line, sizeof(line)-1, dd->input) == NULL)
                line[0]=0;
-       else if (match(line, "Key ")) {
+       else if (strstarts(line, "Key ")) {
                if (!copy_quote(line+4, dd->event_info))
                        return False;
                dd->next_event = DoKey;
-       } else if (match(line, "Mouse ")) {
+       } else if (strstarts(line, "Mouse ")) {
                char *f = copy_quote(line+6, dd->event_info);
                if (!f)
                        return False;
@@ -354,13 +349,13 @@ static bool parse_event(struct pane *p safe)
                if (!f)
                        return False;
                dd->next_event = DoMouse;
-       } else if (match(line, "Display ")) {
+       } else if (strstarts(line, "Display ")) {
                char *f = get_coord(line+8, &dd->event_pos);
                if (!f)
                        return False;
                f = get_hash(f, dd->next_screen);
                dd->next_event = DoCheck;
-       } else if (match(line, "Close")) {
+       } else if (strstarts(line, "Close")) {
                dd->next_event = DoClose;
        }
        LOG("parse %s", line);
@@ -846,13 +841,13 @@ static int cvt_attrs(struct pane *p safe, struct pane *home safe,
                else if (strcmp(tmp, "nobold")==0) attr &= ~A_BOLD;
                else if (strcmp(tmp, "underline")==0) attr |= A_UNDERLINE;
                else if (strcmp(tmp, "nounderline")==0) attr &= ~A_UNDERLINE;
-               else if (strncmp(tmp, "fg:", 3) == 0) {
+               else if (strstarts(tmp, "fg:")) {
                        struct call_return cr =
                                call_ret(all, "colour:map", home,
                                         0, NULL, tmp+3);
                        int rgb[3] = {cr.i, cr.i2, cr.x};
                        fg = find_col(dd, rgb);
-               } else if (strncmp(tmp, "bg:", 3) == 0) {
+               } else if (strstarts(tmp, "bg:")) {
                        struct call_return cr =
                                call_ret(all, "colour:map", home,
                                         0, NULL, tmp+3);
@@ -1043,14 +1038,14 @@ DEF_CMD(nc_draw_image)
 
        if (!ci->str)
                return Enoarg;
-       if (strncmp(ci->str, "file:", 5) == 0) {
+       if (strstarts(ci->str, "file:")) {
                wd = NewMagickWand();
                status = MagickReadImage(wd, ci->str + 5);
                if (status == MagickFalse) {
                        DestroyMagickWand(wd);
                        return Efail;
                }
-       } else if (strncmp(ci->str, "comm:", 5) == 0) {
+       } else if (strstarts(ci->str, "comm:")) {
                struct call_return cr;
                wd = NewMagickWand();
                cr = call_ret(bytes, ci->str+5, ci->focus, 0, NULL, ci->str2);
@@ -1177,14 +1172,14 @@ DEF_CMD(nc_image_size)
 
        if (!ci->str)
                return Enoarg;
-       if (strncmp(ci->str, "file:", 5) == 0) {
+       if (strstarts(ci->str, "file:")) {
                wd = NewMagickWand();
                status = MagickReadImage(wd, ci->str + 5);
                if (status == MagickFalse) {
                        DestroyMagickWand(wd);
                        return Efail;
                }
-       } else if (strncmp(ci->str, "comm:", 5) == 0) {
+       } else if (strstarts(ci->str, "comm:")) {
                struct call_return cr;
                wd = NewMagickWand();
                cr = call_ret(bytes, ci->str+5, ci->focus, 0, NULL, ci->str2);
@@ -1361,7 +1356,7 @@ static struct pane *ncurses_init(struct pane *ed safe,
        alloc(dd, pane);
        dd->scr = scr;
        dd->scr_file = f;
-       dd->is_xterm = (term && strncmp(term, "xterm", 5) == 0);
+       dd->is_xterm = (term && strstarts(term, "xterm"));
 
        p = pane_register(ed, 1, &ncurses_handle.c, dd);
        if (!p) {
index 850309748b6e1d5bff54637b18d3477d06792a7d..35a36a4d9970158f9696abf10925886980bd2e63 100644 (file)
@@ -346,7 +346,7 @@ static void parse_attrs(
        }
 
        while ((word = strsep(&ap, ",")) != NULL) {
-               if (fd && strncmp(word, "family:", 7) == 0)
+               if (fd && strstarts(word, "family:"))
                        pango_font_description_set_family(fd, word+7);
                if (strcmp(word, "large") == 0)
                        size = 14 * 1000;
@@ -374,9 +374,9 @@ static void parse_attrs(
                if (strcmp(word, "nobold") == 0)
                        weight = PANGO_WEIGHT_NORMAL;
 
-               if (strncmp(word, "fg:", 3) == 0)
+               if (strstarts(word, "fg:"))
                        fg = word + 3;
-               if (strncmp(word, "bg:", 3) == 0)
+               if (strstarts(word, "bg:"))
                        bg = word + 3;
                if (strcmp(word, "inverse") == 0)
                        inv = True;
@@ -846,14 +846,14 @@ DEF_CMD(xcb_draw_image)
        ps->bg.g = -1;
        if (!ps->ctx)
                return Efail;
-       if (strncmp(ci->str, "file:", 5) == 0) {
+       if (strstarts(ci->str, "file:")) {
                wd = NewMagickWand();
                status = MagickReadImage(wd, ci->str + 5);
                if (status == MagickFalse) {
                        DestroyMagickWand(wd);
                        return Efail;
                }
-       } else if (strncmp(ci->str, "comm:", 5) == 0) {
+       } else if (strstarts(ci->str, "comm:")) {
                struct call_return cr;
                wd = NewMagickWand();
                cr = call_ret(bytes, ci->str+5, ci->focus, 0, NULL, ci->str2);
@@ -941,14 +941,14 @@ DEF_CMD(xcb_image_size)
 
        if (!ci->str)
                return Enoarg;
-       if (strncmp(ci->str, "file:", 5) == 0) {
+       if (strstarts(ci->str, "file:")) {
                wd = NewMagickWand();
                status = MagickReadImage(wd, ci->str + 5);
                if (status == MagickFalse) {
                        DestroyMagickWand(wd);
                        return Efail;
                }
-       } else if (strncmp(ci->str, "comm:", 5) == 0) {
+       } else if (strstarts(ci->str, "comm:")) {
                struct call_return cr;
                wd = NewMagickWand();
                cr = call_ret(bytes, ci->str+5, ci->focus, 0, NULL, ci->str2);
index 94a3c7910dfe4409a992c23ecb171a82e1c73348..30b9abed369c02ef4db02c50f90aadc185290066 100644 (file)
@@ -887,8 +887,7 @@ DEF_CMD(open_email)
        struct pane *p;
        struct mark *point;
 
-       if (ci->str == NULL ||
-           strncmp(ci->str, "email:", 6) != 0)
+       if (ci->str == NULL || !strstarts(ci->str, "email:"))
                return Efallthrough;
        fd = open(ci->str+6, O_RDONLY);
        if (fd < 0)
index 87cc13a5e09009004fbe908140289b69eadc3ddc..bf8f2b4385c7a3a90c42754049c8f0ecc94a54a8 100644 (file)
@@ -555,17 +555,17 @@ DEF_CMD(mp_attr)
                /* at the wrong end of a part */
                d += 1;
 
-       if (strncmp(attr, "multipart-next:", 15) == 0) {
+       if (strstarts(attr, "multipart-next:")) {
                d += 1;
                attr += 15;
                if (d >= mpi->nparts)
                        return 1;
-       } else if (strncmp(attr, "multipart-prev:", 15) == 0) {
+       } else if (strstarts(attr, "multipart-prev:")) {
                d -= 1;
                attr += 15;
                if (d < 0)
                        return 1;
-       } else if (strncmp(attr, "multipart-this:", 15) == 0)
+       } else if (strstarts(attr, "multipart-this:"))
                attr += 15;
 
        if (strcmp(attr, "multipart:part-num") == 0) {
@@ -620,17 +620,17 @@ DEF_CMD(mp_set_attr)
        dn = m->ref.docnum;
        m1 = m->ref.m;
 
-       if (strncmp(attr, "multipart-", 10) == 0) {
+       if (strstarts(attr, "multipart-")) {
                /* Set an attribute on a part */
-               if (strncmp(attr, "multipart-prev:", 15) == 0 &&
+               if (strstarts(attr, "multipart-prev:") &&
                    dn > 0 && (p = &mpi->parts[dn-1]) && p->pane)
                        attr_set_str(&p->pane->attrs,
                                     attr+15, ci->str2);
-               else if (strncmp(attr, "multipart-next:", 15) == 0 &&
+               else if (strstarts(attr, "multipart-next:") &&
                         dn < mpi->nparts && (p = &mpi->parts[dn+1]) && p->pane)
                        attr_set_str(&p->pane->attrs,
                                     attr+15, ci->str2);
-               else if (strncmp(attr, "multipart-this:", 15) == 0 &&
+               else if (strstarts(attr, "multipart-this:") &&
                         (p = &mpi->parts[dn]) && p->pane)
                        attr_set_str(&p->pane->attrs,
                                     attr+15, ci->str2);
index 424d012a41b54dd4a9071d8de5fef614222310b6..2c2d9d99cfa44bb8497862958471623da0703f0f 100644 (file)
@@ -547,7 +547,7 @@ static void do_map_init(Pane *self safe)
                                PyObject *tofree = NULL;
                                char *docs = python_as_string(doc, &tofree);
                                if (docs &&
-                                   strncmp(docs, "handle-range", 12) == 0 &&
+                                   strstarts(docs, "handle-range") &&
                                    docs[12]) {
                                        char sep = docs[12];
                                        char *s1 = strchr(docs+13, sep);
@@ -565,7 +565,7 @@ static void do_map_init(Pane *self safe)
                                        }
                                }
                                if (docs &&
-                                   strncmp(docs, "handle-prefix:", 14) == 0) {
+                                   strstarts(docs, "handle-prefix:")) {
                                        char *a = strconcat(self->pane, docs+14);
                                        char *b = strconcat(self->pane,
                                                            a, "\xFF\xFF\xFF\xFF");
@@ -592,14 +592,14 @@ static void do_map_init(Pane *self safe)
                                PyObject *tofree = NULL;
                                char *docs = python_as_string(doc, &tofree);
                                if (docs &&
-                                   strncmp(docs, "handle:", 7) == 0) {
+                                   strstarts(docs, "handle:")) {
                                        struct python_command *comm =
                                                export_callable(m);
                                        key_add(self->map, docs+7, &comm->c);
                                        command_put(&comm->c);
                                }
                                if (docs &&
-                                   strncmp(docs, "handle-list", 11) == 0 &&
+                                   strstarts(docs, "handle-list") &&
                                    docs[11]) {
                                        char sep = docs[11];
                                        char *s1 = docs + 12;
index e247b1c1cab49c3ccbcf05a60fb45b037e66c844..5815011475953440f19944e807c3a31f6376a870 100644 (file)
@@ -193,7 +193,7 @@ static void handle(void *data, char *section safe, char *name safe, char *value
                return;
        }
 
-       if (strncmp(section, "file:", 5) == 0) {
+       if (strstarts(section, "file:")) {
                char *k = strconcat(NULL, "global-file-attr:", section+5);
                call(k, p, append, NULL, name, 0, NULL, value);
                return;
index 7f15b3ee317d3f35e03dbb3addc2fc49735b8b72..952ef5657585c426973af94f09f25fbb0a511488 100644 (file)
@@ -282,7 +282,7 @@ static void call_map_mark(struct pane *f safe, struct mark *m safe,
        const char *val;
 
        while ((key = attr_get_next_key(m->attrs, key, -1, &val)) != NULL &&
-              strncmp(key, "render:", 7) == 0)
+              strstarts(key, "render:"))
                call_comm("map-attr", f, &ar->rtn, 0, m, key, 0, NULL, val);
 }
 
index 17be787a3cd7c089d8f87bbcb0d3d947a58b8467..bc4c2e0e28320438924f03088dcf4f0d360aeac6 100644 (file)
@@ -501,7 +501,7 @@ static int render_image(struct pane *p safe, struct pane *focus safe,
        while (*line && *line != '>') {
                int len = strcspn(line, ",>");
 
-               if (strncmp(line, "image:", 6) == 0) {
+               if (strstarts(line, "image:")) {
                        fname = strndup(line+6, len-6);
                        if (!ssize ||
                            sscanf(ssize, "%hdx%hd", &size.x, &size.y) != 2) {
@@ -518,20 +518,20 @@ static int render_image(struct pane *p safe, struct pane *focus safe,
                                                     "cached-size", ssize);
                                }
                        }
-               } else if (strncmp(line, "width:", 6) == 0) {
+               } else if (strstarts(line, "width:")) {
                        width = atoi(line + 6);
                        width = width * scale / 1000;
-               } else if (strncmp(line, "height:", 7) == 0) {
+               } else if (strstarts(line, "height:")) {
                        height = atoi(line + 7);
                        height = height * scale / 1000;
-               } else if (strncmp(line, "noupscale", 9) == 0 &&
+               } else if (strstarts(line, "noupscale") &&
                           fname && size.x > 0) {
                        if (size.x < p->parent->w)
                                width = size.x;
                        if (size.y < p->parent->h)
                                height = size.y;
                } else if ((offset >= 0 || want_xypos) &&
-                          strncmp(line, "map:", 4) == 0) {
+                          strstarts(line, "map:")) {
                        /*
                         * A map is map:LxxxLxxxLxxxLxxx or similar
                         * Where each "Lxxx" recognised by a CAP followed
@@ -689,7 +689,7 @@ DEF_CMD(renderline)
        if (dodraw)
                home_call(focus, "Draw:clear", p);
 
-       if (strncmp(line, "<image:",7) == 0)
+       if (strstarts(line, "<image:"))
                /* For now an <image> must be on a line by itself.
                 * Maybe this can be changed later if I decide on
                 * something that makes sense.
diff --git a/misc.h b/misc.h
index ffe79278e841c4a4be3e0c6cdfd8280f5e0de29d..9d861f85c7ac0ed8722d63c95426921dde00f4f8 100644 (file)
--- a/misc.h
+++ b/misc.h
@@ -15,6 +15,8 @@ typedef _Bool bool;
 #define True ((bool)1)
 #define False ((bool)0)
 
+#define strstarts(s, prefix) (strncmp(s, prefix, sizeof(prefix)-1) == 0)
+
 #define WERR (0xfffffffeu)
 wint_t get_utf8(const char **cpp safe, const char *end);
 char *safe put_utf8(char *buf safe, wchar_t ch);
index 01dcd3f43f7c36eaed3cdb33fd9c70017a1c7291..db146a5073d013a8b1ebe30367571bb9ef13e70f 100644 (file)
@@ -831,7 +831,7 @@ DEF_CMD(find_complete)
 {
        char *type = ci->home->data;
 
-       if (strncmp(type, "file", 4) == 0)
+       if (strstarts(type, "file"))
                return emacs_file_complete_func(ci);
        if (strcmp(type, "shellcmd") == 0)
                return emacs_file_complete_func(ci);
@@ -858,7 +858,7 @@ DEF_CMD(find_done)
                call("Message:modal", ci->focus, 0, NULL, "Document not found");
                return 1;
        }
-       if (strncmp(type, "file", 4) == 0) {
+       if (strstarts(type, "file")) {
                char *sl;
                bool can_create = True;
                bool can_create_dir = True;
index 72f0bc81f5c58890fb545d139f811d79f15f6309..23eff3b3a6a71ed519e6ac6bb4f62952062c1eb1 100644 (file)
@@ -750,7 +750,7 @@ static int render(struct mark *pm, struct pane *p safe,
                rl->background_uniform = True;
        }
        s = pane_attr_get(focus, "background");
-       if (s && strncmp(s, "call:", 5) == 0) {
+       if (s && strstarts(s, "call:")) {
                home_call(focus, "Draw:clear", p, 0, NULL, "");
                home_call(focus, s+5, p, 0, m);
                refresh_all = True;
@@ -759,13 +759,13 @@ static int render(struct mark *pm, struct pane *p safe,
                ;
        else if (!s)
                home_call(focus, "Draw:clear", p, 0, NULL, "");
-       else if (strncmp(s, "color:", 6) == 0) {
+       else if (strstarts(s, "color:")) {
                char *a = strdup(s);
                strcpy(a, "bg:");
                strcpy(a+3, s+6);
                home_call(focus, "Draw:clear", p, 0, NULL, a);
                free(a);
-       } else if (strncmp(s, "image:", 6) == 0) {
+       } else if (strstarts(s, "image:")) {
                home_call(focus, "Draw:clear", p);
                home_call(focus, "Draw:image", p, 16, NULL, s+6);
                rl->background_uniform = False;
diff --git a/rexel.c b/rexel.c
index 730917ad9aa254c59e0d9f771d426f74dcffcb3c..6618bc2ed86263d2cb4b820625e198bb0d6ed866 100644 (file)
--- a/rexel.c
+++ b/rexel.c
@@ -2583,7 +2583,7 @@ static void run_tests(bool trace)
                                flags |= RXL_SOW;
                        else
                                flags |= RXL_NOWBRK;
-                       if (wc == 'P' && strncmp(target, "oint", 4) == 0)
+                       if (wc == 'P' && strstarts(target, "oint"))
                                flags |= RXL_POINT;
                        prev =  wc;
                        r = rxl_advance(st, wc | flags);