Files
nethack/sys/share/cppregex.cpp
PatR 1547e676f3 finish implementing pmatchregex
I started out cleaning up a bit of lint in the recent run-time options
handling and discovered that pmatchregex wasn't finished.  Finish it and
also deal with the version lint.  Argument declarations for function
definitions in pmatchregex.c have been switched to K&R style.  (The ones
in posixregex.c have been left in ANSI style.)

There wasn't any build rule for pmatchregex.o; now there is (for Unix).
posixregex.o is still the default.

There isn't any build rule for cppregex.o (again, for Unix); the change
to cppregex.cpp is untested.
2015-06-16 02:29:22 -07:00

61 lines
1.5 KiB
C++

/* NetHack 3.6 cppregex.cpp $NHDT-Date$ $NHDT-Branch$:$NHDT-Revision$ */
/* Copyright (c) Sean Hunt 2015. */
/* NetHack may be freely redistributed. See license for details. */
#include <regex>
#include <memory>
/* nhregex interface documented in sys/share/posixregex.c */
extern "C" {
#include <hack.h>
const char regex_id[] = "cppregex";
struct nhregex {
std::unique_ptr<std::regex> re;
std::unique_ptr<std::regex_error> err;
};
struct nhregex *regex_init(void) {
return new nhregex;
}
boolean regex_compile(const char *s, struct nhregex *re) {
if (!re)
return FALSE;
try {
re->re.reset(new std::regex(s, (std::regex::extended
| std::regex::nosubs
| std::regex::optimize)));
re->err.reset(nullptr);
return TRUE;
} catch (const std::regex_error& err) {
re->err.reset(new std::regex_error(err));
re->re.reset(nullptr);
return FALSE;
}
}
const char *regex_error_desc(struct nhregex *re) {
if (re->err)
return re->err->what();
else
return nullptr;
}
boolean regex_match(const char *s, struct nhregex *re) {
if (!re->re)
return false;
try {
return regex_search(s, *re->re, std::regex_constants::match_any);
} catch (const std::regex_error& err) {
return false;
}
}
void regex_free(struct nhregex *re) {
delete re;
}
}