Skip to content

Commit 6ab412d

Browse files
committed
Regexps in 32-mode
1 parent 648d249 commit 6ab412d

3 files changed

Lines changed: 56 additions & 36 deletions

File tree

runtime/runtime.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
/* Runtime library */
22

3-
#define _GNU_SOURCE 1
3+
# define _GNU_SOURCE 1
44

5-
#include "runtime.h"
6-
#include <regex.h>
7-
8-
#include "gc.h"
9-
#include "runtime_common.h"
5+
# include "runtime.h"
6+
# include "gc.h"
107

118
extern size_t __gc_stack_top, __gc_stack_bottom;
129

@@ -486,7 +483,7 @@ extern regex_t *Lregexp (char *regexp) {
486483

487484
int res = regcomp(regexp_compiled, regexp, REG_EXTENDED);
488485

489-
// printf("Lregexp: got compiled regexp %p, for string %s\n", regexp_compiled, regexp);
486+
//printf("Lregexp: got compiled regexp %p, for string %s\n", regexp_compiled, regexp);
490487

491488
if (res != 0) {
492489
char buf[100];
@@ -506,7 +503,7 @@ extern aint LregexpMatch (regex_t *b, char *s, aint pos) {
506503

507504
int res = regexec(b, s + UNBOX(pos), (size_t) 1, &match, 0);
508505

509-
// printf("regexpMatch %p: %s, res=%d so=%d eo=%d\n", b, s + UNBOX(pos), res, match.rm_so, match.rm_eo);
506+
//printf("regexpMatch %p: %s, res=%d so=%d eo=%d\n", b, s + UNBOX(pos), res, match.rm_so, match.rm_eo);
510507

511508
if (res == 0 && match.rm_so == 0) {
512509
return BOX(match.rm_eo);
@@ -826,7 +823,7 @@ extern void *Bclosure (aint* args, aint bn) {
826823
extern void *Barray (aint* args, aint bn) {
827824
data *r;
828825
aint n = UNBOX(bn);
829-
826+
830827
PRE_GC();
831828

832829
for (aint i = 0; i < n; i++) {

runtime32/runtime.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -630,38 +630,40 @@ extern void* Lsubstring (void *subj, int p, int l) {
630630
subject length=%d)", pp, ll, LEN(d->tag));
631631
}
632632

633-
extern struct re_pattern_buffer *Lregexp (char *regexp) {
634-
regex_t *b = (regex_t*) malloc (sizeof (regex_t));
633+
extern regex_t *Lregexp (char *regexp) {
634+
regex_t *regexp_compiled = (regex_t *) malloc(sizeof(regex_t));
635635

636-
/* printf ("regexp: %s,\t%x\n", regexp, b); */
636+
memset(regexp_compiled, 0, sizeof(regex_t));
637637

638-
memset (b, 0, sizeof (regex_t));
638+
int res = regcomp(regexp_compiled, regexp, REG_EXTENDED);
639639

640-
int n = (int) re_compile_pattern (regexp, strlen (regexp), b);
640+
//printf("Lregexp: got compiled regexp %p, for string %s\n", regexp_compiled, regexp);
641641

642-
if (n != 0) {
643-
failure ("%", strerror (n));
644-
};
642+
if (res != 0) {
643+
char buf[100];
644+
regerror(res, regexp_compiled, buf, 100);
645+
failure("%s", buf);
646+
}
645647

646-
return b;
648+
return regexp_compiled;
647649
}
648650

649-
extern int LregexpMatch (struct re_pattern_buffer *b, char *s, int pos) {
650-
int res;
651+
extern void* LregexpMatch (regex_t *b, char *s, int pos) {
652+
regmatch_t match;
651653

652654
ASSERT_BOXED("regexpMatch:1", b);
653655
ASSERT_STRING("regexpMatch:2", s);
654656
ASSERT_UNBOXED("regexpMatch:3", pos);
655657

656-
res = re_match (b, s, LEN(TO_DATA(s)->tag), UNBOX(pos), 0);
658+
int res = regexec(b, s + UNBOX(pos), (size_t) 1, &match, 0);
657659

658-
/* printf ("regexpMatch %x: %s, res=%d\n", b, s+UNBOX(pos), res); */
660+
//printf ("regexpMatch %p: %s, res=%d so=%d eo=%d\n", b, s + UNBOX(pos), res, match.rm_so, match.rm_eo);
659661

660-
if (res) {
661-
return BOX (res);
662+
if (res == 0 && match.rm_so == 0) {
663+
return BOX(match.rm_eo);
664+
} else {
665+
return BOX(-1);
662666
}
663-
664-
return BOX (res);
665667
}
666668

667669
extern void* Bstring (void*);

src/X86_32.ml

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -706,16 +706,37 @@ class env prg =
706706
let n = String.length x in
707707
let buf = Buffer.create (n*2) in
708708
let rec iterate i =
709-
if i < n
710-
then (
711-
(match x.[i] with
712-
| '"' -> Buffer.add_string buf "\\\""
713-
| '\n' -> Buffer.add_string buf "\n"
714-
| '\t' -> Buffer.add_string buf "\t"
715-
| c -> Buffer.add_char buf c
716-
);
717-
iterate (i+1)
718-
)
709+
if i < n then
710+
match x.[i] with
711+
| '"' ->
712+
Buffer.add_char buf '\\';
713+
Buffer.add_char buf '"';
714+
iterate (i + 1)
715+
| '\\' -> (
716+
if i + 1 >= n then (
717+
Buffer.add_char buf '\\';
718+
Buffer.add_char buf '\\')
719+
else
720+
match x.[i + 1] with
721+
| 'n' ->
722+
Buffer.add_char buf '\\';
723+
Buffer.add_char buf 'n';
724+
iterate (i + 2)
725+
| 't' ->
726+
Buffer.add_char buf '\\';
727+
Buffer.add_char buf 't';
728+
iterate (i + 2)
729+
| 'r' ->
730+
Buffer.add_char buf '\\';
731+
Buffer.add_char buf 'r';
732+
iterate (i + 2)
733+
| _ ->
734+
Buffer.add_char buf '\\';
735+
Buffer.add_char buf '\\';
736+
iterate (i + 1))
737+
| c ->
738+
Buffer.add_char buf c;
739+
iterate (i + 1)
719740
in
720741
iterate 0;
721742
Buffer.contents buf

0 commit comments

Comments
 (0)