Is all this Invarient **** er... stuff, premature optimisation?

p9e883002 at sneakemail.com p9e883002 at sneakemail.com
Sun Apr 27 18:10:11 PDT 2008


On Mon, 28 Apr 2008 02:28:23 +0200, "Simen Kjaeraas" 
<simen.kjaras at gmail.com> wrote:
> On Mon, 28 Apr 2008 02:14:19 +0200, Simen Kjaeraas  =
> 
> <simen.kjaras at gmail.com> wrote:
> 
> > <p9e883002 at sneakemail.com> wrote:
> >
> >
> > Is this what you wanted to write?
> >
> > int main(string[] args)
> > {
> >    char[] a =3D cast(char[])args[0];
> >    a[2..5] =3D "XXX";
> >    writefln(a);
> >    return 0;
> > }
> > This compiles and runs, and seems to do what you describe. Sure, there=
> 's  =
> 
> > a
> > cast there, but it's not all that bad, is it?
> 
> 
> Sorry, forgot the .toupper() call there. Should be
>    char[] a =3D cast(char[])args[0].toupper();
> 
> -- Simen

Okay, you got around the first cast by using 

int main( string[] ) {

So now you want to lowercase it again:

import std.stdio;
import std.string;

int main( string[] args) {
    char[] a = cast(char[])args[0].toupper();    
    a[2..5] = "XXX";
    a = a.tolower;
    writefln(a);
    return 0;
}

c:\dmd\test>dmd junk.d
junk.d(7): Error: no property 'tolower' for type 'char[]'
junk.d(7): Error: cannot implicitly convert expression (1) of type int to char[]
junk.d(7): Error: cannot cast int to char[]
junk.d(7): Error: integral constant must be scalar type, not char[]

So, cast a back to being a string, so that we can call tolower() on it and then cast 
the copied mutated string back to a char[]:

import std.stdio;
import std.string;

int main( string[] args) {
    char[] a = cast(char[])args[0].toupper();    
    a[2..5] = "XXX";
    a = cast(char[]) ( ( cast(string)a ).tolower );
    writefln(a);
    return 0;
}

c:\dmd\test>dmd junk.d
junk.d(7): Error: no property 'tolower' for type 'invariant(char)[]'
junk.d(7): Error: cannot cast int to char[]
junk.d(7): Error: integral constant must be scalar type, not char[]
junk.d(7): Error: cannot cast int to char[]
junk.d(7): Error: integral constant must be scalar type, not char[]
junk.d(7): Error: cannot implicitly convert expression (0) of type int to char[]
junk.d(7): Error: cannot cast int to char[]
junk.d(7): Error: integral constant must be scalar type, not char[]

Nope. That don't work. 

import std.stdio;
import std.string;

int main( string[] args) {
    char[] a = cast(char[])args[0].toupper();    
    a[2..5] = "XXX";
    a = cast(char[])tolower( cast(string)a );
    writefln(a);
    return 0;
}

Finally. It works. 

Summary:

If I want to be able to lvalue slice operations on 'strings' (for efficiency) I have 
to have them as char[]. 

If I want to be able to use std.string methods on those same strings, I have to 
cast them to invariant(char)[] and the results back to char[] which involves a at 
least one copy operation, and probably two.

And the invariant-ness of the string library is done "for efficiency"?

Cheers, b.





More information about the Digitalmars-d mailing list