Recent

Author Topic: [Solved] Relationship between high(ashortstring) and length(ashortstring)  (Read 8950 times)

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Arioch

  • Sr. Member
  • ****
  • Posts: 421
Re: Relationship between high(ashortstring) and length(ashortstring)
« Reply #16 on: September 10, 2022, 05:42:40 pm »
https://wiki.lazarus.freepascal.org/Shortstring

Even that link says ShortString = String[255]; :-)

But you see, there are two kinds of people. Or, perhaps, we all assume two approaches towards different problems.

One approach is "holistic understanging, "grokking". You have to understand the whole set of interrelated concepts and their cooperations/interference. Then you easily see every specific case.
Another is just "memorising hich buttons to press to make lightbulb go alight".

The former needs a lot of time and effort before you get started doing something.
The latter means you do thign without understanding and get confused anywhere outside 2-3 memorized patterns.

I think lot of articles and forum posts (back in NNTP not WWW even)  since 1997 dicussed at length how Long String are different from Short String in storing and behavior.
But - who would want to read it? Actually, would you want - you only would have found and read.

So, Laz Wiki focuses on the latter approach. They try to give 2-3 working pattrns to people who do want to use without understanding.
Then youi have a disjoint patchworks with unfilled gaps between those 2-3 patterns.

You just can not have both, you can not make people invest time and effort into learning inner details of libraries 1985-1995, when they do not want it, and when they would run away from any article longer than two paragraphs.

Whether that wiki article was worth making i am not sure. To me it rather creates "illusion of safety", illusion of understanding.But read the Delphi docs linked above, maybe it would make more sense.

The problem is, the dicsussion "in depth" what are short and long strings and why they ended up being what they are, it is all nobrainers AFTER you understood the concepts of managed types, of heap memory, of manual heap control vs ARC vs GC. If you do - it is no brainer. BBut if you do not - then you would have to read a lot on heap management before you start coding some simple program with strings. And no one would want it

avk

  • Hero Member
  • *****
  • Posts: 826
Re: Relationship between high(ashortstring) and length(ashortstring)
« Reply #17 on: September 10, 2022, 05:52:55 pm »
Just curious, what does the FPC documentation say about the mentioned functions (Low(), High())?
https://www.freepascal.org/docs-html/rtl/system/low.html
https://www.freepascal.org/docs-html/rtl/system/high.html

Oh, thank you very much.
To me, the first line for Low() looks like this:
Quote from: Low
Return lowest index of open array or enumerated

Couldn't see anything about strings there.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Relationship between high(ashortstring) and length(ashortstring)
« Reply #18 on: September 10, 2022, 05:54:14 pm »
Just curious, what does the FPC documentation say about the mentioned functions (Low(), High())?
https://www.freepascal.org/docs-html/rtl/system/low.html
https://www.freepascal.org/docs-html/rtl/system/high.html

Oh, thank you very much.
To me, the first line for Low() looks like this:
Quote from: Low
Return lowest index of open array or enumerated

Couldn't see anything about strings there.
Background info: a string is an array of char
You are welcome!
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Relationship between high(ashortstring) and length(ashortstring)
« Reply #19 on: September 10, 2022, 05:56:19 pm »
But you see, there are two kinds of people.
For me there are 10 kind of people.
The one that understand binary and the other  :P
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Fred vS

  • Hero Member
  • *****
  • Posts: 3937
    • StrumPract is the musicians best friend
Re: Relationship between high(ashortstring) and length(ashortstring)
« Reply #20 on: September 10, 2022, 05:56:42 pm »
https://wiki.lazarus.freepascal.org/Shortstring
Even that link says ShortString = String[255]; :-)

Yes, I dont contest that, you are right.
But if you go in bottom of page:
Quote
Caveats
Warning: low(myShortString) returns 0, i. e. the index of the length Byte, not the first character’s index. Likewise, high(myShortString) always returns 255. Use length instead.

Cool please, it is a fpc-Lazarus forum here.

Did you try this and compare the compilation-result with fpc/Delphi?

Code: Pascal  [Select][+][-]
  1.  program cadena;
  2. {$mode objfpc}{$H-}
  3. var
  4.     Frase: String;
  5. begin
  6.    WriteLn(low(Frase));
  7.   WriteLn(High(Frase));
  8.   WriteLn(Length(Frase));
  9.  
  10.   Frase := 'Esto es una prueba';
  11.  
  12.   WriteLn(low(Frase));
  13.   WriteLn(High(Frase));
  14.   WriteLn(Length(Frase));
  15.  
  16.   WriteLn(Frase[20]);  // updated, no warning
  17.  
  18.   Readln()
  19. end.

You will see that fpc not = Delphi, and it is good, but I only want to understand those differences.
« Last Edit: September 10, 2022, 09:42:37 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Arioch

  • Sr. Member
  • ****
  • Posts: 421
Re: Relationship between high(ashortstring) and length(ashortstring)
« Reply #21 on: September 10, 2022, 05:58:43 pm »
i even used to take Spanosh lessons in school, but it was long ago and i forgot everything.

show me code and translation of errors. I was writing from memory and frankly do not feel like making new test projects.

as for me it is all obvious, because i know what are short strings and long strings in memory byte by byte, and i used to work with them from assembler code too.

In brief:

  • string[xxx <= 255]  is essentially array[0...x] of AnsiChar with compiler magic and syntactic sugar added on top of it. Those are called short strings.
  • again, for the sake of having sugar, there is an alias type, ShortString, equal to the most long possible short string, that is string[255]
  • short strings max length limits comes from the fact that s[0] was used as current length variavle. It was a handy trick back then, but Ord(High(AnsiChar)) = 255. There was no reason to allow people having larger strings then - memory was small in 16bit computers, and allocating like 10% of computermemory just to stoe 10 letters in it - that should not had been encouraged! Want longer data - code for it remembering how small is your computer memory!
  • long strings you can start imagining as pointers to records: string is similar to ^record ReferenceCounter, CurrentLength, AllocatedMaxLength: integer; LettersBuffer: Pointer end; - this is simplified and not accurate, but it is the starter.

Imagine that Delphi/FPC had no string types, at all. (Original Wirth's 1977 Pascal can be said not having strings, in modern understanding). Think then, who would you work wit hdata structures above. What would be a := b; what would be a := b + c;  what would be a := ''; - how would you code this yourself if there was not compiler magic


Then there can be some small details added, like CodePage field in the record since Dephi 2009, like Microsoft B_STR aka Delpih WideString having no reference counters, how WideChar is twice as arge as AnsiChar - and how real long strings do not have Buffer pointer, but make special voodoo (compiler magic) instead to keep data frame within letters buffer, etc.

But are you now feeling thrilled or dizzy, drowning in those so many petty details? :-)

That all is really very easy, but you have to build a background first. GetMem/FreeMem vs ARC and manual impementation of your own strings (no need to actually do, but serious thinking through is needed).
« Last Edit: September 10, 2022, 06:20:43 pm by Arioch »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Relationship between high(ashortstring) and length(ashortstring)
« Reply #22 on: September 10, 2022, 06:05:17 pm »
Couldn't see anything about strings there.
The better answer would be found in here https://www.freepascal.org/docs-html/rtl/system/length.html
So please stop using High/Low for string types at all.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Arioch

  • Sr. Member
  • ****
  • Posts: 421
Re: Relationship between high(ashortstring) and length(ashortstring)
« Reply #23 on: September 10, 2022, 06:13:17 pm »
Did you try this and compare the compilation-result wth fpc /Delphi  of this?

Nope, and won't bother. There is nothing interesting for me in it.
Whan a child goes out of the house first time and sees the very first streetin his life - counting houses is interesting. So is counting floors in houses, windows. It is first time information gathering.
But after you saw a dozen or so streets it is no mor interesting. You got idea what is street, what is house, what kind of houses are there and are not. Now regular streets and houses are no more thrilling.
Let's go into that street A and count houses there! It would be different from streets B and street C. Well, no, just no. Yes, different. No, not thrilling.

Code: Pascal  [Select][+][-]
  1.  program cadena;
  2.     Frase: String;
  3.  
  4.   WriteLn(Frase[500]);
  5.  

This should be dependent on the Boundary Checking option ( {$B+{ if i remember, maybe not)

You essentially just wrote
Quote
var Frase: array [0..255] of byte;
begin
   Frase[500]
end;

You obviously try to read outside boundaries of array.
What it would lead to?

Well, maybe compiler would detect it and prohibit it before running.

Maybe compiler would let it go, but in the runtime there would be hidden code to check boundaries and it would through array boundary error.
Then, depending on settings, this can be done TurboPascal way ( calling RunError( NNN ), writing the error to terminal and halting the program altogether), or Delphi way (througing exception EBoundsError or something, but not terminating program)

Maybe compiler would not bother at all, and you would read some random memory cell outside the array. Then what?

If the operating system has memory protection and the array was the last variable, then you ould be reading virtual memory that does not even exist. Then operating system would claim General Protection Fault, or Access Violation, or SegFault signal or whatever OS designers decided to name the error, and OS would kill the program. Do i want to know what name was chosen for it in your OS? No. It is not important.

If the operating system workd with real memory, or there is no operating system at all, or there are many more variables after that array - then what? they you would read out the memory of some other variable instead of array.


There specific route of those would mostly be dependent on compiler settings, including compile-time checks enabled, runtime checks enabled and tunrime operating system.

Am i thrilled to know which set of compiler options you set on your machine? No more than counting houses in the street you live. That is just a random number
with no special importance.

IF you are thrileld ot learn it - i am hapyp for you. Enjoy this discovery. Delphi RTL sources, FPC RTL souces, assmebler-level debugging your program - it all can be very inspiring.

https://stackoverflow.com/questions/1194339/delphi-strings-in-records-bigger-than-255-chars

https://word-sentences.com/code-examples/delphi-strings-in-records-bigger-than-255-chars/

https://docwiki.embarcadero.com/RADStudio/Sydney/en/String_Types_(Delphi)

It surely is thrilling when you dive into it first time - but only if you have a knack for low-level close-to-metal coding. If you do - start your adventure. If you do not - jsut ignor the clues.
« Last Edit: September 10, 2022, 06:29:44 pm by Arioch »

Fred vS

  • Hero Member
  • *****
  • Posts: 3937
    • StrumPract is the musicians best friend
Re: Relationship between high(ashortstring) and length(ashortstring)
« Reply #24 on: September 10, 2022, 06:25:45 pm »
Did you try this and compare the compilation-result wth fpc /Delphi  of this?
Nope, and won't bother. There is nothing interesting for me in it.
Whan a child goes out of the house first time and sees the very first streetin his life - counting houses is interesting. So is counting floors in houses, windows.

Ha, ok, sorry, I am still a child and hope to remain so all my life and always be ready to discover new things.

Thank you to all for that very interesting discussion.

Fre;D
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Arioch

  • Sr. Member
  • ****
  • Posts: 421
Re: Relationship between high(ashortstring) and length(ashortstring)
« Reply #25 on: September 10, 2022, 06:32:06 pm »
and always be ready to discover new things.

That is the key. They are NEW things for you.
Yet for me they are just (N+1)st street with the same standard houses as in any other street.
If it is thrilling you - great. But for me "NEW thing" has to be something different, something new for me not for you.

avk

  • Hero Member
  • *****
  • Posts: 826
Re: Relationship between high(ashortstring) and length(ashortstring)
« Reply #26 on: September 10, 2022, 06:43:03 pm »
Just curious, what does the FPC documentation say about the mentioned functions (Low(), High())?
https://www.freepascal.org/docs-html/rtl/system/low.html
https://www.freepascal.org/docs-html/rtl/system/high.html

Oh, thank you very much.
To me, the first line for Low() looks like this:
Quote from: Low
Return lowest index of open array or enumerated

Couldn't see anything about strings there.
Background info: a string is an array of char
You are welcome!

Just in case, the compiler strongly disagrees with you:
Code: Pascal  [Select][+][-]
  1. ...
  2. procedure Test(const s: array of char);
  3. begin
  4. end;
  5. ...
  6. var
  7.   s: shortstring = '1234567';
  8.  
  9. Test(s);//oops... Error: Incompatible type for arg no. 1: Got "ShortString", expected "{Open} Array Of Char"
  10.  
  11.  

Fred vS

  • Hero Member
  • *****
  • Posts: 3937
    • StrumPract is the musicians best friend
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #27 on: September 10, 2022, 07:04:09 pm »
   
Quote
Background info: a string is an array of char
 
Quote
Just in case, the compiler strongly disagrees with you:

@avk: I think you get it, the confusion given is somewhere there in the wiki-shortstring.
Maybe low() and high() mention should be removed or updated in wiki.
« Last Edit: September 10, 2022, 10:29:53 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

avk

  • Hero Member
  • *****
  • Posts: 826
Re: [Solved] Relationship between high(ashortstring) and length(ashortstring)
« Reply #28 on: September 10, 2022, 07:40:05 pm »
It would be very interesting to know the opinion of the devs about this.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Relationship between high(ashortstring) and length(ashortstring)
« Reply #29 on: September 10, 2022, 11:37:34 pm »
Just in case, the compiler strongly disagrees with you
:-[ It was not literal meant that way  :o
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

 

TinyPortal © 2005-2018