diff options
author | Jim Meyering <meyering@redhat.com> | 2010-04-08 18:39:24 +0200 |
---|---|---|
committer | Jim Meyering <meyering@redhat.com> | 2010-04-08 19:29:42 +0200 |
commit | 699bcc4bd8166abb6ee26a37d9e1189817741277 (patch) | |
tree | b57f4c9bc3bf24c6b565f78966dc20ff98a4c36a /src/kwset.c | |
parent | e4f69c178ee9e68ecdbd743de669ec8c1388e40f (diff) | |
download | grep-699bcc4bd8166abb6ee26a37d9e1189817741277.tar.gz |
convert all TABs to equivalent spaces in indentation
Using this file,
cat > leading-blank.exempt <<\EOF
(?:^|\/)ChangeLog[^/]*$
(?:^|\/)(?:GNU)?[Mm]akefile[^/]*$
\.(?:am|mk)$
EOF
run this command to convert all non-conforming leading white
space to be all spaces:
git ls-files \
| pcregrep -vf leading-blank.exempt \
| xargs pcregrep -l '^ *\t' \
| xargs perl -MText::Tabs -ni -le \
'$m=/^( *\t[ \t]*)(.*)/; print $m ? expand($1) . $2 : $_'
Diffstat (limited to 'src/kwset.c')
-rw-r--r-- | src/kwset.c | 568 |
1 files changed, 284 insertions, 284 deletions
diff --git a/src/kwset.c b/src/kwset.c index 995be79f..d85fa309 100644 --- a/src/kwset.c +++ b/src/kwset.c @@ -151,122 +151,122 @@ kwsincr (kwset_t kws, char const *text, size_t len) label = kwset->trans ? kwset->trans[U(*--text)] : *--text; /* Descend the tree of outgoing links for this trie node, - looking for the current character and keeping track - of the path followed. */ + looking for the current character and keeping track + of the path followed. */ link = trie->links; links[0] = (struct tree *) &trie->links; dirs[0] = L; depth = 1; while (link && label != link->label) - { - links[depth] = link; - if (label < link->label) - dirs[depth++] = L, link = link->llink; - else - dirs[depth++] = R, link = link->rlink; - } + { + links[depth] = link; + if (label < link->label) + dirs[depth++] = L, link = link->llink; + else + dirs[depth++] = R, link = link->rlink; + } /* The current character doesn't have an outgoing link at - this trie node, so build a new trie node and install - a link in the current trie node's tree. */ + this trie node, so build a new trie node and install + a link in the current trie node's tree. */ if (!link) - { - link = (struct tree *) obstack_alloc(&kwset->obstack, - sizeof (struct tree)); - if (!link) - return _("memory exhausted"); - link->llink = NULL; - link->rlink = NULL; - link->trie = (struct trie *) obstack_alloc(&kwset->obstack, - sizeof (struct trie)); - if (!link->trie) - { - obstack_free(&kwset->obstack, link); - return _("memory exhausted"); - } - link->trie->accepting = 0; - link->trie->links = NULL; - link->trie->parent = trie; - link->trie->next = NULL; - link->trie->fail = NULL; - link->trie->depth = trie->depth + 1; - link->trie->shift = 0; - link->label = label; - link->balance = 0; - - /* Install the new tree node in its parent. */ - if (dirs[--depth] == L) - links[depth]->llink = link; - else - links[depth]->rlink = link; - - /* Back up the tree fixing the balance flags. */ - while (depth && !links[depth]->balance) - { - if (dirs[depth] == L) - --links[depth]->balance; - else - ++links[depth]->balance; - --depth; - } - - /* Rebalance the tree by pointer rotations if necessary. */ - if (depth && ((dirs[depth] == L && --links[depth]->balance) - || (dirs[depth] == R && ++links[depth]->balance))) - { - switch (links[depth]->balance) - { - case (char) -2: - switch (dirs[depth + 1]) - { - case L: - r = links[depth], t = r->llink, rl = t->rlink; - t->rlink = r, r->llink = rl; - t->balance = r->balance = 0; - break; - case R: - r = links[depth], l = r->llink, t = l->rlink; - rl = t->rlink, lr = t->llink; - t->llink = l, l->rlink = lr, t->rlink = r, r->llink = rl; - l->balance = t->balance != 1 ? 0 : -1; - r->balance = t->balance != (char) -1 ? 0 : 1; - t->balance = 0; - break; - default: - abort (); - } - break; - case 2: - switch (dirs[depth + 1]) - { - case R: - l = links[depth], t = l->rlink, lr = t->llink; - t->llink = l, l->rlink = lr; - t->balance = l->balance = 0; - break; - case L: - l = links[depth], r = l->rlink, t = r->llink; - lr = t->llink, rl = t->rlink; - t->llink = l, l->rlink = lr, t->rlink = r, r->llink = rl; - l->balance = t->balance != 1 ? 0 : -1; - r->balance = t->balance != (char) -1 ? 0 : 1; - t->balance = 0; - break; - default: - abort (); - } - break; - default: - abort (); - } - - if (dirs[depth - 1] == L) - links[depth - 1]->llink = t; - else - links[depth - 1]->rlink = t; - } - } + { + link = (struct tree *) obstack_alloc(&kwset->obstack, + sizeof (struct tree)); + if (!link) + return _("memory exhausted"); + link->llink = NULL; + link->rlink = NULL; + link->trie = (struct trie *) obstack_alloc(&kwset->obstack, + sizeof (struct trie)); + if (!link->trie) + { + obstack_free(&kwset->obstack, link); + return _("memory exhausted"); + } + link->trie->accepting = 0; + link->trie->links = NULL; + link->trie->parent = trie; + link->trie->next = NULL; + link->trie->fail = NULL; + link->trie->depth = trie->depth + 1; + link->trie->shift = 0; + link->label = label; + link->balance = 0; + + /* Install the new tree node in its parent. */ + if (dirs[--depth] == L) + links[depth]->llink = link; + else + links[depth]->rlink = link; + + /* Back up the tree fixing the balance flags. */ + while (depth && !links[depth]->balance) + { + if (dirs[depth] == L) + --links[depth]->balance; + else + ++links[depth]->balance; + --depth; + } + + /* Rebalance the tree by pointer rotations if necessary. */ + if (depth && ((dirs[depth] == L && --links[depth]->balance) + || (dirs[depth] == R && ++links[depth]->balance))) + { + switch (links[depth]->balance) + { + case (char) -2: + switch (dirs[depth + 1]) + { + case L: + r = links[depth], t = r->llink, rl = t->rlink; + t->rlink = r, r->llink = rl; + t->balance = r->balance = 0; + break; + case R: + r = links[depth], l = r->llink, t = l->rlink; + rl = t->rlink, lr = t->llink; + t->llink = l, l->rlink = lr, t->rlink = r, r->llink = rl; + l->balance = t->balance != 1 ? 0 : -1; + r->balance = t->balance != (char) -1 ? 0 : 1; + t->balance = 0; + break; + default: + abort (); + } + break; + case 2: + switch (dirs[depth + 1]) + { + case R: + l = links[depth], t = l->rlink, lr = t->llink; + t->llink = l, l->rlink = lr; + t->balance = l->balance = 0; + break; + case L: + l = links[depth], r = l->rlink, t = r->llink; + lr = t->llink, rl = t->rlink; + t->llink = l, l->rlink = lr, t->rlink = r, r->llink = rl; + l->balance = t->balance != 1 ? 0 : -1; + r->balance = t->balance != (char) -1 ? 0 : 1; + t->balance = 0; + break; + default: + abort (); + } + break; + default: + abort (); + } + + if (dirs[depth - 1] == L) + links[depth - 1]->llink = t; + else + links[depth - 1]->rlink = t; + } + } trie = link->trie; } @@ -303,7 +303,7 @@ enqueue (struct tree *tree, struct trie **last) well as a last resort failure node. */ static void treefails (struct tree const *tree, struct trie const *fail, - struct trie *recourse) + struct trie *recourse) { struct tree *link; @@ -319,15 +319,15 @@ treefails (struct tree const *tree, struct trie const *fail, { link = fail->links; while (link && tree->label != link->label) - if (tree->label < link->label) - link = link->llink; - else - link = link->rlink; + if (tree->label < link->label) + link = link->llink; + else + link = link->rlink; if (link) - { - tree->trie->fail = link->trie; - return; - } + { + tree->trie->fail = link->trie; + return; + } fail = fail->fail; } @@ -338,8 +338,8 @@ treefails (struct tree const *tree, struct trie const *fail, the preexisting delta value is larger than the current depth. */ static void treedelta (struct tree const *tree, - unsigned int depth, - unsigned char delta[]) + unsigned int depth, + unsigned char delta[]) { if (!tree) return; @@ -406,21 +406,21 @@ kwsprep (kwset_t kws) /* Looking for just one string. Extract it from the trie. */ kwset->target = obstack_alloc(&kwset->obstack, kwset->mind); if (!kwset->target) - return _("memory exhausted"); + return _("memory exhausted"); for (i = kwset->mind - 1, curr = kwset->trie; i >= 0; --i) - { - kwset->target[i] = curr->links->label; - curr = curr->links->trie; - } + { + kwset->target[i] = curr->links->label; + curr = curr->links->trie; + } /* Build the Boyer Moore delta. Boy that's easy compared to CW. */ for (i = 0; i < kwset->mind; ++i) - delta[U(kwset->target[i])] = kwset->mind - (i + 1); + delta[U(kwset->target[i])] = kwset->mind - (i + 1); /* Find the minimal delta2 shift that we might make after - a backwards match has failed. */ + a backwards match has failed. */ c = kwset->target[kwset->mind - 1]; for (i = kwset->mind - 2; i >= 0; --i) - if (kwset->target[i] == c) - break; + if (kwset->target[i] == c) + break; kwset->mind2 = kwset->mind - (i + 1); } else @@ -429,61 +429,61 @@ kwsprep (kwset_t kws) struct trie *last, *next[NCHAR]; /* Traverse the nodes of the trie in level order, simultaneously - computing the delta table, failure function, and shift function. */ + computing the delta table, failure function, and shift function. */ for (curr = last = kwset->trie; curr; curr = curr->next) - { - /* Enqueue the immediate descendents in the level order queue. */ - enqueue(curr->links, &last); - - curr->shift = kwset->mind; - curr->maxshift = kwset->mind; - - /* Update the delta table for the descendents of this node. */ - treedelta(curr->links, curr->depth, delta); - - /* Compute the failure function for the decendents of this node. */ - treefails(curr->links, curr->fail, kwset->trie); - - /* Update the shifts at each node in the current node's chain - of fails back to the root. */ - for (fail = curr->fail; fail; fail = fail->fail) - { - /* If the current node has some outgoing edge that the fail - doesn't, then the shift at the fail should be no larger - than the difference of their depths. */ - if (!hasevery(fail->links, curr->links)) - if (curr->depth - fail->depth < fail->shift) - fail->shift = curr->depth - fail->depth; - - /* If the current node is accepting then the shift at the - fail and its descendents should be no larger than the - difference of their depths. */ - if (curr->accepting && fail->maxshift > curr->depth - fail->depth) - fail->maxshift = curr->depth - fail->depth; - } - } + { + /* Enqueue the immediate descendents in the level order queue. */ + enqueue(curr->links, &last); + + curr->shift = kwset->mind; + curr->maxshift = kwset->mind; + + /* Update the delta table for the descendents of this node. */ + treedelta(curr->links, curr->depth, delta); + + /* Compute the failure function for the decendents of this node. */ + treefails(curr->links, curr->fail, kwset->trie); + + /* Update the shifts at each node in the current node's chain + of fails back to the root. */ + for (fail = curr->fail; fail; fail = fail->fail) + { + /* If the current node has some outgoing edge that the fail + doesn't, then the shift at the fail should be no larger + than the difference of their depths. */ + if (!hasevery(fail->links, curr->links)) + if (curr->depth - fail->depth < fail->shift) + fail->shift = curr->depth - fail->depth; + + /* If the current node is accepting then the shift at the + fail and its descendents should be no larger than the + difference of their depths. */ + if (curr->accepting && fail->maxshift > curr->depth - fail->depth) + fail->maxshift = curr->depth - fail->depth; + } + } /* Traverse the trie in level order again, fixing up all nodes whose - shift exceeds their inherited maxshift. */ + shift exceeds their inherited maxshift. */ for (curr = kwset->trie->next; curr; curr = curr->next) - { - if (curr->maxshift > curr->parent->maxshift) - curr->maxshift = curr->parent->maxshift; - if (curr->shift > curr->maxshift) - curr->shift = curr->maxshift; - } + { + if (curr->maxshift > curr->parent->maxshift) + curr->maxshift = curr->parent->maxshift; + if (curr->shift > curr->maxshift) + curr->shift = curr->maxshift; + } /* Create a vector, indexed by character code, of the outgoing links - from the root node. */ + from the root node. */ for (i = 0; i < NCHAR; ++i) - next[i] = NULL; + next[i] = NULL; treenext(kwset->trie->links, next); if ((trans = kwset->trans) != NULL) - for (i = 0; i < NCHAR; ++i) - kwset->next[i] = next[U(trans[i])]; + for (i = 0; i < NCHAR; ++i) + kwset->next[i] = next[U(trans[i])]; else - memcpy(kwset->next, next, NCHAR * sizeof(struct trie *)); + memcpy(kwset->next, next, NCHAR * sizeof(struct trie *)); } /* Fix things up for any translation table. */ @@ -529,35 +529,35 @@ bmexec (kwset_t kws, char const *text, size_t size) /* 11 is not a bug, the initial offset happens only once. */ for (ep = text + size - 11 * len;;) { - while (tp <= ep) - { - d = d1[U(tp[-1])], tp += d; - d = d1[U(tp[-1])], tp += d; - if (d == 0) - goto found; - d = d1[U(tp[-1])], tp += d; - d = d1[U(tp[-1])], tp += d; - d = d1[U(tp[-1])], tp += d; - if (d == 0) - goto found; - d = d1[U(tp[-1])], tp += d; - d = d1[U(tp[-1])], tp += d; - d = d1[U(tp[-1])], tp += d; - if (d == 0) - goto found; - d = d1[U(tp[-1])], tp += d; - d = d1[U(tp[-1])], tp += d; - } - break; + while (tp <= ep) + { + d = d1[U(tp[-1])], tp += d; + d = d1[U(tp[-1])], tp += d; + if (d == 0) + goto found; + d = d1[U(tp[-1])], tp += d; + d = d1[U(tp[-1])], tp += d; + d = d1[U(tp[-1])], tp += d; + if (d == 0) + goto found; + d = d1[U(tp[-1])], tp += d; + d = d1[U(tp[-1])], tp += d; + d = d1[U(tp[-1])], tp += d; + if (d == 0) + goto found; + d = d1[U(tp[-1])], tp += d; + d = d1[U(tp[-1])], tp += d; + } + break; found: - if (U(tp[-2]) == gc) - { - for (i = 3; i <= len && U(tp[-i]) == U(sp[-i]); ++i) - ; - if (i > len) - return tp - len - text; - } - tp += md2; + if (U(tp[-2]) == gc) + { + for (i = 3; i <= len && U(tp[-i]) == U(sp[-i]); ++i) + ; + if (i > len) + return tp - len - text; + } + tp += md2; } /* Now we have only a few characters left to search. We @@ -568,14 +568,14 @@ bmexec (kwset_t kws, char const *text, size_t size) { d = d1[U((tp += d)[-1])]; if (d != 0) - continue; + continue; if (U(tp[-2]) == gc) - { - for (i = 3; i <= len && U(tp[-i]) == U(sp[-i]); ++i) - ; - if (i > len) - return tp - len - text; - } + { + for (i = 3; i <= len && U(tp[-i]) == U(sp[-i]); ++i) + ; + if (i > len) + return tp - len - text; + } d = md2; } @@ -627,52 +627,52 @@ cwexec (kwset_t kws, char const *text, size_t len, struct kwsmatch *kwsmatch) while (lim - end >= d) { if (qlim && end <= qlim) - { - end += d - 1; - while ((d = delta[c = *end]) && end < qlim) - { - end += d; - end += delta[U(*end)]; - end += delta[U(*end)]; - } - ++end; - } + { + end += d - 1; + while ((d = delta[c = *end]) && end < qlim) + { + end += d; + end += delta[U(*end)]; + end += delta[U(*end)]; + } + ++end; + } else - d = delta[c = (end += d)[-1]]; + d = delta[c = (end += d)[-1]]; if (d) - continue; + continue; beg = end - 1; trie = next[c]; if (trie->accepting) - { - mch = beg; - accept = trie; - } + { + mch = beg; + accept = trie; + } d = trie->shift; while (beg > text) - { - c = trans ? trans[U(*--beg)] : *--beg; - tree = trie->links; - while (tree && c != tree->label) - if (c < tree->label) - tree = tree->llink; - else - tree = tree->rlink; - if (tree) - { - trie = tree->trie; - if (trie->accepting) - { - mch = beg; - accept = trie; - } - } - else - break; - d = trie->shift; - } + { + c = trans ? trans[U(*--beg)] : *--beg; + tree = trie->links; + while (tree && c != tree->label) + if (c < tree->label) + tree = tree->llink; + else + tree = tree->rlink; + if (tree) + { + trie = tree->trie; + if (trie->accepting) + { + mch = beg; + accept = trie; + } + } + else + break; + d = trie->shift; + } if (mch) - goto match; + goto match; } return -1; @@ -687,48 +687,48 @@ cwexec (kwset_t kws, char const *text, size_t len, struct kwsmatch *kwsmatch) while (lim - end >= d) { if ((d = delta[c = (end += d)[-1]]) != 0) - continue; + continue; beg = end - 1; if (!(trie = next[c])) - { - d = 1; - continue; - } + { + d = 1; + continue; + } if (trie->accepting && beg <= mch) - { - lmch = beg; - accept = trie; - } + { + lmch = beg; + accept = trie; + } d = trie->shift; while (beg > text) - { - c = trans ? trans[U(*--beg)] : *--beg; - tree = trie->links; - while (tree && c != tree->label) - if (c < tree->label) - tree = tree->llink; - else - tree = tree->rlink; - if (tree) - { - trie = tree->trie; - if (trie->accepting && beg <= mch) - { - lmch = beg; - accept = trie; - } - } - else - break; - d = trie->shift; - } + { + c = trans ? trans[U(*--beg)] : *--beg; + tree = trie->links; + while (tree && c != tree->label) + if (c < tree->label) + tree = tree->llink; + else + tree = tree->rlink; + if (tree) + { + trie = tree->trie; + if (trie->accepting && beg <= mch) + { + lmch = beg; + accept = trie; + } + } + else + break; + d = trie->shift; + } if (lmch) - { - mch = lmch; - goto match; - } + { + mch = lmch; + goto match; + } if (!d) - d = 1; + d = 1; } kwsmatch->index = accept->accepting / 2; @@ -751,11 +751,11 @@ kwsexec (kwset_t kws, char const *text, size_t size, struct kwsmatch *kwsmatch) { size_t ret = bmexec (kws, text, size); if (ret != (size_t) -1) - { - kwsmatch->index = 0; - kwsmatch->offset[0] = ret; - kwsmatch->size[0] = kwset->mind; - } + { + kwsmatch->index = 0; + kwsmatch->offset[0] = ret; + kwsmatch->size[0] = kwset->mind; + } return ret; } else |