Recent

Author Topic: string confusion. Conflicting documentation.  (Read 1056 times)

lazer

  • Full Member
  • ***
  • Posts: 215
string confusion. Conflicting documentation.
« on: October 25, 2022, 11:58:29 am »
Hi,

I'm seeing odd behaviour so I'm check my string types are used correctly.  Now I'm totally confused.

https://www.freepascal.org/docs-html/rtl/sysutils/strpcopy.html
Code: Pascal  [Select][+][-]
  1. StrPCopy
  2.  
  3. Copy an ansistring to a null-terminated string.
  4. Declaration
  5.  
  6. Source position: syspchh.inc line 39
  7.  
  8. function StrPCopy(
  9.  
  10.   Dest: PChar;
  11.  
  12.   const Source: string
  13.  
  14. ):PChar; overload;
  15.  
  16. function StrPCopy(
  17.  
  18.   Dest: PWideChar;
  19.  
  20.   const Source: UnicodeString
  21.  
  22. ):PWideChar; overload;

However if I cntl-click on StrPcopy in my code where it cals it , it opens sysunih.inc actually declares this:

Code: Pascal  [Select][+][-]
  1. function StrPCopy(Dest: PWideChar; const Source: UnicodeString): PWideChar; overload;


There is no second definition.  So where is that second definition ?  Is the hover hint mistaken or is this the save declaration which is used in compiling my code.

One of those declarations ( which used to covert a pascal string[255] type to Pchar declares the input argument as:
Code: Pascal  [Select][+][-]
  1.   const Source: string
What the hell does that mean ?  Well , it depends ....

So does that stil work as before or has it been redefined ? What is the point in a declaration which is ambiguous?

These genderfluid strings are nightmare at the best of times but if the documentation is not consistent there is no hope of controlling it.

« Last Edit: October 25, 2022, 12:14:42 pm by lazer »

bytebites

  • Hero Member
  • *****
  • Posts: 640
Re: string confusion. Conflicting documentation.
« Reply #1 on: October 25, 2022, 12:21:38 pm »
Show your problematic instead of...

lazer

  • Full Member
  • ***
  • Posts: 215
Re: string confusion. Conflicting documentation.
« Reply #2 on: October 25, 2022, 12:24:03 pm »
My "problematic" is that I have inconsistent doc.

I do not need help writing one line of code, I need to know what functions do when I use them.

Are you able to advise on the documentation problem?

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2065
  • Fifty shades of code.
    • Delphi & FreePascal
Re: string confusion. Conflicting documentation.
« Reply #3 on: October 25, 2022, 12:24:42 pm »
In your form CRTL+LeftClick on StrPCopy lead you to sysunih.inc
In sysunih.inc mark StrPCopy and press CTRL+SHIFT+ArrowDown and it lead to sysuni.inc
inside that you see how it is defined:
Code: Pascal  [Select][+][-]
  1. function StrPLCopy(Dest: PWideChar; const Source: UnicodeString; MaxLen: SizeUInt): PWideChar; overload;
  2. var Len: SizeUInt;
  3. begin
  4.   Len := length(Source);
  5.   if Len > MaxLen then
  6.     Len := MaxLen;
  7.   Move(Source[1], Dest^, Len*sizeof(WideChar));
  8.   Dest[Len] := #0;
  9.   StrPLCopy := Dest;
  10. end;
  11.  
  12.  
  13. function StrPCopy(Dest: PWideChar; const Source: UnicodeString): PWideChar; overload;
  14. begin
  15.   StrPCopy := StrPLCopy(Dest, Source, length(Source));
  16. end;

I hope it helps.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

lazer

  • Full Member
  • ***
  • Posts: 215
Re: string confusion. Conflicting documentation.
« Reply #4 on: October 25, 2022, 12:31:13 pm »
These hot key tricks are handy thanks.

That confirms that there is only one definition of strPcopy in that unit. Again contradicting the online documentation.

Is there another definition elsewhere or is the onlinedoc , indicating two forms of this function, incorrect ?

Thx

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2065
  • Fifty shades of code.
    • Delphi & FreePascal
Re: string confusion. Conflicting documentation.
« Reply #5 on: October 25, 2022, 12:36:04 pm »
These hot key tricks are handy thanks.

That confirms that there is only one definition of strPcopy in that unit. Again contradicting the online documentation.

Is there another definition elsewhere or is the onlinedoc , indicating two forms of this function, incorrect ?

Thx
There are two definitions actually. I showed you the unicode version.
The "string" version you find its first definition in syspchh.inc what lead to syspch.inc
Code: Pascal  [Select][+][-]
  1. function StrPCopy(Dest: PChar; Const Source: string): PChar;overload;
  2. begin
  3.   result := StrMove(Dest, PChar(Source), length(Source)+1);
  4. end ;
Code: Pascal  [Select][+][-]
  1.     function strmove(dest,source : pchar;l : SizeInt) : pchar;
  2.  
  3.       begin
  4.          move(source^,dest^,l);
  5.          strmove:=dest;
  6.       end;

I hope it helps.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

lazer

  • Full Member
  • ***
  • Posts: 215
Re: string confusion. Conflicting documentation.
« Reply #6 on: October 25, 2022, 12:46:04 pm »
Yes that certainly does help, but how am I supposed to know that without happening across a well meaning guru like yourself ?

The cntl-click took me to the wrong definition, with no other in sight.

The aim of that tool it to take you to the definition in the source.  Like getting it from the horse's mouth. Sadly it takes me to the wrong end of the horse.

So I'm back to what does "Const Source: string" mean ?

I'm trying to check whether my syntax is correct.  I define  msgstr:string  with {H-} ie string[255] . I'm guessing RTL is compiled  {H+} .

So does the compiler magically convert my pascal string to something else ( what is that, and where is that defined ) or do I need to append a #0 before calling that function?
« Last Edit: October 25, 2022, 12:52:00 pm by lazer »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2065
  • Fifty shades of code.
    • Delphi & FreePascal
Re: string confusion. Conflicting documentation.
« Reply #7 on: October 25, 2022, 12:50:58 pm »
A CTRL+Click lead you to what you (or the IDE) has currently choosen for your usage.
In attachment you see when you just type "StrPCopy(" that you have 2 to choose.
So it depend on your input...
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

lazer

  • Full Member
  • ***
  • Posts: 215
Re: string confusion. Conflicting documentation.
« Reply #8 on: October 25, 2022, 12:59:24 pm »
Cool , more useful tricks, but ..
Code: Pascal  [Select][+][-]
  1. unit pack;
  2.  
  3. {$mode objfpc}{$H-}
  4. {$LongStrings OFF}   ///aka $H-
  5.  
  6. interface
  7.  
  8.  type
  9.   varstring=shortstring;     // shortstring aka string[255]
  10.  
  11. ....
  12.  
  13. function strcon(s1:varstring):Pchar;
  14. {convert pascal str to the global asciz: strbuf^ , Ret ptr.}
  15. begin
  16.   s1:=s1+#0;  // strPcopy wants a "string" , which probably means ansi string.
  17.   strcon:=strpcopy(strbuf,s1);
  18. end;

Now when I cntl-click on strpcopy it takes me to the ansi version not the pascal version.

Code: Pascal  [Select][+][-]
  1. function StrPCopy(Dest: PWideChar; const Source: UnicodeString): PWideChar; overload;
  2.  
Quote
A CTRL+Click lead you to what you (or the IDE) has currently choosen for your usage.
So does that mean the IDE is broken , or I am not compiling with the options I think I'm compiling with and my gender-fluid variables are having a trans moment ?
« Last Edit: October 25, 2022, 01:02:54 pm by lazer »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2065
  • Fifty shades of code.
    • Delphi & FreePascal
Re: string confusion. Conflicting documentation.
« Reply #9 on: October 25, 2022, 01:09:23 pm »
Cool , more useful tricks, but ..
...but you try calling something where no definition for that actual exists yet.
Watch my above attachment to see how you official can call methods, while you are trying to "cheat"  ;D
Try to be as close as possible to the things that the hint is showing you and you will have no problems.
shortstring i would cast to string, like "string(Trim(myShortString))" as the "Source" parameter, but its untested, its just what my belly tells me.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Avinash

  • Full Member
  • ***
  • Posts: 118
Re: string confusion. Conflicting documentation.
« Reply #10 on: October 25, 2022, 01:14:42 pm »
Historically StrPCopy comes from Turbo Pascal's «Strings» unit.

It's better to download the pdf (rtl.pdf in that case) and just search through all the documentation:
https://www.freepascal.org/docs.html
http://downloads.freepascal.org/fpc/docs-pdf/rtl.pdf

Quote
// strPcopy wants a "string", which probably means ansi string.
Conversion is automatic, afaik.
In any case, Pascal is a typed language that will prevent you from using the wrong type (without special tricks).



lazer

  • Full Member
  • ***
  • Posts: 215
Re: string confusion. Conflicting documentation.
« Reply #11 on: October 25, 2022, 01:26:04 pm »
Yes, the strong typecasting is one of the best features of pascal.

Quote
Conversion is automatic, afaik.

There maybe some opaque trickery. I have seen code were people are adding a #0 before some calls. I don't have a link to hand but it looked like reputable sources. Is that necessary or is the compiler doing that on the quiet?

The fact that something passes a syntax check does not mean it works as I thought it would work. That is why I'm trying to get a definitive check on what I'm doing.

If there is a silent conversion going on this needs to be documented as well. So that is where I need to go next.

Avinash

  • Full Member
  • ***
  • Posts: 118
Re: string confusion. Conflicting documentation.
« Reply #12 on: October 25, 2022, 01:44:19 pm »
I have seen code were people are adding a #0 before some calls.

They probably used strings where PChar was required, like this:
Code: Pascal  [Select][+][-]
  1. S := S + #0;
  2. StrCat(Str, @S[1]);

When in doubt, just use the qualified identifier, then you will be sure where the function is used from:
Code: Pascal  [Select][+][-]
  1. Strings.StrCat(Str, @S[1]);
« Last Edit: October 25, 2022, 01:47:03 pm by Avinash »

lazer

  • Full Member
  • ***
  • Posts: 215
Re: string confusion. Conflicting documentation.
« Reply #13 on: October 25, 2022, 01:59:21 pm »
Thanks but I'm still left with this:

 
Quote
   A CTRL+Click lead you to what you (or the IDE) has currently choosen for your usage.
So does that mean the IDE is broken , or I am not compiling with the options I think I'm compiling with and my gender-fluid variables are having a trans moment ?

Avinash

  • Full Member
  • ***
  • Posts: 118
Re: string confusion. Conflicting documentation.
« Reply #14 on: October 25, 2022, 03:52:47 pm »
This is easy to check:

Code: Pascal  [Select][+][-]
  1. uses SysUtils;
  2. var
  3.   P: PChar;
  4.   B: array [0..255] of Char;
  5.   S: String;
  6. begin
  7.   P := StrPCopy(B, S);
  8. end.

This only compiles if P and B are either PChar and Char or PWideChar and WideChar (but not mixed).
In the second case, Warning is also issued: Implicit string type conversion from "ShortString" to "UnicodeString"

 

TinyPortal © 2005-2018