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

📄 test.c

📁 正则表达式库
💻 C
📖 第 1 页 / 共 2 页
字号:
      free (old_buf.buffer);      if (correct_regs)        free (regs_correct);    }      nonconst_buf.buffer = old_buf.buffer = NULL;  regs_correct = NULL;  regs.start = regs.end = NULL;} /* general_test */voidtest_search_return (match_start_wanted, pattern, string)    int match_start_wanted;    const char *pattern;    char *string;{  struct re_pattern_buffer buf;  char fastmap[1 << BYTEWIDTH];  const char *compile_return;  int match_start;  static num_times_called = 0;  num_times_called++;  buf.allocated = 1;  buf.buffer = xmalloc (buf.allocated);      assert (pattern != NULL);  buf.translate = 0;  compile_return = re_compile_pattern (pattern, strlen (pattern), &buf);    if (compile_return)    {      printf ("\n\nInvalid pattern in test_match_start:\n");      printf ("%s\n", compile_return);    }  else     {      buf.fastmap = fastmap;      match_start = re_search (&buf, string, strlen (string),                               0, strlen (string), 0);      if (match_start != match_start_wanted)	printf ("\nWanted search to start at %d but started at %d.\n",	         match_start, match_start_wanted);    }  free (buf.buffer);  buf.buffer = NULL;}#define SET_FASTMAP()							\  {									\    unsigned this_char;							\ 									\    memset (correct_fastmap, invert, (1 << BYTEWIDTH));			\ 									\    for (this_char = 0; this_char < strlen (fastmap_string); this_char++)\      correct_fastmap[fastmap_string[this_char]] = !invert;		\    correct_fastmap['\n'] = match_newline;				\  }					  voidtest_fastmap (pat, fastmap_string, invert, match_newline)    const char *pat;    char *fastmap_string;    unsigned invert;    unsigned match_newline;{  char correct_fastmap[(1 << BYTEWIDTH)];				  SET_FASTMAP ();  general_test (1, 0, pat, NULL, NULL, -1, 0, -1, correct_fastmap, 0, -1);}voidtest_fastmap_search (pat, str, fastmap_string, invert, match_newline,                      can_be_null, start0, end0)    const char *pat;     char *str;     char *fastmap_string;    unsigned invert;     unsigned match_newline;     int can_be_null;     int start0;     int end0;{  char correct_fastmap[(1 << BYTEWIDTH)];				  struct re_registers correct_regs;  correct_regs.num_regs = RE_NREGS;  correct_regs.start = (int *) xmalloc (RE_NREGS * sizeof (int));  correct_regs.end = (int *) xmalloc (RE_NREGS * sizeof (int));    set_all_registers (start0, end0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,                      -1, -1, -1, -1, -1, -1, -1, -1, &correct_regs);  SET_FASTMAP ();  general_test (1, 0, pat, str, NULL, 0, SAFE_STRLEN (str), SAFE_STRLEN (str),		correct_fastmap, &correct_regs, can_be_null);  free (correct_regs.start);  free (correct_regs.end);}voidtest_all_registers (pat, str1, str2, 		    start0, end0, start1, end1,                     start2, end2, start3, end3,                     start4, end4, start5, end5,                     start6, end6, start7, end7,                     start8, end8, start9, end9)  char *pat; char *str1; char *str2;   int start0; int end0; int start1; int end1;   int start2; int end2; int start3; int end3;   int start4; int end4; int start5; int end5;   int start6; int end6; int start7; int end7;   int start8; int end8; int start9; int end9;{  struct re_registers correct_regs;    if (omit_register_tests) return;    correct_regs.num_regs = RE_NREGS;  correct_regs.start = (int *) xmalloc (RE_NREGS * sizeof (int));  correct_regs.end = (int *) xmalloc (RE_NREGS * sizeof (int));  set_all_registers (start0, end0, start1, end1, start2, end2, start3, end3,                      start4, end4, start5, end5, start6, end6, start7, end7,                      start8, end8, start9, end9, &correct_regs);  general_test (1, 0, pat, str1, str2, 0, 	        SAFE_STRLEN (str1) + SAFE_STRLEN (str2), 		SAFE_STRLEN (str1) + SAFE_STRLEN (str2),                 NULL, &correct_regs, -1);  free (correct_regs.start);  free (correct_regs.end);}voidinvalid_pattern (error_code_expected, pattern)  int error_code_expected;  char *pattern;{  regex_t pattern_buffer;  int cflags    = re_syntax_options == RE_SYNTAX_POSIX_EXTENDED      || re_syntax_options == RE_SYNTAX_POSIX_MINIMAL_EXTENDED      ? REG_EXTENDED : 0;     test_compile (0, error_code_expected, pattern, &pattern_buffer, cflags);}voidvalid_pattern (pattern)  char *pattern;{  regex_t pattern_buffer;  int cflags    = re_syntax_options == RE_SYNTAX_POSIX_EXTENDED      || re_syntax_options == RE_SYNTAX_POSIX_MINIMAL_EXTENDED      ? REG_EXTENDED : 0;     test_compile (1, 0, pattern, &pattern_buffer, cflags);}char *delimiters_to_ops (source, left_delimiter, right_delimiter)  char *source;  char left_delimiter;  char right_delimiter;{  static char *answer = NULL;  char *tmp = NULL;  boolean double_size = false;  unsigned source_char;  unsigned answer_char = 0;    assert (source != NULL);  switch (left_delimiter)    {      case '(': if (!(re_syntax_options & RE_NO_BK_PARENS))	          double_size = true;       	        break;      case '{': if (!(re_syntax_options & RE_NO_BK_BRACES))	          double_size = true;       	        break;      default: printf ("Found strange delimiter %c in delimiter_to_ops.\n",		        left_delimiter);	       printf ("The source was `%s'\n", source);	       exit (0);    }  if (answer == source)    {      tmp = (char *) xmalloc (strlen (source) + 1);      strcpy (tmp, source);      source = tmp;    }  if (answer)    {      free (answer);      answer = NULL;    }  answer = (char *) xmalloc ((double_size 		             ? strlen (source) << 1                             : strlen (source))                            + 1);  if (!double_size)    strcpy (answer, source);  else    {      for (source_char = 0; source_char < strlen (source); source_char++)        {          if (source[source_char] == left_delimiter 	      || source[source_char] == right_delimiter)            answer[answer_char++] = '\\';          answer[answer_char++] = source[source_char];        }      answer[answer_char] = 0;    }      return answer;}voidprint_pattern_info (pattern, pattern_buffer_ptr)  const char *pattern;   regex_t *pattern_buffer_ptr;{  printf ("  Pattern:  `%s'.\n", pattern);  printf ("  Compiled pattern:  ");  print_compiled_pattern (pattern_buffer_ptr);}voidvalid_nonposix_pattern (pattern)  char *pattern;{  struct re_pattern_buffer nonconst_buf;    nonconst_buf.allocated = 0;  nonconst_buf.buffer = NULL;  nonconst_buf.translate = NULL;    assert (pattern != NULL);    if (re_compile_pattern (pattern, strlen (pattern), &nonconst_buf))    {      printf ("Couldn't compile the pattern.\n");      print_pattern_info (pattern, &nonconst_buf);    }}voidcompile_and_print_pattern (pattern)  char *pattern;{  struct re_pattern_buffer nonconst_buf;    nonconst_buf.allocated = 0;  nonconst_buf.buffer = NULL;    if (re_compile_pattern (pattern, strlen (pattern), &nonconst_buf))    printf ("Couldn't compile the pattern.\n");      print_pattern_info (pattern, &nonconst_buf);}voidtest_case_fold (pattern, string)  const char *pattern;  char* string;{  struct re_pattern_buffer nonconst_buf;  const char *ret;  init_pattern_buffer (&nonconst_buf);  nonconst_buf.translate = upcase;  assert (pattern != NULL);  ret = re_compile_pattern (pattern, strlen (pattern), &nonconst_buf);    if (ret)    {      printf ("\nShould have been a valid pattern but wasn't.\n");      print_pattern_info (pattern, &nonconst_buf);    }  else    {      if (test_should_match           && re_match (&nonconst_buf, string, strlen (string), 0, 0)              != strlen (string))       {         printf ("Match failed for case fold.\n");         printf ("  Pattern:  `%s'.\n", pattern);         printf ("  String: `%s'.\n", string == NULL ? "NULL" : string);       }    }}voidtest_match_n_times (n, pattern, string)  unsigned n;  char* pattern;  char* string;{  struct re_pattern_buffer buf;  const char *r;  unsigned match = 0;  unsigned this_match;  buf.allocated = 0;  buf.buffer = NULL;  buf.translate = 0;  assert (pattern != NULL);    r = re_compile_pattern (pattern, strlen (pattern), &buf);  if (r)    {      printf ("Didn't compile.\n");      printf ("  Pattern: %s.\n", pattern);    }  else    {      for (this_match = 1; this_match <= n; this_match++)        match = (re_match (&buf, string, strlen (string),                           0, 0) 	         == strlen (string));      if (match && !test_should_match)          printf ("\n\nMatched but shouldn't have:\n");      else if (!match && test_should_match)          printf ("\n\nDidn't match but should have:\n");            if ((match && !test_should_match) || (!match && test_should_match))        {	  printf("  The string to match was:  ");          if (string)            printf ("`%s' and ", string);          else            printf ("`'");              printf ("  Pattern: %s.\n", pattern);          printf ("  Compiled pattern: %s.\n", pattern);	  print_compiled_pattern (&buf);        }    }}void test_match_2 (pat, str1, str2)  const char *pat;   char *str1;   char *str2;{  general_test (1, 1, pat, str1, str2, 0, 1,		  SAFE_STRLEN (str1) + SAFE_STRLEN (str2), NULL, 0, -1);}void test_match (pat, str)  const char *pat;  char *str;{  test_match_2 (pat, str, NULL);  test_match_2 (pat, NULL, str);}

⌨️ 快捷键说明

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