Is all this Invarient **** er... stuff, premature optimisation?
Janice Caron
caron800 at googlemail.com
Mon Apr 28 00:24:20 PDT 2008
2008/4/28 <p9e883002 at sneakemail.com>:
> import std.string;
>
> int main( string[] args) {
> char[] a = cast(char[])args[0].toupper();
**** UNDEFINED BEHAVIOR ****
(1) args might be placed in a hardware-locked read-only segment. Then
the following line would fail
(2) there might be other pointers to the string, which expect it never
to change.
> a[2..5] = "XXX";
> a = cast(char[])tolower( cast(string)a );
> writefln(a);
> return 0;
> }
>
> Finally. It works.
But not necessarily on all architectures, because of the undefined
behavior. This is how you do it without undefined behavior.
import std.string;
int main( string[] args) {
string a = args[0].toupper();
a = a[0..2] ~ "XXX" ~ a[5..$];
a = a.tolower();
writefln(a);
return 0;
}
More information about the Digitalmars-d
mailing list