Forum > General
Base of string and dynamic array types
jcmontherock:
I believed that function Low() and High() were only designed for dynamic array (?). String is not a dynamic array.
Thaddy:
strings belong to the array family, only by convention they start at 1.
by that convention, all string types have their first usuable character as char=string[1]
That is, only when converting some delphi code above XE and below 10.4 you would need the conditional I referred to. Delphi reversed it later back to index 1.
On shortstrings, like Mark uses, Index 0 holds the string length as byte. with other string types that information is a negative offset too, but 0 is valid.
Basically delphi restored its behavior with strings back to normal 1 indexing in 10.4.
In FreePascal it was always optional. You may assume the first valid char in a string is at string[1] in both dialects.
With hindsight it is a good thing that {$zerobasedstrings on} was never properly documented. Units that use it should be marked either platform or deprecated.
That leaves us with just Low() on strings being counter-intuitive.
PascalDragon:
--- Quote from: MarkMLl on June 29, 2024, 09:44:11 pm ---As an experiment, I've just written this:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type String31= string[31]; procedure experiment; var i: integer; begin i := Low(String31); i := High(String31)end;
The result of the Low() operation was zero, while the first valid character is 1.
What operation can be applied to a string type to return 1, and a dynamic array type to return 0?
--- End quote ---
String[N] is always a ShortString, thus the element at index 0 is the length of the string and valid for usage (both read and write). For non-ShortString types Low() will return 1 (unless $ZeroBasedStrings is enabled).
jcmontherock:
which differences with AnsiString (not short string I believe) ?
Thaddy:
ansi, unicode wide strings all start at 1. The Pchar types not, except cast:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$MODE OBJFPC}{$APPTYPE CONSOLE}var a:AnsiString = 'testa'; b:UnicodeString = 'testu'; c:WideString = 'testw'; d:UTF8String = 'test8'; e:PAnsiChar = 'testPA'; f:PWideChar = 'testPW';begin writeln(low(a)); writeln(low(b)); writeln(low(c)); writeln(low(d)); writeln(low(AnsiString(e))); // needs cast. writeln(low(WideString(f)));end.
Navigation
[0] Message Index
[#] Next page
[*] Previous page