Rearrange CFX_CSSSelector::FromString().
Use early continues/returns in the for-loop.
Change-Id: I6580fbfe4452a731897f08aaa9b0a2dfd3d7abf8
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/70734
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxcrt/css/cfx_cssselector.cpp b/core/fxcrt/css/cfx_cssselector.cpp
index 1ea8231..ca2aeec 100644
--- a/core/fxcrt/css/cfx_cssselector.cpp
+++ b/core/fxcrt/css/cfx_cssselector.cpp
@@ -47,20 +47,23 @@
std::unique_ptr<CFX_CSSSelector> head;
for (size_t i = 0; i < str.GetLength();) {
wchar_t wch = str[i];
- if ((isascii(wch) && isalpha(wch)) || wch == '*') {
- if (head)
- head->set_is_descendant();
- size_t len =
- wch == '*' ? 1 : GetCSSNameLen(str.Last(str.GetLength() - i));
- auto new_head = std::make_unique<CFX_CSSSelector>(str.Substr(i, len),
- std::move(head));
- head = std::move(new_head);
- i += len;
- } else if (wch == ' ') {
+ if (wch == ' ') {
++i;
- } else {
- return nullptr;
+ continue;
}
+
+ const bool is_star = wch == '*';
+ const bool is_valid_char = is_star || (isascii(wch) && isalpha(wch));
+ if (!is_valid_char)
+ return nullptr;
+
+ if (head)
+ head->set_is_descendant();
+ size_t len = is_star ? 1 : GetCSSNameLen(str.Last(str.GetLength() - i));
+ auto new_head =
+ std::make_unique<CFX_CSSSelector>(str.Substr(i, len), std::move(head));
+ head = std::move(new_head);
+ i += len;
}
return head;
}