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

📄 regex.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 3 页
字号:
	    case '9':	      c1 = c - '0';	      if (c1 >= regnum)		goto normal_char;	      for (stackt = stackp - 2;  stackt > stackb;  stackt -= 4) 		if (*stackt == c1)		  goto normal_char;	      laststart = b;	      PATPUSH (duplicate);	      PATPUSH (c1);	      break;	    case '+':	    case '?':	      if (obscure_syntax & RE_BK_PLUS_QM)		goto handle_plus;	    default:	    normal_backsl:	      /* You might think it would be useful for \ to mean		 not to translate; but if we don't translate it		 it will never match anything.  */	      if (translate) c = translate[c];	      goto normal_char;	    }	  break;	default:	normal_char:	  if (!pending_exact || pending_exact + *pending_exact + 1 != b	      || *pending_exact == 0177 || *p == '*' || *p == '^'	      || ((obscure_syntax & RE_BK_PLUS_QM)		  ? *p == '\\' && (p[1] == '+' || p[1] == '?')		  : (*p == '+' || *p == '?')))	    {	      laststart = b;	      PATPUSH (exactn);	      pending_exact = b;	      PATPUSH (0);	    }	  PATPUSH (c);	  (*pending_exact)++;	}    }  if (fixup_jump)    store_jump (fixup_jump, jump, b);  if (stackp != stackb) goto unmatched_open;  bufp->used = b - bufp->buffer;  return 0; invalid_pattern:  return "Invalid regular expression"; unmatched_open:  return "Unmatched \\("; unmatched_close:  return "Unmatched \\)"; end_of_pattern:  return "Premature end of regular expression"; nesting_too_deep:  return "Nesting too deep"; too_big:  return "Regular expression too big"; memory_exhausted:  return "Memory exhausted";}/* Store where `from' points a jump operation to jump to where `to' points.  `opcode' is the opcode to store. */static intstore_jump (from, opcode, to)     char *from, *to;     char opcode;{  from[0] = opcode;  from[1] = (to - (from + 3)) & 0377;  from[2] = (to - (from + 3)) >> 8;}/* Open up space at char FROM, and insert there a jump to TO.   CURRENT_END gives te end of the storage no in use,   so we know how much data to copy up.   OP is the opcode of the jump to insert.   If you call this function, you must zero out pending_exact.  */static intinsert_jump (op, from, to, current_end)     char op;     char *from, *to, *current_end;{  register char *pto = current_end + 3;  register char *pfrom = current_end;  while (pfrom != from)    *--pto = *--pfrom;  store_jump (from, op, to);}/* Given a pattern, compute a fastmap from it. The fastmap records which of the (1 << BYTEWIDTH) possible characters can start a string that matches the pattern. This fastmap is used by re_search to skip quickly over totally implausible text. The caller must supply the address of a (1 << BYTEWIDTH)-byte data area as bufp->fastmap. The other components of bufp describe the pattern to be used.  */voidre_compile_fastmap (bufp)     struct re_pattern_buffer *bufp;{  unsigned char *pattern = (unsigned char *) bufp->buffer;  int size = bufp->used;  register char *fastmap = bufp->fastmap;  register unsigned char *p = pattern;  register unsigned char *pend = pattern + size;  register int j, k;  unsigned char *translate = (unsigned char *) bufp->translate;  unsigned char *stackb[NFAILURES];  unsigned char **stackp = stackb;  bzero (fastmap, (1 << BYTEWIDTH));  bufp->fastmap_accurate = 1;  bufp->can_be_null = 0;        while (p)    {      if (p == pend)	{	  bufp->can_be_null = 1;	  break;	}#ifdef SWITCH_ENUM_BUG      switch ((int) ((enum regexpcode) *p++))#else      switch ((enum regexpcode) *p++)#endif	{	case exactn:	  if (translate)	    fastmap[translate[p[1]]] = 1;	  else	    fastmap[p[1]] = 1;	  break;        case begline:        case before_dot:	case at_dot:	case after_dot:	case begbuf:	case endbuf:	case wordbound:	case notwordbound:	case wordbeg:	case wordend:	  continue;	case endline:	  if (translate)	    fastmap[translate['\n']] = 1;	  else	    fastmap['\n'] = 1;	  if (bufp->can_be_null != 1)	    bufp->can_be_null = 2;	  break;	case finalize_jump:	case maybe_finalize_jump:	case jump:	case dummy_failure_jump:	  bufp->can_be_null = 1;	  j = *p++ & 0377;	  j += SIGN_EXTEND_CHAR (*(char *)p) << 8;	  p += j + 1;		/* The 1 compensates for missing ++ above */	  if (j > 0)	    continue;	  /* Jump backward reached implies we just went through	     the body of a loop and matched nothing.	     Opcode jumped to should be an on_failure_jump.	     Just treat it like an ordinary jump.	     For a * loop, it has pushed its failure point already;	     if so, discard that as redundant.  */	  if ((enum regexpcode) *p != on_failure_jump)	    continue;	  p++;	  j = *p++ & 0377;	  j += SIGN_EXTEND_CHAR (*(char *)p) << 8;	  p += j + 1;		/* The 1 compensates for missing ++ above */	  if (stackp != stackb && *stackp == p)	    stackp--;	  continue;	  	case on_failure_jump:	  j = *p++ & 0377;	  j += SIGN_EXTEND_CHAR (*(char *)p) << 8;	  p++;	  *++stackp = p + j;	  continue;	case start_memory:	case stop_memory:	  p++;	  continue;	case duplicate:	  bufp->can_be_null = 1;	  fastmap['\n'] = 1;	case anychar:	  for (j = 0; j < (1 << BYTEWIDTH); j++)	    if (j != '\n')	      fastmap[j] = 1;	  if (bufp->can_be_null)	    return;	  /* Don't return; check the alternative paths	     so we can set can_be_null if appropriate.  */	  break;	case wordchar:	  for (j = 0; j < (1 << BYTEWIDTH); j++)	    if (SYNTAX (j) == Sword)	      fastmap[j] = 1;	  break;	case notwordchar:	  for (j = 0; j < (1 << BYTEWIDTH); j++)	    if (SYNTAX (j) != Sword)	      fastmap[j] = 1;	  break;#ifdef emacs	case syntaxspec:	  k = *p++;	  for (j = 0; j < (1 << BYTEWIDTH); j++)	    if (SYNTAX (j) == (enum syntaxcode) k)	      fastmap[j] = 1;	  break;	case notsyntaxspec:	  k = *p++;	  for (j = 0; j < (1 << BYTEWIDTH); j++)	    if (SYNTAX (j) != (enum syntaxcode) k)	      fastmap[j] = 1;	  break;#endif emacs	case charset:	  for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)	    if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))	      {		if (translate)		  fastmap[translate[j]] = 1;		else		  fastmap[j] = 1;	      }	  break;	case charset_not:	  /* Chars beyond end of map must be allowed */	  for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)	    if (translate)	      fastmap[translate[j]] = 1;	    else	      fastmap[j] = 1;	  for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)	    if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))	      {		if (translate)		  fastmap[translate[j]] = 1;		else		  fastmap[j] = 1;	      }	  break;	}      /* Get here means we have successfully found the possible starting characters	 of one path of the pattern.  We need not follow this path any farther.	 Instead, look at the next alternative remembered in the stack. */      if (stackp != stackb)	p = *stackp--;      else	break;    }}/* Like re_search_2, below, but only one string is specified. */intre_search (pbufp, string, size, startpos, range, regs)     struct re_pattern_buffer *pbufp;     char *string;     int size, startpos, range;     struct re_registers *regs;{  return re_search_2 (pbufp, 0, 0, string, size, startpos, range, regs, size);}/* Like re_match_2 but tries first a match starting at index STARTPOS,   then at STARTPOS + 1, and so on.   RANGE is the number of places to try before giving up.   If RANGE is negative, the starting positions tried are    STARTPOS, STARTPOS - 1, etc.   It is up to the caller to make sure that range is not so large   as to take the starting position outside of the input strings.The value returned is the position at which the match was found, or -1 if no match was found, or -2 if error (such as failure stack overflow).  */intre_search_2 (pbufp, string1, size1, string2, size2, startpos, range, regs, mstop)     struct re_pattern_buffer *pbufp;     char *string1, *string2;     int size1, size2;     int startpos;     register int range;     struct re_registers *regs;     int mstop;{  register char *fastmap = pbufp->fastmap;  register unsigned char *translate = (unsigned char *) pbufp->translate;  int total = size1 + size2;  int val;  /* Update the fastmap now if not correct already */  if (fastmap && !pbufp->fastmap_accurate)    re_compile_fastmap (pbufp);    /* Don't waste time in a long search for a pattern     that says it is anchored.  */  if (pbufp->used > 0 && (enum regexpcode) pbufp->buffer[0] == begbuf      && range > 0)    {      if (startpos > 0)	return -1;      else	range = 1;    }  while (1)    {      /* If a fastmap is supplied, skip quickly over characters	 that cannot possibly be the start of a match.	 Note, however, that if the pattern can possibly match	 the null string, we must test it at each starting point	 so that we take the first null string we get.  */      if (fastmap && startpos < total && pbufp->can_be_null != 1)	{	  if (range > 0)	    {	      register int lim = 0;	      register unsigned char *p;	      int irange = range;	      if (startpos < size1 && startpos + range >= size1)		lim = range - (size1 - startpos);	      p = ((unsigned char *)		   &(startpos >= size1 ? string2 - size1 : string1)[startpos]);	      if (translate)		{		  while (range > lim && !fastmap[translate[*p++]])		    range--;		}	      else		{		  while (range > lim && !fastmap[*p++])		    range--;		}	      startpos += irange - range;	    }	  else	    {	      register unsigned char c;	      if (startpos >= size1)		c = string2[startpos - size1];	      else		c = string1[startpos];	      c &= 0xff;	      if (translate ? !fastmap[translate[c]] : !fastmap[c])		goto advance;	    }	}      if (range >= 0 && startpos == total	  && fastmap && pbufp->can_be_null == 0)	return -1;      val = re_match_2 (pbufp, string1, size1, string2, size2, startpos, regs,			mstop);      /* Propagate error indication if worse than mere failure.  */      if (val == -2)	return -2;      /* Return position on success.  */      if (0 <= val)	return startpos;#ifdef C_ALLOCA      alloca (0);#endif /* C_ALLOCA */    advance:      if (!range) break;      if (range > 0) range--, startpos++; else range++, startpos--;    }  return -1;}#ifndef emacs   /* emacs never uses this */intre_match (pbufp, string, size, pos, regs)     struct re_pattern_buffer *pbufp;     char *string;     int size, pos;     struct re_registers *regs;{  return re_match_2 (pbufp, 0, 0, string, size, pos, regs, size);}#endif /* emacs *//* Maximum size of failure stack.  Beyond this, overflow is an error.  */int re_max_failures = 2000;static int bcmp_translate();/* Match the pattern described by PBUFP   against data which is the virtual concatenation of STRING1 and STRING2.   SIZE1 and SIZE2 are the sizes of the two data strings.   Start the match at position POS.   Do not consider matching past the position MSTOP.   If pbufp->fastmap is nonzero, then it had better be up to date.   The reason that the data to match are specified as two components   which are to be regarded as concatenated   is so this function can be used directly on the contents of an Emacs buffer.   -1 is returned if there is no match.  -2 is returned if there is   an error (such as match stack overflow).  Otherwise the value is the length   of the substring which was matched.  */intre_match_2 (pbufp, string1, size1, string2, size2, pos, regs, mstop)     struct re_pattern_buffer *pbufp;     unsigned char *string1, *string2;     int size1, size2;     int pos;     struct re_registers *regs;     int mstop;{  register unsigned char *p = (unsigned char *) pbufp->buffer;  register unsigned char *pend = p + pbufp->used;  /* End of first string */  unsigned char *end1;  /* End of second string */  unsigned char *end2;  /* Pointer just past last char to consider matching */  unsigned char *end_match_1, *end_match_2;  register unsigned char *d, *dend;  register int mcnt;  unsigned char *translate = (unsigned char *) pbufp->translate; /* Failure point stack.  Each place that can handle a failure further down the line    pushes a failure point on this stack.  It consists of two char *'s.    The first one pushed is where to resume scanning the pattern;    the second pushed is where to resume scanning the strings.    If the latter is zero, the failure point is a "dummy".    If a failure happens and the innermost failure point is dormant,    it discards that failure point and tries the next one. */  unsigned char *initial_stack[2 * NFAILURES];  unsigned char **stackb = initial_stack;  unsigned char **stackp = stackb, **stacke = &stackb[2 * NFAILURES];  /* Information on the "contents" of registers.     These are pointers into the input strings; they record     just what was matched (on this attempt) by some part of the pattern.     The start_memory command stores the start of a register's contents     and the stop_memory command stores the end.     At that point, regstart[regnum] points to the first character in the register,     regend[regnum] points to the first character beyond the end of the register,     regstart_seg1[regnum] is true iff regstart[regnum] points into string1,     and regend_seg1[regnum] is true iff regend[regnum] points into string1.  */  unsigned char *regstart[RE_NREGS];  unsigned char *regend[RE_NREGS];  unsigned char regstart_seg1[RE_NREGS], regend_seg1[RE_NREGS];  /* Set up pointers to ends of strings.     Don't allow the second string to be empty unless both are empty.  */  if (!size2)    {      string2 = string1;      size2 = size1;      string1 = 0;      size1 = 0;    }  end1 = string1 + size1;  end2 = string2 + size2;  /* Compute where to stop matching, within the two strings */  if (mstop <= size1)    {      end_match_1 = string1 + mstop;      end_match_2 = string2;    }  else    {      end_match_1 = end1;      end_match_2 = string2 + mstop - size1;    }  /* Initialize \) text positions to -1     to mark ones that no \( or \) has been seen for.  */  for (mcnt = 0; mcnt < sizeof (regend) / sizeof (*regend); mcnt++)    regend[mcnt] = (unsigned char *) -1;  /* `p' scans through the pattern as `d' scans through the data.     `dend' is the end of the input string that `d' points within.     `d' is advanced into the following input string whenever necessary,     but this happens before fetching;     therefore, at the beginning of the loop,     `d' can be pointing at the end of a string,     but it cannot equal string2.  */  if (pos <= size1)    d = string1 + pos, dend = end_match_1;  else    d = string2 + pos - size1, dend = end_match_2;/* Write PREFETCH; just before fetching a character with *d.  */#define PREFETCH \ while (d == dend)						    \  { if (dend == end_match_2) goto fail;  /* end of string2 => failure */   \    d = string2;  /* end of string1 => advance to string2. */       \    dend = end_match_2; }  /* This loop loops over pattern commands.     It exits by returning from the function if match is complete,     or it drops through if match fails at this starting point in the input data. */  while (1)    {      if (p == pend)	/* End of pattern means we have succeeded! */	{	  /* If caller wants register contents data back, convert it to indices */	  if (regs)	    { 	      regs->start[0] = pos; 	      if (dend == end_match_1) 		regs->end[0] = d - string1; 	      else 		regs->end[0] = d - string2 + size1; 	      for (mcnt = 1; mcnt < RE_NREGS; mcnt++)		{		  if (regend[mcnt] == (unsigned char *) -1)		    {		      regs->start[mcnt] = -1;

⌨️ 快捷键说明

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