⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 regex.c

📁 如果RH
💻 C
📖 第 1 页 / 共 4 页
字号:
/* Extended regular expression matching and search.   Copyright (C) 1985 Free Software Foundation, Inc.		       NO WARRANTY  BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELYNO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPTWHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY ANDFITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITYAND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVEDEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR ORCORRECTION. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTYWHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BELIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OROTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THEUSE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA ORDATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES ORA FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THISPROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCHDAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.		GENERAL PUBLIC LICENSE TO COPY  1. You may copy and distribute verbatim copies of this source fileas you receive it, in any medium, provided that you conspicuously andappropriately publish on each copy a valid copyright notice "Copyright(C) 1985 Free Software Foundation, Inc."; and include following thecopyright notice a verbatim copy of the above disclaimer of warrantyand of this License.  You may charge a distribution fee for thephysical act of transferring a copy.  2. You may modify your copy or copies of this source file orany portion of it, and copy and distribute such modifications underthe terms of Paragraph 1 above, provided that you also do the following:    a) cause the modified files to carry prominent notices stating    that you changed the files and the date of any change; and    b) cause the whole of any work that you distribute or publish,    that in whole or in part contains or is a derivative of this    program or any part thereof, to be licensed at no charge to all    third parties on terms identical to those contained in this    License Agreement (except that you may choose to grant more extensive    warranty protection to some or all third parties, at your option).    c) You may charge a distribution fee for the physical act of    transferring a copy, and you may at your option offer warranty    protection in exchange for a fee.Mere aggregation of another unrelated program with this program (or itsderivative) on a volume of a storage or distribution medium does not bringthe other program under the scope of these terms.  3. You may copy and distribute this program (or a portion or derivativeof it, under Paragraph 2) in object code or executable form under the termsof Paragraphs 1 and 2 above provided that you also do one of the following:    a) accompany it with the complete corresponding machine-readable    source code, which must be distributed under the terms of    Paragraphs 1 and 2 above; or,    b) accompany it with a written offer, valid for at least three    years, to give any third party free (except for a nominal    shipping charge) a complete machine-readable copy of the    corresponding source code, to be distributed under the terms of    Paragraphs 1 and 2 above; or,    c) accompany it with the information you received as to where the    corresponding source code may be obtained.  (This alternative is    allowed only for noncommercial distribution and only if you    received the program in object code or executable form alone.)For an executable file, complete source code means all the source code forall modules it contains; but, as a special exception, it need not includesource code for modules which are standard libraries that accompany theoperating system on which the executable file runs.  4. You may not copy, sublicense, distribute or transfer this programexcept as expressly provided under this License Agreement.  Any attemptotherwise to copy, sublicense, distribute or transfer this program is void andyour rights to use the program under this License agreement shall beautomatically terminated.  However, parties who have received computersoftware programs from you with this License Agreement will not havetheir licenses terminated so long as such parties remain in full compliance.  5. If you wish to incorporate parts of this program into other freeprograms whose distribution conditions are different, write to the FreeSoftware Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yetworked out a simple rule that can be stated here, but we will often permitthis.  We will be guided by the two goals of preserving the free status ofall derivatives of our free software and of promoting the sharing and reuse ofsoftware.In other words, you are welcome to use, share and improve this program.You are forbidden to forbid anyone else to use, share and improvewhat you give them.   Help stamp out software-hoarding!  *//* To test, compile with -Dtest. This Dtestable feature turns this into a self-contained program which reads a pattern, describes how it compiles, then reads a string and searches for it.  *//*  * Modification: alloca.h included for sparc machines * By :		 Po Cheung, po@cerc.utexas.edu * Date :	 July 27, 1990 */#ifndef NeXT#include <malloc.h>#endif#include <string.h>#include <stdio.h>#include <stdlib.h>#include <assert.h>#ifdef sparc#include <alloca.h>#else#pragma alloca#endif#define FAILURE_STACK   20000           /* max failure stack size */#ifdef __GNUC__#ifdef alloca#undef alloca#endif#define alloca __builtin_alloca#endif#ifdef __STDC__static int bcmp_translate (unsigned char *, unsigned char *s2,                           int len, unsigned char *translate);	/* (MJH) */#endif /* __STDC__ *//* bcopy, bzero and bcmp are needed if we are on Solaris */#ifdef SYSV#ifdef SUNOS4void bcopy(s1, s2, len)	register char *s1, *s2;	int len;{	for (; len; len--)		*(s2++) = *(s1++);}int bcmp(s1, s2, len)	register char *s1, *s2;	int len;{	for (; len; len--)		if (*(s1++) != *(s2++))			return 1;	return 0;}void bzero(sp, len)	register char *sp;	int len;{	for (; len; len--)		*(sp++) = 0;}#endif /* SUNOS4 */#endif /* SYSV */#ifdef emacs/* The `emacs' switch turns on certain special matching commands that make sense only in emacs. */#include "config.h"#include "lisp.h"#include "buffer.h"#include "syntax.h"#else  /* not emacs *//* * Define the syntax stuff, so we can do the \<...\> things. */#ifndef Sword /* must be non-zero in some of the tests below... */#define Sword 1#endif#define SYNTAX(c) re_syntax_table[c]#ifdef SYNTAX_TABLEchar *re_syntax_table;#elsestatic char re_syntax_table[256];static voidinit_syntax_once (){   register int c;   static int done = 0;   if (done)     return;   bzero (re_syntax_table, sizeof re_syntax_table);   for (c = 'a'; c <= 'z'; c++)     re_syntax_table[c] = Sword;   for (c = 'A'; c <= 'Z'; c++)     re_syntax_table[c] = Sword;   for (c = '0'; c <= '9'; c++)     re_syntax_table[c] = Sword;   done = 1;}#endif /* SYNTAX_TABLE */#endif /* not emacs */#include "regex.h"/* Number of failure points to allocate space for initially, when matching.  If this number is exceeded, more space is allocated, so it is not a hard limit.  */#ifndef NFAILURES#define NFAILURES 80#endif /* NFAILURES *//* width of a byte in bits */#define BYTEWIDTH 8#ifndef SIGN_EXTEND_CHAR#define SIGN_EXTEND_CHAR(x) ((signed char)(x))#endif/* compile_pattern takes a regular-expression descriptor string in the   user's format and converts it into a buffer full of byte commands   for matching.  pattern   is the address of the pattern string  size      is the length of it.  bufp	    is a  struct re_pattern_buffer *  which points to the info	    on where to store the byte commands.	    This structure contains a  char *  which points to the	    actual space, which should have been obtained with malloc.	    compile_pattern may use  realloc  to grow the buffer space.  The number of bytes of commands can be found out by looking in  the  struct re_pattern_buffer  that bufp pointed to,  after compile_pattern returns.*/#define PATPUSH(ch) (*b++ = (char) (ch))#define PATFETCH(c) \ {if (p == pend) goto end_of_pattern; \  c = * (unsigned char *) p++; \  if (translate) c = translate[c]; }#define PATFETCH_RAW(c) \ {if (p == pend) goto end_of_pattern; \  c = * (unsigned char *) p++; }#define PATUNFETCH p--/* This version of EXTEND_BUFFER will now work Alpha Machines,   and non Alpha Machines.   It isn't quite clean but it is cleaner than the previous version   that really only worked at all with 32bit pointers.   Dean Michaels, TRLabs.   dean@trlabs.ca*/#define EXTEND_BUFFER \  { char *old_buffer = bufp->buffer; \    if (bufp->allocated == (1<<16)) goto too_big; \    bufp->allocated *= 2; \    if (bufp->allocated > (1<<16)) bufp->allocated = (1<<16); \    if (!(bufp->buffer = (char *) realloc (bufp->buffer, bufp->allocated))) \      goto memory_exhausted; \    c = bufp->buffer - old_buffer; \    b -= (-(int)c); \    if (fixup_jump) \      fixup_jump -= (-(int)c); \    if (laststart) \      laststart -= (-(int)c); \    begalt -= (-(int)c); \    if (pending_exact) \      pending_exact -= (-(int)c); \  }static int store_jump (), insert_jump ();char *re_compile_pattern (pattern, size, bufp)     char *pattern;     int size;     struct re_pattern_buffer *bufp;{  register char *b = bufp->buffer;  register char *p = pattern;  char *pend = pattern + size;  register unsigned c, c1;  char *p1;  unsigned char *translate = (unsigned char *) bufp->translate;  /* address of the count-byte of the most recently inserted "exactn" command.    This makes it possible to tell whether a new exact-match character    can be added to that command or requires a new "exactn" command. */       char *pending_exact = 0;  /* address of the place where a forward-jump should go    to the end of the containing expression.    Each alternative of an "or", except the last, ends with a forward-jump    of this sort. */  char *fixup_jump = 0;  /* address of start of the most recently finished expression.    This tells postfix * where to find the start of its operand. */  char *laststart = 0;  /* In processing a repeat, 1 means zero matches is allowed */  char zero_times_ok;  /* In processing a repeat, 1 means many matches is allowed */  char many_times_ok;  /* address of beginning of regexp, or inside of last \( */  char *begalt = b;  /* Stack of information saved by \( and restored by \).     Four stack elements are pushed by each \(:       First, the value of b.       Second, the value of fixup_jump.       Third, the value of regnum.       Fourth, the value of begalt.  */  int stackb[40];  int *stackp = stackb;  int *stacke = stackb + 40;  int *stackt;  /* Counts \('s as they are encountered.  Remembered for the matching \),     where it becomes the "register number" to put in the stop_memory command */  int regnum = 1;  bufp->fastmap_accurate = 0;#ifndef emacs#ifndef SYNTAX_TABLE  /*   * Initialize the syntax table.   */   init_syntax_once();#endif#endif  if (bufp->allocated == 0)    {      bufp->allocated = 28;      if (bufp->buffer)	/* EXTEND_BUFFER loses when bufp->allocated is 0 */	bufp->buffer = (char *) realloc (bufp->buffer, 28);      else	/* Caller did not allocate a buffer.  Do it for him */	bufp->buffer = (char *) malloc (28);      if (!bufp->buffer) goto memory_exhausted;      begalt = b = bufp->buffer;    }  while (p != pend)    {      if (b - bufp->buffer > bufp->allocated - 10)	/* Note that EXTEND_BUFFER clobbers c */	EXTEND_BUFFER;      PATFETCH (c);      switch (c)	{	case '$':	  /* $ means succeed if at end of line, but only in special contexts.	    If randonly in the middle of a pattern, it is a normal character. */	  if (p == pend || (*p == '\\' && (p[1] == ')' || p[1] == '|')))	    {	      PATPUSH (endline);	      break;	    }	  goto normal_char;	case '^':	  /* ^ means succeed if at beg of line, but only if no preceding pattern. */	  if (laststart) goto normal_char;	  PATPUSH (begline);	  break;	case '*':	case '+':	case '?':	  /* If there is no previous pattern, char not special. */	  if (!laststart)	    goto normal_char;	  /* If there is a sequence of repetition chars,	     collapse it down to equivalent to just one.  */	  zero_times_ok = 0;	  many_times_ok = 0;	  while (1)	    {	      zero_times_ok |= c != '+';	      many_times_ok |= c != '?';	      if (p == pend)		break;	      PATFETCH (c);	      if (!(c == '*' || c == '+' || c == '?'))		{		  PATUNFETCH;		  break;		}	    }	  /* Now we know whether 0 matches is allowed,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -