Subject: Bug with Regexp Containing only a Character Class with a Caret (PATCH)

From: Cameron Desautels <[email-masked]>
Date: Thu, 5 Dec 2013 13:26:58 -0600

After further experimentation, I suspect that "[^]" is simply not
a valid regular expression.  For instance, grep(1) gives the
following behavior:

    $ echo "^" | grep "[^]"
    grep: brackets ([ ]) not balanced

This suggests that the broken behavior is within
`regexp-opt-charset`.  I've attached a patch for that function.

Here are some test cases which reveal the behavior of the unpatched
and patched versions of the function (the only difference is the
handling of the "[^]" case):

    ;; Pre-patch
    (regexp-opt-charset (list ?^))          ; "[^]"
    (regexp-opt-charset (list ?^ ?a))       ; "[a^]"
    (regexp-opt-charset (list ?^ ?-))       ; "[-^]"
    (regexp-opt-charset (list ?^ ?\]))      ; "[]^]"
    (regexp-opt-charset (list ?^ ?- ?\]))   ; "[]^-]"

    ;; Post-patch
    (regexp-opt-charset (list ?^))          ; "\\^"
    (regexp-opt-charset (list ?^ ?a))       ; "[a^]"
    (regexp-opt-charset (list ?^ ?-))       ; "[-^]"
    (regexp-opt-charset (list ?^ ?\]))      ; "[]^]"
    (regexp-opt-charset (list ?^ ?- ?\]))   ; "[]^-]"

--
Cameron Desautels <[email-masked]>
*** regexp-opt.el.orig	Thu Dec  5 11:17:19 2013
--- regexp-opt.el	Thu Dec  5 11:19:31 2013
*************** CHARS should be a list of characters."
*** 285,291 ****
      ;;
      ;; Make sure a caret is not first and a dash is first or last.
      (if (and (string-equal charset "") (string-equal bracket ""))
! 	(concat "[" dash caret "]")
        (concat "[" bracket charset caret dash "]"))))
  
  (provide 'regexp-opt)
--- 285,293 ----
      ;;
      ;; Make sure a caret is not first and a dash is first or last.
      (if (and (string-equal charset "") (string-equal bracket ""))
! 	(if (string-equal dash "")
!             "\\^"                       ; [^] is not a valid regexp
!           (concat "[" dash caret "]"))
        (concat "[" bracket charset caret dash "]"))))
  
  (provide 'regexp-opt)