方法十:Ricardo de O. Soares
function ExtractBasePath(const path1, path2: string): string;
var
cnt: integer;
begin
Result := '';
if UpCase(path1[1]) <> UpCase(path2[1]) then
Exit
else
begin
for cnt := 1 to Min(Length(path1),Length(path2)) do
if CompareText(LeftStr(path1,cnt),LeftStr(path2,cnt)) <> 0 then
break;
Result := Result + LeftStr(path1,cnt-1) ;
while RightStr(Result,1) <> '\' do
Delete(Result,Length(Result),1) ;
end;
end;
方法十一:Antonio Bakula
function ExtractBasePath(APath1, APath2: string): string;
var
tempRez: string;
xx, minLen: integer;
begin
minLen := Min(Length(APath1), Length(APath2)) ;
Result := '';
tempRez := '';
for xx := 1 to minLen do
begin
if APath1[xx] <> APath2[xx] then
Break;
tempRez := tempRez + APath1[xx];
if APath1[xx] = '\' then
Result := tempRez;
end;
end;
最后一种ASM:Jens Borrisholt:
function ExtractBasePath(const Path1, Path2: string): string;
var
CompareLength: Integer;
cnt: Integer;
P, Q: PChar;
begin
Result := '';
//Determent the shortest string
asm
mov eax, Path1
mov edx, Path2
test eax, edx //Test for nil string
jnz @NotNilString
mov esp, ebp
pop ebp
ret //restore registers and exit
@NotNilString:
mov ecx, [eax - 4]
cmp ecx, [edx - 4]
jle @Path2Shortest //Length(P1) > Length(P2)
mov ecx, [edx - 4]
@Path2Shortest:
mov CompareLength, ecx
end;
p := PChar(Path1) ;
q := PChar(Path2) ;
cnt := 1;
while cnt <= CompareLength do
if CSTR_EQUAL <> CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE, P + cnt, 1, Q + cnt, 1) then
break
else
inc(cnt) ;
while (p[cnt] <> PathDelim) and (cnt > 0) do Dec(cnt) ;
if cnt <> 0 then SetString(Result, p, cnt + 1) ;
end;