Tcl Source Code

View Ticket
Login
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2019 Conference, Houston/TX, US, Nov 4-8
Send your abstracts to tclconference@googlegroups.com
or submit via the online form by Sep 9.
Ticket UUID: 1152746
Title: TIP #241 -nocase option for [lsearch], [lsort], and [switch]
Type: Patch Version: TIP Implementation
Submitter: mistachkin Created on: 2005-02-27 04:53:25
Subsystem: 17. Commands I-L Assigned To: dkf
Priority: 5 Medium Severity:
Status: Closed Last Modified: 2005-11-15 15:56:32
Resolution: Fixed Closed By: dkf
    Closed on: 2005-06-01 12:14:56
Description:
This provided patch was made against HEAD.  It has been
tested on Windows NT and FreeBSD.

Please refer to TIP #241 for more information.
User Comments: dkf added on 2005-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.

kchansen added on 2005-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

kchansen added on 2005-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>

dkf added on 2005-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)

dkf added on 2005-03-02 16:59:00:

File Deleted - 123311:

dkf added on 2005-03-02 16:58:59:

File Deleted - 123374:

mistachkin added on 2005-03-02 13:53:53:

File Added - 123800: TIP-241v4.diff

mistachkin added on 2005-03-02 13:53:52:
Logged In: YES 
user_id=113501


Updated v4 patch.  Added [switch -nocase] with docs and tests.

mistachkin added on 2005-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.

mistachkin added on 2005-02-27 15:01:37:

File Added - 123311: TIP-241v2.diff

mistachkin added on 2005-02-27 15:01:36:
Logged In: YES 
user_id=113501


Updated v2 patch (optimized).  Added docs and test cases.

mistachkin added on 2005-02-27 11:53:26:

File Added - 123296: TIP-241v1.diff

Attachments: