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" |
ross.kirsling@sony.com | 3d654ba | 2019-03-11 06:20:53 +0000 | [diff] [blame] | 32 | #include "YarrFlags.h" |
fpizlo@apple.com | 280ef00 | 2016-04-05 22:13:16 +0000 | [diff] [blame] | 33 | #include "YarrInterpreter.h" |
joepeck@webkit.org | 40a3ebb | 2014-01-24 06:07:24 +0000 | [diff] [blame] | 34 | #include <wtf/Assertions.h> |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 35 | #include <wtf/BumpPointerAllocator.h> |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 36 | |
joepeck@webkit.org | 40a3ebb | 2014-01-24 06:07:24 +0000 | [diff] [blame] | 37 | namespace JSC { namespace Yarr { |
darin | f4b05b2 | 2006-07-10 05:20:17 +0000 | [diff] [blame] | 38 | |
eric@webkit.org | 302c99f | 2009-09-11 08:43:32 +0000 | [diff] [blame] | 39 | class RegularExpression::Private : public RefCounted<RegularExpression::Private> { |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 40 | public: |
cdumez@apple.com | b5f8ebc | 2022-03-28 17:25:14 +0000 | [diff] [blame] | 41 | static Ref<Private> create(StringView pattern, TextCaseSensitivity caseSensitivity, MultilineMode multilineMode, UnicodeMode unicodeMode) |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 42 | { |
utatane.tea@gmail.com | 2856b09 | 2018-03-07 17:44:06 +0000 | [diff] [blame] | 43 | return adoptRef(*new Private(pattern, caseSensitivity, multilineMode, unicodeMode)); |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 44 | } |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 45 | |
andersca@apple.com | 0f509b1 | 2008-06-04 21:12:16 +0000 | [diff] [blame] | 46 | private: |
cdumez@apple.com | b5f8ebc | 2022-03-28 17:25:14 +0000 | [diff] [blame] | 47 | Private(StringView pattern, TextCaseSensitivity caseSensitivity, MultilineMode multilineMode, UnicodeMode unicodeMode) |
utatane.tea@gmail.com | 2856b09 | 2018-03-07 17:44:06 +0000 | [diff] [blame] | 48 | : m_regExpByteCode(compile(pattern, caseSensitivity, multilineMode, unicodeMode)) |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 49 | { |
| 50 | } |
darin@apple.com | faced26 | 2009-01-12 07:44:27 +0000 | [diff] [blame] | 51 | |
cdumez@apple.com | b5f8ebc | 2022-03-28 17:25:14 +0000 | [diff] [blame] | 52 | std::unique_ptr<JSC::Yarr::BytecodePattern> compile(StringView patternString, TextCaseSensitivity caseSensitivity, MultilineMode multilineMode, UnicodeMode unicodeMode) |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 53 | { |
ross.kirsling@sony.com | 3d654ba | 2019-03-11 06:20:53 +0000 | [diff] [blame] | 54 | OptionSet<JSC::Yarr::Flags> flags; |
msaboff@apple.com | 3f19465 | 2016-03-09 20:11:46 +0000 | [diff] [blame] | 55 | |
| 56 | if (caseSensitivity == TextCaseInsensitive) |
ross.kirsling@sony.com | 3d654ba | 2019-03-11 06:20:53 +0000 | [diff] [blame] | 57 | flags.add(Flags::IgnoreCase); |
msaboff@apple.com | 3f19465 | 2016-03-09 20:11:46 +0000 | [diff] [blame] | 58 | |
| 59 | if (multilineMode == MultilineEnabled) |
ross.kirsling@sony.com | 3d654ba | 2019-03-11 06:20:53 +0000 | [diff] [blame] | 60 | flags.add(Flags::Multiline); |
msaboff@apple.com | 3f19465 | 2016-03-09 20:11:46 +0000 | [diff] [blame] | 61 | |
utatane.tea@gmail.com | 2856b09 | 2018-03-07 17:44:06 +0000 | [diff] [blame] | 62 | if (unicodeMode == UnicodeAwareMode) |
ross.kirsling@sony.com | 3d654ba | 2019-03-11 06:20:53 +0000 | [diff] [blame] | 63 | flags.add(Flags::Unicode); |
utatane.tea@gmail.com | 2856b09 | 2018-03-07 17:44:06 +0000 | [diff] [blame] | 64 | |
utatane.tea@gmail.com | fa8d279 | 2017-12-19 19:16:21 +0000 | [diff] [blame] | 65 | JSC::Yarr::YarrPattern pattern(patternString, flags, m_constructionErrorCode); |
| 66 | if (JSC::Yarr::hasError(m_constructionErrorCode)) { |
cdumez@apple.com | 1392b8b | 2022-03-24 01:40:35 +0000 | [diff] [blame] | 67 | LOG_ERROR("RegularExpression: YARR compile failed with '%s'", JSC::Yarr::errorMessage(m_constructionErrorCode).characters()); |
aroben@apple.com | 71e211b | 2011-05-03 13:54:58 +0000 | [diff] [blame] | 68 | return nullptr; |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | m_numSubpatterns = pattern.m_numSubpatterns; |
| 72 | |
ysuzuki@apple.com | 6137b8e | 2019-06-13 18:47:22 +0000 | [diff] [blame] | 73 | return JSC::Yarr::byteCompile(pattern, &m_regexAllocator, m_constructionErrorCode); |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 74 | } |
| 75 | |
utatane.tea@gmail.com | fa8d279 | 2017-12-19 19:16:21 +0000 | [diff] [blame] | 76 | JSC::Yarr::ErrorCode m_constructionErrorCode { Yarr::ErrorCode::NoError }; |
rmorisset@apple.com | 8e329b6 | 2019-04-15 20:39:11 +0000 | [diff] [blame] | 77 | BumpPointerAllocator m_regexAllocator; |
| 78 | |
| 79 | public: |
| 80 | int lastMatchLength { -1 }; |
| 81 | unsigned m_numSubpatterns; |
| 82 | std::unique_ptr<JSC::Yarr::BytecodePattern> m_regExpByteCode; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
cdumez@apple.com | b5f8ebc | 2022-03-28 17:25:14 +0000 | [diff] [blame] | 85 | RegularExpression::RegularExpression(StringView pattern, TextCaseSensitivity caseSensitivity, MultilineMode multilineMode, UnicodeMode unicodeMode) |
utatane.tea@gmail.com | 2856b09 | 2018-03-07 17:44:06 +0000 | [diff] [blame] | 86 | : d(Private::create(pattern, caseSensitivity, multilineMode, unicodeMode)) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 87 | { |
| 88 | } |
| 89 | |
weinig@apple.com | 5ca5e8b | 2008-02-22 22:30:57 +0000 | [diff] [blame] | 90 | RegularExpression::RegularExpression(const RegularExpression& re) |
| 91 | : d(re.d) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 92 | { |
| 93 | } |
| 94 | |
| 95 | RegularExpression::~RegularExpression() |
| 96 | { |
| 97 | } |
| 98 | |
weinig@apple.com | 5ca5e8b | 2008-02-22 22:30:57 +0000 | [diff] [blame] | 99 | RegularExpression& RegularExpression::operator=(const RegularExpression& re) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 100 | { |
darin@apple.com | faced26 | 2009-01-12 07:44:27 +0000 | [diff] [blame] | 101 | d = re.d; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 102 | return *this; |
| 103 | } |
| 104 | |
cdumez@apple.com | b5f8ebc | 2022-03-28 17:25:14 +0000 | [diff] [blame] | 105 | int RegularExpression::match(StringView str, int startFrom, int* matchLength) const |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 106 | { |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 107 | if (!d->m_regExpByteCode) |
darin@apple.com | faced26 | 2009-01-12 07:44:27 +0000 | [diff] [blame] | 108 | return -1; |
| 109 | |
jmalonzo@webkit.org | 9654c2b | 2008-08-06 12:46:40 +0000 | [diff] [blame] | 110 | if (str.isNull()) |
| 111 | return -1; |
| 112 | |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 113 | int offsetVectorSize = (d->m_numSubpatterns + 1) * 2; |
msaboff@apple.com | 540e7d9 | 2012-02-24 23:55:01 +0000 | [diff] [blame] | 114 | unsigned* offsetVector; |
| 115 | Vector<unsigned, 32> nonReturnedOvector; |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 116 | |
cdumez@apple.com | 8570951 | 2017-07-20 21:06:12 +0000 | [diff] [blame] | 117 | nonReturnedOvector.grow(offsetVectorSize); |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 118 | offsetVector = nonReturnedOvector.data(); |
| 119 | |
| 120 | ASSERT(offsetVector); |
| 121 | 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] | 122 | offsetVector[j] = JSC::Yarr::offsetNoMatch; |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 123 | |
msaboff@apple.com | 540e7d9 | 2012-02-24 23:55:01 +0000 | [diff] [blame] | 124 | unsigned result; |
| 125 | if (str.length() <= INT_MAX) |
benjamin@webkit.org | cff06e4 | 2012-08-30 21:23:51 +0000 | [diff] [blame] | 126 | result = JSC::Yarr::interpret(d->m_regExpByteCode.get(), str, startFrom, offsetVector); |
msaboff@apple.com | 540e7d9 | 2012-02-24 23:55:01 +0000 | [diff] [blame] | 127 | else { |
joepeck@webkit.org | 40a3ebb | 2014-01-24 06:07:24 +0000 | [diff] [blame] | 128 | // 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] | 129 | // can be represented as ints. |
| 130 | result = JSC::Yarr::offsetNoMatch; |
| 131 | } |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 132 | |
msaboff@apple.com | 540e7d9 | 2012-02-24 23:55:01 +0000 | [diff] [blame] | 133 | if (result == JSC::Yarr::offsetNoMatch) { |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 134 | d->lastMatchLength = -1; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 135 | return -1; |
| 136 | } |
darin@apple.com | faced26 | 2009-01-12 07:44:27 +0000 | [diff] [blame] | 137 | |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 138 | // 1 means 1 match; 0 means more than one match. First match is recorded in offsetVector. |
| 139 | d->lastMatchLength = offsetVector[1] - offsetVector[0]; |
weinig@apple.com | 5ca5e8b | 2008-02-22 22:30:57 +0000 | [diff] [blame] | 140 | if (matchLength) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 141 | *matchLength = d->lastMatchLength; |
pvarga@webkit.org | 4ab8255 | 2011-02-09 12:00:56 +0000 | [diff] [blame] | 142 | return offsetVector[0]; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 143 | } |
| 144 | |
cdumez@apple.com | b5f8ebc | 2022-03-28 17:25:14 +0000 | [diff] [blame] | 145 | int RegularExpression::searchRev(StringView str) const |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 146 | { |
darin@apple.com | faced26 | 2009-01-12 07:44:27 +0000 | [diff] [blame] | 147 | // FIXME: This could be faster if it actually searched backwards. |
| 148 | // Instead, it just searches forwards, multiple times until it finds the last match. |
| 149 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 150 | int start = 0; |
| 151 | int pos; |
| 152 | int lastPos = -1; |
| 153 | int lastMatchLength = -1; |
| 154 | do { |
| 155 | int matchLength; |
| 156 | pos = match(str, start, &matchLength); |
| 157 | if (pos >= 0) { |
weinig@apple.com | 5ca5e8b | 2008-02-22 22:30:57 +0000 | [diff] [blame] | 158 | if (pos + matchLength > lastPos + lastMatchLength) { |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 159 | // replace last match if this one is later and not a subset of the last match |
| 160 | lastPos = pos; |
| 161 | lastMatchLength = matchLength; |
| 162 | } |
| 163 | start = pos + 1; |
| 164 | } |
| 165 | } while (pos != -1); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 166 | d->lastMatchLength = lastMatchLength; |
| 167 | return lastPos; |
| 168 | } |
| 169 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 170 | int RegularExpression::matchedLength() const |
| 171 | { |
| 172 | return d->lastMatchLength; |
| 173 | } |
| 174 | |
cdumez@apple.com | b5f8ebc | 2022-03-28 17:25:14 +0000 | [diff] [blame] | 175 | void replace(String& string, const RegularExpression& target, StringView replacement) |
weinig@apple.com | 5ca5e8b | 2008-02-22 22:30:57 +0000 | [diff] [blame] | 176 | { |
| 177 | int index = 0; |
| 178 | while (index < static_cast<int>(string.length())) { |
| 179 | int matchLength; |
| 180 | index = target.match(string, index, &matchLength); |
| 181 | if (index < 0) |
| 182 | break; |
cdumez@apple.com | 71b40b0 | 2022-04-25 15:25:06 +0000 | [diff] [blame] | 183 | string = makeStringByReplacing(string, index, matchLength, replacement); |
weinig@apple.com | 5ca5e8b | 2008-02-22 22:30:57 +0000 | [diff] [blame] | 184 | index += replacement.length(); |
| 185 | if (!matchLength) |
joepeck@webkit.org | 40a3ebb | 2014-01-24 06:07:24 +0000 | [diff] [blame] | 186 | 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] | 187 | } |
darin | f4b05b2 | 2006-07-10 05:20:17 +0000 | [diff] [blame] | 188 | } |
weinig@apple.com | 5ca5e8b | 2008-02-22 22:30:57 +0000 | [diff] [blame] | 189 | |
tkent@chromium.org | f71b54f | 2013-04-25 22:51:08 +0000 | [diff] [blame] | 190 | bool RegularExpression::isValid() const |
| 191 | { |
gyuyoung.kim@samsung.com | c6ae179 | 2014-11-28 00:51:32 +0000 | [diff] [blame] | 192 | return d->m_regExpByteCode.get(); |
tkent@chromium.org | f71b54f | 2013-04-25 22:51:08 +0000 | [diff] [blame] | 193 | } |
| 194 | |
joepeck@webkit.org | 40a3ebb | 2014-01-24 06:07:24 +0000 | [diff] [blame] | 195 | } } // namespace JSC::Yarr |