Skip to content

Conversation

gitlost
Copy link
Contributor

@gitlost gitlost commented Aug 29, 2017

Issue #43

Removes context regex and always uses PREG_OFFSET_CAPTURE mode in preg_match_all() , to avoid missing matches in contexts.

Also no longer adds UTF-8 modifier u to regex ever, as not needed without context regex and was never needed anyway in --regex case.

Also rechecks encoding when non-UTF-8 for each value of $col_val as could get ASCII first time (unfortunately behat/gherkin can't handle non-UTF-8 chars in a feature file so will need a phpunit test to test this properly).

$offset = $match_arr[1];
// Offsets are in bytes, so need to use `strlen()` and `substr()` before using `safe_substr()`.
$before = $before_context && $offset ? \cli\safe_substr( substr( $col_val, 0, $offset ), -$before_context, null /*length*/, false /*is_width*/, $col_encoding ) : '';
$after = $after_context ? \cli\safe_substr( substr( $col_val, $offset + strlen( $match ) ), 0, $after_context, false /*is_width*/, $col_encoding ) : '';
Copy link
Member

Choose a reason for hiding this comment

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

Just a general tip for code readability:

If you have scalar arguments where it is not immediately clear what their meaning is, you can use a simple assignment to make it clearer.

So, instead of null /*length*/, false /*is_width*/, you could also do $length = null, $is_width = false.

I personally find this easier to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I agree, it is easier to read - however, gasp horror it's an unnecessary assignment! Knowingly doing an unnecessary assignment is the sort of thing that keeps me from sleeping! So I'll leave it for the mo...

@schlessera
Copy link
Member

@gitlost Please merge in latest master.

@gitlost gitlost merged commit b659443 into master Aug 30, 2017
@gitlost gitlost deleted the context_bug branch August 30, 2017 19:59
@gitlost gitlost added this to the 1.2.1 milestone Aug 30, 2017
danielbachhuber pushed a commit that referenced this pull request Nov 18, 2022
db search: fix match in context in non-regex case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants