Tracker: Patches

5 TIP #241 -nocase option for [lsearch], [lsort], and [switch] - ID: 1152746
Last Update: Comment added ( dkf )


This provided patch was made against HEAD. It has been
tested on Windows NT and FreeBSD.

Please refer to TIP #241 for more information.


Joe Mistachkin ( mistachkin ) - 2005-02-26 20:53:25 PST

5

Closed

Fixed

Donal K. Fellows

17. Commands I-L

TIP Implementation

Public


Comments ( 7 )

Date: 2005-11-15 00:56:32 PST
Sender: dkfProject Admin

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.


Date: 2005-11-14 10:38:28 PST
Sender: kchansen

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



Date: 2005-11-14 10:31:45 PST
Sender: kchansen

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>



Date: 2005-06-01 05:14:56 PDT
Sender: dkfProject Admin

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)


Date: 2005-03-01 22:53:52 PST
Sender: mistachkinAccepting Donations

Logged In: YES
user_id=113501


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



Date: 2005-02-27 12:20:55 PST
Sender: mistachkinAccepting Donations

Logged In: YES
user_id=113501


Updated v3 patch. Added [lsort -nocase] with docs and tests.



Date: 2005-02-27 00:01:36 PST
Sender: mistachkinAccepting Donations

Logged In: YES
user_id=113501


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




Attached File ( 1 )

Filename Description Download
TIP-241v4.diff reference implementation v4 Download

Changes ( 12 )

Field Old Value Date By
status_id Open 2005-06-01 05:14:56 PDT dkf
close_date - 2005-06-01 05:14:56 PDT dkf
resolution_id None 2005-06-01 05:14:56 PDT dkf
File Deleted 123311: 2005-03-02 01:59:00 PST dkf
File Deleted 123296: 2005-03-02 01:59:00 PST dkf
File Deleted 123374: 2005-03-02 01:58:59 PST dkf
File Added 123800: TIP-241v4.diff 2005-03-01 22:53:53 PST mistachkin
summary TIP #241 [lsearch -nocase] and [lsort -nocase] 2005-03-01 22:53:52 PST mistachkin
File Added 123374: TIP-241v3.diff 2005-02-27 12:20:55 PST mistachkin
summary TIP #241 [lsearch -nocase] Implementation 2005-02-27 12:02:03 PST mistachkin
File Added 123311: TIP-241v2.diff 2005-02-27 00:01:37 PST mistachkin
File Added 123296: TIP-241v1.diff 2005-02-26 20:53:26 PST mistachkin