darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 1 | /* |
darin@apple.com | faced26 | 2009-01-12 07:44:27 +0000 | [diff] [blame] | 2 | * Copyright (C) 2004, 2008, 2009 Apple Inc. All rights reserved. |
jmalonzo@webkit.org | 9654c2b | 2008-08-06 12:46:40 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008 Collabora Ltd. |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 4 | * Copyright (C) 2011 Peter Varga (pvarga@webkit.org), University of Szeged |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
mjs@apple.com | 9204733 | 2014-03-15 04:08:27 +0000 | [diff] [blame] | 15 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 16 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
mjs@apple.com | 9204733 | 2014-03-15 04:08:27 +0000 | [diff] [blame] | 18 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 19 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
joepeck@webkit.org | 40a3ebb | 2014-01-24 06:07:24 +0000 | [diff] [blame] | 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | #include "config.h" |
| 29 | #include "RegularExpression.h" |
darin | f4b05b2 | 2006-07-10 05:20:17 +0000 | [diff] [blame] | 30 | |
joepeck@webkit.org | 40a3ebb | 2014-01-24 06:07:24 +0000 | [diff] [blame] | 31 | #include "Yarr.h" |
fpizlo@apple.com | 280ef00 | 2016-04-05 22:13:16 +0000 | [diff] [blame] | 32 | #include "YarrInterpreter.h" |
joepeck@webkit.org | 40a3ebb | 2014-01-24 06:07:24 +0000 | [diff] [blame] | 33 | #include <wtf/Assertions.h> |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 34 | #include <wtf/BumpPointerAllocator.h> |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 35 | |
joepeck@webkit.org | 40a3ebb | 2014-01-24 06:07:24 +0000 | [diff] [blame] | 36 | namespace JSC { namespace Yarr { |
darin | f4b05b2 | 2006-07-10 05:20:17 +0000 | [diff] [blame] | 37 | |
eric@webkit.org | 302c99f | 2009-09-11 08:43:32 +0000 | [diff] [blame] | 38 | class RegularExpression::Private : public RefCounted<RegularExpression::Private> { |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 39 | public: |
utatane.tea@gmail.com | 2856b09 | 2018-03-07 17:44:06 +0000 | [diff] [blame] | 40 | static Ref<Private> create(const String& pattern, TextCaseSensitivity caseSensitivity, MultilineMode multilineMode, UnicodeMode unicodeMode) |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 41 | { |
utatane.tea@gmail.com | 2856b09 | 2018-03-07 17:44:06 +0000 | [diff] [blame] | 42 | return adoptRef(*new Private(pattern, caseSensitivity, multilineMode, unicodeMode)); |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 43 | } |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 44 | |
utatane.tea@gmail.com | fa8d279 | 2017-12-19 19:16:21 +0000 | [diff] [blame] | 45 | int lastMatchLength { -1 }; |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 46 | |
| 47 | unsigned m_numSubpatterns; |
gyuyoung.kim@samsung.com | c6ae179 | 2014-11-28 00:51:32 +0000 | [diff] [blame] | 48 | std::unique_ptr<JSC::Yarr::BytecodePattern> m_regExpByteCode; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 49 | |
andersca@apple.com | 0f509b1 | 2008-06-04 21:12:16 +0000 | [diff] [blame] | 50 | private: |
utatane.tea@gmail.com | 2856b09 | 2018-03-07 17:44:06 +0000 | [diff] [blame] | 51 | Private(const String& pattern, TextCaseSensitivity caseSensitivity, MultilineMode multilineMode, UnicodeMode unicodeMode) |
| 52 | : m_regExpByteCode(compile(pattern, caseSensitivity, multilineMode, unicodeMode)) |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 53 | { |
| 54 | } |
darin@apple.com | faced26 | 2009-01-12 07:44:27 +0000 | [diff] [blame] | 55 | |
utatane.tea@gmail.com | 2856b09 | 2018-03-07 17:44:06 +0000 | [diff] [blame] | 56 | std::unique_ptr<JSC::Yarr::BytecodePattern> compile(const String& patternString, TextCaseSensitivity caseSensitivity, MultilineMode multilineMode, UnicodeMode unicodeMode) |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 57 | { |
msaboff@apple.com | 3f19465 | 2016-03-09 20:11:46 +0000 | [diff] [blame] | 58 | RegExpFlags flags = NoFlags; |
| 59 | |
| 60 | if (caseSensitivity == TextCaseInsensitive) |
| 61 | flags = static_cast<RegExpFlags>(flags | FlagIgnoreCase); |
| 62 | |
| 63 | if (multilineMode == MultilineEnabled) |
| 64 | flags = static_cast<RegExpFlags>(flags | FlagMultiline); |
| 65 | |
utatane.tea@gmail.com | 2856b09 | 2018-03-07 17:44:06 +0000 | [diff] [blame] | 66 | if (unicodeMode == UnicodeAwareMode) |
| 67 | flags = static_cast<RegExpFlags>(flags | FlagUnicode); |
| 68 | |
utatane.tea@gmail.com | fa8d279 | 2017-12-19 19:16:21 +0000 | [diff] [blame] | 69 | JSC::Yarr::YarrPattern pattern(patternString, flags, m_constructionErrorCode); |
| 70 | if (JSC::Yarr::hasError(m_constructionErrorCode)) { |
| 71 | LOG_ERROR("RegularExpression: YARR compile failed with '%s'", JSC::Yarr::errorMessage(m_constructionErrorCode)); |
aroben@apple.com | 71e211b | 2011-05-03 13:54:58 +0000 | [diff] [blame] | 72 | return nullptr; |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | m_numSubpatterns = pattern.m_numSubpatterns; |
| 76 | |
| 77 | return JSC::Yarr::byteCompile(pattern, &m_regexAllocator); |
| 78 | } |
| 79 | |
| 80 | BumpPointerAllocator m_regexAllocator; |
utatane.tea@gmail.com | fa8d279 | 2017-12-19 19:16:21 +0000 | [diff] [blame] | 81 | JSC::Yarr::ErrorCode m_constructionErrorCode { Yarr::ErrorCode::NoError }; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
utatane.tea@gmail.com | 2856b09 | 2018-03-07 17:44:06 +0000 | [diff] [blame] | 84 | RegularExpression::RegularExpression(const String& pattern, TextCaseSensitivity caseSensitivity, MultilineMode multilineMode, UnicodeMode unicodeMode) |
| 85 | : d(Private::create(pattern, caseSensitivity, multilineMode, unicodeMode)) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 86 | { |
| 87 | } |
| 88 | |
weinig@apple.com | 5ca5e8b | 2008-02-22 22:30:57 +0000 | [diff] [blame] | 89 | RegularExpression::RegularExpression(const RegularExpression& re) |
| 90 | : d(re.d) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 91 | { |
| 92 | } |
| 93 | |
| 94 | RegularExpression::~RegularExpression() |
| 95 | { |
| 96 | } |
| 97 | |
weinig@apple.com | 5ca5e8b | 2008-02-22 22:30:57 +0000 | [diff] [blame] | 98 | RegularExpression& RegularExpression::operator=(const RegularExpression& re) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 99 | { |
darin@apple.com | faced26 | 2009-01-12 07:44:27 +0000 | [diff] [blame] | 100 | d = re.d; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 101 | return *this; |
| 102 | } |
| 103 | |
weinig@apple.com | 5ca5e8b | 2008-02-22 22:30:57 +0000 | [diff] [blame] | 104 | int RegularExpression::match(const String& str, int startFrom, int* matchLength) const |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 105 | { |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 106 | if (!d->m_regExpByteCode) |
darin@apple.com | faced26 | 2009-01-12 07:44:27 +0000 | [diff] [blame] | 107 | return -1; |
| 108 | |
jmalonzo@webkit.org | 9654c2b | 2008-08-06 12:46:40 +0000 | [diff] [blame] | 109 | if (str.isNull()) |
| 110 | return -1; |
| 111 | |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 112 | int offsetVectorSize = (d->m_numSubpatterns + 1) * 2; |
msaboff@apple.com | 540e7d9 | 2012-02-24 23:55:01 +0000 | [diff] [blame] | 113 | unsigned* offsetVector; |
| 114 | Vector<unsigned, 32> nonReturnedOvector; |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 115 | |
cdumez@apple.com | 8570951 | 2017-07-20 21:06:12 +0000 | [diff] [blame] | 116 | nonReturnedOvector.grow(offsetVectorSize); |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 117 | offsetVector = nonReturnedOvector.data(); |
| 118 | |
| 119 | ASSERT(offsetVector); |
| 120 | for (unsigned j = 0, i = 0; i < d->m_numSubpatterns + 1; j += 2, i++) |
msaboff@apple.com | dd6b6fe | 2012-02-25 01:11:14 +0000 | [diff] [blame] | 121 | offsetVector[j] = JSC::Yarr::offsetNoMatch; |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 122 | |
msaboff@apple.com | 540e7d9 | 2012-02-24 23:55:01 +0000 | [diff] [blame] | 123 | unsigned result; |
| 124 | if (str.length() <= INT_MAX) |
benjamin@webkit.org | cff06e4 | 2012-08-30 21:23:51 +0000 | [diff] [blame] | 125 | result = JSC::Yarr::interpret(d->m_regExpByteCode.get(), str, startFrom, offsetVector); |
msaboff@apple.com | 540e7d9 | 2012-02-24 23:55:01 +0000 | [diff] [blame] | 126 | else { |
joepeck@webkit.org | 40a3ebb | 2014-01-24 06:07:24 +0000 | [diff] [blame] | 127 | // This code can't handle unsigned offsets. Limit our processing to strings with offsets that |
msaboff@apple.com | 540e7d9 | 2012-02-24 23:55:01 +0000 | [diff] [blame] | 128 | // can be represented as ints. |
| 129 | result = JSC::Yarr::offsetNoMatch; |
| 130 | } |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 131 | |
msaboff@apple.com | 540e7d9 | 2012-02-24 23:55:01 +0000 | [diff] [blame] | 132 | if (result == JSC::Yarr::offsetNoMatch) { |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 133 | d->lastMatchLength = -1; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 134 | return -1; |
| 135 | } |
darin@apple.com | faced26 | 2009-01-12 07:44:27 +0000 | [diff] [blame] | 136 | |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 137 | // 1 means 1 match; 0 means more than one match. First match is recorded in offsetVector. |
| 138 | d->lastMatchLength = offsetVector[1] - offsetVector[0]; |
weinig@apple.com | 5ca5e8b | 2008-02-22 22:30:57 +0000 | [diff] [blame] | 139 | if (matchLength) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 140 | *matchLength = d->lastMatchLength; |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 141 | return offsetVector[0]; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 142 | } |
| 143 | |
weinig@apple.com | 5ca5e8b | 2008-02-22 22:30:57 +0000 | [diff] [blame] | 144 | int RegularExpression::searchRev(const String& str) const |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 145 | { |
darin@apple.com | faced26 | 2009-01-12 07:44:27 +0000 | [diff] [blame] | 146 | // FIXME: This could be faster if it actually searched backwards. |
| 147 | // Instead, it just searches forwards, multiple times until it finds the last match. |
| 148 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 149 | int start = 0; |
| 150 | int pos; |
| 151 | int lastPos = -1; |
| 152 | int lastMatchLength = -1; |
| 153 | do { |
| 154 | int matchLength; |
| 155 | pos = match(str, start, &matchLength); |
| 156 | if (pos >= 0) { |
weinig@apple.com | 5ca5e8b | 2008-02-22 22:30:57 +0000 | [diff] [blame] | 157 | if (pos + matchLength > lastPos + lastMatchLength) { |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 158 | // replace last match if this one is later and not a subset of the last match |
| 159 | lastPos = pos; |
| 160 | lastMatchLength = matchLength; |
| 161 | } |
| 162 | start = pos + 1; |
| 163 | } |
| 164 | } while (pos != -1); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 165 | d->lastMatchLength = lastMatchLength; |
| 166 | return lastPos; |
| 167 | } |
| 168 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 169 | int RegularExpression::matchedLength() const |
| 170 | { |
| 171 | return d->lastMatchLength; |
| 172 | } |
| 173 | |
weinig@apple.com | 5ca5e8b | 2008-02-22 22:30:57 +0000 | [diff] [blame] | 174 | void replace(String& string, const RegularExpression& target, const String& replacement) |
| 175 | { |
| 176 | int index = 0; |
| 177 | while (index < static_cast<int>(string.length())) { |
| 178 | int matchLength; |
| 179 | index = target.match(string, index, &matchLength); |
| 180 | if (index < 0) |
| 181 | break; |
| 182 | string.replace(index, matchLength, replacement); |
| 183 | index += replacement.length(); |
| 184 | if (!matchLength) |
joepeck@webkit.org | 40a3ebb | 2014-01-24 06:07:24 +0000 | [diff] [blame] | 185 | break; // Avoid infinite loop on 0-length matches, e.g. [a-z]* |
weinig@apple.com | 5ca5e8b | 2008-02-22 22:30:57 +0000 | [diff] [blame] | 186 | } |
darin | f4b05b2 | 2006-07-10 05:20:17 +0000 | [diff] [blame] | 187 | } |
weinig@apple.com | 5ca5e8b | 2008-02-22 22:30:57 +0000 | [diff] [blame] | 188 | |
tkent@chromium.org | f71b54f | 2013-04-25 22:51:08 +0000 | [diff] [blame] | 189 | bool RegularExpression::isValid() const |
| 190 | { |
gyuyoung.kim@samsung.com | c6ae179 | 2014-11-28 00:51:32 +0000 | [diff] [blame] | 191 | return d->m_regExpByteCode.get(); |
tkent@chromium.org | f71b54f | 2013-04-25 22:51:08 +0000 | [diff] [blame] | 192 | } |
| 193 | |
joepeck@webkit.org | 40a3ebb | 2014-01-24 06:07:24 +0000 | [diff] [blame] | 194 | } } // namespace JSC::Yarr |