This new code seems to work correctly, but it's outside of the original scope of this pull request to improve performance. This is a better search (it searches for all whitespace and not just space chars) but I think it's slower due to the calls to `substr()`.

The performance for this might be better if the code did a single match of whitespace, and then found the gaps before and after the cursor from the results array.

I think we should remove this code (except for `trim()` in condition) from this PR and do it in a separate pr.