File: rxx.h

package info (click to toggle)
sfs 1%3A0.5k-8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,388 kB
  • ctags: 8,556
  • sloc: cpp: 43,410; ansic: 17,574; sh: 8,412; makefile: 771; yacc: 277; lex: 96; sed: 47
file content (168 lines) | stat: -rw-r--r-- 4,468 bytes parent folder | download
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// -*-c++-*-
/* $Id: rxx.h,v 1.9 2001/08/20 22:38:04 dm Exp $ */

/*
 *
 * Copyright (C) 1998 David Mazieres (dm@uun.org)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 */

#ifndef _RXX_H_
#define _RXX_H_ 1

#include "sysconf.h"
extern "C" {
#include "pcre.h"
}
#include "str.h"
#include "init.h"

INIT (rxxinit);

extern "C" void *rcmalloc (size_t n);
extern "C" void rcfree (void *p);
extern "C" void *rccopy (void *p);

class rxx {
protected:
  pcre *re;
  pcre_extra *extra;

  int nsubpat;
  int *ovector;
  int ovecsize;
  str subj;

  str init (const char *pat, const char *opt);
  void copy (const rxx &r) {
    re = static_cast<pcre *> (rccopy (r.re));
    extra = static_cast<pcre_extra *> (rccopy (r.extra));
    nsubpat = 0;
    ovector = NULL;
    ovecsize = r.ovecsize;
  }
  rxx &operator= (const rxx &);
  void mknull () {
    re = NULL;
    extra = NULL;
    nsubpat = 0;
    ovector = NULL;
    ovecsize = NULL;
    subj = NULL;
  }
  rxx () {}

public:
  void _exec (const char *p, size_t len, int options);
  void exec (str s, int options);

  class matchresult {
    const rxx &r;
  public:
    matchresult (const rxx &r) : r (r) {}
    operator bool () const { return r.success (); }
    operator str () const { return r.at (0); }
    str operator[] (ptrdiff_t n) const { return r.at (n); }
  };

  rxx (const char *pat, const char *opt = "")
    { if (str s = init (pat, opt)) panic ("%s", s.cstr ()); }
  rxx (const rxx &r) { assert (r.re); copy (r); }
  ~rxx () { rcfree (re); rcfree (extra); delete[] ovector; }

  void study () { extra = pcre_study (re, 0, NULL); }
  void clear () { nsubpat = 0; subj = NULL; }

  matchresult search (str s, int opt = 0) { exec (s, opt); return *this; }
  matchresult match (str s, int opt = 0) {
    exec (s, opt | PCRE_ANCHORED);
    if (nsubpat > 0 && implicit_cast<size_t> (ovector[1]) != s.len ())
      nsubpat = 0;
    return *this;
  }

  bool success () const { return nsubpat > 0; }

  str at (ptrdiff_t n) const;
  int start (int n) const
    { assert (n >= 0); return n < nsubpat ? ovector[2*n] : -1; }
  int end (int n) const
    { assert (n >= 0); return n < nsubpat ? ovector[2*n+1] : -1; }
  int len (int n) const {
    assert (n >= 0);
    size_t i = 2 * n;
    return n < nsubpat && ovector[i] >= 0
      ? ovector[i+1] - ovector[i] : -1;
  }

  str operator[] (ptrdiff_t n) const { return at (n); }
};

class rrxx : public rxx {
  str err;
  void mknull () { err = "uninitialized"; rxx::mknull (); }
public:
  rrxx () { mknull (); }
  explicit rrxx (const char *pat, const char *opt = "")
    { err = init (pat, opt); }
  rrxx (const rxx &r) { err = NULL; copy (r); }
  rrxx (const rrxx &r) { err = r.err; copy (r); }
  bool compile (const char *pat, const char *opt = "")
    { this->~rrxx (); mknull (); err = init (pat, opt); return !err; }
  const str &geterr () const { return err; }
};

inline rxx::matchresult
operator/ (const str &s, rxx &r)
{
  return r.search (s);
}

class strstrmatch {
  const str &s;
  const char *const p;
protected:
  mutable const char *o;
public:
  strstrmatch (const str &s, const char *p)
    : s (s), p (p), o ("") {}
  operator bool () const
    { rxx r (p, o); return r.search (s); }
  operator str () const
    { rxx r (p, o); return r.search (s)[0]; }
  str operator[] (ptrdiff_t n) const
    { rxx r (p, o); return r.search (s)[n]; }
};
class strstroptmatch : public strstrmatch {
public:
  strstroptmatch (const str &s, const char *p)
    : strstrmatch (s, p) {}
  const strstrmatch &operator/ (const char *opt) const
    { o = opt; return *this; }
};

inline strstroptmatch
operator/ (const str &s, const char *p)
{
  return strstroptmatch (s, p);
}

int split (vec<str> *out, rxx pat, str expr, size_t lim = (size_t) -1);
str join (str sep, const vec<str> &v);

#endif /* !_RXX_H_ */