1 2 // Requires Int64 type. Earliest versions of Delphi may lack it.3 4 type5 a8ch = array[1..8] of char;
6 PA8C = ^a8ch; // pointer type, to above array of characters7 PI64 = ^Int64; // pointer type, for Int64 numbers8 9 10 // converts short strings (8 chars max) to a number holding ASCII bytes11 function StrToAsc(s: string): Int64;
12 begin13 SetLength(s, 8); // increase or decrease to length of 814 Result := PI64(Pointer(PA8C(@(s[1]))))^;
15 end;
16 // Intermediate use of generic Pointer yields no compiler warnings17 // The compiler translates the characters in the string18 // first into the array-of-chars type, and then into the numeric type.19 // The 8 character-bytes is essentially an array of ASCII codes, and20 // since they ARE a group of adjacent memory cells holding numbers, 21 // they can be directly interpreted as the 8 bytes of an Int64 in one22 // swoop. So, no time-wasting byte-at-a-time copying loop is needed!23 24 25 26 // WITH THE PRECEDING PREPARATION, HERE IS THE COMPARER FUNCTION:27 // Result: positive: s1 is larger28 // negative: s2 is larger29 // zero: first 8 characters of strings are the same30 function CmpShortS(s1, s2: string): Int64;
31 begin32 Result := StrToAsc(s1) - StrToAsc(s2);
33 end;
34 // The two swoops are the two lower-level function calls :)35 // Yes, the Result is a C-language kind of value. Useful... 36 37 38 39 //(included for sake of completeness)40 // interprets a 64-bit number as a sequence of characters41 function AscToStr(a: Int64): string;
42 begin43 SetLength(Result, 8);
44 Result := string(PA8C(Pointer(@a))^);
45 end;