What am i missing here? This is how TStringHelper.Remove looks:

What am i missing here? This is how TStringHelper.Remove looks:

function TStringHelper.Remove(StartIndex, Count: Integer): string;
begin
  Result := Self;
  System.Delete(Result, StartIndex + 1, Count);
end;

The Result := Self will copy the string. Do you think this was intentional?

The TStringHelper.Replace function operates on the string itself so that renders the result i was expecting.

The docs are vague: "Removes the substring at the position StartIndex and optionally until the position StartIndex + Count, if specified, from this 0-based string."

I'd interpret "from this 0-based" that the function operates on the string "object" itself.

I think i'll refrain from TStringHelper in the same way i do from With. Especially as long as they are a pain to debug.

Comments

  1. The function returns a string. Also, the input string (self) could be a const.  And, all Stringhelper functions are emulating 0-based strings in preparation to the future.

    ReplyDelete
  2. Oliver, i understand 0- and 1-based accesses.

    I'm also beginning to understand that TStringHelper does not operate on the Self - perhaps that is the way to think about it.

    Thanks.

    ReplyDelete
  3. Afaik all the TStringHelper methods treat Self as immutable.

    ReplyDelete
  4. Oliver Funcke  "emulating 0-based strings in preparation to the future" 
    We are so lucky to have a so bright future.
    This is a feature I desperately expected and pray for every night before going to bed.
    Next step would be to make all strings immutable (just because Java or C# do), and break all the COW benefits.
    All this is non-sense...

    ReplyDelete
  5. A. Bouchez I see your point. But I'm the wrong addressee. I was only describing an existing behaviour.

    ReplyDelete
  6. Oliver Funcke It was not about you, of course. You were just testifying the official Embarcadero decisions about the languages. So your comment was just right and appropriate.
    Those are those decisions which I wanted to react to. You are doing just great. Sorry if you felt offended.

    ReplyDelete
  7. Stefan Glienke You are right.
    BTW, the fact that TStringHelper treat self as immutable is another testimony of the potential "string immutability" we were talking to.
    As a result, this TStringHelper.Remove pseudo-method would be much less efficient than the system.Delete() function, since both strings would remain in memory, so there would be a new memory allocation, whereas delete() is able to change the original string content in place (if its refcount=1, which is very likely).
    If the string is several MB long, the system.Delete() would be very fast (just a move in-place of the right part of the string, with FastMM4 doing in-place reallocation), whereas TStringHelper.Remove() would use twice more memory, and would always move the whole string content.
    When I read in the official EMB documentation that "older System unit string-handling functions are deprecated and support might be removed in the future", I really think: are those people crazy enough to shot themself in the foot? Source: http://docwiki.embarcadero.com/Libraries/XE8/en/System.SysUtils.TStringHelper

    ReplyDelete
  8. A. Bouchez Removing one of the unique features of Delphi sounds like a good plan, no?

    ReplyDelete
  9. Stefan Glienke If the "feature" is 0-based  string indexing, I remember already having seen this discussion some time ago.
    The 1-indexed string is not a feature, this is a convention.
    Just like driving your car on the left side or the ride side on the road.
    If you know which side you have to drive on, it is perfectly safe. And it is not difficult to do the switch.

    If the "feature" is COW, I would not exchange it with immutable strings. They could be real PITA about performance, and I have seen huge amount of obfuscated code in C# and Java just to circumvent them.
    So I disagree, removing COW is NOT a good plan.

    ReplyDelete
  10. Arnaud, calm down, give your mormot a hug and realize that I was being sarcastic :)

    ReplyDelete
  11. Stefan Glienke  Yes, after a small hug, I feel better.
    :)

    ReplyDelete

Post a Comment