File: cppregex.cpp

package info (click to toggle)
nethack 3.6.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,468 kB
  • sloc: ansic: 266,495; cpp: 13,652; yacc: 2,903; perl: 1,426; lex: 581; sh: 535; xml: 372; awk: 98; makefile: 68; fortran: 51; sed: 11
file content (61 lines) | stat: -rw-r--r-- 1,557 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* NetHack 3.6  cppregex.cpp */
/* $NHDT-Date: 1524684157 2018/04/25 19:22:37 $  $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.8 $ */
/* 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>

  extern 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;
  }
}