Recent

Author Topic: Determine if a char  (Read 9500 times)

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: Determine if a char
« Reply #30 on: December 18, 2018, 11:37:19 am »
Quick question: For stuff like this (checking if a source-string consists of legal characters) i used (albeit in another programming language) the C-Function StrSpn.

Now researching this i found the Wiki-Entry regarding the libc-unit and libc-library --> http://wiki.freepascal.org/libc_unit

If i understood the wiki correctly: The unit is deprecated, but the library still works.

So, if'd use an external declare against the libc-library, the StrSpn-Function should still work.
But: I know for a fact, that under Windows there are two different declares for the StrSpn:
1) StrSpnA (you guessed it: Ansi)
2) StrSpnW (have a guess...)
Is the external declare of the StrSpn-Function in Freepascal Ansi/Wide-String-aware?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Determine if a char
« Reply #31 on: December 18, 2018, 12:14:25 pm »
Code: Bash  [Select][+][-]
  1. packages/libc/src$ fgrep -i "strspn" *
  2. stringh.inc:function strspn(__s:Pchar; _accept:Pchar):size_t;cdecl;external clib name 'strspn';
  3.  

libc was/is intended exclusively for Kylix compatibility. It shouldn't be used for any other thing, much less in Windows!

Do note what the wiki says (emphasis mine):
Quote
The libc unit is deprecated, and frozen in time for Kylix compatibility, and won't be ported to other architectures or operating systems.
« Last Edit: December 18, 2018, 12:17:38 pm by lucamar »
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.

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: Determine if a char
« Reply #32 on: December 18, 2018, 12:35:47 pm »
Lucamar, i've read that too, but it says: libc-unit (not the libc-library), and the StrSpn-Function (and its sisters) have been around since Adam and Eve (at least it feels like it)
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Determine if a char
« Reply #33 on: December 18, 2018, 02:11:19 pm »
Well, you asked for the external declaration in Free Pascal so that's what I gave you.
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.

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: Determine if a char
« Reply #34 on: December 18, 2018, 04:07:10 pm »
Lucamar, i know the declaration, so no worries.  ;) :)
My question was more along the lines: Does the StrSpn in the libc-library recognize if it's an Ansi- or WideString being checked?

Aircode
Code: Pascal  [Select][+][-]
  1. ResultA:=StrSpn(MyAnsiString, MyLegalAnsiChars);
  2. ResultW:=StrSpn(MyWideString, MyLegalWideChars);
  3.  

In Visual Basic for Windows i had to specify which one i wanted to use, since VB doesn't support overloading.
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Determine if a char
« Reply #35 on: December 18, 2018, 04:23:05 pm »
Lucamar, i know the declaration, so no worries.  ;) :)
My question was more along the lines: Does the StrSpn in the libc-library recognize if it's an Ansi- or WideString being checked?

isn't lib-c linux specific? in that case it should be utf8 enabled. On windows lcl is using the 65001 ansi code page for its strings which is utf8 so the Ansi variant of the call is used.

Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Determine if a char
« Reply #36 on: December 18, 2018, 04:30:01 pm »
isn't lib-c linux specific? in that case it should be utf8 enabled. On windows lcl is using the 65001 ansi code page for its strings which is utf8 so the Ansi variant of the call is used.
Libc is linux specific but strspn is a standard C library function. I would not use it in Pascal since Pascal strings can contain zero's and C strings can't.
Specialize a type, not a var.

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: Determine if a char
« Reply #37 on: December 18, 2018, 05:49:13 pm »
isn't lib-c linux specific? in that case it should be utf8 enabled. On windows lcl is using the 65001 ansi code page for its strings which is utf8 so the Ansi variant of the call is used.
Libc is linux specific but strspn is a standard C library function. I would not use it in Pascal since Pascal strings can contain zero's and C strings can't.

Ah, OK.

If you're wondering what my questions have to do with the issue of the OP:
As i said, i used StrSpn to check a String if it consists of legal characters (or not!) since it is easy to use:

Pseudo-Code:
Code: Pascal  [Select][+][-]
  1. Var
  2.     MyResult:Boolean;
  3. Begin
  4.     MyResult:=(Length(SourceString)=StrSpn(SourceString, LegalChars));
  5. // Or: If Length(SourceString)=StrSpn(SourceString, LegalChars) Then MyResult:=True Else MyResult:=False;
  6. End;
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Determine if a char
« Reply #38 on: December 18, 2018, 06:47:45 pm »
Lucamar, i know the declaration, so no worries.  ;) :)
My question was more along the lines: Does the StrSpn in the libc-library recognize if it's an Ansi- or WideString being checked?

Aircode
Code: Pascal  [Select][+][-]
  1. ResultA:=StrSpn(MyAnsiString, MyLegalAnsiChars);
  2. ResultW:=StrSpn(MyWideString, MyLegalWideChars);
  3.  

In Visual Basic for Windows i had to specify which one i wanted to use, since VB doesn't support overloading.

As can be seen from the declaration, it doesn't recognize anything: PChar is the equivalent of C char* which (in C) is esentially a pointer to byte (or short, depending). StrSpnA and StrSpnW are Microsoft's adds-on to the standard.

But Thaddy's reason for not using it is the correct one: Pascal strings and C char* (or char[]) are not completely equivalent.
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.

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: Determine if a char
« Reply #39 on: December 18, 2018, 08:47:14 pm »
But Thaddy's reason for not using it is the correct one: Pascal strings and C char* (or char[]) are not completely equivalent.

No Argument from me
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018