if (re instanceof ECompiledRE) {
regex = (ECompiledRE) re;
} else {
ETuple2 res = compile(re, opts);
EObject val = res.elem2;
if (res.elem1 == ERT.am_ok && ( val instanceof ECompiledRE)) {
regex = (ECompiledRE) val;
} else {
return val;
}
}
ESeq o = opts.testSeq();
Options o2;
if (o.isNil()) {
o2 = regex.options;
} else {
o2 = regex.options.re_init(o);
if (o2 == null) {
throw ERT.badarg(subj, re, opts);
}
}
String subject = regex.options.decode(subj);
if (subject == null) {
throw ERT.badarg(subj, re, opts);
}
if (o2.offset > subject.length() || o2.offset < 0) {
throw ERT.badarg(subj, re, opts);
}
Matcher matcher = regex.patt.matcher(subject.substring(o2.offset ));
if (o2.global) {
ESeq result = ERT.NIL;
while (matcher.find()) {
MatchResult mr = matcher.toMatchResult();
ESeq list;
if (o2.capture_spec == am_all) {
ESeq l = ERT.NIL;
for (int i = mr.groupCount(); i >= 0; i--) {
l = l.cons( capture (subject, mr, i, o2) );
}
result = result.cons(l);
} else if ((list = o2.capture_spec.testSeq()) != null) {
ESeq l = ERT.NIL;
while (!list.isNil()) {
EObject group = list.head();
ESmall num;
EAtom nam;
EString nam2;
if ((num=group.testSmall()) != null)
{
l = l.cons( capture (subject, mr, num.value, o2 ));
} else if ((nam=group.testAtom()) != null) {
Integer groupNo = o2.named_groups.get(nam.getName());
if (groupNo != null) {
l = l.cons( capture (subject, mr, groupNo.intValue(), o2 ));
}
} else if ((nam2=group.testString()) != null) {
Integer groupNo = o2.named_groups.get(nam2.stringValue());
if (groupNo != null) {
l = l.cons( capture (subject, mr, groupNo.intValue(), o2 ));
}
} else {
throw new NotImplemented("named capture groups");
}
list = list.tail();
}
result = result.cons(l);
} else {
throw new NotImplemented("global and not all");
}
}
if (result == ERT.NIL) {
return am_nomatch;
} else {
return new ETuple2(am_match, result.reverse());
}
} else {
if (matcher.find()) {
if (o2.capture_spec == am_none) {
return am_match;
}
MatchResult mr = matcher.toMatchResult();
int max = mr.groupCount();
while( mr.start(max) == -1)
max -= 1;
ESeq il;
if (o2.capture_spec == am_all) {
ESeq l = ERT.NIL;
for (int i = max; i >= 0; i--) {
l = l.cons( capture (subject, mr, i, o2) );
}
return new ETuple2(am_match, l);
} else if (o2.capture_spec == am_all_but_first) {
ESeq l = ERT.NIL;
for (int i = max; i > 0; i--) {
l = l.cons( capture (subject, mr, i, o2) );
}
return new ETuple2(am_match, l);
} else if (o2.capture_spec == am_first) {
EObject l = capture (subject, mr, 0, o2);
return new ETuple2(am_match, l);
} else if ((il = o2.capture_spec.testSeq()) != null) {
ESeq out = ERT.NIL;
for (; !il.isNil(); il = il.tail()) {
EObject what = il.head();
ESmall idx = what.testSmall();
EAtom nam;
if (idx != null && mr.start(idx.value) != -1) {
EObject val = capture (subject, mr, idx.value, o2);
out = out.cons(val);
} else if ((nam=what.testAtom())!=null) {
Integer idx2 = o2.named_groups.get(nam.getName());
if (idx2 != null) {
EObject val = capture (subject, mr, idx2, o2);
out = out.cons(val);
} else {
// badarg?
}
} else {