Skip to content

Commit 19df0ac

Browse files
HunseopJeongwebkit-commit-queue
authored andcommitted
Use modern for-loops in WebCore/xml.
https://bugs.webkit.org/show_bug.cgi?id=145046 Patch by Hunseop Jeong <hs85.jeong@samsung.com> on 2015-05-19 Reviewed by Chris Dumez. No new tests, no new tests. * xml/XMLHttpRequest.cpp: (WebCore::XMLHttpRequest::uppercaseKnownHTTPMethod): * xml/XPathExpressionNode.cpp: (WebCore::XPath::Expression::setSubexpressions): * xml/XPathFunctions.cpp: (WebCore::XPath::FunSum::evaluate): (WebCore::XPath::populateFunctionMap): * xml/XPathNodeSet.cpp: (WebCore::XPath::NodeSet::traversalSort): * xml/XPathParser.cpp: (WebCore::XPath::populateAxisNamesMap): * xml/XPathPath.cpp: (WebCore::XPath::Filter::evaluate): (WebCore::XPath::LocationPath::evaluate): * xml/XPathPredicate.cpp: (WebCore::XPath::EqTestOp::compare): (WebCore::XPath::Union::evaluate): * xml/XPathStep.cpp: (WebCore::XPath::Step::optimize): (WebCore::XPath::Step::predicatesAreContextListInsensitive): (WebCore::XPath::Step::evaluate): (WebCore::XPath::nodeMatches): * xml/XSLStyleSheetLibxslt.cpp: (WebCore::XSLStyleSheet::~XSLStyleSheet): (WebCore::XSLStyleSheet::isLoading): (WebCore::XSLStyleSheet::clearDocuments): (WebCore::XSLStyleSheet::locateStylesheetSubResource): * xml/XSLTProcessorLibxslt.cpp: (WebCore::xsltParamArrayFromParameterMap): Canonical link: https://commits.webkit.org/163172@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@184566 268f45cc-cd09-0410-ab3c-d52691b4dbfc
1 parent 284be13 commit 19df0ac

11 files changed

Lines changed: 108 additions & 78 deletions

Source/WebCore/ChangeLog

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
1+
2015-05-19 Hunseop Jeong <hs85.jeong@samsung.com>
2+
3+
Use modern for-loops in WebCore/xml.
4+
https://bugs.webkit.org/show_bug.cgi?id=145046
5+
6+
Reviewed by Chris Dumez.
7+
8+
No new tests, no new tests.
9+
10+
* xml/XMLHttpRequest.cpp:
11+
(WebCore::XMLHttpRequest::uppercaseKnownHTTPMethod):
12+
* xml/XPathExpressionNode.cpp:
13+
(WebCore::XPath::Expression::setSubexpressions):
14+
* xml/XPathFunctions.cpp:
15+
(WebCore::XPath::FunSum::evaluate):
16+
(WebCore::XPath::populateFunctionMap):
17+
* xml/XPathNodeSet.cpp:
18+
(WebCore::XPath::NodeSet::traversalSort):
19+
* xml/XPathParser.cpp:
20+
(WebCore::XPath::populateAxisNamesMap):
21+
* xml/XPathPath.cpp:
22+
(WebCore::XPath::Filter::evaluate):
23+
(WebCore::XPath::LocationPath::evaluate):
24+
* xml/XPathPredicate.cpp:
25+
(WebCore::XPath::EqTestOp::compare):
26+
(WebCore::XPath::Union::evaluate):
27+
* xml/XPathStep.cpp:
28+
(WebCore::XPath::Step::optimize):
29+
(WebCore::XPath::Step::predicatesAreContextListInsensitive):
30+
(WebCore::XPath::Step::evaluate):
31+
(WebCore::XPath::nodeMatches):
32+
* xml/XSLStyleSheetLibxslt.cpp:
33+
(WebCore::XSLStyleSheet::~XSLStyleSheet):
34+
(WebCore::XSLStyleSheet::isLoading):
35+
(WebCore::XSLStyleSheet::clearDocuments):
36+
(WebCore::XSLStyleSheet::locateStylesheetSubResource):
37+
* xml/XSLTProcessorLibxslt.cpp:
38+
(WebCore::xsltParamArrayFromParameterMap):
39+
140
2015-05-19 Jon Lee <jonlee@apple.com>
241

342
Improve text rendering of placeholder

Source/WebCore/xml/XMLHttpRequest.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,12 @@ bool XMLHttpRequest::isAllowedHTTPMethod(const String& method)
405405
String XMLHttpRequest::uppercaseKnownHTTPMethod(const String& method)
406406
{
407407
const char* const methods[] = { "DELETE", "GET", "HEAD", "OPTIONS", "POST", "PUT" };
408-
for (unsigned i = 0; i < WTF_ARRAY_LENGTH(methods); ++i) {
409-
if (equalIgnoringCase(method, methods[i])) {
408+
for (auto* value : methods) {
409+
if (equalIgnoringCase(method, value)) {
410410
// Don't bother allocating a new string if it's already all uppercase.
411-
if (method == methods[i])
411+
if (method == value)
412412
break;
413-
return ASCIILiteral(methods[i]);
413+
return ASCIILiteral(value);
414414
}
415415
}
416416
return method;

Source/WebCore/xml/XPathExpressionNode.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ void Expression::setSubexpressions(Vector<std::unique_ptr<Expression>> subexpres
5050
{
5151
ASSERT(m_subexpressions.isEmpty());
5252
m_subexpressions = WTF::move(subexpressions);
53-
for (unsigned i = 0; i < m_subexpressions.size(); ++i) {
54-
m_isContextNodeSensitive |= m_subexpressions[i]->m_isContextNodeSensitive;
55-
m_isContextPositionSensitive |= m_subexpressions[i]->m_isContextPositionSensitive;
56-
m_isContextSizeSensitive |= m_subexpressions[i]->m_isContextSizeSensitive;
53+
for (auto& subexpression : m_subexpressions) {
54+
m_isContextNodeSensitive |= subexpression->m_isContextNodeSensitive;
55+
m_isContextPositionSensitive |= subexpression->m_isContextPositionSensitive;
56+
m_isContextSizeSensitive |= subexpression->m_isContextSizeSensitive;
5757
}
5858
}
5959

Source/WebCore/xml/XPathFunctions.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -634,8 +634,8 @@ Value FunSum::evaluate() const
634634
// To be really compliant, we should sort the node-set, as floating point addition is not associative.
635635
// However, this is unlikely to ever become a practical issue, and sorting is slow.
636636

637-
for (unsigned i = 0; i < nodes.size(); i++)
638-
sum += Value(stringValue(nodes[i])).toNumber();
637+
for (auto& node : nodes)
638+
sum += Value(stringValue(node.get())).toNumber();
639639

640640
return sum;
641641
}
@@ -708,8 +708,8 @@ static void populateFunctionMap(HashMap<String, FunctionMapValue>& functionMap)
708708
{ "true", { createFunctionTrue, 0 } },
709709
};
710710

711-
for (size_t i = 0; i < WTF_ARRAY_LENGTH(functions); ++i)
712-
functionMap.add(functions[i].name, functions[i].function);
711+
for (auto& function : functions)
712+
functionMap.add(function.name, function.function);
713713
}
714714

715715
std::unique_ptr<Function> Function::create(const String& name, unsigned numArguments)

Source/WebCore/xml/XPathNodeSet.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,8 @@ void NodeSet::traversalSort() const
197197

198198
unsigned nodeCount = m_nodes.size();
199199
ASSERT(nodeCount > 1);
200-
for (unsigned i = 0; i < nodeCount; ++i) {
201-
Node* node = m_nodes[i].get();
202-
nodes.add(node);
200+
for (auto& node : m_nodes) {
201+
nodes.add(node.get());
203202
if (node->isAttributeNode())
204203
containsAttributeNodes = true;
205204
}

Source/WebCore/xml/XPathParser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ static void populateAxisNamesMap(HashMap<String, Step::Axis>& axisNames)
9999
{ "preceding-sibling", Step::PrecedingSiblingAxis },
100100
{ "self", Step::SelfAxis }
101101
};
102-
for (unsigned i = 0; i < WTF_ARRAY_LENGTH(axisNameList); ++i)
103-
axisNames.add(axisNameList[i].name, axisNameList[i].axis);
102+
for (auto& axisName : axisNameList)
103+
axisNames.add(axisName.name, axisName.axis);
104104
}
105105

106106
static bool parseAxisName(const String& name, Step::Axis& type)

Source/WebCore/xml/XPathPath.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,16 @@ Value Filter::evaluate() const
5151
nodes.sort();
5252

5353
EvaluationContext& evaluationContext = Expression::evaluationContext();
54-
for (unsigned i = 0; i < m_predicates.size(); i++) {
54+
for (auto& predicate : m_predicates) {
5555
NodeSet newNodes;
5656
evaluationContext.size = nodes.size();
5757
evaluationContext.position = 0;
5858

59-
for (unsigned j = 0; j < nodes.size(); j++) {
60-
Node* node = nodes[j];
61-
59+
for (auto& node : nodes) {
6260
evaluationContext.node = node;
6361
++evaluationContext.position;
6462

65-
if (evaluatePredicate(*m_predicates[i]))
63+
if (evaluatePredicate(*predicate))
6664
newNodes.append(node);
6765
}
6866
nodes = WTF::move(newNodes);
@@ -109,32 +107,30 @@ void LocationPath::evaluate(NodeSet& nodes) const
109107
{
110108
bool resultIsSorted = nodes.isSorted();
111109

112-
for (unsigned i = 0; i < m_steps.size(); i++) {
113-
Step& step = *m_steps[i];
110+
for (auto& step : m_steps) {
114111
NodeSet newNodes;
115112
HashSet<Node*> newNodesSet;
116113

117-
bool needToCheckForDuplicateNodes = !nodes.subtreesAreDisjoint() || (step.axis() != Step::ChildAxis && step.axis() != Step::SelfAxis
118-
&& step.axis() != Step::DescendantAxis && step.axis() != Step::DescendantOrSelfAxis && step.axis() != Step::AttributeAxis);
114+
bool needToCheckForDuplicateNodes = !nodes.subtreesAreDisjoint() || (step->axis() != Step::ChildAxis && step->axis() != Step::SelfAxis
115+
&& step->axis() != Step::DescendantAxis && step->axis() != Step::DescendantOrSelfAxis && step->axis() != Step::AttributeAxis);
119116

120117
if (needToCheckForDuplicateNodes)
121118
resultIsSorted = false;
122119

123120
// This is a simplified check that can be improved to handle more cases.
124-
if (nodes.subtreesAreDisjoint() && (step.axis() == Step::ChildAxis || step.axis() == Step::SelfAxis))
121+
if (nodes.subtreesAreDisjoint() && (step->axis() == Step::ChildAxis || step->axis() == Step::SelfAxis))
125122
newNodes.markSubtreesDisjoint(true);
126123

127-
for (unsigned j = 0; j < nodes.size(); j++) {
124+
for (auto& node : nodes) {
128125
NodeSet matches;
129-
step.evaluate(*nodes[j], matches);
126+
step->evaluate(*node, matches);
130127

131128
if (!matches.isSorted())
132129
resultIsSorted = false;
133130

134-
for (size_t nodeIndex = 0; nodeIndex < matches.size(); ++nodeIndex) {
135-
Node* node = matches[nodeIndex];
136-
if (!needToCheckForDuplicateNodes || newNodesSet.add(node).isNewEntry)
137-
newNodes.append(node);
131+
for (auto& match : matches) {
132+
if (!needToCheckForDuplicateNodes || newNodesSet.add(match.get()).isNewEntry)
133+
newNodes.append(match);
138134
}
139135
}
140136

Source/WebCore/xml/XPathPredicate.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,28 +111,32 @@ bool EqTestOp::compare(const Value& lhs, const Value& rhs) const
111111
// there is a node in the first node-set and a node in the second node-set such that the result of
112112
// performing the comparison on the string-values of the two nodes is true.
113113
const NodeSet& rhsSet = rhs.toNodeSet();
114-
for (unsigned lindex = 0; lindex < lhsSet.size(); ++lindex)
115-
for (unsigned rindex = 0; rindex < rhsSet.size(); ++rindex)
116-
if (compare(stringValue(lhsSet[lindex]), stringValue(rhsSet[rindex])))
114+
for (auto& lhs : lhsSet) {
115+
for (auto& rhs : rhsSet) {
116+
if (compare(stringValue(lhs.get()), stringValue(rhs.get())))
117117
return true;
118+
}
119+
}
118120
return false;
119121
}
120122
if (rhs.isNumber()) {
121123
// If one object to be compared is a node-set and the other is a number, then the comparison will be true
122124
// if and only if there is a node in the node-set such that the result of performing the comparison on the number
123125
// to be compared and on the result of converting the string-value of that node to a number using the number function is true.
124-
for (unsigned lindex = 0; lindex < lhsSet.size(); ++lindex)
125-
if (compare(Value(stringValue(lhsSet[lindex])).toNumber(), rhs))
126+
for (auto& lhs : lhsSet) {
127+
if (compare(Value(stringValue(lhs.get())).toNumber(), rhs))
126128
return true;
129+
}
127130
return false;
128131
}
129132
if (rhs.isString()) {
130133
// If one object to be compared is a node-set and the other is a string, then the comparison will be true
131134
// if and only if there is a node in the node-set such that the result of performing the comparison on
132135
// the string-value of the node and the other string is true.
133-
for (unsigned lindex = 0; lindex < lhsSet.size(); ++lindex)
134-
if (compare(stringValue(lhsSet[lindex]), rhs))
136+
for (auto& lhs : lhsSet) {
137+
if (compare(stringValue(lhs.get()), rhs))
135138
return true;
139+
}
136140
return false;
137141
}
138142
if (rhs.isBoolean()) {
@@ -146,15 +150,17 @@ bool EqTestOp::compare(const Value& lhs, const Value& rhs) const
146150
if (rhs.isNodeSet()) {
147151
const NodeSet& rhsSet = rhs.toNodeSet();
148152
if (lhs.isNumber()) {
149-
for (unsigned rindex = 0; rindex < rhsSet.size(); ++rindex)
150-
if (compare(lhs, Value(stringValue(rhsSet[rindex])).toNumber()))
153+
for (auto& rhs : rhsSet) {
154+
if (compare(lhs, Value(stringValue(rhs.get())).toNumber()))
151155
return true;
156+
}
152157
return false;
153158
}
154159
if (lhs.isString()) {
155-
for (unsigned rindex = 0; rindex < rhsSet.size(); ++rindex)
156-
if (compare(lhs, stringValue(rhsSet[rindex])))
160+
for (auto& rhs : rhsSet) {
161+
if (compare(lhs, stringValue(rhs.get())))
157162
return true;
163+
}
158164
return false;
159165
}
160166
if (lhs.isBoolean())
@@ -236,13 +242,12 @@ Value Union::evaluate() const
236242
const NodeSet& rhsNodes = rhs.toNodeSet();
237243

238244
HashSet<Node*> nodes;
239-
for (size_t i = 0; i < resultSet.size(); ++i)
240-
nodes.add(resultSet[i]);
245+
for (auto& result : resultSet)
246+
nodes.add(result.get());
241247

242-
for (size_t i = 0; i < rhsNodes.size(); ++i) {
243-
Node* node = rhsNodes[i];
244-
if (nodes.add(node).isNewEntry)
245-
resultSet.append(node);
248+
for (auto& node : rhsNodes) {
249+
if (nodes.add(node.get()).isNewEntry)
250+
resultSet.append(node.get());
246251
}
247252

248253
// It would also be possible to perform a merge sort here to avoid making an unsorted result,

Source/WebCore/xml/XPathStep.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ void Step::optimize()
6363
// E.g., there is no need to build a set of all "foo" nodes to evaluate "foo[@bar]", we can check the predicate while enumerating.
6464
// This optimization can be applied to predicates that are not context node list sensitive, or to first predicate that is only context position sensitive, e.g. foo[position() mod 2 = 0].
6565
Vector<std::unique_ptr<Expression>> remainingPredicates;
66-
for (size_t i = 0; i < m_predicates.size(); ++i) {
67-
auto& predicate = m_predicates[i];
66+
for (auto& predicate : m_predicates) {
6867
if ((!predicateIsContextPositionSensitive(*predicate) || m_nodeTest.m_mergedPredicates.isEmpty()) && !predicate->isContextSizeSensitive() && remainingPredicates.isEmpty())
6968
m_nodeTest.m_mergedPredicates.append(WTF::move(predicate));
7069
else
@@ -108,15 +107,13 @@ void optimizeStepPair(Step& first, Step& second, bool& dropSecondStep)
108107

109108
bool Step::predicatesAreContextListInsensitive() const
110109
{
111-
for (size_t i = 0; i < m_predicates.size(); ++i) {
112-
auto& predicate = *m_predicates[i];
113-
if (predicateIsContextPositionSensitive(predicate) || predicate.isContextSizeSensitive())
110+
for (auto& predicate : m_predicates) {
111+
if (predicateIsContextPositionSensitive(*predicate) || predicate->isContextSizeSensitive())
114112
return false;
115113
}
116114

117-
for (size_t i = 0; i < m_nodeTest.m_mergedPredicates.size(); ++i) {
118-
auto& predicate = *m_nodeTest.m_mergedPredicates[i];
119-
if (predicateIsContextPositionSensitive(predicate) || predicate.isContextSizeSensitive())
115+
for (auto& predicate : m_nodeTest.m_mergedPredicates) {
116+
if (predicateIsContextPositionSensitive(*predicate) || predicate->isContextSizeSensitive())
120117
return false;
121118
}
122119

@@ -131,9 +128,7 @@ void Step::evaluate(Node& context, NodeSet& nodes) const
131128
nodesInAxis(context, nodes);
132129

133130
// Check predicates that couldn't be merged into node test.
134-
for (unsigned i = 0; i < m_predicates.size(); i++) {
135-
auto& predicate = *m_predicates[i];
136-
131+
for (auto& predicate : m_predicates) {
137132
NodeSet newNodes;
138133
if (!nodes.isSorted())
139134
newNodes.markSorted(false);
@@ -144,7 +139,7 @@ void Step::evaluate(Node& context, NodeSet& nodes) const
144139
evaluationContext.node = node;
145140
evaluationContext.size = nodes.size();
146141
evaluationContext.position = j + 1;
147-
if (evaluatePredicate(predicate))
142+
if (evaluatePredicate(*predicate))
148143
newNodes.append(node);
149144
}
150145

@@ -233,11 +228,10 @@ inline bool nodeMatches(Node& node, Step::Axis axis, const Step::NodeTest& nodeT
233228
// Only the first merged predicate may depend on position.
234229
++evaluationContext.position;
235230

236-
auto& mergedPredicates = nodeTest.m_mergedPredicates;
237-
for (unsigned i = 0; i < mergedPredicates.size(); i++) {
231+
for (auto& predicate : nodeTest.m_mergedPredicates) {
238232
// No need to set context size - we only get here when evaluating predicates that do not depend on it.
239233
evaluationContext.node = &node;
240-
if (!evaluatePredicate(*mergedPredicates[i]))
234+
if (!evaluatePredicate(*predicate))
241235
return false;
242236
}
243237

Source/WebCore/xml/XSLStyleSheetLibxslt.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,16 @@ XSLStyleSheet::~XSLStyleSheet()
8282
if (!m_stylesheetDocTaken)
8383
xmlFreeDoc(m_stylesheetDoc);
8484

85-
for (unsigned i = 0; i < m_children.size(); ++i) {
86-
ASSERT(m_children.at(i)->parentStyleSheet() == this);
87-
m_children.at(i)->setParentStyleSheet(0);
85+
for (auto& child : m_children) {
86+
ASSERT(child->parentStyleSheet() == this);
87+
child->setParentStyleSheet(0);
8888
}
8989
}
9090

9191
bool XSLStyleSheet::isLoading() const
9292
{
93-
for (unsigned i = 0; i < m_children.size(); ++i) {
94-
if (m_children.at(i)->isLoading())
93+
for (auto& child : m_children) {
94+
if (child->isLoading())
9595
return true;
9696
}
9797
return false;
@@ -117,8 +117,7 @@ xmlDocPtr XSLStyleSheet::document()
117117
void XSLStyleSheet::clearDocuments()
118118
{
119119
m_stylesheetDoc = 0;
120-
for (unsigned i = 0; i < m_children.size(); ++i) {
121-
XSLImportRule* import = m_children.at(i).get();
120+
for (auto& import : m_children) {
122121
if (import->styleSheet())
123122
import->styleSheet()->clearDocuments();
124123
}
@@ -273,8 +272,7 @@ Document* XSLStyleSheet::ownerDocument()
273272
xmlDocPtr XSLStyleSheet::locateStylesheetSubResource(xmlDocPtr parentDoc, const xmlChar* uri)
274273
{
275274
bool matchedParent = (parentDoc == document());
276-
for (unsigned i = 0; i < m_children.size(); ++i) {
277-
XSLImportRule* import = m_children.at(i).get();
275+
for (auto& import : m_children) {
278276
XSLStyleSheet* child = import->styleSheet();
279277
if (!child)
280278
continue;

0 commit comments

Comments
 (0)