Re: Where is stringbuilder?



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
.



Relevant Pages

  • System.IO.DirectoryNotFoundException: HRESULT: 0x80070003
    ... bstrUrl, String& pbstrServerRelativeUrl, String& pbstrTitle, String& ... Int16& pnCollation, UInt32& pnCollationLCID, Int16& pnCalendarType, ... Int16& pnAdjustHijriDays, Int16& pnAltCalendarType, Boolean& ... Win32 Version: 2.0.50727.42 ...
    (microsoft.public.sharepoint.portalserver)
  • Re: Unicode question
    ... You are right about the null termination being a problem, I usually use the following when interfacing with Win32: ... std::string string; ... are where I employ CString, otherwise I use std::string as much as possible. ...
    (microsoft.public.vc.language)
  • CDO exception with Web.Mail
    ... CultureInfo culture, String[] namedParameters) ... Win32 Version: 1.1.4322.573 ... CodeBase: ... To enable just in time debugging, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Delphi 2008 native?
    ... Do not forget that all http services are nothing more than string manipulation - string request and string response. ... If you have good libraries inside Delphi, and you really know Win32 API, you do not need anything more. ...
    (borland.public.delphi.non-technical)
  • Re: copy from CString to char array
    ... Nothing specific to eVC, see below... ... temporary and the result is stored in lpstrInitialDir. ... This method usually works under win32, ... char based string literal. ...
    (microsoft.public.pocketpc.developer)