Skip to content
Tcl Source Code View Ticket
Login

View Ticket

string match & glob: close-bracket in character set

Open3556218fffffffffffffffffffffffffffffffffCreated 14 years agoUpdated 44 days ago
Descriptionplain2012-08-11 00:22:57
As of now, it is not possible to include a close-bracket character in a bracketed set of allowed characters.
[scan]'s format uses bracketed sets of allowed characters, too, and it *does* allow a close-bracket as first char.

Having looked into the implementation of tcl's string-globbing, it seems that *before* entering that loop over the
bracketed characters, an initial check for ']' in the pattern and in the string to be matched would be a really simple
fix. Doing it that way, would even still allow a dash to be included verbatim, as well, by placing it right after the
close-bracket.
Referenced by1
  • 7ca44862f9Fix [219233ffff] and [3556218fff]: in glob patterns ([string match], [glob], [lsearch -glob], [switch -glob]), a "-" right before the closing bracket of a set now is a literal, and a closing bracket right after the opening one is a member of the set, as in POSIX. Before, such a "-" started a range up to the closing bracket, and a set starting with a closing bracket never matched. Only for 9.1: patterns that relied on a range ending in the closing bracket change meaning. check-in2026-10-19 17:38:04
dkfplain2012-08-13 06:19:51
See also Bug 219233, for related issues.
serhiy.storchakawiki2026-10-19 17:43:27

Still present in 8.6, 9.0 and 9.1: there is no way to include a closing bracket in a set. Backslash is not special inside a set, and {[]a]} never matches anything. The only workaround was a range ending in the closing bracket, which always includes other characters: the narrowest one, {[\-]]}, also matches a backslash. And such ranges are what [219233ffff] removes.

Proposed fix for 9.1 in branch glob-bracket-posix, together with [219233ffff]: a closing bracket right after the opening one is a member of the set, as in POSIX and Python's fnmatch, so {[]a]} matches "]" or "a". Such patterns never matched before, so nothing that works changes. string.n now describes both rules. Test string-11.58.

Log in (anonymous is fine) to add a comment.