Jump to content
Rollo62

Is the missing System.TStringHelper SetChars in Chars property on purpose ?

Recommended Posts

Hi there,

 

I always try to use 0-based strings in new code, making it more compatible  with mobile platforms.

What always nag's me there:

    property Chars[Index: Integer]: Char read GetChars;

...

{$ZEROBASEDSTRINGS ON}
function TStringHelper.GetChars(Index: Integer): Char;
begin
  Result := Self[Index];
end;

Why on earth Embarcadero did not implement the according SetChars function as well ?

Is there any deeper conceptional issue I'm blind to see ?

 

 

Edited by Rollo62

Share this post


Link to post

@David Heffernan

Your're spot-on, I think thats the explanation.


Moreover this thought leads to the safety on consts string objects.

 

Edited by Rollo62

Share this post


Link to post
7 hours ago, Rollo62 said:

I always try to use 0-based strings in new code, making it more compatible  with mobile platforms.

You do know that is no longer necessary starting with RAD Studio 10.4, don't you?  Embarcadero has moved mobile platforms away from ARC memory management for objects, and has done away with all of the NEXTGEN compiler features, which includes 0-based string indexing on mobile.  Mobile now defaults to 1-based indexing, just like desktop platforms.  If you really want to continue using 0-based string indexing, you can use {$ZEROBASEDSTRINGS ON}, or TStringHelper.

Share this post


Link to post
7 hours ago, David Heffernan said:

These helpers are often just copied from .net, and .net strings are immutable.

FYI, Embarcadero was considering making Delphi strings immutable in the NEXTGEN compilers, but they ultimately decided against that.

 

  • Like 1

Share this post


Link to post

Delphi already has the best string type, there is nothing to improve. But if they remove the code page identifier from each instance of a string in memory, the type will be even better. Because then all unwanted implicit string conversions will be gone.

Share this post


Link to post
8 hours ago, Kryvich said:

if they remove the code page identifier from each instance of a string in memory, the type will be even better.

Better for you, but not for user of the code page feature...

Share this post


Link to post
2 hours ago, FPiette said:

Better for you, but not for user of the code page feature...

I suppose he meant that any other text should be processed as buffers just like JS, .Net etc do.

Share this post


Link to post
Guest
11 hours ago, Kryvich said:

Delphi already has the best string type, there is nothing to improve. But if they remove the code page identifier from each instance of a string in memory, the type will be even better. Because then all unwanted implicit string conversions will be gone.

Watch this if you got time, it is very informative, (notice they called it Pascal string as it should)

https://www.youtube.com/watch?v=kPR8h4-qZdk

 

 

Share this post


Link to post
19 hours ago, Remy Lebeau said:

You do know that is no longer necessary starting with RAD Studio 10.4, don't you?  Mobile now defaults to 1-based indexing, just like desktop platforms.  If you really want to continue using 0-based string indexing, you can use {$ZEROBASEDSTRINGS ON}, or TStringHelper.

Thanks Remy, yes I'm aware of that fact.

But dislike the 1-based approach very much (maybe from my old C/C++ background), and it leads always to headaches.

I also try to avoid the {$ZEROBASEDSTRINGS ON} to keep everything most compatible.

 

Especially I dislike mixing of 0- and 1-based in string and array access, so thats why I try to keep everything at 0-based, if possible,
not to have the need for two different mindsets in place.

Moreover the elegant index checking via Cardinal, as @Marat1961 described here (this was the one) is not possible in 1-based,
which I use a lot, but rarely on 1-based strings for obvious reasons.

 

The missing SetChars method is really annoying, because that means I would have to mix 0- and 1-based stuff in the String[] / Chars itself to gain read and write access, which I dislike even more. :classic_sad:

 

Edited by Rollo62

Share this post


Link to post

In my opinion, using zero based strings is a source of a lot of headaches. I was very upset when Embarcadero introduced that for mobile platform. Now I'm happy they abandoned the idea.

  • Like 2

Share this post


Link to post
17 hours ago, Kryvich said:

Delphi already has the best string type, there is nothing to improve.

I really lol'ed at that one - stockholm syndrome anyone? :classic_blink:

  • Like 3
  • Haha 1

Share this post


Link to post
Quote

The missing SetChars method is really annoying, because that means I would have to mix 0- and 1-based stuff in the String[] / Chars itself to gain read and write access, which I dislike even more. :classic_sad:

That is easy enough to account for by using Low(String), eg:

var
  s: String;
  c: Char;
...
c := s.Chars[ZeroBasedIndex];
s[Low(s)+ZeroBasedIndex] := ...;

Low(s) will be 0 for {$ZEROBASEDSTRINGS ON}, and 1 for {$ZEROBASEDSTRINGS OFF}.

Edited by Remy Lebeau

Share this post


Link to post

Exactly thats the code what I dislike, then better  to have pointer math.

But of coarse I  have to use like that a lot too.

 

 

Share this post


Link to post

@Stefan Glienke I have always considered strings with reference counting and automatic memory allocation as one of Delphi/Pascal's best features. This is one of those features that makes it worth staying on Delphi and not switching to NET or Java.

Share this post


Link to post
14 hours ago, Remy Lebeau said:

That is easy enough to account for by using Low(String), eg:

Available since XE6-7 IIRC.

Share this post


Link to post
22 hours ago, Rollo62 said:

I also try to avoid the {$ZEROBASEDSTRINGS ON} to keep everything most compatible

Compatible with what?

Share this post


Link to post
34 minutes ago, Anders Melander said:

Compatible with what?

With the default IDE settings.

I usually work with the intended settings, without changing too much.

This is for getting valid support answers.

If someone changes everything and then something odd happens,

he cannot expect to get reasonable answers from people which use the default settings.

 

I assume the default settings are what the Embarcadero developers use too.

 

Edited by Rollo62

Share this post


Link to post
11 hours ago, Fr0sT.Brutal said:

Available since XE6-7 IIRC.

(Low|High)(string) were introduced in XE3, the same version that introduced {$ZEROBASEDSTRINGS} and TStringHelper.

  • Thanks 1

Share this post


Link to post
9 hours ago, Rollo62 said:

With the default IDE settings.

That makes no sense.  {$ZEROBASEDSTRINGS} is local to the unit only.  Its use depends on whether YOU want to access YOUR strings inside YOUR unit using 0-based or 1-based indexes, The IDE settings have nothing to do with that.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×