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

📄 psx-interf.c

📁 正则表达式库
💻 C
📖 第 1 页 / 共 2 页
字号:
  if (should_match && error_code_returned == REG_NOMATCH)    {        printf ("\nIgnore-case test failed:\n");      printf ("  The string to match was:  `%s'.\n", string);      print_pattern_info (pattern, &pattern_buffer);      if (cflags & REG_ICASE)        printf ("  The cflag REG_ICASE was set.\n");    }  regfree (&pattern_buffer);}static voidtest_newline (should_match, pattern, string, cflags)  boolean should_match;  char *pattern;  char *string;  int cflags;{  regex_t pattern_buffer;  int error_code_returned;  test_compile (1, 0, pattern, &pattern_buffer, cflags);  error_code_returned = regexec (&pattern_buffer, string, 0, 0, 0);  if (should_match && error_code_returned == REG_NOMATCH)    {        printf ("\nNewline test failed:\n");      printf ("  The string to match was:  `%s'.\n", string);      print_pattern_info (pattern, &pattern_buffer);      if (cflags & REG_NEWLINE)        printf ("  The cflag REG_NEWLINE was set.\n");      else        printf ("  The cflag REG_NEWLINE wasn't set.\n");    }  regfree (&pattern_buffer);}static voidtest_posix_match (should_match, pattern, string, cflags)    boolean should_match;    char *pattern;    char *string;    int cflags;{  regex_t pattern_buffer;  int error_code_returned;  boolean was_error = false;  test_compile (1, 0, pattern, &pattern_buffer, cflags);  error_code_returned = regexec (&pattern_buffer, string, 0, 0, 0);  if (should_match && error_code_returned == REG_NOMATCH)    {        printf ("\nShould have matched but didn't:\n");      was_error = true;    }  else if (!should_match && error_code_returned != REG_NOMATCH)    {        printf ("\nShould not have matched but did:\n");      was_error = true;    }  if (was_error)    {      printf ("  The string to match was:  `%s'.\n", string);      print_pattern_info (pattern, &pattern_buffer);    }  regfree (&pattern_buffer);}static voidtest_regexec (){  regmatch_t pmatch[3];  regmatch_t correct_pmatch[3];  int cflags = 0;  int eflags = 0;      printf ("\nStarting regexec tests.\n");  cflags = REG_NOSUB;    /* shouldn't look at any of pmatch.  */  test_pmatch ("a", "a", 0, pmatch, correct_pmatch, cflags);      /* Ask for less `pmatch'es than there are pattern subexpressions.     (Shouldn't look at pmatch[2].  */  cflags = REG_EXTENDED;  fill_pmatch (correct_pmatch, 0, 1, 0, 1, 100, 101);  test_pmatch ("((a))", "a", 2, pmatch, correct_pmatch, cflags);      /* Ask for same number of `pmatch'es as there are pattern subexpressions.  */  cflags = REG_EXTENDED;  fill_pmatch(correct_pmatch, 0, 1, 0, 1, -1, -1);  test_pmatch ("(a)", "a", 2, pmatch, correct_pmatch, cflags);      /* Ask for more `pmatch'es than there are pattern subexpressions.  */  cflags = REG_EXTENDED;  fill_pmatch (correct_pmatch, 0, 1, -1, -1, -1, -1);  test_pmatch ("a", "a", 2, pmatch, correct_pmatch, cflags);  eflags = REG_NOTBOL;  test_eflags (true, false, "^a", "a", cflags, eflags);  test_eflags (true, false, "(^a)", "a", cflags, eflags);  test_eflags (true, false, "a|^b", "b", cflags, eflags);  test_eflags (true, false, "^b|a", "b", cflags, eflags);  eflags = REG_NOTEOL;  test_eflags (false, true, "a$", "a", cflags, eflags);  test_eflags (false, true, "(a$)", "a", cflags, eflags);  test_eflags (false, true, "a|b$", "b", cflags, eflags);  test_eflags (false, true, "b$|a", "b", cflags, eflags);  eflags = REG_NOTBOL | REG_NOTEOL;  test_eflags (true, true, "^a$", "a", cflags, eflags);  test_eflags (true, true, "(^a$)", "a", cflags, eflags);  test_eflags (true, true, "a|(^b$)", "b", cflags, eflags);  test_eflags (true, true, "(^b$)|a", "b", cflags, eflags);  cflags = REG_ICASE;  test_ignore_case (true, "a", "a", cflags);  test_ignore_case (true, "A", "A", cflags);  test_ignore_case (true, "A", "a", cflags);  test_ignore_case (true, "a", "A", cflags);  test_ignore_case (true, "@", "@", cflags);  test_ignore_case (true, "\\[", "[", cflags);  test_ignore_case (true, "`", "`", cflags);  test_ignore_case (true, "{", "{", cflags);  test_ignore_case (true, "[!-`]", "A", cflags);  test_ignore_case (true, "[!-`]", "a", cflags);  cflags = 0;  test_ignore_case (false, "a", "a", cflags);  test_ignore_case (false, "A", "A", cflags);  test_ignore_case (false, "A", "a", cflags);  test_ignore_case (false, "a", "A", cflags);  test_ignore_case (true, "@", "@", cflags);  test_ignore_case (true, "\\[", "[", cflags);  test_ignore_case (true, "`", "`", cflags);  test_ignore_case (true, "{", "{", cflags);  test_ignore_case (true, "[!-`]", "A", cflags);  test_ignore_case (false, "[!-`]", "a", cflags);  /* Test newline stuff.  */  cflags = REG_EXTENDED | REG_NEWLINE;  test_newline (true, "\n", "\n", cflags);  test_newline (true, "a\n", "a\n", cflags);  test_newline (true, "\nb", "\nb", cflags);  test_newline (true, "a\nb", "a\nb", cflags);  test_newline (false, ".", "\n", cflags);  test_newline (false, "[^a]", "\n", cflags);  test_newline (true, "\n^a", "\na", cflags);  test_newline (true, "\n(^a|b)", "\na", cflags);  test_newline (true, "a$\n", "a\n", cflags);  test_newline (true, "(a$|b)\n", "a\n", cflags);  test_newline (true, "(a$|b|c)\n", "a\n", cflags);  test_newline (true, "((a$|b|c)$)\n", "a\n", cflags);  test_newline (true, "((a$|b|c)$)\n", "b\n", cflags);  test_newline (true, "(a$|b)\n|a\n", "a\n", cflags);  test_newline (true, "^a", "\na", cflags);  test_newline (true, "a$", "a\n", cflags);    /* Now test normal behavior.  */  cflags = REG_EXTENDED;  test_newline (true, "\n", "\n", cflags);  test_newline (true, "a\n", "a\n", cflags);  test_newline (true, "\nb", "\nb", cflags);  test_newline (true, "a\nb", "a\nb", cflags);  test_newline (true, ".", "\n", cflags);  test_newline (true, "[^a]", "\n", cflags);  test_newline (false, "\n^a", "\na", cflags);  test_newline (false, "a$\n", "a\n", cflags);  test_newline (false, "^a", "\na", cflags);  test_newline (false, "a$", "a\n", cflags);  /* Test that matches whole string only.  */  cflags = 0;  test_posix_match (true, "a", "a", cflags);    /* Tests that match substrings.  */  test_posix_match (true, "a", "ab", cflags);  test_posix_match (true, "b", "ab", cflags);    /* Test that doesn't match.  */  test_posix_match (false, "a", "b", cflags);  printf ("\nFinished regexec tests.\n");}static voidtest_error_code_message (error_code, expected_error_message)  int error_code;  char *expected_error_message;{  char returned_error_message[TEST_ERRBUF_SIZE];  char error_code_string[ERROR_CODE_LENGTH];  size_t expected_error_message_length = strlen (expected_error_message) + 1;  size_t returned_error_message_length = regerror (error_code, 0, 					             returned_error_message, 	                                             TEST_ERRBUF_SIZE);  if (returned_error_message_length != expected_error_message_length)    {      printf ("\n\n  Testing returned error codes, with expected error \message  `%s':\n", expected_error_message);      printf ("\n\n  and returned error message `%s':\n",       	      returned_error_message);      printf ("  should have returned a length of %d but returned %d.\n",	      expected_error_message_length, returned_error_message_length);    }  if (strncmp (expected_error_message, returned_error_message, 	       TEST_ERRBUF_SIZE - 1) != 0)    {            get_error_string (error_code, error_code_string),       printf ("\n\n  With error code %s (%d), expected error message:\n",      	      error_code_string, error_code);                          printf ("    `%s'\n", expected_error_message);      printf ("  but got:\n");      printf ("    `%s'\n", returned_error_message);    }}static voidtest_error_code_allocation (error_code, expected_error_message)  int error_code;  char *expected_error_message;{  char *returned_error_message = NULL;  char error_code_string[ERROR_CODE_LENGTH];  size_t returned_error_message_length = regerror (error_code, 0, 					             returned_error_message, 	                                             (size_t)0);  returned_error_message = xmalloc (returned_error_message_length + 1);      regerror (error_code, 0, returned_error_message, 	    returned_error_message_length);    if (strcmp (expected_error_message, returned_error_message) != 0)    {      get_error_string (error_code, error_code_string),             printf ("\n\n  Testing error code allocation,\n");       printf ("with error code %s (%d), expected error message:\n", 	       error_code_string, error_code);      printf ("    `%s'\n", expected_error_message);      printf ("  but got:\n");      printf ("    `%s'\n", returned_error_message);    }}static voidtest_regerror (){  test_error_code_message (REG_NOMATCH, "No match");   test_error_code_message (REG_BADPAT, "Invalid regular expression");  test_error_code_message (REG_ECOLLATE, "Invalid collation character");   test_error_code_message (REG_ECTYPE, "Invalid character class name");   test_error_code_message (REG_EESCAPE, "Trailing backslash");   test_error_code_message (REG_ESUBREG, "Invalid back reference");   test_error_code_message (REG_EBRACK, "Unmatched [ or [^");   test_error_code_message (REG_EPAREN, "Unmatched ( or \\(");   test_error_code_message (REG_EBRACE, "Unmatched \\{");   test_error_code_message (REG_BADBR, "Invalid content of \\{\\}");   test_error_code_message (REG_ERANGE, "Invalid range end");   test_error_code_message (REG_ESPACE, "Memory exhausted");   test_error_code_message (REG_BADRPT, "Invalid preceding regular expression");  test_error_code_message (REG_EEND, "Premature end of regular expression");  test_error_code_message (REG_ESIZE, "Regular expression too big");  test_error_code_allocation (REG_ERPAREN, "Unmatched ) or \\)");}voidtest_posix_interface (){  printf ("\nStarting POSIX interface tests.\n");  t = posix_interface_test;    test_regcomp ();  test_regexec ();  test_regerror ();    printf ("\nFinished POSIX interface tests.\n");}

⌨️ 快捷键说明

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