NHDT->ANH, nethack->anethack, nhdat->anhdat
[aNetHack.git] / sys / share / cppregex.cpp
blobd2d186fc2e345912c85784e44c816cc025469817
1 /* aNetHack 0.0.1 cppregex.cpp $ANH-Date$ $ANH-Branch$:$ANH-Revision$ */
2 /* Copyright (c) Sean Hunt 2015. */
3 /* aNetHack may be freely redistributed. See license for details. */
5 #include <regex>
6 #include <memory>
8 /* nhregex interface documented in sys/share/posixregex.c */
10 extern "C" {
11 #include <hack.h>
13 extern const char regex_id[] = "cppregex";
15 struct nhregex {
16 std::unique_ptr<std::regex> re;
17 std::unique_ptr<std::regex_error> err;
20 struct nhregex *regex_init(void) {
21 return new nhregex;
24 boolean regex_compile(const char *s, struct nhregex *re) {
25 if (!re)
26 return FALSE;
27 try {
28 re->re.reset(new std::regex(s, (std::regex::extended
29 | std::regex::nosubs
30 | std::regex::optimize)));
31 re->err.reset(nullptr);
32 return TRUE;
33 } catch (const std::regex_error& err) {
34 re->err.reset(new std::regex_error(err));
35 re->re.reset(nullptr);
36 return FALSE;
40 const char *regex_error_desc(struct nhregex *re) {
41 if (re->err)
42 return re->err->what();
43 else
44 return nullptr;
47 boolean regex_match(const char *s, struct nhregex *re) {
48 if (!re->re)
49 return false;
50 try {
51 return regex_search(s, *re->re, std::regex_constants::match_any);
52 } catch (const std::regex_error& err) {
53 return false;
57 void regex_free(struct nhregex *re) {
58 delete re;