1 | // -*- c-basic-offset: 2 -*- |
2 | /* |
3 | * This file is part of the KDE libraries |
4 | * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) |
5 | * |
6 | * This library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Lesser General Public |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2 of the License, or (at your option) any later version. |
10 | * |
11 | * This library is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | * Lesser General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Lesser General Public |
17 | * License along with this library; if not, write to the Free Software |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | * |
20 | */ |
21 | |
22 | #ifndef _KJS_REGEXP_H_ |
23 | #define _KJS_REGEXP_H_ |
24 | |
25 | #include <sys/types.h> |
26 | |
27 | #include "global.h" |
28 | |
29 | #include <config-kjs.h> |
30 | |
31 | #ifdef HAVE_PCREPOSIX |
32 | #include <pcre.h> |
33 | #else // POSIX regex - not so good... |
34 | extern "C" { // bug with some libc5 distributions |
35 | #include <regex.h> |
36 | } |
37 | #endif //HAVE_PCREPOSIX |
38 | |
39 | #if defined _WIN32 || defined _WIN64 |
40 | #undef HAVE_SYS_TIME_H |
41 | #endif |
42 | #if HAVE(SYS_TIME_H) |
43 | #include <sys/resource.h> |
44 | #endif |
45 | |
46 | #include "ustring.h" |
47 | |
48 | namespace KJS { |
49 | |
50 | |
51 | // Represents a strign re-encoded to whatever PCRE can handle |
52 | class RegExpStringContext { |
53 | public: |
54 | explicit RegExpStringContext(const UString& pattern); |
55 | ~RegExpStringContext(); |
56 | |
57 | char* buffer() const { return _buffer; } |
58 | int bufferSize() const { return _bufferSize; } |
59 | int originalPos(int c) const { return _originalPos[c]; } |
60 | |
61 | private: |
62 | // Cached encoding info... |
63 | char* _buffer; |
64 | int* _originalPos; |
65 | int _bufferSize; |
66 | |
67 | void prepareUtf8 (const UString& s); |
68 | void prepareASCII (const UString& s); |
69 | #ifndef NDEBUG |
70 | public: |
71 | UString _originalS; // the original string, used for sanity-checking |
72 | #endif |
73 | }; |
74 | |
75 | class RegExp { |
76 | public: |
77 | enum { None = 0, Global = 1, IgnoreCase = 2, Multiline = 4 }; |
78 | |
79 | explicit RegExp(const UString &pattern, char flags = None); |
80 | ~RegExp(); |
81 | |
82 | char flags() const { return _flags; } |
83 | bool isValid() const { return _valid; } |
84 | |
85 | UString match(const RegExpStringContext& c, const UString& s, bool *error, int i, int *pos = 0, int **ovector = 0); |
86 | unsigned subPatterns() const { return _numSubPatterns; } |
87 | UString pattern() const { return _pat; } |
88 | |
89 | static bool tryGrowingMaxStackSize; |
90 | static bool didIncreaseMaxStackSize; |
91 | #if HAVE(SYS_TIME_H) |
92 | static rlim_t availableStackSize; |
93 | #else |
94 | static int availableStackSize; |
95 | #endif |
96 | private: |
97 | #ifdef HAVE_PCREPOSIX |
98 | pcre *_regex; |
99 | #else |
100 | regex_t _regex; |
101 | #endif |
102 | UString _pat; |
103 | char _flags; |
104 | bool _valid; |
105 | unsigned _numSubPatterns; |
106 | |
107 | RegExp(const RegExp &); |
108 | RegExp &operator=(const RegExp &); |
109 | |
110 | public: |
111 | enum UTF8SupportState { |
112 | Unknown, |
113 | Supported, |
114 | Unsupported |
115 | }; |
116 | |
117 | static UTF8SupportState utf8Support; |
118 | }; |
119 | |
120 | } // namespace |
121 | |
122 | #endif |
123 | |