Re: [fpc-pascal] Oberon-0

2023-04-20 Thread Ralf Quint via fpc-pascal

On 4/19/2023 7:46 PM, Adriaan van Os via fpc-pascal wrote:

Ralf Quint via fpc-pascal wrote:


What does Oberon and Forth have to do with the FreePascal compiler? 


1. Niklaus Wirth
2. Strong resemblance between Oberon and Pascal
3. An early version of his book Compiler Construction used Pascal 
rather than Oberon


Well, still, neither one of those IMHO is a valid reason as to why this 
is discussed on a Free Pascal mailing list, beside possibly on 
fpc-other, as suggested.



Ralf


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


Re: [fpc-pascal] Is there a Pos() function for TBytes?

2023-04-20 Thread Alexey Torgashin via fpc-pascal


function PosInBytes(const Pattern: byte; Target: TBytes): integer;


Let's use IndexByte standard procedure here?
Maybe also in the 1st overloaded code too?
Alex
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Is there a Pos() function for TBytes?

2023-04-20 Thread Bo Berglund via fpc-pascal
On Thu, 20 Apr 2023 08:40:48 +0200 (CEST), Michael Van Canneyt via fpc-pascal
 wrote:

>> I found this page:
>> https://www.freepascal.org/docs-html/rtl/system/copy.html
>>
>> Here there is also reference to the other functions for strings:
>> Delete  (string and dyn array)
>> Copy(string and dyn array)
>> Insert  (string and dyn array but limited to 255 elements, why?)
>> Pos (string only..)
>>
>> They describe what can be done with strings and in some cases dynamic arrays
>> (like TBytes?), but for Pos() there is only string...
>>
>> And Insert seems to have a limit of 255 elements, why?
>>
>> What I need is a Pos(token) for TBytes where the token can be a single or
>> multiple bytes in size (just like for strings).
>>
>> Is there such a thing?
>
>Not that I am aware of, but I think it would be a welcome addition to one of
>the RTL units. I've been in need of such a function on multiple occasions,
>and had to resort to custom code each time.
>

FWIW, I threw together two overloaded Pos functions for a TBytes container like
this (except I didn't name them Pos in order not to interfere with the RTL):

function PosInBytes(const Pattern, Target: TBytes): integer; overload;
function PosInBytes(const Pattern: byte; const Target: TBytes): integer;
overload;

implementation

function PosInBytes(const Pattern, Target: TBytes): integer;
var
  i, j, res: integer;
begin
  Result := -1;
  for i := 0 to Length(Target) - 1 - Length(Pattern) do
  begin
if Target[i] = Pattern[0] then  //First byte of pattern found
begin
  res := i;
  for j := 1 to Length(Pattern)-1 do
  begin
if Target[i+j] <> Pattern[j] then
begin
  res := -1;
  break;
end;
  end;
  if res >= 0 then break;
end;
  end;
  Result := res;
end;

function PosInBytes(const Pattern: byte; Target: TBytes): integer;
var
  i: integer;
begin
  Result := -1;
  for i :=  0 to Length(Target) - 1 do
  begin
if Target[i] = Pattern then
begin
  Result := i;
  break;
end;
  end;
end;


-- 
Bo Berglund
Developer in Sweden

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


Re: [fpc-pascal] Is there a Pos() function for TBytes?

2023-04-20 Thread Michael Van Canneyt via fpc-pascal




On Thu, 20 Apr 2023, Bo Berglund via fpc-pascal wrote:


I am transferring data via serial comm and they wind up inside a TBytes dyn
array.
Now I want to easily find a certain pattern of bytes inside that array, but how?

I have a cludge that does not feel right but works and I want to get it replaced
by the "correct" way of doing it.

So what I have done so far is the following kind of thing using an AnsiString to
supply the Pos() functionality:

var
 bData: TBytes;
 sData: AnsiString;
 p: integer;
begin
 GetSerialData(bData); //Load the array with incoming data
 SetLength(sData, Length(bData); //Prepare string to receive data
 Move(bData[0], sData[1], Length(bData)); //Copy all data
 p := Pos(pattern, sData);
 ...

I'd rather use some built-in function that is *not* using intermediate strings
but can operate on the TBytes array directly.

I found this page:
https://www.freepascal.org/docs-html/rtl/system/copy.html

Here there is also reference to the other functions for strings:
Delete  (string and dyn array)
Copy(string and dyn array)
Insert  (string and dyn array but limited to 255 elements, why?)
Pos (string only..)

They describe what can be done with strings and in some cases dynamic arrays
(like TBytes?), but for Pos() there is only string...

And Insert seems to have a limit of 255 elements, why?

What I need is a Pos(token) for TBytes where the token can be a single or
multiple bytes in size (just like for strings).

Is there such a thing?


Not that I am aware of, but I think it would be a welcome addition to one of
the RTL units. I've been in need of such a function on multiple occasions,
and had to resort to custom code each time.

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


[fpc-pascal] Is there a Pos() function for TBytes?

2023-04-20 Thread Bo Berglund via fpc-pascal
I am transferring data via serial comm and they wind up inside a TBytes dyn
array.
Now I want to easily find a certain pattern of bytes inside that array, but how?

I have a cludge that does not feel right but works and I want to get it replaced
by the "correct" way of doing it.

So what I have done so far is the following kind of thing using an AnsiString to
supply the Pos() functionality:

var
  bData: TBytes;
  sData: AnsiString;
  p: integer;
begin
  GetSerialData(bData); //Load the array with incoming data
  SetLength(sData, Length(bData); //Prepare string to receive data
  Move(bData[0], sData[1], Length(bData)); //Copy all data
  p := Pos(pattern, sData);
  ...

I'd rather use some built-in function that is *not* using intermediate strings
but can operate on the TBytes array directly.

I found this page:
https://www.freepascal.org/docs-html/rtl/system/copy.html

Here there is also reference to the other functions for strings:
Delete  (string and dyn array)
Copy(string and dyn array)
Insert  (string and dyn array but limited to 255 elements, why?)
Pos (string only..)

They describe what can be done with strings and in some cases dynamic arrays
(like TBytes?), but for Pos() there is only string...

And Insert seems to have a limit of 255 elements, why?

What I need is a Pos(token) for TBytes where the token can be a single or
multiple bytes in size (just like for strings).

Is there such a thing?


-- 
Bo Berglund
Developer in Sweden

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