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

📄 regex.c

📁 TURBOC 文本处理常规库
💻 C
📖 第 1 页 / 共 4 页
字号:
		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. */

int
re_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).  */

int
re_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);
      if (0 <= val)
	{
	  if (val == -2)
	    return -2;
	  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 */
int
re_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.  */

int
re_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;
		      regs->end[mcnt] = -1;
		      continue;
		    }
 		  if (regstart_seg1[mcnt])
		    regs->start[mcnt] = regstart[mcnt] - string1;
		  else
		    regs->start[mcnt] = regstart[mcnt] - string2 + size1;
 		  if (regend_seg1[mcnt])
		    regs->end[mcnt] = regend[mcnt] - string1;
		  else
		    regs->end[mcnt] = regend[mcnt] - string2 + size1;
		}
	    }
 	  if (dend == end_match_1)
	    return (d - string1 - pos);
	  else
	    return d - string2 + size1 - pos;
	}

      /* Otherwise match next pattern command */
#ifdef SWITCH_ENUM_BUG
      switch ((int) ((enum regexpcode) *p++))
#else
      switch ((enum regexpcode) *p++)
#endif
	{

	/* \( is represented by a start_memory, \) by a stop_memory.
	    Both of those commands contain a "register number" argument.
	    The text matched within the \( and \) is recorded under that number.
	    Then, \<digit> turns into a `duplicate' command which
	    is followed by the numeric value of <digit> as the register number. */

	case start_memory:
	  regstart[*p] = d;
 	  regstart_seg1[*p++] = (dend == end_match_1);
	  break;

	case stop_memory:
	  regend[*p] = d;
 	  regend_seg1[*p++] = (dend == end_match_1);
	  break;

	case duplicate:
	  {
	    int regno = *p++;   /* Get which register to match against */
	    register unsigned char *d2, *dend2;

	    d2 = regstart[regno];
 	    dend2 = ((regstart_seg1[regno] == regend_seg1[regno])
		     ? regend[regno] : end_match_1);
	    while (1)
	      {
		/* Advance to next segment in register contents, if necessary */
		while (d2 == dend2)
		  {
		    if (dend2 == end_match_2) break;
		    if (dend2 == regend[regno]) break;
		    d2 = string2, dend2 = regend[regno];  /* end of string1 => advance to string2. */
		  }
		/* At end of register contents => success */
		if (d2 == dend2) break;

		/* Advance to next segment in data being matched, if necessary */
		PREFETCH;

		/* mcnt gets # consecutive chars to compare */
		mcnt = dend - d;
		if (mcnt > dend2 - d2)
		  mcnt = dend2 - d2;
		/* Compare that many; failure if mismatch, else skip them. */
		if (translate ? bcmp_translate (d, d2, mcnt, translate) : bcmp (d, d2, mcnt))
		  goto fail;
		d += mcnt, d2 += mcnt;
	      }
	  }
	  break;

	case anychar:
	  /* fetch a data character */
	  PREFETCH;
	  /* Match anything but a newline.  */
	  if ((translate ? translate[*d++] : *d++) == '\n')
	    goto fail;
	  break;

	case charset:
	case charset_not:
	  {
	    /* Nonzero for charset_not */
	    int not = 0;
	    register int c;
	    if (*(p - 1) == (unsigned char) charset_not)
	      not = 1;

	    /* fetch a data character */
	    PREFETCH;

	    if (translate)
	      c = translate [*d];
	    else
	      c = *d;

	    if (c < *p * BYTEWIDTH
		&& p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
	      not = !not;

	    p += 1 + *p;

	    if (!not) goto fail;
	    d++;
	    break;
	  }

	case begline:
	  if (d == string1 || d[-1] == '\n')
	    break;
	  goto fail;

	case endline:
	  if (d == end2
	      || (d == end1 ? (size2 == 0 || *string2 == '\n') : *d == '\n'))
	    break;
	  goto fail;

	/* "or" constructs ("|") are handled by starting each alternative
	    with an on_failure_jump that points to the start of the next alternative.
	    Each alternative except the last ends with a jump to the joining point.
	    (Actually, each jump except for the last one really jumps
	     to the following jump, because tensioning the jumps is a hassle.) */

	/* The start of a stupid repeat has an on_failure_jump that points
	   past the end of the repeat text.
	   This makes a failure point so that, on failure to match a repetition,
	   matching restarts past as many repetitions have been found
	   with no way to fail and look for another one.  */

	/* A smart repeat is similar but loops back to the on_failure_jump
	   so that each repetition makes another failure point. */

	case on_failure_jump:
	  if (stackp == stacke)
	    {
	      unsigned char **stackx;

⌨️ 快捷键说明

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