fixed a minor bug in calling tre_add_tags()
added qse_tre_open()/qse_tre_close()/qse_tre_geterrnum()/qse_tre_geterrmsg() added a parameter to return the number of submatches into qse_tre_compx()/qse_tre_comp()
This commit is contained in:
@ -40,7 +40,8 @@ qse_rex_setoption (rex, QSE_REX_STRICT);
|
||||
str.ptr = argv[2];
|
||||
str.len = qse_strlen(argv[2]);
|
||||
|
||||
qse_printf (QSE_T("compile ok\n"));
|
||||
qse_printf (QSE_T("compile ok\n"));
|
||||
|
||||
n = qse_rex_exec (rex, &str, &str, &matstr);
|
||||
if (n <= -1)
|
||||
{
|
||||
|
@ -1,35 +1,69 @@
|
||||
|
||||
#include <qse/cmn/tre.h>
|
||||
#include <qse/cmn/main.h>
|
||||
#include <qse/cmn/tre.h>
|
||||
#include <qse/cmn/mem.h>
|
||||
#include <qse/cmn/misc.h>
|
||||
#include <qse/cmn/stdio.h>
|
||||
|
||||
static int test_main (int argc, qse_char_t* argv[], qse_char_t* envp[])
|
||||
{
|
||||
qse_tre_t tre;
|
||||
unsigned int nsubmat;
|
||||
qse_tre_match_t* mat = QSE_NULL;
|
||||
|
||||
if (argc != 3)
|
||||
{
|
||||
qse_printf (QSE_T("USAGE: %s pattern string\n"),
|
||||
qse_basename(argv[0]));
|
||||
return -1;
|
||||
}
|
||||
|
||||
qse_tre_init (&tre, QSE_NULL);
|
||||
|
||||
if (qse_tre_comp (&tre, argv[1], QSE_TRE_EXTENDED|QSE_TRE_NOSUBREG) <= -1)
|
||||
if (qse_tre_comp (&tre, argv[1], &nsubmat, QSE_TRE_EXTENDED) <= -1)
|
||||
{
|
||||
qse_printf (QSE_T("Cannot compile pattern [%s] - %d\n"), argv[1], QSE_TRE_ERRNUM(&tre));
|
||||
qse_printf (QSE_T("ERROR: Cannot compile pattern [%s] - %s\n"), argv[1], qse_tre_geterrmsg(&tre));
|
||||
goto oops;
|
||||
}
|
||||
|
||||
if (qse_tre_exec(&tre, argv[2], (size_t) 0, NULL, 0) <= -1)
|
||||
if (nsubmat > 0)
|
||||
{
|
||||
if (QSE_TRE_ERRNUM(&tre) == QSE_TRE_ENOMATCH) qse_printf (QSE_T("no match\n"));
|
||||
else qse_printf (QSE_T("ERROR %d\n"), QSE_TRE_ERRNUM(&tre));
|
||||
goto oops;
|
||||
mat = QSE_MMGR_ALLOC (qse_tre_getmmgr(&tre), QSE_SIZEOF(*mat) * nsubmat);
|
||||
if (mat == QSE_NULL)
|
||||
{
|
||||
qse_printf (QSE_T("ERROR: Cannot allocate submatch array\n"));
|
||||
goto oops;
|
||||
}
|
||||
}
|
||||
|
||||
if (qse_tre_exec(&tre, argv[2], mat, nsubmat, 0) <= -1)
|
||||
{
|
||||
if (QSE_TRE_ERRNUM(&tre) == QSE_TRE_ENOMATCH) qse_printf (QSE_T("Match: NO\n"));
|
||||
else
|
||||
{
|
||||
qse_printf (QSE_T("ERROR: Cannot not match pattern - %s\n"), qse_tre_geterrmsg(&tre));
|
||||
goto oops;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
qse_printf (QSE_T("match...\n"));
|
||||
unsigned int i;
|
||||
qse_printf (QSE_T("Match: YES\n"));
|
||||
|
||||
for (i = 0; i < nsubmat; i++)
|
||||
{
|
||||
if (mat[i].rm_so == -1) break;
|
||||
qse_printf (QSE_T("SUBMATCH[%u] = [%.*s]\n"), i,
|
||||
(int)(mat[i].rm_eo - mat[i].rm_so), &argv[2][mat[i].rm_so]);
|
||||
}
|
||||
}
|
||||
|
||||
if (mat) QSE_MMGR_FREE (qse_tre_getmmgr(&tre), mat);
|
||||
qse_tre_fini (&tre);
|
||||
return 0;
|
||||
|
||||
oops:
|
||||
if (mat) QSE_MMGR_FREE (qse_tre_getmmgr(&tre), mat);
|
||||
qse_tre_fini (&tre);
|
||||
return -1;
|
||||
}
|
||||
|
Reference in New Issue
Block a user