[Mono-devel-list] patch for TextWriter

Ben Maurer bmaurer at ximian.com
Mon Dec 6 00:16:23 EST 2004


On Mon, 2004-12-06 at 13:46 +0900, Atsushi Eno wrote:
> Hi,
> 
> >>I attached a patch for TextWriter.cs not to create a new string for
> >>every call to NewLine. If no problem, I'll check in later. One concern
> >>I have is that it costs one string field (which might be extraneous
> >>for some kind of use.)
> > 
> > 
> > `CoreNewLine' could be modified by someone, since it is protected. So,
> > it is invalid to cache it.
> 
> Oh, good catch. Then, how about having a cache for CoreNewLine that
> is set everytime we set string cache? I attached another patch.

No, the *elements* of the array could be modified. MSFT should have used
string as it is immutable.

If someone else is calling NewLine (and I don't know why they would be)
We could do the following:

switch (CoreNewLine.Length) {
	case 1:
		if (CoreNewLine [0] == '\n');
			return "\n";
		if (CoreNewLine [0] == '\r');
			return "\r";
		break;
	case 2:
		if (CoreNewLine [0] == '\r' && CoreNewLine [1] == '\n')
			return "\r\n";
		break;
}

return new string (CoreNewLine);

That is 3 (correctly predicted) branches for the Linux case. But I am
not sure if the gains (are there any?) are worth the code bloat here.

-- Ben




More information about the Mono-devel-list mailing list