Guest User

Untitled

a guest
May 10th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. /* NetHack may be freely redistributed.  See license for details. */
  2.  
  3. #include <re2/re2.h>
  4.  
  5. /* RE2 is recommended for public servers as it blocks DoS attacks. It is not
  6.  * fully POSIX-compliant, but is feature-rich. Available at
  7.  * https://github.com/google/re2
  8.  *
  9.  * nhregex interface documented in sys/share/posixregex.c
  10.  */
  11. extern "C" {
  12.   #include <hack.h>
  13.  
  14.   extern const char regex_id[] = "re2regex";
  15.  
  16.   struct nhregex {
  17.     re2::RE2 *re;
  18.     nhregex() : re(nullptr) {}
  19.     ~nhregex() { delete re; }
  20.   };
  21.  
  22.   struct nhregex *regex_init(void) {
  23.     return new nhregex;
  24.   }
  25.  
  26.   boolean regex_compile(const char *s, struct nhregex *re) {
  27.     if (!s || !re)
  28.       return false;
  29.  
  30.     if (re->re) {
  31.       delete re->re;
  32.       re->re = nullptr;
  33.     }
  34.  
  35.     re->re = new re2::RE2(s);
  36.     return re->re->ok();
  37.   }
  38.  
  39.   const char *regex_error_desc(struct nhregex *re) {
  40.     return (re && re->re && !re->re->ok()) ? re->re->error().c_str() : nullptr;
  41.   }
  42.  
  43.   boolean regex_match(const char *s, struct nhregex *re) {
  44.     return (s && re && re->re) ? RE2::PartialMatch(s, *(re->re)) : false;
  45.   }
  46.  
  47.   void regex_free(struct nhregex *re) {
  48.     delete re;
  49.   }
  50. }
Add Comment
Please, Sign In to add comment