Skip to content

Commit 1e9e453

Browse files
committed
es: add a function for extracting a specified group in the pattern matching
Signed-off-by: Masatake YAMATO <[email protected]>
1 parent 0c384ec commit 1e9e453

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

dsl/es.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,29 @@ es_regex_exec (const EsObject* regex,
13521352
0, NULL, 0)? es_false: es_true;
13531353
}
13541354

1355+
EsObject*
1356+
es_regex_exec_extract_match_new (const EsObject* regex,
1357+
const EsObject* str,
1358+
unsigned int group)
1359+
{
1360+
EsObject *r;
1361+
regmatch_t *pmatch = calloc(group + 1, sizeof(regmatch_t));
1362+
if (!pmatch)
1363+
return ES_ERROR_MEMORY;
1364+
1365+
const char *s = es_string_get (str);
1366+
if (regexec(((EsRegex*)regex)->code, s, group + 1, pmatch, 0))
1367+
r = es_false;
1368+
else
1369+
r = pmatch[group].rm_so == -1
1370+
? es_nil:
1371+
es_string_newL(s + pmatch[group].rm_so,
1372+
pmatch[group].rm_eo - pmatch[group].rm_so);
1373+
1374+
free (pmatch);
1375+
return r;
1376+
}
1377+
13551378
/*
13561379
* Error
13571380
*/

dsl/es.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ int es_regex_p (const EsObject* object);
159159
EsObject* es_regex_exec (const EsObject* regex,
160160
const EsObject* str);
161161

162+
/* Return #f if unmatched.
163+
* Retrun NIL is the associate group is not in REGEX. */
164+
EsObject* es_regex_exec_extract_match_new (const EsObject* regex,
165+
const EsObject* str,
166+
unsigned int group);
167+
162168
/*
163169
* Foreign pointer
164170
*/

0 commit comments

Comments
 (0)