Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-04

Bending System.IndexByte to my will!!?

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Reinier Olislagers

Hi all,

(FPC 2.6.2 x86, trunk x64, on Windows; found similar behaviour on Linux x64)

Busy getting DBase III memo support correct ;)

In fcl-db\src\dbase\dbf_common.pas we find this part used to e.g. find end of file ($1A/ASCII 26) markers in a buffer with memo data: [1]

The problem is: the first MemScan function doesn't work: System.IndexByte just always return -1 even if the character is there. Tried system.indexbyte with a simple test program and of course it worked. If I comment out $ifdef fpc to force use of the second function, the $1A is found perfectly.

Where lies the problem?

Thanks,
Reinier

[1]
{$ifdef FPC}
function MemScan(const Buffer: Pointer; Chr: Byte; Length: Integer):
Pointer;
var
  I: Integer;
begin
  I := System.IndexByte(Buffer, Length, Chr);
  if I = -1 then
    Result := nil
  else
    Result := Buffer+I;
end;
{$else}

function MemScan(const Buffer: Pointer; Chr: Byte; Length: Integer): Pointer;
asm
PUSH EDI
MOV EDI,Buffer
MOV AL, Chr
MOV ECX,Length
REPNE SCASB
MOV EAX,0
JNE @@1
MOV EAX,EDI
DEC EAX
@@1: POP EDI
end;

{$endif}

Reply by email

Jeppe Græsdal Johansen
to Reinier Olislagers
Reinier Olislagers wrote 53 lines
Hi all, (FPC 2.6.2 x86, trunk x64, on Windows; found similar behaviour on Linux x64) Busy getting DBase III memo support correct ;) In fcl-db\src\dbase\dbf_common.pas we find this part used to e.g. find end of file ($1A/ASCII 26) markers in a buffer with memo data: [1] The problem is: the first MemScan function doesn't work: System.IndexByte just always return -1 even if the character is there. Tried system.indexbyte with a simple test program and of course it worked. If I comment out $ifdef fpc to force use of the second function, the $1A is found perfectly. Where lies the problem? Thanks, Reinier [1] {$ifdef FPC} function MemScan(const Buffer: Pointer; Chr: Byte; Length: Integer): Pointer; var I: Integer; begin I := System.IndexByte(Buffer, Length, Chr); if I = -1 then Result := nil else Result := Buffer+I; end; {$else} function MemScan(const Buffer: Pointer; Chr: Byte; Length: Integer): Pointer; asm PUSH EDI MOV EDI,Buffer MOV AL, Chr MOV ECX,Length REPNE SCASB MOV EAX,0 JNE @@1 MOV EAX,EDI DEC EAX @@1: POP EDI end; {$endif}

Since you have a typed const pointer instead of an untyped const parameter, you need to do this:

System.IndexByte(PByte(Buffer)^, Length, Chr);

Reply by email

Reinier Olislagers
to Jeppe Græsdal Johansen
Jeppe Græsdal Johansen wrote 12 lines
Den 18-04-2013 07:57, Reinier Olislagers skrev: > In fcl-db\src\dbase\dbf_common.pas we find this part used to e.g. find > end of file ($1A/ASCII 26) markers in a buffer with memo data: [1] > > The problem is: the first MemScan function doesn't work: > System.IndexByte just always return -1 even if the character is there. > Tried system.indexbyte with a simple test program and of course it > worked. > If I comment out $ifdef fpc to force use of the second function, the $1A > is found perfectly. > > Where lies the problem?

<snip>

Show quoted text 5 lines
Since you have a typed const pointer instead of an untyped const parameter, you need to do this: System.IndexByte(PByte(Buffer)^, Length, Chr);

Thanks a lot, that works beautifully!

Reply by email