File: Regex.ec

package info (click to toggle)
ecere-sdk 0.44.15-1
  • links: PTS
  • area: main
  • in suites: sid, stretch
  • size: 97,712 kB
  • ctags: 54,695
  • sloc: ansic: 593,042; makefile: 12,250; yacc: 4,955; lex: 707; objc: 259; python: 252; xml: 102
file content (217 lines) | stat: -rw-r--r-- 5,494 bytes parent folder | download | duplicates (2)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217

#define __restrict
#ifndef __restrict_arr
#define __restrict_arr
#endif

#define uint _uint
#ifdef __WIN32__
#include <sys/types.h>
#endif
#include <regex.h>
#undef uint

#ifdef BUILDING_ECERE_COM
namespace sys;
import "instance"
#else
#ifdef ECERE_STATIC
public import static "ecere"
#else
public import "ecere"
#endif
#endif

public class Regex : struct
{
public:
   property String regex
   {
      set
      {
         if((value || regex) && (value != regex || strcmp(value, regex)))
         {
            if(value)
            {
               delete regex;
               regex = CopyString(value);
               Compile();
            }
            else
               Free();
         }

      }
      get { return regex; }
   }

   property bool caseInsensitive
   {
      set
      {
         if(value != ((compileFlags & REG_ICASE) != 0))
         {
            value ? (compileFlags |= REG_ICASE) : (compileFlags &= ~REG_ICASE);
            if(regex)
               Compile();
         }
      }
      get { return (compileFlags & REG_ICASE) != 0; }
   }

   property bool newLineException
   {
      set
      {
         //if(value != ((compileFlags & REG_NEWLINE) != 0))
         if(value != ((compileFlags & REG_EXTENDED)))
         {
            value ? (compileFlags |= REG_NEWLINE) : (compileFlags &= ~REG_NEWLINE);
            if(regex)
               Compile();
         }
      }
      get { return (compileFlags & REG_NEWLINE) != 0; }
   }

   property bool lineStartException
   {
      set
      {
         if(value != ((executeFlags & REG_NOTBOL) != 0))
            value ? (executeFlags |= REG_NOTBOL) : (executeFlags &= ~REG_NOTBOL);
      }
      get { return (executeFlags & REG_NOTBOL) != 0; }
   }

   property bool lineEndException
   {
      set
      {
         if(value != ((executeFlags & REG_NOTEOL) != 0))
            value ? (executeFlags |= executeFlags | REG_NOTEOL) : (executeFlags &= ~REG_NOTEOL);
      }
      get { return (executeFlags & REG_NOTEOL) != 0; }
   }

   property bool extendedSyntax
   {
      set
      {
         if(value != ((compileFlags & REG_EXTENDED) != 0))
         {
            value ? (compileFlags |= REG_EXTENDED) : (compileFlags &= ~REG_EXTENDED);
            if(regex)
               Compile();
         }
      }
      get { return (compileFlags & REG_EXTENDED) != 0; }
   }

   property bool valid { get { return valid; } }

   property int maxMatchCount
   {
      set
      {
         if(value != maxMatchCount)
         {
            maxMatchCount = value > 0 ? value : 1;
            delete matches;
            matches = new regmatch_t[maxMatchCount];
         }
      }
      get { return maxMatchCount; }
   }

   property int matchCount { get { return matchCount; } }

   char * Match(const String string)
   {
      if(valid)
      {
         int c;
         int result;
         result = regexec(&compiledRegex, string, maxMatchCount, matches, executeFlags);
         if(result == 0) // != REG_NOMATCH
         {
            for(c = 0; c < maxMatchCount; c++)
            {
               if(matches[c].rm_so == -1)
               {
                  matchCount = c;
                  break;
               }
            }
            if(c == maxMatchCount)
               matchCount = maxMatchCount;
            return (char *)string + matches[0].rm_so;
         }
         else
            matchCount = 0;
      }
      return null;
   }

   int GetMatchStartOffset(int matchPos)
   {
      return matches[matchPos].rm_so;
   }

   int GetMatchEndOffset(int matchPos)
   {
      return matches[matchPos].rm_eo;
   }

private:
   bool valid;
   int compileFlags;
   int executeFlags;
   int matchCount;
   int maxMatchCount;
   regex_t compiledRegex;
   regmatch_t * matches;

   ~Regex()
   {
      Free();
   }

   void Free()
   {
      delete matches;
      delete regex;
      regfree(&compiledRegex);
      valid = false;
   }

   void Compile()
   {
      int result;
      regfree(&compiledRegex);
      // compileFlags -- REG_NOSUB --  no substring addressing of matches -- nmatch and pmatch in regexec(...) are ignored if used
      result = regcomp(&compiledRegex, regex, compileFlags/* | REG_NOSUB*/);
      valid = result == 0;
      if(valid && !maxMatchCount)
         property::maxMatchCount = 1;

      // TODO: handle errors?
      // size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size);
      // REG_BADBR      Invalid use of back reference operator.
      // REG_BADPAT     Invalid use of pattern operators such as group or list.
      // REG_BADRPT     Invalid use of repetition operators such as using '*' as the first character.
      // REG_EBRACE     Un-matched brace interval operators.
      // REG_EBRACK     Un-matched bracket list operators.
      // REG_ECOLLATE   Invalid collating element.
      // REG_ECTYPE     Unknown character class name.
      // REG_EEND       Non specific error. This is not defined by POSIX.2.
      // REG_EESCAPE    Trailing backslash.
      // REG_EPAREN     Un-matched parenthesis group operators.
      // REG_ERANGE     Invalid use of the range operator, eg. the ending point of the range occurs prior to the starting point.
      // REG_ESIZE      Compiled regular expression requires a pattern buffer larger than 64Kb. This is not defined by POSIX.2.
      // REG_ESPACE     The regex routines ran out of memory.
      // REG_ESUBREG    Invalid back reference to a subexpression.
   }

   String regex;
}