search: merge and fix parens heuristics
Created by: rvantonder
Part of #13128 (fixes first bullet). Basically we had some heuristic to convert certain parentheses in patterns (see the function comment in this PR).
Previously the heuristics didn't properly consider that the parentheses might be escaped. This PR takes escape sequences into account so we interpret the original pattern correctly, and merges two existing heuristic since they need basically the same linear pass.