Changeset 561 for trunk/src/corelib/tools/qregexp.cpp
- Timestamp:
- Feb 11, 2010, 11:19:06 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property svn:mergeinfo
set to (toggle deleted branches)
/branches/vendor/nokia/qt/4.6.1 merged eligible /branches/vendor/nokia/qt/current merged eligible /branches/vendor/trolltech/qt/current 3-149
-
Property svn:mergeinfo
set to (toggle deleted branches)
-
trunk/src/corelib/tools/qregexp.cpp
r2 r561 2 2 ** 3 3 ** 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]) 5 6 ** 6 7 ** This file is part of the QtCore module of the Qt Toolkit. … … 21 22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 22 23 ** 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. 27 27 ** 28 28 ** GNU General Public License Usage … … 34 34 ** met: http://www.gnu.org/copyleft/gpl.html. 35 35 ** 36 ** If you are unsure which license is appropriate for your use, please37 ** contact the sales department at qt-sales@nokia.com.36 ** If you 37 ** @nokia.com. 38 38 ** $QT_END_LICENSE$ 39 39 ** … … 53 53 #include "qstringmatcher.h" 54 54 #include "qvector.h" 55 55 56 56 57 #include <limits.h> … … 71 72 #define RXERR_END QT_TRANSLATE_NOOP("QRegExp", "unexpected end") 72 73 #define RXERR_LIMIT QT_TRANSLATE_NOOP("QRegExp", "met internal limit") 74 75 73 76 74 77 /* … … 82 85 83 86 \ingroup tools 84 \ingroup misc85 87 \ingroup shared 86 \mainclass 88 87 89 \keyword regular expression 88 90 … … 521 523 \endtable 522 524 525 526 527 528 523 529 For example if we are in wildcard mode and have strings which 524 530 contain filenames we could identify HTML files with \bold{*.html}. … … 686 692 {tools/regexp}{Regular Expression Example} 687 693 */ 694 695 696 697 688 698 689 699 const int NumBadChars = 64; … … 746 756 Translates a wildcard pattern to an equivalent regular expression 747 757 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 */ 762 static QString wc2rx(const QString &wc_str, const bool enableEscaping) 763 { 764 const int wclen = wc_str.length(); 752 765 QString rx; 753 766 int i = 0; 767 754 768 const QChar *wc = wc_str.unicode(); 769 755 770 while (i < wclen) { 756 QChar c = wc[i++];771 QChar c = wc[i++]; 757 772 switch (c.unicode()) { 773 774 775 776 777 778 779 780 781 782 783 784 785 758 786 case '*': 759 rx += QLatin1String(".*"); 787 if (isEscaping) { 788 rx += QLatin1String("\\*"); 789 isEscaping = false; 790 } else { 791 rx += QLatin1String(".*"); 792 } 760 793 break; 761 794 case '?': 762 rx += QLatin1Char('.'); 795 if (isEscaping) { 796 rx += QLatin1String("\\?"); 797 isEscaping = false; 798 } else { 799 rx += QLatin1Char('.'); 800 } 801 763 802 break; 764 803 case '$': … … 767 806 case '+': 768 807 case '.': 769 case '\\':770 808 case '^': 771 809 case '{': 772 810 case '|': 773 811 case '}': 812 813 814 815 774 816 rx += QLatin1Char('\\'); 775 817 rx += c; 776 818 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('^')) 783 826 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 } 788 835 } 789 836 } 837 838 839 840 841 842 843 844 790 845 break; 846 791 847 default: 848 849 850 851 792 852 rx += c; 793 853 } … … 828 888 }; 829 889 830 bool operator==(const QRegExpEngineKey &key1, const QRegExpEngineKey &key2)890 bool operator==(const QRegExpEngineKey &key1, const QRegExpEngineKey &key2) 831 891 { 832 892 return key1.pattern == key2.pattern && key1.patternSyntax == key2.patternSyntax … … 1024 1084 bool isValid() const { return valid; } 1025 1085 const QString &errorString() const { return yyError; } 1026 int numCaptures() const { return officialncap; }1086 int () const { return officialncap; } 1027 1087 1028 1088 int createState(QChar ch); … … 1117 1177 Qt::CaseSensitivity cs; // case sensitive? 1118 1178 bool greedyQuantifiers; // RegExp2? 1179 1119 1180 #ifndef QT_NO_REGEXP_BACKREF 1120 1181 int nbrefs; // number of back-references … … 1193 1254 1194 1255 friend class Box; 1256 1257 1195 1258 1196 1259 /* … … 1217 1280 int yyLen; // the length of yyIn 1218 1281 int yyCh; // the last character read 1219 Q RegExpCharClass *yyCharClass; // attribute for Tok_CharClass tokens1282 QyyCharClass; // attribute for Tok_CharClass tokens 1220 1283 int yyMinRep; // attribute for Tok_Quantifier 1221 1284 int yyMaxRep; // ditto … … 1233 1296 int yyTok; // the last token read 1234 1297 bool yyMayCapture; // set this to false to disable capturing 1298 1235 1299 1236 1300 friend struct QRegExpMatchState; … … 1253 1317 #endif 1254 1318 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 1255 1344 QRegExpEngine::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) 1257 1347 { 1258 1348 setup(); 1259 1349 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); 1274 1351 1275 1352 valid = (parse(rx.unicode(), rx.length()) == rx.length()); … … 1298 1375 int ncap = eng->ncap; 1299 1376 #ifndef QT_NO_REGEXP_OPTIM 1300 slideTabSize = qMax(eng->minl + 1, 16);1377 lideTabSize = qMax(eng->minl + 1, 16); 1301 1378 #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; 1308 1390 inNextStack = bigArray; 1309 1391 memset(inNextStack, -1, ns * sizeof(int)); … … 1480 1562 #endif 1481 1563 1482 aa.resize(n + 1); 1483 aa[n].a = a; 1484 aa[n].b = b; 1564 QRegExpAnchorAlternation element = {a, b}; 1565 aa.append(element); 1485 1566 return Anchor_Alternation | n; 1486 1567 } … … 2622 2703 } 2623 2704 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 2624 2851 int QRegExpEngine::getChar() 2625 2852 { … … 2714 2941 yyCharClass->addSingleton(0x005f); // '_' 2715 2942 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 2716 3114 #endif 2717 3115 #ifndef QT_NO_REGEXP_ESCAPE … … 2790 3188 yyLen = len; 2791 3189 yyCh = getChar(); 2792 yyCharClass = new QRegExpCharClass;3190 yyCharClass; 2793 3191 yyMinRep = 0; 2794 3192 yyMaxRep = 0; … … 2935 3333 } 2936 3334 if (yyMaxRep < yyMinRep) 2937 qSwap(yyMinRep, yyMaxRep);3335 ); 2938 3336 if (yyCh != '}') 2939 3337 error(RXERR_REPETITION); … … 2984 3382 box.cat(middleBox); 2985 3383 box.cat(rightBox); 2986 delete yyCharClass; 2987 yyCharClass = 0; 3384 yyCharClass.reset(0); 2988 3385 2989 3386 #ifndef QT_NO_REGEXP_CAPTURE … … 3282 3679 if (globalEngineCache()) { 3283 3680 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 { 3287 3688 delete eng; 3689 3288 3690 #else 3289 3691 Q_UNUSED(key); … … 3297 3699 bool initMatchState = !priv->eng; 3298 3700 #if !defined(QT_NO_REGEXP_OPTIM) 3299 if (!priv->eng ) {3701 if (!priv->eng) { 3300 3702 QMutexLocker locker(mutex()); 3301 3703 priv->eng = globalEngineCache()->take(priv->engineKey); … … 3371 3773 globbing". See \l{Wildcard Matching}. 3372 3774 3775 3776 3777 3778 3373 3779 \value FixedString The pattern is a fixed string. This is 3374 3780 equivalent to using the RegExp pattern on a string in 3375 3781 which all metacharacters are escaped using escape(). 3782 3783 3784 3376 3785 3377 3786 \sa setPatternSyntax() … … 3760 4169 #ifndef QT_NO_REGEXP_CAPTURE 3761 4170 /*! 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 3762 4183 Returns the number of captures contained in the regular expression. 3763 4184 */ 3764 int QRegExp:: numCaptures() const4185 int QRegExp::() const 3765 4186 { 3766 4187 prepareEngine(priv); 3767 return priv->eng-> numCaptures();4188 return priv->eng->(); 3768 4189 } 3769 4190 … … 4066 4487 return in; 4067 4488 } 4068 #endif 4489 #endif 4069 4490 4070 4491 QT_END_NAMESPACE
Note:
See TracChangeset
for help on using the changeset viewer.