2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
16 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC.
20 * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "ContentSearchUtils.h"
34 #include "InspectorValues.h"
35 #include "RegularExpression.h"
40 namespace ContentSearchUtils {
43 // This should be kept the same as the one in front-end/utilities.js
44 static const char regexSpecialCharacters[] = "[](){}+-*.,?\\^$|";
47 static String createSearchRegexSource(const String& text)
50 const UChar* characters = text.characters();
51 String specials(regexSpecialCharacters);
53 for (unsigned i = 0; i < text.length(); i++) {
54 if (specials.find(characters[i]) != notFound)
56 result.append(characters[i]);
62 static Vector<pair<int, String> > getRegularExpressionMatchesByLines(const RegularExpression& regex, const String& text)
64 Vector<pair<int, String> > result;
70 while (start < text.length()) {
71 size_t lineEnd = text.find('\n', start);
72 if (lineEnd == notFound)
73 lineEnd = text.length();
77 String line = text.substring(start, lineEnd - start);
78 if (line.endsWith("\r\n"))
79 line = line.left(line.length() - 2);
80 if (line.endsWith("\n"))
81 line = line.left(line.length() - 1);
84 if (regex.match(line, 0, &matchLength) != -1)
85 result.append(pair<int, String>(lineNumber, line));
93 static PassRefPtr<InspectorObject> buildObjectForSearchMatch(int lineNumber, String lineContent)
95 RefPtr<InspectorObject> result = InspectorObject::create();
96 result->setNumber("lineNumber", lineNumber);
97 result->setString("lineContent", lineContent);
102 RegularExpression createSearchRegex(const String& query, bool caseSensitive, bool isRegex)
104 String regexSource = isRegex ? query : createSearchRegexSource(query);
105 return RegularExpression(regexSource, caseSensitive ? TextCaseSensitive : TextCaseInsensitive);
108 int countRegularExpressionMatches(const RegularExpression& regex, const String& content)
110 if (content.isEmpty())
117 while ((position = regex.match(content, start, &matchLength)) != -1) {
118 if (start >= content.length())
122 start = position + 1;
127 PassRefPtr<InspectorArray> searchInTextByLines(const String& text, const String& query, const bool caseSensitive, const bool isRegex)
129 RefPtr<InspectorArray> result = InspectorArray::create();
131 RegularExpression regex = ContentSearchUtils::createSearchRegex(query, caseSensitive, isRegex);
132 Vector<pair<int, String> > matches = getRegularExpressionMatchesByLines(regex, text);
134 for (Vector<pair<int, String> >::const_iterator it = matches.begin(); it != matches.end(); ++it)
135 result->pushValue(buildObjectForSearchMatch(it->first, it->second));
140 } // namespace ContentSearchUtils
141 } // namespace WebCore
143 #endif // ENABLE(INSPECTOR)