X-Git-Url: https://git.distorted.org.uk/~mdw/anag/blobdiff_plain/650bb9da7cf5b677960c03e0a6a5616d48340845..904ac13a4c7d86c18d031ad3f113659440ab86b8:/trackword.c?ds=sidebyside diff --git a/trackword.c b/trackword.c index b2e3074..ce51e2e 100644 --- a/trackword.c +++ b/trackword.c @@ -63,22 +63,16 @@ long isqrt(long a) { long i, q; - /* --- Initial guess is half the input length --- */ - - for (i = q = a; q; q >>= 2) - i >>= 1; - - /* --- Do the main series iteration --- */ + /* Initial guess is half the input length. */ + for (i = q = a; q; q >>= 2) i >>= 1; + /* Do the main series iteration. */ for (;;) { - q = i * i - a; - if (!q || (q < 0 && -q <= 2 * i)) - return (i); - q /= 2 * i; - if (!q) - i--; - else - i -= q; + q = i*i - a; + if (q > -2*i && q <= 0) return (i); + q /= 2*i; + if (!q) i--; + else i -= q; } } @@ -98,21 +92,16 @@ static int track(tcell *c, const char *p) { tcell **cc; - if (*p++ != c->ch) - return (0); - if (!*p) - return (1); - c->f = 1; + if (*p++ != c->ch) return (0); + if (!*p) return (1); + c->f = 1; for (cc = c->hop; *cc; cc++) { - if ((*cc)->f) - continue; - if (track(*cc, p)) { - c->f = 0; - return (1); - } + if ((*cc)->f) continue; + if (track(*cc, p)) { c->f = 0; return (1); } } c->f = 0; + return (0); } @@ -123,12 +112,9 @@ static int n_track(node *nn, const char *p, size_t sz) node_track *n = (node_track *)nn; tcell *c; - if (!*p) - return (1); - for (c = n->c; c->ch; c++) { - if (track(c, p)) - return (1); - } + if (!*p) return (1); + for (c = n->c; c->ch; c++) + if (track(c, p)) return (1); return (0); } @@ -143,48 +129,41 @@ node *trackword(const char *const *av) size_t sz = strlen(p); tcell *c, **cc, **ccc; - /* --- Work out the dimensions --- */ - + /* Work out the dimensions. */ x = strcspn(p, "/"); if (!p[x]) { x = isqrt(sz); - if (x * x != sz) + if (x*x != sz) die("bad trackword `%s': not square, and no line markers", p); y = x; } - if (!x) - die("bad trackword `%s': no columns", p); + if (!x) die("bad trackword `%s': no columns", p); y = 0; l = p + sz; q = p; for (;;) { - if (*q == '/') - q++; - if (!*q) - break; + if (*q == '/') q++; + if (!*q) break; if (l - q < x) die("bad trackword `%s': inconsistent line lengths", p); q += x; y++; } - if (!y) - die("bad trackword `%s': no rows", p); - - /* --- Build the match node --- */ + if (!y) die("bad trackword `%s': no rows", p); + /* Build the match node. */ n = xmalloc(sizeof(*n)); n->n.func = n_track; n->x = x; n->y = y; - n->c = xmalloc((x * y + 1) * sizeof(tcell)); + n->c = xmalloc((x*y + 1)*sizeof(tcell)); q = p; c = n->c; for (j = 0; j < y; j++) { - if (*q == '/') - q++; + if (*q == '/') q++; for (i = 0; i < x; i++) { c->ch = *q++; c->f = 0; @@ -216,20 +195,15 @@ node *trackword(const char *const *av) } c->ch = 0; - /* --- Now prune out bogus links --- */ - + /* Now prune out bogus links. */ for (c = n->c; c->ch; c++) { ccc = c->hop; - for (cc = c->hop; *cc; cc++) { - if (isalpha((unsigned char)(*cc)->ch)) { - *ccc++ = *cc; - } - } + for (cc = c->hop; *cc; cc++) + if (isalpha((unsigned char)(*cc)->ch)) *ccc++ = *cc; *ccc++ = 0; } - /* --- Done --- */ - + /* Done. */ return (&n->n); }