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>
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;
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, '-');
}
}
-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;
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;
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);
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);
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);
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);
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) {
}
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;
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;
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);
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);
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)
/* 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) {
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);
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);
}
}
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");
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;
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;
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);
}
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) {
"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
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.
#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);
{
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);
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;
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;
;
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;
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);