File indexing completed on 2024-05-12 15:43:33

0001 /*
0002  *  This file is part of the KDE libraries
0003  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
0004  *
0005  *  This library is free software; you can redistribute it and/or
0006  *  modify it under the terms of the GNU Lesser General Public
0007  *  License as published by the Free Software Foundation; either
0008  *  version 2 of the License, or (at your option) any later version.
0009  *
0010  *  This library is distributed in the hope that it will be useful,
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  *  Lesser General Public License for more details.
0014  *
0015  *  You should have received a copy of the GNU Lesser General Public
0016  *  License along with this library; if not, write to the Free Software
0017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0018  *
0019  */
0020 
0021 #ifndef _KJS_REGEXP_H_
0022 #define _KJS_REGEXP_H_
0023 
0024 #include <sys/types.h>
0025 
0026 #include "global.h"
0027 
0028 #if HAVE_PCREPOSIX
0029 #include <pcre.h>
0030 #else  // POSIX regex - not so good...
0031 extern "C" { // bug with some libc5 distributions
0032 #include <regex.h>
0033 }
0034 #endif //HAVE_PCREPOSIX
0035 
0036 #if defined _WIN32 || defined _WIN64
0037 #undef HAVE_SYS_TIME_H
0038 #endif
0039 #if HAVE(SYS_TIME_H)
0040 #include <sys/resource.h>
0041 #endif
0042 
0043 #include "ustring.h"
0044 
0045 namespace KJS
0046 {
0047 
0048 // Represents a string re-encoded to whatever PCRE can handle
0049 class RegExpStringContext
0050 {
0051 public:
0052     explicit RegExpStringContext(const UString &pattern);
0053     ~RegExpStringContext();
0054 
0055     char *buffer() const
0056     {
0057         return _buffer;
0058     }
0059     int   bufferSize() const
0060     {
0061         return _bufferSize;
0062     }
0063     int   originalPos(int c) const
0064     {
0065         return _originalPos[c];
0066     }
0067 
0068 private:
0069     // Cached encoding info...
0070     char *_buffer;
0071     int  *_originalPos;
0072     int   _bufferSize;
0073 
0074     void prepareUtf8(const UString &s);
0075     void prepareASCII(const UString &s);
0076 #ifndef NDEBUG
0077 public:
0078     UString _originalS; // the original string, used for sanity-checking
0079 #endif
0080 };
0081 
0082 class RegExp
0083 {
0084 public:
0085     enum { None = 0, Global = 1, IgnoreCase = 2, Multiline = 4 };
0086 
0087     explicit RegExp(const UString &pattern, char flags = None);
0088     ~RegExp();
0089 
0090     char flags() const
0091     {
0092         return _flags;
0093     }
0094     bool isValid() const
0095     {
0096         return _valid;
0097     }
0098 
0099     UString match(const RegExpStringContext &c, const UString &s, bool *error, int i, int *pos = nullptr, int **ovector = nullptr);
0100     unsigned subPatterns() const
0101     {
0102         return _numSubPatterns;
0103     }
0104     UString  pattern() const
0105     {
0106         return _pat;
0107     }
0108 
0109     static bool tryGrowingMaxStackSize;
0110     static bool didIncreaseMaxStackSize;
0111 #if HAVE(SYS_TIME_H)
0112     static rlim_t availableStackSize;
0113 #else
0114     static int availableStackSize;
0115 #endif
0116 private:
0117 #if HAVE_PCREPOSIX
0118     pcre *_regex;
0119 #else
0120     regex_t _regex;
0121 #endif
0122     UString _pat;
0123     char _flags;
0124     bool _valid;
0125     unsigned _numSubPatterns;
0126 
0127     RegExp(const RegExp &);
0128     RegExp &operator=(const RegExp &);
0129 
0130 public:
0131     enum UTF8SupportState {
0132         Unknown,
0133         Supported,
0134         Unsupported
0135     };
0136 
0137     static UTF8SupportState utf8Support;
0138 };
0139 
0140 } // namespace
0141 
0142 #endif