Dear Delphi Folks who know Assembler —
I have this great routine that worked fine in the pre-Unicode world.
function StringRefCount(const Src: String): Integer;
asm
test eax, eax
je @Exit
mov edx, eax
mov eax, [edx - 8]
@Exit:
end;
Given that my ASM skills are rudimentary at best, how might this be done in D2009+ ?
ADDED: This test fails:
procedure TestStringStuff.TestStringRefCount;
var
TempString: string;
TempResult: Integer;
SecondString: string;
begin
TempString := 'this is a temp string';
TempResult := StringRefCount(TempString);
CheckEquals(1, TempResult);
SecondString := TempString;
TempResult := StringRefCount(TempString);
CheckEquals(2, TempResult);
end;
Thanks — I’m perfectly happy to admit to a major brain fart. 🙂
Nick
No changes are necessary to that code for 32-bit compilers. It’s the test case that’s faulty because string literals have built-in reference counts of −1.
For 64-bit code (and 32-bit code on newer compilers), you’re best off using the function by the same name already provided by the RTL.