Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2005-11-15
15:56 • Ticket [1152746fff] TIP #241 -nocase option for lsearch, lsort, and switch... artifact: ce55de083c user: dkf
01:38 • Ticket [1152746fff]: 4 changes artifact: c3a36b6a0b user: kchansen
01:31 • Ticket [1152746fff]: 4 changes artifact: bccb4aa0bb user: kchansen
2005-06-01
19:14 • Closed ticket [1152746fff]. artifact: 5d6f691306 user: dkf
2005-03-02
16:59 • Ticket [1152746fff]: 4 changes artifact: e5b4a26fff user: dkf
16:58 • Ticket [1152746fff]: 4 changes artifact: fc9dfd8a18 user: dkf
13:53 • Ticket [1152746fff]: 4 changes artifact: f7866f35f9 user: mistachkin
13:53 • Add attachment TIP-241v4.diff to ticket [1152746fff] artifact: b1dcc02475 user: mistachkin
13:53 • Ticket [1152746fff] TIP #241 -nocase option for lsearch, lsort, and switch... artifact: a2c50b7eeb user: mistachkin
2005-02-28
03:20 • Ticket [1152746fff]: 4 changes artifact: 4a8c653b3f user: mistachkin
03:02 • Ticket [1152746fff]: 1 change artifact: ff53e48340 user: mistachkin
2005-02-27
15:01 • Ticket [1152746fff]: 4 changes artifact: 9df2bf18ca user: mistachkin
15:01 • Ticket [1152746fff]: 4 changes artifact: ff013b0761 user: mistachkin
11:53 • Ticket [1152746fff]: 4 changes artifact: 3d40d31361 user: mistachkin
04:53 • New ticket [1152746fff]. artifact: c5ed5afd0a user: mistachkin

TIP #241 -nocase option for [lsearch], [lsort], and [switch]

Closed / Fixed1152746fffffffffffffffffffffffffffffffffCreated 22 years agoUpdated 21 years ago
Descriptionplain2005-02-27 04:53:25
This provided patch was made against HEAD.  It has been
tested on Windows NT and FreeBSD.

Please refer to TIP #241 for more information.
mistachkinplain2005-02-27 11:53:26
File Added - 123296: TIP-241v1.diff
mistachkinplain2005-02-27 15:01:36
Logged In: YES 
user_id=113501


Updated v2 patch (optimized).  Added docs and test cases.
mistachkinplain2005-02-27 15:01:37
File Added - 123311: TIP-241v2.diff
mistachkin Ticket [1152746fff]: 1 change [History diff]2005-02-28 03:02:03
mistachkinplain2005-02-28 03:20:55
File Added - 123374: TIP-241v3.diff

Logged In: YES 
user_id=113501


Updated v3 patch.  Added [lsort -nocase] with docs and tests.
mistachkinplain2005-03-02 13:53:52
Logged In: YES 
user_id=113501


Updated v4 patch.  Added [switch -nocase] with docs and tests.
mistachkinplain2005-03-02 13:53:53
File Added - 123800: TIP-241v4.diff
dkfplain2005-03-02 16:58:59
File Deleted - 123374:
dkfplain2005-03-02 16:59:00
File Deleted - 123311:
dkfplain2005-06-01 19:14:56
Logged In: YES 
user_id=79902

Implemented using the patch, thanks!

I also added compilation of [switch -glob -nocase]; I'd have
done it for -exact -nocase too, but I don't know what
opcodes to issue (INST_STR_EQ doesn't take an immediate
operand for nocaseness, unlike INST_STR_MATCH)
kchansenplain2005-11-15 01:31:45
Logged In: YES 
user_id=614439

A Tcl-only approach....

I needed a -nocase option for the switch statement, scanned 
the tips and found this one.  Here is the approach I took to 
get effectively the same behavior:

proc CreateCaselessRegExp {Text} {
  set TextUpper [split [string toupper $Text] {}]
  set TextLower [split [string tolower $Text] {}]

  foreach Upper $TextUpper Lower $TextLower {
    lappend TextResult "\[$TextUpper$TextLower\]"
    }

  set RegExp [join $TextResult {}]

  return $RegExp
  };# CreateCaselessRegExp

With this proc, a "switch" with "-exact" and "-nocase" 
replaces those options with "-regexp", and replaces the exact 
strings with the result of calling CreateCaselessRegExp.  You 
must also check to see if the test value is "default" and not 
change it when it is encountered.

For example:

switch -exact -nocase -- $MyText {
  First {
    ...
    }
  Third {
    ...
    }
  "Another one here" {
    ...
    }
  "Test #9" {
    }
  default {
    }
  }

becomes:

switch -regexp -- $MyText {
  [Ff][Ii][Rr][Ss][Tt] {
    ...
    }
  [Tt][Hh][Ii][Rr][Dd] {
    ...
    }
  "[Aa][Nn][Oo][Tt][Hh][Ee][Rr][  ][Oo][Nn][Ee][  ][Hh][Ee][Rr]
[Ee]" {
    ...
    }
  "[Tt][Ee][Ss][Tt][  ][##][99]" {
    }
  default {
    }
  }

I actually ended up coding as:

eval {switch -exact -nocase -- $MyText {
  [CreateCaselessRegExp First] {
    ...
    }
  [CreateCaselessRegExp Third] {
    ...
    }
  [CreateCaselessRegExp "Another one here"] {
    ...
    }
  [CreateCaselessRegExp "Test #9"] {
    }
  default {
    }
  }}

As you can see, it could be made a little smarter because all 
non-alphabet characters end up doubled.  A test could be put 
in to not create a "one of list" selections for things which are 
not alphabet, but I figured it was fine for a first cut....  I also 
liked the fact that I was able to get the case insensitivity with 
very little extra effort....
<Karl C. Hansen>
kchansenplain2005-11-15 01:38:28
Logged In: YES 
user_id=614439

Typo in previous proc....

proc CreateCaselessRegExp {Text} {
  set TextUpper [split [string toupper $Text] {}]
  set TextLower [split [string tolower $Text] {}]

  foreach Upper $TextUpper Lower $TextLower {
    lappend TextResult "\[$Upper$Lower\]"
#                                   ^^^^^^^^^^^^^^^^
    }

  set RegExp [join $TextResult {}]

  return $RegExp
  };# CreateCaselessRegExp
dkfplain2005-11-15 15:56:32
Logged In: YES 
user_id=79902

If you're going to do that, you should remember to anchor
the generated REs. You might (assuming you're not stuck on
Tcl 8.0 or before) also like to use the RE:
  (?i)^FooBar$
instead of:
  ^[Ff][Oo][Oo][Bb][Aa][Rr]$
if you see what I mean.

None of which makes much odds. This is a closed issue.
Log in (anonymous is fine) to add a comment.

Attachments: