Recent

Author Topic: What is WideChar on Free Pascal?  (Read 3041 times)

JiaXing

  • Jr. Member
  • **
  • Posts: 75
What is WideChar on Free Pascal?
« on: April 25, 2020, 04:30:23 am »
I want to interface with C code. Does WideChar is char16_t on C? If it's so it's UTF16. What about UTF8 on Free Pascal? I have read the documents on both Free Pascal wiki, Lazarus wiki and even the Free Pascal programmer guide but I still found it vague.
I'm subscribed to the church of 440bx. Say no to OOP  :P

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: What is WideChar on Free Pascal?
« Reply #1 on: April 25, 2020, 07:54:54 am »
WideChar is indeed UTF16.  UTF8Char is AnsiChar(cp_utf8)
See the reference manual chapter 3.2:
https://freepascal.org/docs-html/ref/refse13.html#refsu8.html

and the wiki for some practical application:

https://wiki.freepascal.org/Character_and_string_types
https://wiki.freepascal.org/String
https://wiki.freepascal.org/Widestring
« Last Edit: April 25, 2020, 08:31:01 am by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

JiaXing

  • Jr. Member
  • **
  • Posts: 75
Re: What is WideChar on Free Pascal?
« Reply #2 on: April 25, 2020, 09:46:51 am »
WideChar is indeed UTF16.  UTF8Char is AnsiChar(cp_utf8)
See the reference manual chapter 3.2:
https://freepascal.org/docs-html/ref/refse13.html#refsu8.html

and the wiki for some practical application:

https://wiki.freepascal.org/Character_and_string_types
https://wiki.freepascal.org/String
https://wiki.freepascal.org/Widestring

I want to know:

WideChar = which datatype in C? char16_t?

UTF8Char = which datatype in C? char8_t?

I have read all of that but still didn't found the answer. There is little or no information about it.
I'm subscribed to the church of 440bx. Say no to OOP  :P

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: What is WideChar on Free Pascal?
« Reply #3 on: April 25, 2020, 10:00:14 am »
In principle, yes, but note that many C compilers have char16_t as UCS2, which is a sub set of UTF16 with a guaranteed 2 byte length. Same goes for char8_t.
But if you do a straight translation from C to Pascal you can use the equivalent Pascal types anyway. The other way around can be a head ache since C needs libraries for string handling UTF8 or full UTF16. In that case ask on a C forum how to handle unicode encodings for your C compiler.
« Last Edit: April 25, 2020, 10:07:01 am by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Bart

  • Hero Member
  • *****
  • Posts: 5680
    • Bart en Mariska's Webstek
Re: What is WideChar on Free Pascal?
« Reply #4 on: April 25, 2020, 01:08:36 pm »
UTF8Char is AnsiChar(cp_utf8)
See the reference manual chapter 3.2:
https://freepascal.org/docs-html/ref/refse13.html#refsu8.html

Where in the documents does it say that (UTF8Char is AnsiChar(cp_utf8))?

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
« Last Edit: April 25, 2020, 01:31:46 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12645
  • FPC developer.
Re: What is WideChar on Free Pascal?
« Reply #6 on: April 25, 2020, 01:32:59 pm »
Afaik C has no types that are identical to Pascal Char.

All C char types are afaik assignable to int, while Pascal's are not.

If I search for "freepascal widechar" and press "I feel lucky" I get to:

https://www.freepascal.org/docs-html/rtl/system/widechar.html

so IMHO you really should put more effort into searchign.
« Last Edit: April 25, 2020, 02:04:06 pm by marcov »

Bart

  • Hero Member
  • *****
  • Posts: 5680
    • Bart en Mariska's Webstek
Re: What is WideChar on Free Pascal?
« Reply #7 on: April 25, 2020, 01:42:37 pm »

JiaXing

  • Jr. Member
  • **
  • Posts: 75
Re: What is WideChar on Free Pascal?
« Reply #8 on: April 26, 2020, 09:18:01 am »
If I search for "freepascal widechar" and press "I feel lucky" I get to:

https://www.freepascal.org/docs-html/rtl/system/widechar.html

so IMHO you really should put more effort into searchign.

I have already read this page. But I don't understand it. Is WideChar defined as a range? This range looked like the range of Word. So WideChar is actually Word?
I'm subscribed to the church of 440bx. Say no to OOP  :P

JiaXing

  • Jr. Member
  • **
  • Posts: 75
Re: What is WideChar on Free Pascal?
« Reply #9 on: April 26, 2020, 09:20:57 am »
In principle, yes, but note that many C compilers have char16_t as UCS2, which is a sub set of UTF16 with a guaranteed 2 byte length. Same goes for char8_t.
But if you do a straight translation from C to Pascal you can use the equivalent Pascal types anyway. The other way around can be a head ache since C needs libraries for string handling UTF8 or full UTF16. In that case ask on a C forum how to handle unicode encodings for your C compiler.

So for safety could I use uint16_t?
I'm subscribed to the church of 440bx. Say no to OOP  :P

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: What is WideChar on Free Pascal?
« Reply #10 on: April 26, 2020, 09:25:36 am »
Most unixes behave the same as Freepascal, so char16_t = widechar and char8_t can hold UTF8. On windows too. It are other platforms that have some issues.
If you are only on windows and nixes, both are safe to use with the default compilers for the platforms: GNU cc and msvc.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: What is WideChar on Free Pascal?
« Reply #11 on: April 26, 2020, 09:54:56 am »
Is WideChar defined as a range? This range looked like the range of Word. So WideChar is actually Word?

No, the range is defined as characters; note the "#"" before the hexadeciamal values. It's the same as  if it were defined as:

Code: Pascal  [Select][+][-]
  1. type WideChar = Chr($0000)..Chr(($FFFF);

Although since it's actually two consecutive bytes it can be cast to Word.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12645
  • FPC developer.
Re: What is WideChar on Free Pascal?
« Reply #12 on: April 26, 2020, 11:37:52 am »
In principle, yes, but note that many C compilers have char16_t as UCS2, which is a sub set of UTF16 with a guaranteed 2 byte length. Same goes for char8_t.
But if you do a straight translation from C to Pascal you can use the equivalent Pascal types anyway. The other way around can be a head ache since C needs libraries for string handling UTF8 or full UTF16. In that case ask on a C forum how to handle unicode encodings for your C compiler.

So for safety could I use uint16_t?

On Windows it is usually wchar_t, but according to the standard wchar_t exact size is not fixed, and afaik on Linux it is 4 bytes.

 

TinyPortal © 2005-2018