File: qaregexp.cpp

package info (click to toggle)
regexplorer 0.1.3-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 332 kB
  • ctags: 222
  • sloc: cpp: 1,166; perl: 99; makefile: 45; sh: 32
file content (340 lines) | stat: -rw-r--r-- 7,544 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// 
// $Id: qaregexp.cpp,v 1.14 1999/08/16 00:49:44 amos Exp $
//
// Implementation of QaRegExp class
//
// Jan Borsodi <amos@abn.hibu.no>
// Created on: <14-Jul-1999 19:14:57 amos>
//


#include "qaregexp.hpp"
#include "qaregexperror.hpp"
#include <qstring.h>
#include <qstringlist.h>
#include <regex.h>

/*!
  \class QaRegExp qaregexp.hpp
  \brief Matches Regular Expressions against strings.

  \warning This version does not support unicode strings, they are converted to ascii before being used.

  This class uses the system calls reg_comp and reg_exec, see the man pages for regex for more information
  on regular expressions.

  Example:
  \code
  // Match for <text> = <text>
  QaRegExp reg( "([a-zA-Z]+)[ \t]*=[ \t]*([a-zA-Z]+)" );

  // Now match the string
  if ( reg.match( "RegExpTest = True" ) )
  {
      QaRegExpMatch m = reg.matches();

      // Now print out all matches
      for ( QStringList::ConstIterator it = m.toString().begin(); it != m.toString().end(); ++it )
      {
          qDebug( "\"%s\"", (*it).ascii() );
      }
  }

  // Or use it like this
  if ( reg.match( "RegExpTest = True", 2 ) )
      qDebug( "%s", reg.matchString( 2 ).ascii() );

  \endcode
*/

/*!
  Creates a regexp query.
  \param r The string to use for regular expression matching.
  \param c True means match case sensitive.
*/

QaRegExp::QaRegExp( const QString &r, bool c )
{
    Case = c;
    Alloced = false;
    Subs = true;
    Flags = REG_EXTENDED;
    RegString = r;
    compileRegExp();
}

QaRegExp::QaRegExp( const QaRegExp &r )
{
    RegString = r.RegString;
    String = r.String;
    Flags = r.Flags;
    Case = r.Case;
    Subs = r.Subs ;
    Matches = r.Matches;
    compileRegExp();
}

/*!
  Destroys the object
*/

QaRegExp::~QaRegExp()
{
    regfree( &RegComp );
}

/*!
  \return True if matching is done case sensitive.
*/

bool QaRegExp::caseSensitive() const
{
    return Case;
}

/*!
  Sets the case sensitivity flag.
*/

void QaRegExp::setCaseSensitive( bool s )
{
    if ( s == Case )
        return;
    if ( s == false )
        Flags |= REG_ICASE;
    else
        Flags -= Flags & REG_ICASE;
    Case = s;
    compileRegExp();
}

void QaRegExp::setSubReg( bool b )
{
    if ( b == Subs )
        return;
    if ( b == false )
        Flags |= REG_NOSUB;
    else
        Flags -= Flags & REG_NOSUB;
    Subs = b;
    compileRegExp();
}


/*!
  Matches a string against the regexp on the given sub expression.
  \param s The string to match on.
  \param sub Which sub expression should be checked, 0 is default and means the whole regexp.
  \return True if the subexpression matched.
*/

bool QaRegExp::match( const QString &s, int sub = 0 )
{
    setSubReg( true );
    regmatch_t *tmp = new regmatch_t[NumSub + 1];
    for ( int i = 0; i < NumSub + 1; i++ )
    {
        tmp[i].rm_so = -1;
        tmp[i].rm_eo = -1;
    }
    String = s;
    const char *str = String.ascii();
    if ( str == 0 )
        str = "";
    int b = regexec( &RegComp, str, NumSub + 1, tmp, 0 );
    Matches = QaRegExpMatch( String, tmp, NumSub + 1 );
    delete []tmp;
    if ( sub == 0 )
        return b == 0;
    else
    {
        return ( tmp[sub].rm_so != -1 );
    }
}

/*!
  Matches a string against the regexp on the given sub expression, will match as
  many times as possible and extracting the wanted subexpression every time it
  matches.
  \param s The string to match on.
  \param sub Which sub expression should be checked, 0 is default and means the whole regexp.
  \return Number of matches.
*/

int QaRegExp::matchMultiple( const QString &s, int sub = 0 )
{
    setSubReg( true );
    regmatch_t *tmp = new regmatch_t[NumSub + 1];
    for ( int i = 0; i < NumSub + 1; i++ )
    {
        tmp[i].rm_so = -1;
        tmp[i].rm_eo = -1;
    }
    String = s;
    QString right = s;

    int err, start, count;
    start = 0;
    count = 0;
    bool ok = true;
    Matches = QaRegExpMatch( String );
    while ( ok )
    {
        const char *str = right.ascii();
        if ( str == 0 )
            str = "";
        if ( (err = regexec( &RegComp, str, NumSub + 1, tmp, 0 ) ) == 0 )
        {
            Matches.appendMatch( QaRegExpRange( tmp[sub].rm_so + start, tmp[sub].rm_eo + start ) );
            if ( tmp[0].rm_eo == tmp[0].rm_so )
                start++;
            else
                start += tmp[0].rm_eo;
            if ( tmp[0].rm_eo >= 0 )
            {
                if ( tmp[0].rm_eo == tmp[0].rm_so )
                {
                    if ( (unsigned int)tmp[0].rm_eo + 1 < right.length() )
                        right = right.mid( tmp[0].rm_eo + 1 );
                    else
                        ok = false;
                }
                else
                {
                    if ( (unsigned int)tmp[0].rm_eo < right.length() )
                        right = right.mid( tmp[0].rm_eo );
                    else
                        ok = false;
                }
            }
            else
                ok = false;
            count++;
        }
        else
            ok = false;
    }
    delete []tmp;
    return count;
}

/*!
  Splits up the string using the regexp string as the delimiter.
  \param s The string to split.
*/

void QaRegExp::split( const QString &s )
{
    setSubReg( true );
    regmatch_t *tmp = new regmatch_t[NumSub + 1];
    String = s;
    QString right = s;
    Matches = QaRegExpMatch( String );
    int err, count, start;
    start = 0;
    count = 0;
    while ( ( err = regexec( &RegComp, right.ascii(), NumSub + 1, tmp, 0 ) ) == 0 )
    {
        Matches.appendMatch( QaRegExpRange( start, tmp[0].rm_so + start ) );
        start += tmp[0].rm_eo;
        count++;
        right = right.mid(tmp[0].rm_eo);
    }
    Matches.appendMatch( QaRegExpRange( start, String.length()) );
    delete []tmp;
}

/*!
  \return The string that matched the sub expression.
*/

QString QaRegExp::matchString( int sub = 0 )
{
    return Matches.toString(sub);
}

/*!
  \return A list of sub expression strings.
*/

QStringList QaRegExp::matchStrings()
{
    return Matches.toString();
}

/*!
  \return The start offset of the matched sub expression.
*/

int QaRegExp::matchBeginning( int sub = 0 ) const
{
    return Matches[sub].start();
}

/*!
  \return The start offset of the matched sub expression.
*/

int QaRegExp::matchEnd( int sub = 0 ) const
{
    return Matches[sub].end();
}

/*!
  \return A class with all matching information.
*/

const QaRegExpMatch &QaRegExp::matches() const
{
    return Matches;
}

void QaRegExp::compileRegExp()
{
    if ( Alloced )
    {
        regfree( &RegComp );
        Alloced = true;
    }
    int err = regcomp( &RegComp, RegString.ascii(), Flags );
    if ( err != 0 )
    {
        char *tmp;
        int size = regerror( err, &RegComp, 0, 0 );
        tmp = new char[size];
        regerror( err, &RegComp, tmp, size );
//          qDebug( "Error compiling regexp: %s", tmp );
        QString s;
        s.sprintf( "RegExp error: %s", tmp );
        delete []tmp;
        throw QaRegExpError( err, s );
    }
    NumSub = RegComp.re_nsub;
}

/*!
  \return The subexpression count, excluding the master expression.
*/

int QaRegExp::subCount() const
{
    return NumSub;
}

QaRegExp &QaRegExp::operator =( const QaRegExp &r )
{
    RegString = r.RegString;
    String = r.String;
    Flags = r.Flags;
    Case = r.Case;
    Subs = r.Subs;
    Matches = r.Matches;
    compileRegExp();
    return *this;
}

void QaRegExp::setExpression( const QString &r )
{
    RegString = r;
    compileRegExp();
}