Adapt the kwset code to Git
Signed-off-by: Fredrik Kuivinen <frekui@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>maint
parent
05f3dbba67
commit
fca65d4b26
33
kwset.c
33
kwset.c
|
@ -1,3 +1,9 @@
|
||||||
|
/*
|
||||||
|
* This file has been copied from commit e7ac713d^ in the GNU grep git
|
||||||
|
* repository. A few small changes have been made to adapt the code to
|
||||||
|
* Git.
|
||||||
|
*/
|
||||||
|
|
||||||
/* kwset.c - search for any of a set of keywords.
|
/* kwset.c - search for any of a set of keywords.
|
||||||
Copyright 1989, 1998, 2000, 2005 Free Software Foundation, Inc.
|
Copyright 1989, 1998, 2000, 2005 Free Software Foundation, Inc.
|
||||||
|
|
||||||
|
@ -28,22 +34,13 @@
|
||||||
String Matching: An Aid to Bibliographic Search," CACM June 1975,
|
String Matching: An Aid to Bibliographic Search," CACM June 1975,
|
||||||
Vol. 18, No. 6, which describes the failure function used below. */
|
Vol. 18, No. 6, which describes the failure function used below. */
|
||||||
|
|
||||||
#ifdef HAVE_CONFIG_H
|
#include "cache.h"
|
||||||
# include <config.h>
|
|
||||||
#endif
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include "system.h"
|
|
||||||
#include "kwset.h"
|
#include "kwset.h"
|
||||||
#include "obstack.h"
|
#include "obstack.h"
|
||||||
|
|
||||||
#ifdef GREP
|
|
||||||
extern char *xmalloc();
|
|
||||||
# undef malloc
|
|
||||||
# define malloc xmalloc
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define NCHAR (UCHAR_MAX + 1)
|
#define NCHAR (UCHAR_MAX + 1)
|
||||||
#define obstack_chunk_alloc malloc
|
#define obstack_chunk_alloc xmalloc
|
||||||
#define obstack_chunk_free free
|
#define obstack_chunk_free free
|
||||||
|
|
||||||
#define U(c) ((unsigned char) (c))
|
#define U(c) ((unsigned char) (c))
|
||||||
|
@ -93,9 +90,7 @@ kwsalloc (char const *trans)
|
||||||
{
|
{
|
||||||
struct kwset *kwset;
|
struct kwset *kwset;
|
||||||
|
|
||||||
kwset = (struct kwset *) malloc(sizeof (struct kwset));
|
kwset = (struct kwset *) xmalloc(sizeof (struct kwset));
|
||||||
if (!kwset)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
obstack_init(&kwset->obstack);
|
obstack_init(&kwset->obstack);
|
||||||
kwset->words = 0;
|
kwset->words = 0;
|
||||||
|
@ -174,7 +169,7 @@ kwsincr (kwset_t kws, char const *text, size_t len)
|
||||||
link = (struct tree *) obstack_alloc(&kwset->obstack,
|
link = (struct tree *) obstack_alloc(&kwset->obstack,
|
||||||
sizeof (struct tree));
|
sizeof (struct tree));
|
||||||
if (!link)
|
if (!link)
|
||||||
return _("memory exhausted");
|
return "memory exhausted";
|
||||||
link->llink = NULL;
|
link->llink = NULL;
|
||||||
link->rlink = NULL;
|
link->rlink = NULL;
|
||||||
link->trie = (struct trie *) obstack_alloc(&kwset->obstack,
|
link->trie = (struct trie *) obstack_alloc(&kwset->obstack,
|
||||||
|
@ -182,7 +177,7 @@ kwsincr (kwset_t kws, char const *text, size_t len)
|
||||||
if (!link->trie)
|
if (!link->trie)
|
||||||
{
|
{
|
||||||
obstack_free(&kwset->obstack, link);
|
obstack_free(&kwset->obstack, link);
|
||||||
return _("memory exhausted");
|
return "memory exhausted";
|
||||||
}
|
}
|
||||||
link->trie->accepting = 0;
|
link->trie->accepting = 0;
|
||||||
link->trie->links = NULL;
|
link->trie->links = NULL;
|
||||||
|
@ -405,7 +400,7 @@ kwsprep (kwset_t kws)
|
||||||
/* Looking for just one string. Extract it from the trie. */
|
/* Looking for just one string. Extract it from the trie. */
|
||||||
kwset->target = obstack_alloc(&kwset->obstack, kwset->mind);
|
kwset->target = obstack_alloc(&kwset->obstack, kwset->mind);
|
||||||
if (!kwset->target)
|
if (!kwset->target)
|
||||||
return _("memory exhausted");
|
return "memory exhausted";
|
||||||
for (i = kwset->mind - 1, curr = kwset->trie; i >= 0; --i)
|
for (i = kwset->mind - 1, curr = kwset->trie; i >= 0; --i)
|
||||||
{
|
{
|
||||||
kwset->target[i] = curr->links->label;
|
kwset->target[i] = curr->links->label;
|
||||||
|
@ -597,9 +592,7 @@ cwexec (kwset_t kws, char const *text, size_t len, struct kwsmatch *kwsmatch)
|
||||||
register struct tree const *tree;
|
register struct tree const *tree;
|
||||||
register char const *trans;
|
register char const *trans;
|
||||||
|
|
||||||
#ifdef lint
|
|
||||||
accept = NULL;
|
accept = NULL;
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Initialize register copies and look for easy ways out. */
|
/* Initialize register copies and look for easy ways out. */
|
||||||
kwset = (struct kwset *) kws;
|
kwset = (struct kwset *) kws;
|
||||||
|
|
19
kwset.h
19
kwset.h
|
@ -1,3 +1,8 @@
|
||||||
|
/* This file has been copied from commit e7ac713d^ in the GNU grep git
|
||||||
|
* repository. A few small changes have been made to adapt the code to
|
||||||
|
* Git.
|
||||||
|
*/
|
||||||
|
|
||||||
/* kwset.h - header declaring the keyword set library.
|
/* kwset.h - header declaring the keyword set library.
|
||||||
Copyright (C) 1989, 1998, 2005 Free Software Foundation, Inc.
|
Copyright (C) 1989, 1998, 2005 Free Software Foundation, Inc.
|
||||||
|
|
||||||
|
@ -27,22 +32,23 @@ struct kwsmatch
|
||||||
size_t size[1]; /* Length of each submatch. */
|
size_t size[1]; /* Length of each submatch. */
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef ptr_t kwset_t;
|
struct kwset_t;
|
||||||
|
typedef struct kwset_t* kwset_t;
|
||||||
|
|
||||||
/* Return an opaque pointer to a newly allocated keyword set, or NULL
|
/* Return an opaque pointer to a newly allocated keyword set, or NULL
|
||||||
if enough memory cannot be obtained. The argument if non-NULL
|
if enough memory cannot be obtained. The argument if non-NULL
|
||||||
specifies a table of character translations to be applied to all
|
specifies a table of character translations to be applied to all
|
||||||
pattern and search text. */
|
pattern and search text. */
|
||||||
extern kwset_t kwsalloc PARAMS((char const *));
|
extern kwset_t kwsalloc(char const *);
|
||||||
|
|
||||||
/* Incrementally extend the keyword set to include the given string.
|
/* Incrementally extend the keyword set to include the given string.
|
||||||
Return NULL for success, or an error message. Remember an index
|
Return NULL for success, or an error message. Remember an index
|
||||||
number for each keyword included in the set. */
|
number for each keyword included in the set. */
|
||||||
extern const char *kwsincr PARAMS((kwset_t, char const *, size_t));
|
extern const char *kwsincr(kwset_t, char const *, size_t);
|
||||||
|
|
||||||
/* When the keyword set has been completely built, prepare it for
|
/* When the keyword set has been completely built, prepare it for
|
||||||
use. Return NULL for success, or an error message. */
|
use. Return NULL for success, or an error message. */
|
||||||
extern const char *kwsprep PARAMS((kwset_t));
|
extern const char *kwsprep(kwset_t);
|
||||||
|
|
||||||
/* Search through the given buffer for a member of the keyword set.
|
/* Search through the given buffer for a member of the keyword set.
|
||||||
Return a pointer to the leftmost longest match found, or NULL if
|
Return a pointer to the leftmost longest match found, or NULL if
|
||||||
|
@ -50,7 +56,8 @@ extern const char *kwsprep PARAMS((kwset_t));
|
||||||
the matching substring in the integer it points to. Similarly,
|
the matching substring in the integer it points to. Similarly,
|
||||||
if foundindex is non-NULL, store the index of the particular
|
if foundindex is non-NULL, store the index of the particular
|
||||||
keyword found therein. */
|
keyword found therein. */
|
||||||
extern size_t kwsexec PARAMS((kwset_t, char const *, size_t, struct kwsmatch *));
|
extern size_t kwsexec(kwset_t, char const *, size_t, struct kwsmatch *);
|
||||||
|
|
||||||
/* Deallocate the given keyword set and all its associated storage. */
|
/* Deallocate the given keyword set and all its associated storage. */
|
||||||
extern void kwsfree PARAMS((kwset_t));
|
extern void kwsfree(kwset_t);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue