Re: Where is stringbuilder?
- From: Rob Kennedy <me3@xxxxxxxxxxx>
- Date: Thu, 19 Oct 2006 23:29:35 -0500
J French wrote:
On Wed, 18 Oct 2006 12:14:46 -0500, Rob Kennedy <me3@xxxxxxxxxxx>
wrote:
<snip>
One of the reasons for StringBuilder is that in .Net (and Java), strings are immutable -- once you create one, you cannot change its contents. In Win32 Delphi, this statement is easy:
BigStr[L9 + Start] := SmallStr[L9];
In .Net, implementing that statement requires more work from the compiler. It turns into something like this:
BigStr := Copy(BigStr, 1, L9 + Start - 1)
+ SmallStr[L9]
+ Copy(BigStr, L9 + Start + 1, MaxInt);
The old string gets discarded and a new string gets allocated because we cannot change the contents of the old string, even when the reference count is 1.
Fascinating, so VB.NET does not support the equivalent of Mid$( S$, 2, 1 ) = "x"
Dunno. You're the VB expert around here. :)
Since Mid is a compiler built-in function, it could probably be implemented the same way I expanded the Delphi assignment statement above.
What an odd idea A string is just a block of memory
In Win32 Delphi, yes. In Java and .Net, no. In those, a string is an object. If it had methods that could change its contents, then anything that stored a reference to a string would be in danger of having that string change later.
It can't be solved with Delphi's copy-on-write behavior for strings, either. Since String is now a descendant of Object, anything that can be done to an Object can be done to a String, and the compiler can't do anything to ensure that the object gets cloned first. The object itself can't do anything about it, either, since it is powerless to change the reference its methods are called on (for the same reason TObject.Free can't set its reference to nil).
Even in Win32 Delphi, a string isn't "just" a block of memory. It has special semantics attached to it for assignment and comparison.
There is the old Concat Function, but it is not much use for this sort
of thing.
As it turns out, the Concat function is compiler magic. It does nothing more than invoke the same routines that the "+" operator uses.
Drat, I expected to find the source for it.
You can still find it. It's just not called Concat. The compiler will call any of _LStrCat, _LStrCat3, and _LStrCatN, depending on how many strings are being concatenated.
--
Rob
.
- Follow-Ups:
- Re: Where is stringbuilder?
- From: Maarten Wiltink
- Re: Where is stringbuilder?
- From: J French
- Re: Where is stringbuilder?
- References:
- Where is stringbuilder?
- From: brett
- Re: Where is stringbuilder?
- From: Rob Kennedy
- Re: Where is stringbuilder?
- From: J French
- Re: Where is stringbuilder?
- From: Rob Kennedy
- Re: Where is stringbuilder?
- From: J French
- Where is stringbuilder?
- Prev by Date: Re: Where is stringbuilder?
- Next by Date: Re: Adding combobox to grid?
- Previous by thread: Re: Where is stringbuilder?
- Next by thread: Re: Where is stringbuilder?
- Index(es):
Relevant Pages
|