Re: Where is stringbuilder?
- From: Rob Kennedy <me3@xxxxxxxxxxx>
- Date: Wed, 18 Oct 2006 12:14:46 -0500
J French wrote:
On Wed, 18 Oct 2006 01:58:03 +0100, nowhere@xxxxxxxxxxxxxx wrote:
<snip>
Win32 doesn't have a Garbage Collector, so Stringbuilders are
irrelevant.
I would guess that if you created a Win32 application in D2005/D2006,
you wouldn't have Stringbuilders either. (Or if you do, they're
probably just strings in reality).
Win32 or rather Delphi does not have a garbage collecter, but the
problem of 'Swiss Cheese' memory certainly exists.
If one /knows/ that one will be concatenation a whole load of strings,
or more precisely 'growing' just one string, then it makes sense to
create a longer string then push the smaller strings into their
destination positions.
This is what I use :-
{ ###########################################################
MidSet( L, 4, S )
}
Procedure MidSet( Var BigStr:String; Const HStart:Integer ; Const
SmallStr : String ) ;
Var
L9, BL, SL, L, Start : Integer ;
Begin
BL := Length( BigStr ) ;
SL := Length( SmallStr ) ;
Start := HStart - 1 ; { adjust start }
L := Min( SL, BL - Start ) ;
For L9 := 1 To L Do
BigStr[ L9 + Start ] := SmallStr[ L9 ] ;
End; {MidSet}
Alternatively one could use a Stream of some sort.
Personally I don't think that this is worth worrying about unless one
is doing a heck of a lot of concatenations on the same String
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.
So in .Net, we need Stringbuilder because the old way of allocating a long string and then filling it in later won't work -- it will result in just as many memory allocations as before. StringBuilder purposely uses something other than a string to holds its intermediate value, thus avoiding the problem of immutable strings.
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.
--
Rob
.
- Follow-Ups:
- 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
- Where is stringbuilder?
- Prev by Date: Re: Detecting WIN32 or .NET example in a compiler directive
- Next by Date: Re: {$ELSEIF} not working in Delphi 2006 ?
- Previous by thread: Re: Where is stringbuilder?
- Next by thread: Re: Where is stringbuilder?
- Index(es):
Relevant Pages
|