Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: third_party/WebKit/Source/platform/v8_inspector/V8StringUtil.cpp

Issue 2226863003: [DevTools] Reduce API surface of String16. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebased Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "platform/v8_inspector/V8StringUtil.h" 5 #include "platform/v8_inspector/V8StringUtil.h"
6 6
7 #include "platform/inspector_protocol/String16.h" 7 #include "platform/inspector_protocol/String16.h"
8 #include "platform/v8_inspector/V8InspectorImpl.h" 8 #include "platform/v8_inspector/V8InspectorImpl.h"
9 #include "platform/v8_inspector/V8InspectorSessionImpl.h" 9 #include "platform/v8_inspector/V8InspectorSessionImpl.h"
10 #include "platform/v8_inspector/V8Regex.h" 10 #include "platform/v8_inspector/V8Regex.h"
11 11
12 namespace blink { 12 namespace blink {
13 13
14 namespace { 14 namespace {
15 15
16 String16 findMagicComment(const String16& content, const String16& name, bool mu ltiline) 16 String16 findMagicComment(const String16& content, const String16& name, bool mu ltiline)
17 { 17 {
18 DCHECK(name.find("=") == kNotFound); 18 DCHECK(name.find("=") == String16::kNotFound);
19 unsigned length = content.length(); 19 unsigned length = content.length();
20 unsigned nameLength = name.length(); 20 unsigned nameLength = name.length();
21 21
22 size_t pos = length; 22 size_t pos = length;
23 size_t equalSignPos = 0; 23 size_t equalSignPos = 0;
24 size_t closingCommentPos = 0; 24 size_t closingCommentPos = 0;
25 while (true) { 25 while (true) {
26 pos = content.reverseFind(name, pos); 26 pos = content.reverseFind(name, pos);
27 if (pos == kNotFound) 27 if (pos == String16::kNotFound)
28 return String16(); 28 return String16();
29 29
30 // Check for a /\/[\/*][@#][ \t]/ regexp (length of 4) before found name . 30 // Check for a /\/[\/*][@#][ \t]/ regexp (length of 4) before found name .
31 if (pos < 4) 31 if (pos < 4)
32 return String16(); 32 return String16();
33 pos -= 4; 33 pos -= 4;
34 if (content[pos] != '/') 34 if (content[pos] != '/')
35 continue; 35 continue;
36 if ((content[pos + 1] != '/' || multiline) 36 if ((content[pos + 1] != '/' || multiline)
37 && (content[pos + 1] != '*' || !multiline)) 37 && (content[pos + 1] != '*' || !multiline))
38 continue; 38 continue;
39 if (content[pos + 2] != '#' && content[pos + 2] != '@') 39 if (content[pos + 2] != '#' && content[pos + 2] != '@')
40 continue; 40 continue;
41 if (content[pos + 3] != ' ' && content[pos + 3] != '\t') 41 if (content[pos + 3] != ' ' && content[pos + 3] != '\t')
42 continue; 42 continue;
43 equalSignPos = pos + 4 + nameLength; 43 equalSignPos = pos + 4 + nameLength;
44 if (equalSignPos < length && content[equalSignPos] != '=') 44 if (equalSignPos < length && content[equalSignPos] != '=')
45 continue; 45 continue;
46 if (multiline) { 46 if (multiline) {
47 closingCommentPos = content.find("*/", equalSignPos + 1); 47 closingCommentPos = content.find("*/", equalSignPos + 1);
48 if (closingCommentPos == kNotFound) 48 if (closingCommentPos == String16::kNotFound)
49 return String16(); 49 return String16();
50 } 50 }
51 51
52 break; 52 break;
53 } 53 }
54 54
55 DCHECK(equalSignPos); 55 DCHECK(equalSignPos);
56 DCHECK(!multiline || closingCommentPos); 56 DCHECK(!multiline || closingCommentPos);
57 size_t urlPos = equalSignPos + 1; 57 size_t urlPos = equalSignPos + 1;
58 String16 match = multiline 58 String16 match = multiline
59 ? content.substring(urlPos, closingCommentPos - urlPos) 59 ? content.substring(urlPos, closingCommentPos - urlPos)
60 : content.substring(urlPos); 60 : content.substring(urlPos);
61 61
62 size_t newLine = match.find("\n"); 62 size_t newLine = match.find("\n");
63 if (newLine != kNotFound) 63 if (newLine != String16::kNotFound)
64 match = match.substring(0, newLine); 64 match = match.substring(0, newLine);
65 match = match.stripWhiteSpace(); 65 match = match.stripWhiteSpace();
66 66
67 String16 disallowedChars("\"' \t");
68 for (unsigned i = 0; i < match.length(); ++i) { 67 for (unsigned i = 0; i < match.length(); ++i) {
69 if (disallowedChars.find(match[i]) != kNotFound) 68 UChar c = match[i];
69 if (c == '"' || c == '\'' || c == ' ' || c == '\t')
70 return ""; 70 return "";
71 } 71 }
72 72
73 return match; 73 return match;
74 } 74 }
75 75
76 String16 createSearchRegexSource(const String16& text) 76 String16 createSearchRegexSource(const String16& text)
77 { 77 {
78 String16Builder result; 78 String16Builder result;
79 String16 specials("[](){}+-*.,?\\^$|");
80 79
81 for (unsigned i = 0; i < text.length(); i++) { 80 for (unsigned i = 0; i < text.length(); i++) {
82 if (specials.find(text[i]) != kNotFound) 81 UChar c = text[i];
82 if (c == '[' || c == ']' || c == '(' || c == ')' || c == '{' || c == '}'
83 || c == '+' || c == '-' || c == '*' || c == '.' || c == ',' || c == '?'
84 || c == '\\' || c == '^' || c == '$' || c == '|') {
83 result.append('\\'); 85 result.append('\\');
86 }
84 result.append(text[i]); 87 result.append(text[i]);
85 } 88 }
86 89
87 return result.toString(); 90 return result.toString();
88 } 91 }
89 92
90 std::unique_ptr<std::vector<unsigned>> lineEndings(const String16& text) 93 std::unique_ptr<std::vector<unsigned>> lineEndings(const String16& text)
91 { 94 {
92 std::unique_ptr<std::vector<unsigned>> result(new std::vector<unsigned>()); 95 std::unique_ptr<std::vector<unsigned>> result(new std::vector<unsigned>());
93 96
97 const String16 lineEndString = "\n";
94 unsigned start = 0; 98 unsigned start = 0;
95 while (start < text.length()) { 99 while (start < text.length()) {
96 size_t lineEnd = text.find('\n', start); 100 size_t lineEnd = text.find(lineEndString, start);
97 if (lineEnd == kNotFound) 101 if (lineEnd == String16::kNotFound)
98 break; 102 break;
99 103
100 result->push_back(static_cast<unsigned>(lineEnd)); 104 result->push_back(static_cast<unsigned>(lineEnd));
101 start = lineEnd + 1; 105 start = lineEnd + 1;
102 } 106 }
103 result->push_back(text.length()); 107 result->push_back(text.length());
104 108
105 return result; 109 return result;
106 } 110 }
107 111
108 std::vector<std::pair<int, String16>> scriptRegexpMatchesByLines(const V8Regex& regex, const String16& text) 112 std::vector<std::pair<int, String16>> scriptRegexpMatchesByLines(const V8Regex& regex, const String16& text)
109 { 113 {
110 std::vector<std::pair<int, String16>> result; 114 std::vector<std::pair<int, String16>> result;
111 if (text.isEmpty()) 115 if (text.isEmpty())
112 return result; 116 return result;
113 117
114 std::unique_ptr<std::vector<unsigned>> endings(lineEndings(text)); 118 std::unique_ptr<std::vector<unsigned>> endings(lineEndings(text));
115 unsigned size = endings->size(); 119 unsigned size = endings->size();
116 unsigned start = 0; 120 unsigned start = 0;
117 for (unsigned lineNumber = 0; lineNumber < size; ++lineNumber) { 121 for (unsigned lineNumber = 0; lineNumber < size; ++lineNumber) {
118 unsigned lineEnd = endings->at(lineNumber); 122 unsigned lineEnd = endings->at(lineNumber);
119 String16 line = text.substring(start, lineEnd - start); 123 String16 line = text.substring(start, lineEnd - start);
120 if (line.endsWith('\r')) 124 if (line.length() && line[line.length() - 1] == '\r')
121 line = line.substring(0, line.length() - 1); 125 line = line.substring(0, line.length() - 1);
122 126
123 int matchLength; 127 int matchLength;
124 if (regex.match(line, 0, &matchLength) != -1) 128 if (regex.match(line, 0, &matchLength) != -1)
125 result.push_back(std::pair<int, String16>(lineNumber, line)); 129 result.push_back(std::pair<int, String16>(lineNumber, line));
126 130
127 start = lineEnd + 1; 131 start = lineEnd + 1;
128 } 132 }
129 return result; 133 return result;
130 } 134 }
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 return nullptr; 272 return nullptr;
269 jsonObject->setValue(toProtocolString(propertyName), std::move(prope rtyValue)); 273 jsonObject->setValue(toProtocolString(propertyName), std::move(prope rtyValue));
270 } 274 }
271 return std::move(jsonObject); 275 return std::move(jsonObject);
272 } 276 }
273 NOTREACHED(); 277 NOTREACHED();
274 return nullptr; 278 return nullptr;
275 } 279 }
276 280
277 } // namespace blink 281 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698