Ignore:
Timestamp:
Feb 11, 2010, 11:19:06 PM (15 years ago)
Author:
Dmitry A. Kuminov
Message:

trunk: Merged in qt 4.6.1 sources.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/corelib/tools/qregexp.cpp

    r2 r561  
    22**
    33** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
    4 ** Contact: Qt Software Information ([email protected])
     4** All rights reserved.
     5** Contact: Nokia Corporation ([email protected])
    56**
    67** This file is part of the QtCore module of the Qt Toolkit.
     
    2122** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    2223**
    23 ** In addition, as a special exception, Nokia gives you certain
    24 ** additional rights. These rights are described in the Nokia Qt LGPL
    25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
    26 ** package.
     24** In addition, as a special exception, Nokia gives you certain additional
     25** rights.  These rights are described in the Nokia Qt LGPL Exception
     26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
    2727**
    2828** GNU General Public License Usage
     
    3434** met: http://www.gnu.org/copyleft/gpl.html.
    3535**
    36 ** If you are unsure which license is appropriate for your use, please
    37 ** contact the sales department at qt-sales@nokia.com.
     36** If you
     37** @nokia.com.
    3838** $QT_END_LICENSE$
    3939**
     
    5353#include "qstringmatcher.h"
    5454#include "qvector.h"
     55
    5556
    5657#include <limits.h>
     
    7172#define RXERR_END        QT_TRANSLATE_NOOP("QRegExp", "unexpected end")
    7273#define RXERR_LIMIT      QT_TRANSLATE_NOOP("QRegExp", "met internal limit")
     74
     75
    7376
    7477/*
     
    8285
    8386    \ingroup tools
    84     \ingroup misc
    8587    \ingroup shared
    86     \mainclass
     88
    8789    \keyword regular expression
    8890
     
    521523    \endtable
    522524
     525
     526
     527
     528
    523529    For example if we are in wildcard mode and have strings which
    524530    contain filenames we could identify HTML files with \bold{*.html}.
     
    686692        {tools/regexp}{Regular Expression Example}
    687693*/
     694
     695
     696
     697
    688698
    689699const int NumBadChars = 64;
     
    746756  Translates a wildcard pattern to an equivalent regular expression
    747757  pattern (e.g., *.cpp to .*\.cpp).
    748 */
    749 static QString wc2rx(const QString &wc_str)
    750 {
    751     int wclen = wc_str.length();
     758
     759  If enableEscaping is true, it is possible to escape the wildcard
     760  characters with \
     761*/
     762static QString wc2rx(const QString &wc_str, const bool enableEscaping)
     763{
     764    const int wclen = wc_str.length();
    752765    QString rx;
    753766    int i = 0;
     767
    754768    const QChar *wc = wc_str.unicode();
     769
    755770    while (i < wclen) {
    756         QChar c = wc[i++];
     771        QChar c = wc[i++];
    757772        switch (c.unicode()) {
     773
     774
     775
     776
     777
     778
     779
     780
     781
     782
     783
     784
     785
    758786        case '*':
    759             rx += QLatin1String(".*");
     787            if (isEscaping) {
     788                rx += QLatin1String("\\*");
     789                isEscaping = false;
     790            } else {
     791                rx += QLatin1String(".*");
     792            }
    760793            break;
    761794        case '?':
    762             rx += QLatin1Char('.');
     795            if (isEscaping) {
     796                rx += QLatin1String("\\?");
     797                isEscaping = false;
     798            } else {
     799                rx += QLatin1Char('.');
     800            }
     801
    763802            break;
    764803        case '$':
     
    767806        case '+':
    768807        case '.':
    769         case '\\':
    770808        case '^':
    771809        case '{':
    772810        case '|':
    773811        case '}':
     812
     813
     814
     815
    774816            rx += QLatin1Char('\\');
    775817            rx += c;
    776818            break;
    777         case '[':
    778             rx += c;
    779             if (wc[i] == QLatin1Char('^'))
    780                 rx += wc[i++];
    781             if (i < wclen) {
    782                 if (rx[i] == QLatin1Char(']'))
     819         case '[':
     820            if (isEscaping) {
     821                isEscaping = false;
     822                rx += QLatin1String("\\[");
     823            } else {
     824                rx += c;
     825                if (wc[i] == QLatin1Char('^'))
    783826                    rx += wc[i++];
    784                 while (i < wclen && wc[i] != QLatin1Char(']')) {
    785                     if (wc[i] == QLatin1Char('\\'))
    786                         rx += QLatin1Char('\\');
    787                     rx += wc[i++];
     827                if (i < wclen) {
     828                    if (rx[i] == QLatin1Char(']'))
     829                        rx += wc[i++];
     830                    while (i < wclen && wc[i] != QLatin1Char(']')) {
     831                        if (wc[i] == QLatin1Char('\\'))
     832                            rx += QLatin1Char('\\');
     833                        rx += wc[i++];
     834                    }
    788835                }
    789836            }
     837
     838
     839
     840
     841
     842
     843
     844
    790845            break;
     846
    791847        default:
     848
     849
     850
     851
    792852            rx += c;
    793853        }
     
    828888};
    829889
    830 bool operator==(const QRegExpEngineKey &key1, const QRegExpEngineKey &key2)
     890bool operator==(const QRegExpEngineKey &key1, const QRegExpEngineKey &key2)
    831891{
    832892    return key1.pattern == key2.pattern && key1.patternSyntax == key2.patternSyntax
     
    10241084    bool isValid() const { return valid; }
    10251085    const QString &errorString() const { return yyError; }
    1026     int numCaptures() const { return officialncap; }
     1086    int () const { return officialncap; }
    10271087
    10281088    int createState(QChar ch);
     
    11171177    Qt::CaseSensitivity cs; // case sensitive?
    11181178    bool greedyQuantifiers; // RegExp2?
     1179
    11191180#ifndef QT_NO_REGEXP_BACKREF
    11201181    int nbrefs; // number of back-references
     
    11931254
    11941255    friend class Box;
     1256
     1257
    11951258
    11961259    /*
     
    12171280    int yyLen; // the length of yyIn
    12181281    int yyCh; // the last character read
    1219     QRegExpCharClass *yyCharClass; // attribute for Tok_CharClass tokens
     1282    QyyCharClass; // attribute for Tok_CharClass tokens
    12201283    int yyMinRep; // attribute for Tok_Quantifier
    12211284    int yyMaxRep; // ditto
     
    12331296    int yyTok; // the last token read
    12341297    bool yyMayCapture; // set this to false to disable capturing
     1298
    12351299
    12361300    friend struct QRegExpMatchState;
     
    12531317#endif
    12541318
     1319
     1320
     1321
     1322
     1323
     1324
     1325
     1326
     1327
     1328
     1329
     1330
     1331
     1332
     1333
     1334
     1335
     1336
     1337
     1338
     1339
     1340
     1341
     1342
     1343
    12551344QRegExpEngine::QRegExpEngine(const QRegExpEngineKey &key)
    1256     : cs(key.cs), greedyQuantifiers(key.patternSyntax == QRegExp::RegExp2)
     1345    : cs(key.cs), greedyQuantifiers(key.patternSyntax == QRegExp::RegExp2),
     1346      xmlSchemaExtensions(key.patternSyntax == QRegExp::W3CXmlSchema11)
    12571347{
    12581348    setup();
    12591349
    1260     QString rx;
    1261 
    1262     switch (key.patternSyntax) {
    1263     case QRegExp::Wildcard:
    1264 #ifndef QT_NO_REGEXP_WILDCARD
    1265         rx = wc2rx(key.pattern);
    1266 #endif
    1267         break;
    1268     case QRegExp::FixedString:
    1269         rx = QRegExp::escape(key.pattern);
    1270         break;
    1271     default:
    1272         rx = key.pattern;
    1273     }
     1350    QString rx = qt_regexp_toCanonical(key.pattern, key.patternSyntax);
    12741351
    12751352    valid = (parse(rx.unicode(), rx.length()) == rx.length());
     
    12981375    int ncap = eng->ncap;
    12991376#ifndef QT_NO_REGEXP_OPTIM
    1300     slideTabSize = qMax(eng->minl + 1, 16);
     1377    lideTabSize = qMax(eng->minl + 1, 16);
    13011378#else
    1302     slideTabSize = 0;
    1303 #endif
    1304     int numCaptures = eng->numCaptures();
    1305     capturedSize = 2 + 2 * numCaptures;
    1306     bigArray = (int *)realloc(bigArray, ((3 + 4 * ncap) * ns + 4 * ncap + slideTabSize + capturedSize)*sizeof(int));
    1307 
     1379    int newSlideTabSize = 0;
     1380#endif
     1381    int numCaptures = eng->captureCount();
     1382    int newCapturedSize = 2 + 2 * numCaptures;
     1383    bigArray = q_check_ptr((int *)realloc(bigArray, ((3 + 4 * ncap) * ns + 4 * ncap + newSlideTabSize + newCapturedSize)*sizeof(int)));
     1384
     1385    // set all internal variables only _after_ bigArray is realloc'ed
     1386    // to prevent a broken regexp in oom case
     1387
     1388    slideTabSize = newSlideTabSize;
     1389    capturedSize = newCapturedSize;
    13081390    inNextStack = bigArray;
    13091391    memset(inNextStack, -1, ns * sizeof(int));
     
    14801562#endif
    14811563
    1482     aa.resize(n + 1);
    1483     aa[n].a = a;
    1484     aa[n].b = b;
     1564    QRegExpAnchorAlternation element = {a, b};
     1565    aa.append(element);
    14851566    return Anchor_Alternation | n;
    14861567}
     
    26222703}
    26232704
     2705
     2706
     2707
     2708
     2709
     2710
     2711
     2712
     2713
     2714
     2715
     2716
     2717
     2718
     2719
     2720
     2721
     2722
     2723
     2724
     2725
     2726
     2727
     2728
     2729
     2730
     2731
     2732
     2733
     2734
     2735
     2736
     2737
     2738
     2739
     2740
     2741
     2742
     2743
     2744
     2745
     2746
     2747
     2748
     2749
     2750
     2751
     2752
     2753
     2754
     2755
     2756
     2757
     2758
     2759
     2760
     2761
     2762
     2763
     2764
     2765
     2766
     2767
     2768
     2769
     2770
     2771
     2772
     2773
     2774
     2775
     2776
     2777
     2778
     2779
     2780
     2781
     2782
     2783
     2784
     2785
     2786
     2787
     2788
     2789
     2790
     2791
     2792
     2793
     2794
     2795
     2796
     2797
     2798
     2799
     2800
     2801
     2802
     2803
     2804
     2805
     2806
     2807
     2808
     2809
     2810
     2811
     2812
     2813
     2814
     2815
     2816
     2817
     2818
     2819
     2820
     2821
     2822
     2823
     2824
     2825
     2826
     2827
     2828
     2829
     2830
     2831
     2832
     2833
     2834
     2835
     2836
     2837
     2838
     2839
     2840
     2841
     2842
     2843
     2844
     2845
     2846
     2847
     2848
     2849
     2850
    26242851int QRegExpEngine::getChar()
    26252852{
     
    27142941        yyCharClass->addSingleton(0x005f); // '_'
    27152942        return Tok_CharClass;
     2943
     2944
     2945
     2946
     2947
     2948
     2949
     2950
     2951
     2952
     2953
     2954
     2955
     2956
     2957
     2958
     2959
     2960
     2961
     2962
     2963
     2964
     2965
     2966
     2967
     2968
     2969
     2970
     2971
     2972
     2973
     2974
     2975
     2976
     2977
     2978
     2979
     2980
     2981
     2982
     2983
     2984
     2985
     2986
     2987
     2988
     2989
     2990
     2991
     2992
     2993
     2994
     2995
     2996
     2997
     2998
     2999
     3000
     3001
     3002
     3003
     3004
     3005
     3006
     3007
     3008
     3009
     3010
     3011
     3012
     3013
     3014
     3015
     3016
     3017
     3018
     3019
     3020
     3021
     3022
     3023
     3024
     3025
     3026
     3027
     3028
     3029
     3030
     3031
     3032
     3033
     3034
     3035
     3036
     3037
     3038
     3039
     3040
     3041
     3042
     3043
     3044
     3045
     3046
     3047
     3048
     3049
     3050
     3051
     3052
     3053
     3054
     3055
     3056
     3057
     3058
     3059
     3060
     3061
     3062
     3063
     3064
     3065
     3066
     3067
     3068
     3069
     3070
     3071
     3072
     3073
     3074
     3075
     3076
     3077
     3078
     3079
     3080
     3081
     3082
     3083
     3084
     3085
     3086
     3087
     3088
     3089
     3090
     3091
     3092
     3093
     3094
     3095
     3096
     3097
     3098
     3099
     3100
     3101
     3102
     3103
     3104
     3105
     3106
     3107
     3108
     3109
     3110
     3111
     3112
     3113
    27163114#endif
    27173115#ifndef QT_NO_REGEXP_ESCAPE
     
    27903188    yyLen = len;
    27913189    yyCh = getChar();
    2792     yyCharClass = new QRegExpCharClass;
     3190    yyCharClass;
    27933191    yyMinRep = 0;
    27943192    yyMaxRep = 0;
     
    29353333        }
    29363334        if (yyMaxRep < yyMinRep)
    2937             qSwap(yyMinRep, yyMaxRep);
     3335            );
    29383336        if (yyCh != '}')
    29393337            error(RXERR_REPETITION);
     
    29843382    box.cat(middleBox);
    29853383    box.cat(rightBox);
    2986     delete yyCharClass;
    2987     yyCharClass = 0;
     3384    yyCharClass.reset(0);
    29883385
    29893386#ifndef QT_NO_REGEXP_CAPTURE
     
    32823679        if (globalEngineCache()) {
    32833680            QMutexLocker locker(mutex());
    3284             globalEngineCache()->insert(key, eng, 4 + key.pattern.length() / 4);
    3285         }
    3286         else
     3681            QT_TRY {
     3682                globalEngineCache()->insert(key, eng, 4 + key.pattern.length() / 4);
     3683            } QT_CATCH(const std::bad_alloc &) {
     3684                // in case of an exception (e.g. oom), just delete the engine
     3685                delete eng;
     3686            }
     3687        } else {
    32873688            delete eng;
     3689
    32883690#else
    32893691        Q_UNUSED(key);
     
    32973699    bool initMatchState = !priv->eng;
    32983700#if !defined(QT_NO_REGEXP_OPTIM)
    3299     if (!priv->eng) {
     3701    if (!priv->eng) {
    33003702        QMutexLocker locker(mutex());
    33013703        priv->eng = globalEngineCache()->take(priv->engineKey);
     
    33713773    globbing". See \l{Wildcard Matching}.
    33723774
     3775
     3776
     3777
     3778
    33733779    \value FixedString The pattern is a fixed string. This is
    33743780    equivalent to using the RegExp pattern on a string in
    33753781    which all metacharacters are escaped using escape().
     3782
     3783
     3784
    33763785
    33773786    \sa setPatternSyntax()
     
    37604169#ifndef QT_NO_REGEXP_CAPTURE
    37614170/*!
     4171
     4172
     4173
     4174
     4175
     4176
     4177
     4178
     4179
     4180
     4181
     4182
    37624183  Returns the number of captures contained in the regular expression.
    37634184 */
    3764 int QRegExp::numCaptures() const
     4185int QRegExp::() const
    37654186{
    37664187    prepareEngine(priv);
    3767     return priv->eng->numCaptures();
     4188    return priv->eng->();
    37684189}
    37694190
     
    40664487    return in;
    40674488}
    4068 #endif
     4489#endif
    40694490
    40704491QT_END_NAMESPACE
Note: See TracChangeset for help on using the changeset viewer.