Recent

Author Topic: What are main differences between free pascal and c#?  (Read 3615 times)

Ally

  • Jr. Member
  • **
  • Posts: 52
Re: What are main differences between free pascal and c#?
« Reply #15 on: January 31, 2023, 04:50:39 pm »
That's how it works too.

Code: Pascal  [Select][+][-]
  1. uses
  2.   ...
  3.   ...
  4.   LazUTF8;
  5. ...
  6. ...
  7. ...
  8. procedure TForm1.Button1Click(Sender: TObject);
  9. var
  10.   s: String;
  11.   i: Integer;
  12. begin
  13.   s := '€ÄÜÖ@';
  14.   for i := 0 to UTF8Length(s) - 1 do
  15.     Memo1.Append(UTF8Copy(s, i, 1));
  16. end;

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: What are main differences between free pascal and c#?
« Reply #16 on: January 31, 2023, 05:09:25 pm »
For small strings this works quite well, but because it has to traverse the whole string to the copyable char every time, this can be quite slow on larger tests.

But unless you try to parse large textfiles like for a compiler or similar, this should be completely fine

tetrastes

  • Sr. Member
  • ****
  • Posts: 473
Re: What are main differences between free pascal and c#?
« Reply #17 on: January 31, 2023, 05:29:07 pm »
PS: Interestingly, when I remove everything from the program:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. var
  3.   s: String;
  4. begin
  5.   s := '€';
  6.   WriteLn(Length(s));
  7. end.
It still outputs 3, so it also uses UTF8 even without a codepage statement
This is because your source in UTF8, and s consists of 3 bytes (but for fpc it is in default system CP). Convert source file to CP1252, and output will be 1.

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: What are main differences between free pascal and c#?
« Reply #18 on: January 31, 2023, 05:33:46 pm »
So effectively it will always be UTF8 because no modern editor creates files in a different encoding anymore

tetrastes

  • Sr. Member
  • ****
  • Posts: 473
Re: What are main differences between free pascal and c#?
« Reply #19 on: January 31, 2023, 05:43:29 pm »
So effectively it will always be UTF8 because no modern editor creates files in a different encoding anymore
??? Modern editors (including one in Lazarus) create files in many different encodings.

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: What are main differences between free pascal and c#?
« Reply #20 on: January 31, 2023, 05:59:48 pm »
??? Modern editors (including one in Lazarus) create files in many different encodings.
In lazarus I have not found an option to change the file encoding. VSCode, another editor I use regularly, has that option quite visible, but by default it is UTF-8, and most people never change any defaults. Same for VIM, while it supports multiple encodings, any new file created with it will by default be UTF-8.

UTF-8 is the quasi standard of encodings today. Other encodings are mostly just used for backwards compatibility. So unless you specifically ask for it, you will always work with UTF-8. This means that any string processing needs to assume that the default format for text will be UTF-8.

Even Java and the Windows API, the two system that kept having UTF-16LE as their default for quite some time, switched to UTF-8 some time ago.

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: What are main differences between free pascal and c#?
« Reply #21 on: January 31, 2023, 06:26:43 pm »
Iterating through utf8 characters can get tricky though.

The following seems to work (but not everything is being displayed correctly in this forum page)

Code: Pascal  [Select][+][-]
  1. program unicode;
  2.  
  3. {$mode objfpc}
  4. {$h+}
  5. {$codepage utf8}
  6.  
  7. uses
  8.   sysutils,
  9.   types;
  10.  
  11. function utf8chars(const str_in: string): TStringDynArray ;
  12.   var
  13.     curP, endP: pchar;
  14.     len: integer;
  15.   begin
  16.     result := [];
  17.     curP := pchar(str_in);
  18.     endP := curP + length(str_in);
  19.     while curP < endP do
  20.     begin
  21.       len := Utf8CodePointLen(curP, 64, true);
  22.       setlength(result, length(result)+1);
  23.       setstring(result[high(result)], curP, len);
  24.       inc(curP, len);
  25.     end;
  26.   end;
  27.  
  28. var
  29.   chars: TStringDynArray;
  30.   str: string;
  31.  
  32. begin
  33.   chars := utf8chars('ábcdéfghíǝ́Á̊Áwow!');
  34.   for str in chars do writeln(str);
  35. end.

Code: Text  [Select][+][-]
  1. $ fpc unicode.pas
  2. Free Pascal Compiler version 3.2.2 [2022/08/17] for x86_64
  3. Copyright (c) 1993-2021 by Florian Klaempfl and others
  4. Target OS: Linux for x86-64
  5. Compiling unicode.pas
  6. Linking unicode
  7. 35 lines compiled, 0.2 sec


Code: Text  [Select][+][-]
  1. $ ./unicode
  2. á
  3. b
  4. c
  5. d
  6. é
  7. f
  8. g
  9. h
  10. í
  11. ́
  12. Á̊
  13. A
  14. w
  15. o
  16. w
  17. !


dsiders

  • Hero Member
  • *****
  • Posts: 1052
Re: What are main differences between free pascal and c#?
« Reply #22 on: January 31, 2023, 06:38:10 pm »
??? Modern editors (including one in Lazarus) create files in many different encodings.

In lazarus I have not found an option to change the file encoding.


Right Click on any source code file in the editor.
Use the File Settings > Encoding option.

Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: What are main differences between free pascal and c#?
« Reply #23 on: January 31, 2023, 08:12:44 pm »
As far as I know, C# does not target low end and limited devices as well as free pascal. (devices like 8 bit micro-controllers/microprocessors that are supported as free pascal targets).


tetrastes

  • Sr. Member
  • ****
  • Posts: 473
Re: What are main differences between free pascal and c#?
« Reply #24 on: January 31, 2023, 08:15:33 pm »
UTF-8 is the quasi standard of encodings today. Other encodings are mostly just used for backwards compatibility. So unless you specifically ask for it, you will always work with UTF-8. This means that any string processing needs to assume that the default format for text will be UTF-8.
String processing needs the ability to auto determine text encoding without assuming something. But this is different question, not related to how fpc deals with strings.

Even Java and the Windows API, the two system that kept having UTF-16LE as their default for quite some time, switched to UTF-8 some time ago.
What do you mean by Windows API? Now with SomeWinFuncA and SomeWinFuncW there is SomeWinFuncUTF8?

tetrastes

  • Sr. Member
  • ****
  • Posts: 473
Re: What are main differences between free pascal and c#?
« Reply #25 on: January 31, 2023, 08:20:07 pm »
??? Modern editors (including one in Lazarus) create files in many different encodings.
In lazarus I have not found an option to change the file encoding. VSCode, another editor I use regularly, has that option quite visible, but by default it is UTF-8, and most people never change any defaults. Same for VIM, while it supports multiple encodings, any new file created with it will by default be UTF-8.

And what? For my windows console programs I use sources in console CP, and fpc (and Lazarus) do what I need.
« Last Edit: January 31, 2023, 08:28:38 pm by tetrastes »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5448
  • Compiler Developer
Re: What are main differences between free pascal and c#?
« Reply #26 on: January 31, 2023, 08:56:53 pm »
Even Java and the Windows API, the two system that kept having UTF-16LE as their default for quite some time, switched to UTF-8 some time ago.
What do you mean by Windows API? Now with SomeWinFuncA and SomeWinFuncW there is SomeWinFuncUTF8?

The *A-APIs can deal with UTF-8 in Windows 10 since some years ago if the application has an appropriate manifest.

tetrastes

  • Sr. Member
  • ****
  • Posts: 473
Re: What are main differences between free pascal and c#?
« Reply #27 on: January 31, 2023, 09:11:23 pm »
Even Java and the Windows API, the two system that kept having UTF-16LE as their default for quite some time, switched to UTF-8 some time ago.
What do you mean by Windows API? Now with SomeWinFuncA and SomeWinFuncW there is SomeWinFuncUTF8?

The *A-APIs can deal with UTF-8 in Windows 10 since some years ago if the application has an appropriate manifest.

Thank you for the answer. But this does not mean the switch from UTF-16 as default to UTF-8 as default, am I not right? It is simply adding one more [multibyte] CP.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11384
  • FPC developer.
Re: What are main differences between free pascal and c#?
« Reply #28 on: January 31, 2023, 09:21:48 pm »
The *A-APIs can deal with UTF-8 in Windows 10 since some years ago if the application has an appropriate manifest.

1903+, and the manifest can be added by a simple click in options -> application

Quote
Thank you for the answer. But this does not mean the switch from UTF-16 as default to UTF-8 as default, am I not right? It is simply adding one more [multibyte] CP.

It makes the one byte -A apis unicode aware, roughly equal to the 2-byte -W apis. A default only matters for a A/W group each.

Besides unicode there is also a check mark to allow longer PATHS/filenames.
« Last Edit: January 31, 2023, 09:24:02 pm by marcov »

tetrastes

  • Sr. Member
  • ****
  • Posts: 473
Re: What are main differences between free pascal and c#?
« Reply #29 on: January 31, 2023, 09:42:55 pm »
Quote
Thank you for the answer. But this does not mean the switch from UTF-16 as default to UTF-8 as default, am I not right? It is simply adding one more [multibyte] CP.

It makes the one byte -A apis unicode aware, roughly equal to the 2-byte -W apis. A default only matters for a A/W group each.

Thank you for the answer. So I am right. And at my Windows 10 ver. 22H2 the option to use UTF-8 is still beta-version.

And as you wrote about manifest, I have one question about it. There are lines in manifest for supportedOS ID from Windows Vista to Widows 10. May be you know, what about Windows 11? I'm just curious. Sorry for offtopic.

 

TinyPortal © 2005-2018