Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize LVCssSelectorRule::check() #528

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions crengine/src/lvstsheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5137,7 +5137,7 @@ bool LVCssSelectorRule::check( const ldomNode * & node, bool allow_cache )
break;
case cssrt_id: // E#id
{
const lString32 val = node->getAttributeValue(attr_id);
const lString32 &val = node->getAttributeValue(attr_id);
if ( val.empty() )
return false;
// With EPUBs and CHMs, using ldomDocumentFragmentWriter,
Expand All @@ -5161,20 +5161,21 @@ bool LVCssSelectorRule::check( const ldomNode * & node, bool allow_cache )
break;
case cssrt_class: // E.class
{
const lString32 val = node->getAttributeValue(attr_class);
const lString32 &val = node->getAttributeValue(attr_class);
if ( val.empty() )
return false;
// val.lowercase(); // className should be case sensitive
// we have appended a space when there was some inner space, meaning
// this class attribute contains multiple class names, which needs
// more complex checks
if ( val[val.length()-1] == ' ' ) {
lString32 value_w_space_after = _value + " ";
if (val.pos(value_w_space_after) == 0)
return true; // at start
lString32 value_w_spaces_before_after = " " + _value + " ";
if (val.pos(value_w_spaces_before_after) != -1)
return true; // in between or at end
int start = 0;
int pos;
while ((pos = val.pos(_value, start)) >= 0) {
if ((pos == 0 || val[pos - 1] == ' ') && val[pos + _value.length()] == ' ')
return true;
start += _value.length();
}
Comment on lines +5174 to +5178
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving by the pattern length to continue the search feels like you may miss some stuff, like finding aaa in aa aa aaaa aaa aa aaa. It may work because of the boundary checks for a space, but it feels not obvious :)
Sure it's all sane?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My assumption is that _value doesn't contain any spaces. With that I can write start += _value.length() + 1 but that looks even more suspicious :p

return false;
}
return val == _value;
Expand Down