git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] avoid exponential regex match for java and objc function names

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 17, 2009, 16:42 UTC
Message-ID
<7vab46rev0.fsf@alter.siamese.dyndns.org>
In-Reply-To
<1245248766-14867-1-git-send-email-bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org> writes:
Show 14 quoted lines
> In the old regex
>
> ^[ \t]*(([ \t]*[A-Za-z_][A-Za-z_0-9]*){2,}[ \t]*\([^;]*)$
>         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
> you can backtrack arbitrarily from [A-Za-z_0-9]* into [A-Za-z_], thus
> causing an exponential number of backtracks.  Ironically it also causes
> the regex not to work as intended; for example "catch" can match the
> underlined part of the regex, the first repetition matching "c" and
> the second matching "atch".
>
> The replacement regex avoids this problem, because it makes sure that
> at least a space/tab is eaten on each repetition.  In other words,
> a suffix of a repetition can never be a prefix of the next repetition.
Thanks; nicely done.

Should I remove the "/* -- */" or is it for better readability I should keep?

Show 30 quoted lines
> Signed-off-by: Paolo Bonzini <bonzini@gnu.org>
> ---
>  userdiff.c |    5 +++--
>  1 files changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/userdiff.c b/userdiff.c
> index d556da9..57529ae 100644
> --- a/userdiff.c
> +++ b/userdiff.c
> @@ -13,7 +13,8 @@ PATTERNS("html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$",
>  	 "[^<>= \t]+|[^[:space:]]|[\x80-\xff]+"),
>  PATTERNS("java",
>  	 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
> -	 "^[ \t]*(([ \t]*[A-Za-z_][A-Za-z_0-9]*){2,}[ \t]*\\([^;]*)$",
> +	 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
> +	 /* -- */
>  	 "[a-zA-Z_][a-zA-Z0-9_]*"
>  	 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
>  	 "|[-+*/<>%&^|=!]="
> @@ -25,7 +26,7 @@ PATTERNS("objc",
>  	 /* Objective-C methods */
>  	 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
>  	 /* C functions */
> -	 "^[ \t]*(([ \t]*[A-Za-z_][A-Za-z_0-9]*){2,}[ \t]*\\([^;]*)$\n"
> +	 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
>  	 /* Objective-C class/protocol definitions */
>  	 "^(@(implementation|interface|protocol)[ \t].*)$",
>  	 /* -- */
> -- 
> 1.6.0.3
Previous: Paolo BonziniNext: Paolo Bonzini
Message 34 of 37 in “git diff looping?”
  1. John BitoJun 16, 2009
  2. Jeff EplerJun 16, 2009
  3. John BitoJun 16, 2009
  4. Jeff KingJun 16, 2009
  5. Jeff KingJun 16, 2009
  6. 1/2 Makefile: refactor regex compat supportJeff King, Jun 16, 2009
  7. Johannes SixtJun 16, 2009
  8. Jeff KingJun 16, 2009
  9. 1/2 Makefile: refactor regex compat supportJeff King, Jun 16, 2009
  10. 2/2 Makefile: use compat regex on SolarisJeff King, Jun 16, 2009
  11. Brandon CaseyJun 16, 2009
  12. Mike RalphsonJun 17, 2009
  13. Mike RalphsonJun 17, 2009
  14. 2/2 Makefile: use compat regex on SolarisJeff King, Jun 16, 2009
  15. John BitoJun 16, 2009
  16. Junio C HamanoJun 16, 2009
  17. Jeff KingJun 16, 2009
  18. Brandon CaseyJun 16, 2009
  19. John BitoJun 16, 2009
  20. Jeff KingJun 16, 2009
  21. Brandon CaseyJun 16, 2009
  22. Paolo BonziniJun 17, 2009
  23. Jeff KingJun 17, 2009
  24. Paolo BonziniJun 17, 2009
  25. Andreas EricssonJun 17, 2009
  26. Paolo BonziniJun 17, 2009
  27. Andreas EricssonJun 17, 2009
  28. Paolo BonziniJun 17, 2009
  29. avoid exponential regex match for java and objc function namesPaolo Bonzini, Jun 17, 2009
  30. demerphqJun 17, 2009
  31. Jeff KingJun 17, 2009
  32. demerphqJun 17, 2009
  33. Paolo BonziniJun 17, 2009
  34. Junio C HamanoJun 17, 2009
  35. Paolo BonziniJun 18, 2009
  36. John BitoJun 16, 2009
  37. Jeff KingJun 16, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.