Recent

Author Topic: s0 := s1 + s2; => Pointer issue.  (Read 544 times)

paule32

  • Sr. Member
  • ****
  • Posts: 280
s0 := s1 + s2; => Pointer issue.
« on: March 01, 2024, 12:29:02 pm »
I have the following Code Snippet:

var
  s1, s2: String;
begin
  s1 := 'mufoLo';
  s2 := 'Hello World' + s1;
end;

But with my Custom RTL, I get Error during Compilation on Line: s2
=> Error: Call by var for arg no. 1 has to match exactly: Got "AnsiString" expected "Pointer"

Is there a CompilerProc available or how can I implement this ?

Thaddy

  • Hero Member
  • *****
  • Posts: 16158
  • Censorship about opinions does not belong here.
Re: s0 := s1 + s2; => Pointer issue.
« Reply #1 on: March 01, 2024, 01:10:18 pm »
look in system.fpd
If I smell bad code it usually is bad code and that includes my own code.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11935
  • FPC developer.
Re: s0 := s1 + s2; => Pointer issue.
« Reply #2 on: March 01, 2024, 01:10:27 pm »
Yeah probably something in sstrings.inc or astrings.inc

Thaddy

  • Hero Member
  • *****
  • Posts: 16158
  • Censorship about opinions does not belong here.
Re: s0 := s1 + s2; => Pointer issue.
« Reply #3 on: March 01, 2024, 01:32:03 pm »
Dead giveaway 2:
AnsiStrings have a refcount and a length stored at negative offset of ansistring[1] and all ansistrings must add an extra termination zero. (Because of casting to Pchars).
Apart from that you will have to implement refcount.

If I were you I would forget about Ansi and use shortstring, which is much easier to implement.
« Last Edit: March 01, 2024, 01:36:51 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

paule32

  • Sr. Member
  • ****
  • Posts: 280
Re: s0 := s1 + s2; => Pointer issue.
« Reply #4 on: March 01, 2024, 02:13:18 pm »
I am successfully implement the "Assign", and "Concatenate" CompilerProc:

Code: Pascal  [Select][+][-]
  1. procedure fpc_ansistr_assign(var DestS: Pointer; S2: Pointer); [public, alias: 'FPC_ANSISTR_ASSIGN']; compilerproc;
  2. var
  3.     SLen: SIZE_T;
  4. begin
  5.     SLen  := strlen( LPCSTR( S2 ) );
  6.     DestS := VirtualAlloc( nil, SLen, MEM_COMMIT or MEM_RESERVE, PAGE_READWRITE );
  7.    
  8.     if (not (DestS = nil)) then
  9.     begin
  10.         FillChar( DestS^, SLen, #0 );
  11.         move( S2^, DestS^, SLen );
  12.     end else
  13.     begin
  14.         MessageBox( 0, 'Error: fpc_AnsiStr_Assign memory allocation fail.', 'Error', 0 );
  15.         ExitProcess( 1 );
  16.     end;
  17.  
  18.     // TODO: add delete
  19.     //VirtualFree( DestS, 0, MEM_RELEASE );
  20. end;
  21. procedure fpc_ansistr_concat(var dst: String; const S1,S2 : String; cp: DWORD); compilerproc;
  22. Var
  23.     S1Len, S2Len, S3Len: SIZE_T;
  24.     DestS: Pointer;
  25. begin
  26.     S1Len := strlen( LPCSTR( S1 ) );
  27.     S2Len := strlen( LPCSTR( S2 ) );
  28.    
  29.     S3Len := S1Len + S2Len + 1;
  30.     DestS := VirtualAlloc( nil, S3Len, MEM_COMMIT or MEM_RESERVE, PAGE_READWRITE );
  31.    
  32.     if (not (DestS = nil)) then
  33.     begin
  34.         FillChar( DestS^, S3Len, #0 );
  35.  
  36.         move( PChar( S1 )^,        DestS^ , S1Len );
  37.         move( PChar( S2 )^, (PChar(DestS) + S1Len)^, S2Len );
  38.        
  39.         dst := String( DestS );
  40.     end else
  41.     begin
  42.         MessageBox( 0, 'Error: fpc_AnsiStr_Concat memory allocation fail.', 'Error', 0 );
  43.         ExitProcess( 1 );
  44.     end;
  45. end;
  46.  

This is not simply portable, because I use win32api Windows 10 API Funktion's.
But for me it is okay - I have tiny .ExE - Goal reached so far.

Okay. This could be a nogo for productive System's - I am not a professional Programmer/Developer.
But for me it is enough.

Thanks for your patient, and Hints.
« Last Edit: March 01, 2024, 02:43:45 pm by paule32 »

 

TinyPortal © 2005-2018