From 22aacd2a6052d72b26eb4d0852349eb93c59729d Mon Sep 17 00:00:00 2001 From: Jonas Maebe Date: Thu, 1 Jan 2009 22:02:17 +0000 Subject: [PATCH] * return 0 for length(pchar(0)), like Kylix does (using corrected and multi-platform version of patch in r12461, which caused the i386 version of fpc_pchar_length to return 0 in all cases, which used tabs, and did not include a test case) git-svn-id: trunk@12464 - --- .gitattributes | 1 + rtl/arm/arm.inc | 3 +++ rtl/i386/i386.inc | 3 +++ rtl/inc/cgeneric.inc | 5 ++++- rtl/inc/generic.inc | 4 +++- tests/test/units/system/tpchlen.pp | 12 ++++++++++++ 6 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 tests/test/units/system/tpchlen.pp diff --git a/.gitattributes b/.gitattributes index 8ac5e628bf..7fb99d496c 100644 --- a/.gitattributes +++ b/.gitattributes @@ -8083,6 +8083,7 @@ tests/test/units/system/tjmp.pp svneol=native#text/plain tests/test/units/system/tmem.pp svneol=native#text/plain tests/test/units/system/todd.pp svneol=native#text/plain tests/test/units/system/tparam.pp svneol=native#text/plain +tests/test/units/system/tpchlen.pp svneol=native#text/plain tests/test/units/system/tpi.pp svneol=native#text/plain tests/test/units/system/trandom.pp svneol=native#text/plain tests/test/units/system/trdtxt01.pp svneol=native#text/plain diff --git a/rtl/arm/arm.inc b/rtl/arm/arm.inc index 57a5e6e131..91733f1197 100644 --- a/rtl/arm/arm.inc +++ b/rtl/arm/arm.inc @@ -439,7 +439,9 @@ end; function fpc_Pchar_length(p:Pchar):longint;assembler;nostackframe;[public,alias:'FPC_PCHAR_LENGTH'];compilerproc; asm + cmp r0,#0 mov r1,r0 + beq .Ldone .Lnextchar: (*Are we aligned?*) tst r1,#3 @@ -462,6 +464,7 @@ asm (*Dirty trick: we need to subtract 1 extra because we have counted the terminating 0, due to the known carry flag sbc can do this.*) sbc r0,r1,r0 +.Ldone: mov pc,lr .L01010101: .long 0x01010101 diff --git a/rtl/i386/i386.inc b/rtl/i386/i386.inc index 9f5377cf36..3786c780c4 100644 --- a/rtl/i386/i386.inc +++ b/rtl/i386/i386.inc @@ -1037,12 +1037,15 @@ asm {$endif} movl $0xffffffff,%ecx xorl %eax,%eax + test %edi,%edi + jz .LStrLenDone cld repne scasb movl $0xfffffffe,%eax subl %ecx,%eax movl saveedi,%edi +.LStrLenDone: end; {$endif FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH} diff --git a/rtl/inc/cgeneric.inc b/rtl/inc/cgeneric.inc index 981b98f5ea..0647e92128 100644 --- a/rtl/inc/cgeneric.inc +++ b/rtl/inc/cgeneric.inc @@ -112,7 +112,10 @@ function libc_pchar_length(p:pchar):size_t; cdecl; external 'c' name 'strlen'; function fpc_pchar_length(p:pchar):longint;[public,alias:'FPC_PCHAR_LENGTH']; compilerproc; begin - fpc_pchar_length:=libc_pchar_length(p); + if assigned(p) then + fpc_pchar_length:=libc_pchar_length(p) + else + fpc_pchar_length:=0; end; {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH} diff --git a/rtl/inc/generic.inc b/rtl/inc/generic.inc index 54e0980294..0e64f75e6d 100644 --- a/rtl/inc/generic.inc +++ b/rtl/inc/generic.inc @@ -1187,7 +1187,9 @@ function fpc_pchar_length(p:pchar):longint;[public,alias:'FPC_PCHAR_LENGTH']; co var i : longint; begin i:=0; - while p[i]<>#0 do inc(i); + if assigned(p) then + while p[i]<>#0 do + inc(i); exit(i); end; diff --git a/tests/test/units/system/tpchlen.pp b/tests/test/units/system/tpchlen.pp new file mode 100644 index 0000000000..1096f716e5 --- /dev/null +++ b/tests/test/units/system/tpchlen.pp @@ -0,0 +1,12 @@ +procedure test(p: pchar; len: longint); +begin + if (length(p)<>len) then + halt(1); +end; + +begin + test(nil,0); + test(#0,0); + test('a',1); + test('hello',5); +end.