您现在的位置:首页 >> 基础算法 >> window基础 >> 内容

12种方法返回2个文件路径之间的公共基路径ExtractBasePath(4)

时间:2011/9/3 15:31:41 点击:

方法十: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;


 

上一页1234下一页

作者:网络 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • 盒子文章(www.2ccc.com) © 2024 版权所有 All Rights Reserved.
  • 沪ICP备05001939号