Re: Where is stringbuilder?



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
.



Relevant Pages

  • Re: Efficiently passing strings?
    ... > I'm instantiating the same string object twice (once for the parameter ... first is probably fine after optimization. ... compiler; there's really no other way to tell. ... using a reference exposes some of your implementation. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Multiple Inheritance with Interfaces
    ... I cast one to the Interface it compiled, ... it compiles ok only because the java compiler does not ... It is very hard for a compiler to determine what to do in this case, because a carefully timed thread could plant a String into mumble between line 1 and line 2, allowing line 2 to execute without error. ... but with reference expression treated as being capable of referencing ...
    (comp.lang.java.programmer)
  • Re: Object reference not set to an instance of an object
    ... TObject" you're declaring a reference to an instance of TObject. ... The compiler can't know what ... The Y TObject variable is just a reference to an object. ... In the Win32 personality of Delphi, that new string is actually a nil ...
    (alt.comp.lang.borland-delphi)
  • Re: comunication between JScript and C#
    ... public function Eval(expr: String): String ... If you have a metadata reference, ... JScript.NET compiler knows about MyNS and you can directly do: ... is there another way to use the jscript function ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Array of strings
    ... > pointers to the same constant string. ... When you assign the literal to s, the compiler calls LStrLAsg. ... perform any reference counting on the source string. ...
    (comp.lang.pascal.delphi.misc)