Code: Select all
proc replace {{args ""}} {
set switches ""
for {set i 0} {[string match -* [set arg [lindex $args $i]]]} {incr i} {
if {![regexp -- {^-(nocase|-)$} $arg -> switch]} {
error "bad switch \"$arg\": must be -nocase, or --"
}
if {$switch == "-"} {
incr i
break
}; lappend switches $switch
}
set nocase [expr {([lsearch -exact $switches "nocase"] >= "0") ? 1 : 0}]
set text [lindex $args $i]
set substitutions [lindex $args [expr $i+1]]
# Check to see if $substitutions is in list format, if not make it so.
set substitutions [split $substitutions]
if {[info tclversion] >= "8.1"} {
return [expr {($nocase)?
[string map -nocase $substitutions $text]:
[string map $substitutions $text]}]
}
set re_syntax {([][\\\*\+\?\{\}\,\(\)\:\.\^\$\=\!\|])}
foreach {a b} $substitutions {
regsub -all -- $re_syntax $a {\\\1} a
if {$nocase} {regsub -all -nocase -- $a $text $b text} \
else {regsub -all -- $a $text $b text}
}; return $text
}
proc mirc_strip {{args ""}} {
set switches ""
if {$switches == ""} {set switches all}
set arg [lindex $args 0]
set all [expr {([lsearch -exact $switches all] >= 0) ? 1 : 0}]
set list [list \002 "" \017 "" \026 "" \037 ""]
regsub -all -- "\003(\[0-9\]\[0-9\]?(,\[0-9\]\[0-9\]?)?)?" $arg "" arg
set arg [replace -- $arg [join $list]]
return $arg
}
proc mq:filter {data} {
regsub -all -- "(\002|\017|\026|\037|\003(\[0-9\]\[0-9\]?(,\[0-9\]\[0-9\]?)?)?)" $data "" data
set data [mirc_strip $data]
return $data
}