The regex implementation in FreeBSD's libc is quite outdated. It is not developed any more, it is slow and it does not support wide characters. We need a modern version with better I18N support, good performance and standard-compliance. The work that has been done on grep also showed that there is quite much to optimize in this area. This project pretends to port the TRE regex implementation and apply some compatibility/optimization changes to suit better our needs.
The regex implementation in FreeBSD's libc is quite outdated. It is not developed any more, it is slow and it does not support wide characters. We need a modern version with better I18N support, good performance and standard-compliance. The work that has been done on grep also showed that there is quite much to optimize in this area. This project pretends to port the TRE regex implementation and apply some compatibility/optimization changes to suit better our needs. This is the best available option. I have looked at different alternatives. In short, Oniguruma is slow, PCRE is not strict POSIX and the Plan 9 implementation is limited to egrep syntax. TRE is BSD-licensed, (almost) strict POSIX and it has a good performance. The rational of the implementation details is also explained in the author's thesis.
Apart from these common requirements against the regex implementation, we have some specific needs in FreeBSD. These were studied and elaborated during the work on grep. First, the GNU regex implementation has been used in grep for a long time, which is more permissive with the regular expressions than POSIX, e.g. it allows [a|], which is invalid in strict implementations. TRE does not support such non-standard forms but we need it to preserve compatibility. It should to be added as an optional feature, which can be controlled with a custom flag passed to regcomp(). Secondly, the POSIX interface is limiting. GNU grep uses a heuristic for pattern matching, e.g. to match “foo.*bar” or something similar, a very fast fixed string algorithm (like Boyer-Moore) is used then the matching line is separated and the heavy matching algorithm is only applied on the potentially matching line. This is not possible with POSIX regexec(), which works with NUL-terminated strings instead of byte-counted buffers and thus implies char-at-a-time processing. An alternative interface should be provided for this behaviour. This interface could be used internally in the FreeBSD userland to provide really fast pattern matching, while preserving the POSIX compatibility and offering the standard API for third-party applications.
Reference: http://lists.freebsd.org/pipermail/freebsd-current/2010-August/019310.html (more specifically http://lists.freebsd.org/pipermail/freebsd-current/2010-August/019358.html)
As a side note, GNU grep does these optimizations internally, which imho is not a good idea and it should better be done in the regex library for the following reasons:
Most testing steps will be done with grep because it is the most massive regex user.