Use more WideStringViews in CFX_CSSSelector.
Work with indices and length, instead of manipulating pointers.
Change-Id: Iaa692770f89ec8dd1531427fd6c91124d3f7b3ab
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/70733
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 53f00b0..1ea8231 100644
--- a/core/fxcrt/css/cfx_cssselector.cpp
+++ b/core/fxcrt/css/cfx_cssselector.cpp
@@ -12,24 +12,20 @@
namespace {
-int32_t GetCSSNameLen(const wchar_t* psz, const wchar_t* pEnd) {
- const wchar_t* pStart = psz;
- while (psz < pEnd) {
- if (!isascii(*psz) || (!isalnum(*psz) && *psz != '_' && *psz != '-')) {
- break;
- }
- ++psz;
+size_t GetCSSNameLen(WideStringView str) {
+ for (size_t i = 0; i < str.GetLength(); ++i) {
+ wchar_t wch = str[i];
+ if (!isascii(wch) || (!isalnum(wch) && wch != '_' && wch != '-'))
+ return i;
}
- return psz - pStart;
+ return str.GetLength();
}
} // namespace
-CFX_CSSSelector::CFX_CSSSelector(const wchar_t* psz,
- int32_t iLen,
+CFX_CSSSelector::CFX_CSSSelector(WideStringView str,
std::unique_ptr<CFX_CSSSelector> next)
- : name_hash_(
- FX_HashCode_GetW(WideStringView(psz, iLen), /*bIgnoreCase=*/true)),
+ : name_hash_(FX_HashCode_GetW(str, /*bIgnoreCase=*/true)),
next_(std::move(next)) {}
CFX_CSSSelector::~CFX_CSSSelector() = default;
@@ -39,11 +35,8 @@
WideStringView str) {
ASSERT(!str.IsEmpty());
- const wchar_t* psz = str.unterminated_c_str();
- const wchar_t* pStart = psz;
- const wchar_t* pEnd = psz + str.GetLength();
- for (; psz < pEnd; ++psz) {
- switch (*psz) {
+ for (wchar_t wch : str) {
+ switch (wch) {
case '>':
case '[':
case '+':
@@ -52,18 +45,19 @@
}
std::unique_ptr<CFX_CSSSelector> head;
- for (psz = pStart; psz < pEnd;) {
- wchar_t wch = *psz;
+ 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();
- int32_t iNameLen = wch == '*' ? 1 : GetCSSNameLen(psz, pEnd);
- auto new_head =
- std::make_unique<CFX_CSSSelector>(psz, iNameLen, std::move(head));
+ 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);
- psz += iNameLen;
+ i += len;
} else if (wch == ' ') {
- psz++;
+ ++i;
} else {
return nullptr;
}
diff --git a/core/fxcrt/css/cfx_cssselector.h b/core/fxcrt/css/cfx_cssselector.h
index 91778ab..41de083 100644
--- a/core/fxcrt/css/cfx_cssselector.h
+++ b/core/fxcrt/css/cfx_cssselector.h
@@ -15,9 +15,7 @@
public:
static std::unique_ptr<CFX_CSSSelector> FromString(WideStringView str);
- CFX_CSSSelector(const wchar_t* psz,
- int32_t iLen,
- std::unique_ptr<CFX_CSSSelector> next);
+ CFX_CSSSelector(WideStringView str, std::unique_ptr<CFX_CSSSelector> next);
~CFX_CSSSelector();
bool is_descendant() const { return is_descendant_; }