Re: set character at string position
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.tcl archive

Re: set character at string position

From: Lisa Pearlson <no@spam.plz>
Date: Fri Jan 06 2006 - 21:03:10 CET

I asked this question because I wanted to perform a bit operation on each
byte of a string (xor).

proc invert {str} {
    upvar 1 $str s
    set len [string length $s]
    for {set i 0} {$i < $len} {incr i} {
        set s [string replace $s $i $i [expr [string index $s $i] ^ 0xff]]
    }
}

Seems like a horrible syntax and performance to me, compared to C:

void invert(unsigned char *str, int len) {
    int i;
    for (i=0; i<len; i=i+1) {
        str[i] = str[i] ^ 0xff;
    }
}

My strings are about 64k size, per 'client' and must handle several clients
simultaneously.

So, I would like to make the C code above available in TCL. Is this done
through a 'stub' ?

Is there an example implementation for a custom tcl command calling code
implemented in C ?

Lisa

"Lisa Pearlson" <no@spam.plz> wrote in message
news:43b597e1$0$16227$e4fe514c@dreader25.news.xs4all.nl...
> Hi,
>
> I looked at the string class, but I don't see how I can set/replace a
> character on a certain string position.
> In C I'd do something like:
>
> char s[] = "Hello";
> s[2] = 'x';
>
> This would give "Hexlo"
>
> How do I this in Tcl?
>
> Lisa
>
Received on Tue Jan 17 14:56:05 2006