Rollo62 536 Posted October 21, 2020 (edited) 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 October 21, 2020 by Rollo62 Share this post Link to post
David Heffernan 2345 Posted October 21, 2020 (edited) These helpers are often just copied from .net, and .net strings are immutable. Here is the .net property: https://docs.microsoft.com/en-us/dotnet/api/system.string.chars?view=netcore-3.1 Edited October 21, 2020 by David Heffernan 1 Share this post Link to post
Rollo62 536 Posted October 21, 2020 (edited) @David Heffernan Your're spot-on, I think thats the explanation. Moreover this thought leads to the safety on consts string objects. Edited October 21, 2020 by Rollo62 Share this post Link to post
Remy Lebeau 1393 Posted October 21, 2020 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
Remy Lebeau 1393 Posted October 21, 2020 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. 1 Share this post Link to post
Kryvich 165 Posted October 21, 2020 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
FPiette 382 Posted October 22, 2020 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
Fr0sT.Brutal 900 Posted October 22, 2020 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
David Heffernan 2345 Posted October 22, 2020 What will it take for Delphi programmers to give up on the idea that strings and byte arrays are the same thing! Share this post Link to post
Guest Posted October 22, 2020 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
Rollo62 536 Posted October 22, 2020 (edited) 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. Edited October 22, 2020 by Rollo62 Share this post Link to post
FPiette 382 Posted October 22, 2020 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. 2 Share this post Link to post
Stefan Glienke 2002 Posted October 22, 2020 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? 3 1 Share this post Link to post
Remy Lebeau 1393 Posted October 22, 2020 (edited) 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. 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 October 22, 2020 by Remy Lebeau Share this post Link to post
Rollo62 536 Posted October 22, 2020 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
Kryvich 165 Posted October 22, 2020 @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
Fr0sT.Brutal 900 Posted October 23, 2020 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
Anders Melander 1782 Posted October 23, 2020 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
Rollo62 536 Posted October 23, 2020 (edited) 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 October 23, 2020 by Rollo62 Share this post Link to post
Remy Lebeau 1393 Posted October 23, 2020 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. 1 Share this post Link to post
Remy Lebeau 1393 Posted October 23, 2020 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