std.stringbuffer

Me Here p9e883002 at sneakemail.com
Wed Apr 30 07:28:05 PDT 2008


Janice Caron wrote:

>2008/4/30 Me Here <p9e883002 at sneakemail.com>:
>>     char[] a = ...2000 chars from somewhere.
>>
>>     char[] field1 = a[ 312 .. 357 ];
>>     field1.toUpper();
>
>I've kind of lost track of the number of times I've said this in
>recent days, but...
>
>You cannot uppercase in place, because for any given dchar, c, the
>number of UTF-8 bytes required to express c may be different from the
>number of UTF-8 bytes required to express toupper(c).
>
>If any of you have plans to uppercase or lowercase UTF-8 in place,
>forget that now. It just ain't possible. (You can uppercase ASCII,
>UTF-16, or UTF-32 in place. But not UTF-8, and char[], by definition,
>is UTF-8).

Ignoring for the moment Matti's pronouncement that this is an obscure and 
unlikely event,
it really depends upon how the library is coded.

For example, if the case change is effected in place for the majority of 
cases when it can be,
when the occasion occurs that it cannot, and raises a runtime exception, 
catch the error
and use replaceSlice to handle it:

	import std.stdio;
	import std.string;

	int main( char[][] args ) {
	    char [] s = "the quick brown fox";
	    try{
	        s[ 8 .. 9 ] = \u1234;
	    }
	    catch {
	        s =  s.replaceSlice( s[ 8 .. 9 ], \u1234  );
	    }
	    writefln( s );
	    return 0;
	}

Though it would be (much) nicer if the builtin lvalue slice handled this 
for us.
I was just disappointed for the second to (re)discover this imitation of
Ds slicing. I had forgotten because other languages I used do not.

This is one of those things that I doubt I will ever agree with the 
decision.
But I'm just another jerk on the internet with an opinion, and we all know
what that is analogous to.

If the language doesn't handle it, the the library should.
If it doesn't, then I will have to. And you, and Bill and Fred and Sue ,,,

Cheers, b.


-- 




More information about the Digitalmars-d mailing list