[fpc-pascal] Remove last character from pchar

2021-06-10 Thread Darius Blaszyk via fpc-pascal
Hi,

I am a bit struggling with what should be fairly simple. I have a pchar
where I am adding and removing characters. What I did so far is to allocate
new memory every time and copying the source pchar over to a new one to
append or delete a character. This required two helper variables in order
to allow for the swap. This does not seem to be very efficient to me and I
was wondering if I am missing anything?

What also confused me in the beginning that assigning a string to a pchar
automatically assigns memory for that pchar. Is there a way to control
this? e.g. to switch this off and generate a compiler error or runtime
error when this happens? I'd like to prevent runtime errors or memory leaks
instead.

Thank you very much!

Rgds, Darius
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Remove last character from pchar

2021-06-10 Thread James Richters via fpc-pascal
Pchars are just a pain, I do not use any Pchars.. instead I use AnsiStrings 
that I can manipulate in a way that is easy and then I just do 
Pchar(myvariable) whenever I need to use a function that needs a Pchar… like 
this:
 
Var 
   MessageBoxText:Ansistring;
 
..
..
windows.messagebox(0,pchar(MessageBoxtext)…..bla bla bla);  
 
 
There is probably a better way but for me conversion to Pchar is a small price 
to pay for easy string manipulation.  AnsiStrings take care of their own memory 
allocation and the Pchar conversion doesn’t require an actual Pchar variable to 
bother with managing.
 
James
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Remove last character from pchar

2021-06-10 Thread James Richters via fpc-pascal
Also StrPCopy will take the contents of an existing PChar and put it in an 
AnsiString…
And sometimes I have regular pascal strings I want to pass to something as a 
Pchar, so then I do:
Pchar(AnsiString(mystring));
But lately I’ve been just making all strings AnsiStrings anyway.
 
 
James
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Remove last character from pchar

2021-06-10 Thread Jean SUZINEAU via fpc-pascal

I think you could just manage the end of your string with a #0 char.

Just allocate enough space for the maximum size of your string plus the 
terminal #0 char.


For example

program Project1;
{$mode objfpc}{$H+}
uses
  SysUtils, Classes;
var
   s: array[0..30] of char;
   p: PChar;
begin
 p:= @s;
 StrPLCopy( p, 'Test', 30);
 Writeln( p);
 s[3]:= #0;
 Writeln( p);
end.

produces

$ ./project1
Test
Tes
$
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Remove last character from pchar

2021-06-10 Thread Jean SUZINEAU via fpc-pascal
Note: if your string is UTF8 encoded, the last "character" can be 
encoded, can span over 1 to 4 "Char", you need to know the length of 
your character.


For example:

program Project1;
{$mode objfpc}{$H+}

uses
  SysUtils, Classes;
var
   s: array[0..30] of char;
   p: PChar;
   i: Integer;
begin
 p:= @s;
 StrPLCopy( p, 'Test€a', 30);
 Writeln( p);
 for i:= 0 to 7
 do
   Write( ord(s[i]),':',s[i],' ');
 Writeln;
 s[5]:= #0;
 Writeln( p);
 for i:= 0 to 7
 do
   Write( ord(s[i]),':',s[i],' ');
 Writeln;
end.

gives

$ ./project1
Test€a
84:T 101:e 115:s 116:t 226:� 130:� 172:� 97:a
Test�
84:T 101:e 115:s 116:t 226:� 0: 172:� 97:a
$

(the € character which spans over 3 chars is broken and so the end of 
the string becomes unreadable)


___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal