Recent

Author Topic: Quick question about the string type  (Read 585 times)

djongepier

  • Newbie
  • Posts: 6
Quick question about the string type
« on: November 29, 2024, 12:49:36 pm »
Hello everyone, finaly really beginning to practice with Free Pascal.

Given this function, is it memory leaking because I did not free the string after initialization?

Code: Pascal  [Select][+][-]
  1. function reverseName(name: string):string;
  2. var
  3.   temp: string;
  4.   c: char;
  5. begin
  6.   temp := String.Create('');
  7.   for c in name do
  8.     temp.Insert(-1, c);
  9.   reverseName := temp;
  10. end;
  11.  

What would be the 'better' way to do this?

cdbc

  • Hero Member
  • *****
  • Posts: 1655
    • http://www.cdbc.dk
Re: Quick question about the string type
« Reply #1 on: November 29, 2024, 01:00:25 pm »
Hi
The 'String' type is a /managed/ type, i.e.: automatic memory-management by the compiler via ref-counting.
So it doesn't take a constructor per se, but just an assignment like so:
Code: Pascal  [Select][+][-]
  1. ...
  2. var S: string;
  3. begin
  4.   S:= ''; // init to empty
  5.   S:= 'Fpc & Lazarus';
  6.   writeln(S);
  7. end; // here the compiler sees that S goes out of scope and free's it.
  8.  

That means your code would look like this:
Code: Pascal  [Select][+][-]
  1. function reverseName(name: string):string;
  2. var
  3.   temp: string;
  4.   c: char;
  5. begin
  6.   temp := ''; // no constructor
  7.   for c in name do
  8.     temp.Insert(1, c);
  9.   reverseName := temp;
  10. end;
  11.  
eta: NOTE: This function will *only* work with 7 bit chars < ord(127)!
For UTF8 and Unicode it will screw up the character sequences...

Regards Benny
eta2: See dseligo's answer...
« Last Edit: November 29, 2024, 01:07:20 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

dseligo

  • Hero Member
  • *****
  • Posts: 1408
Re: Quick question about the string type
« Reply #2 on: November 29, 2024, 01:02:41 pm »
I never used construction 'String.Create'.
In Pascal you can just write:
Code: Pascal  [Select][+][-]
  1. temp := '';

In case you could have UTF8 characters in your string, I would do it like this:
Code: Pascal  [Select][+][-]
  1. ...
  2. uses LazUTF8;
  3. ...
  4. function reverseName(name: string):string;
  5. var i: Integer;
  6. begin
  7.   Result := '';
  8.   For i := UTF8Length(name) downto 1 do
  9.     Result := Result + UTF8Copy(name, i, 1);
  10. end;

djongepier

  • Newbie
  • Posts: 6
Re: Quick question about the string type
« Reply #3 on: November 29, 2024, 01:20:38 pm »
Thank you for all the answers, very insightfull.

My background is rookie Python  ::)  (like bare minimum knowledge) and to need to think about UTF8 is new for me.

I'll practice some with your awnsers to really understand it.

wp

  • Hero Member
  • *****
  • Posts: 12459
Re: Quick question about the string type
« Reply #4 on: November 29, 2024, 01:33:57 pm »
Or:
Code: Pascal  [Select][+][-]
  1. uses
  2.   LazUnicode;
  3.  
  4. function ReverseName(AName: String): String;
  5. var
  6.   ch: string;
  7. begin
  8.   Result := '';
  9.   for ch in AName do
  10.     Resule := ch + Result;
  11. end;

Thaddy

  • Hero Member
  • *****
  • Posts: 16174
  • Censorship about opinions does not belong here.
Re: Quick question about the string type
« Reply #5 on: November 29, 2024, 01:54:24 pm »
String is not a distinct type.
It used to be shortstring, then ansistring then utf8string then utf16string.
pure pascal translates to:
Code: Pascal  [Select][+][-]
  1. program revstr;
  2. {$mode objfpc}
  3.  
  4. function ReverseString(const S: String): String;
  5. var
  6.   I: Integer;
  7. begin
  8.   Result := '';
  9.   for I := Length(S) downto 1 do
  10.     Result := Result + S[I];
  11. end;
  12.  
  13. var
  14.   Original, Reversed: String;
  15. begin
  16.   Original := 'Hello, World!';
  17.   Reversed := ReverseString(Original);
  18.   WriteLn('Original: ', Original);
  19.   WriteLn('Reversed: ', Reversed);
  20. end.
This should work for any string type....
[edit]most of the previous answers were correct, but did not cover "string"
« Last Edit: November 29, 2024, 02:15:43 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

wp

  • Hero Member
  • *****
  • Posts: 12459
Re: Quick question about the string type
« Reply #6 on: November 29, 2024, 02:55:40 pm »
This should work for any string type....
No. It will break utf8 code points.

djongepier

  • Newbie
  • Posts: 6
Re: Quick question about the string type
« Reply #7 on: November 29, 2024, 02:58:29 pm »
Thanks again, lots of ways to achieve the same thing.

Now seeing the special 'Result' variable again reminded me of it's existence.

I'm also using a copy Lazarus within WSL2 (via fpcupdeluxe) only as a way to install the tooling, I'm using FreePascal Toolkit within VSCode to code and compile on my Work laptop.

For my personal computer running Fedora I'm also using fpcupdeluxe to get the software, but run Lazarus IDE on the desktop.

Bart

  • Hero Member
  • *****
  • Posts: 5467
    • Bart en Mariska's Webstek
Re: Quick question about the string type
« Reply #8 on: November 29, 2024, 04:07:01 pm »
There is a ReverseString() function in StrUtils if I'm not mistaken.
And a ReverseStringUtf8 in LazUtf8 (or maybe LazStrUtils?)

And, you can always look at The Slowest Pascal ReverseString competition, just to see how not to implement it  :)

Bart
« Last Edit: November 29, 2024, 04:09:03 pm by Bart »

Thaddy

  • Hero Member
  • *****
  • Posts: 16174
  • Censorship about opinions does not belong here.
Re: Quick question about the string type
« Reply #9 on: November 29, 2024, 05:13:31 pm »
Hm,
you were right about utf8.
but my example doesn't get as slow as I want it...
I should probably drop the downto... :P
« Last Edit: November 29, 2024, 05:16:00 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

 

TinyPortal © 2005-2018