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


方法一:Boris Kumpar
function ExtractBasePath(const Path1,Path2:string):string;
const
  PATH_DELIMITER = '\';
  DRIVE_DELIMITER = ':';
var
  P1,P2:PChar;
  cnt,j:Integer;
begin
  P1:=PChar(Path1) ;
  P2:=PChar(Path2) ;

  cnt := 1;
  j := 0;
  {$B-}
  while (P1^ <> #0) and (P2^ <> #0) and (UpCase(P1^) = UpCase(P2^) ) do
  begin
    if (P1^=PATH_DELIMITER) or (P2^=PATH_DELIMITER) or ((j=0) and (P1^=DRIVE_DELIMITER)) then j:=cnt;

    Inc(cnt) ;
    Inc(P1) ;
    Inc(P2) ;
  end;

  if (P1^=PATH_DELIMITER) or (P2^=PATH_DELIMITER) then j := cnt - 1;

  Result:=Copy(Path1,1,j) ;
end;
方法二:Pablo Anizio
function ExtractBasePath(const path1, path2 : string) : string;
var
  sP1, sP2, stemp, rslt: String;
  slP1, slP2: TStringList;
  dif: Boolean;
  cnt, max: integer;
begin
  rslt := EmptyStr;
  if ((path1 <> EmptyStr) and (path2 <> EmptyStr)) then
  begin
    sP1 := ExtractFilePath(path1) ;
    sP2 := ExtractFilePath(path2) ;

    slP1 := TStringList.Create;
    while length(sP1) <> 0 do
    begin
      stemp := Copy(sP1,1,pos('\',sP1)) ;
      Delete(sP1,1,pos('\',sP1)) ;
      slP1.Add(stemp) ;
    end;

    slP2 := TStringList.Create;
    while length(sP2) <> 0 do
    begin
      stemp := Copy(sP2,1,pos('\',sP2)) ;
      Delete(sP2,1,pos('\',sP2)) ;
      slP2.Add(stemp) ;
    end;

    dif := False;
    cnt := 0;
    if (slP1.Count >= slP2.Count) then
      max := slP2.Count
    else
      max := slP1.Count;

    while (not dif) and (cnt < max) do
    begin
      if slP1.Strings[cnt] = slP2.Strings[cnt] then
        rslt := rslt + slP1.Strings[cnt]
      else
        dif := True;
      inc(cnt) ;
    end;

    slP1.Free;
    slP2.Free;
  end;

  Result := rslt;
end;

方法三:Vlad Man
function ExtractBasePath(const path1, path2: string): string;
var
  j: Integer;
  vStrLength: Integer;
  vLastDelemiterIndex: Integer;
begin
  Result := '';

  if Length(path1) > Length(path2) then
    vStrLength := Length(path2)
  else
    vStrLength := Length(path1) ;

  for j := 1 to vStrLength do
    if path1[j] = path2[j] then
      Result := Result + path1[j]
    else
      Break;

  vLastDelemiterIndex := LastDelimiter('\', Result) ;
  Delete(Result, vLastDelemiterIndex + 1, Length(Result) - vLastDelemiterIndex) ;
end;
方法四:Josip Brozovic
function ExtractBasePath( const path1, path2 : string ): string;
var
  s_shorter, s_longer: string;
  j: integer;
begin
  if Length( path1 ) > Length( path2 ) then
  begin
    s_longer := path1;
    s_shorter := path2;
  end
else
begin
    s_longer := path2;
    s_shorter := path1;
  end;

  result := s_shorter;

  for j := 1 to Length( s_shorter ) do
  begin
    if UpCase( path1[ j ] ) <> UpCase( path2[ j ] ) then
    begin
      Delete( result, j, MaxInt ) ;
      break;
    end;
  end;

  if ( result = s_shorter ) and
     ( Length( s_longer ) > Length( s_shorter )) and
     ( s_longer[ Length( s_shorter ) + 1 ] = '\' ) then
  begin
      result := result + '\';
  end;

  result := ExtractFilePath( result ) ;
end;

方法五:Korhan
function ExtractBasePath(const path1, path2 : string) : string;
var
  minLength : Integer;
  cnt : Integer;
  samePart : String;
begin
  if Length(path1) < Length(path2) then
    minLength := length(path1)
  else
    minLength := length(path2) ;

  Result := '';
  samePart := '';

  for cnt := 1 to minLength do
  begin
    if path1[cnt] = path2[cnt] then
    begin
      samePart := samePart + path1[cnt];
      if (path1[cnt] = '\') or ( (Length(path1) = Length(path2)) and (minLength = cnt) ) then
      begin
        Result := Result + samePart;
        samePart := '';
      end;
    end
    else
      Break;
  end;
end;

方法六:Jeff Lawson
function ExtractBasePath(const Path1, Path2: string): string;
var
  P1, P2,
  Dir1, Dir2,
  Base: string;
begin
  Base := '';
  P1 := LowerCase(Path1) ;
  P2 := LowerCase(Path2) ;

  if (ExtractFileExt(P1) = '') and (P1[Length(P1) - 1] <> '\') then P1 := P1 + '\';

  if (ExtractFileExt(P2) = '') and (P2[Length(P2) - 1] <> '\') then P2 := P2 + '\';

  while (P1 <> '') and (P2 <> '') do
  begin
    Dir1 := Copy(P1, 0, AnsiPos('\', P1)) ;
    Dir2 := Copy(P2, 0, AnsiPos('\', P2)) ;
    P1 := Copy(P1, Length(Dir1) + 1, Length(P1) - Length(Dir1) + 1) ;
    P2 := Copy(P2, Length(Dir2) + 1, Length(P2) - Length(Dir2) + 1) ;
    if Dir1 <> Dir2 then Break;
    Base := Base + Dir1;
  end;

  Result := Base;
end;
方法七:Ivan Cvetkovic
function ExtractBasePath(const path1, path2 : string) : string;
  procedure SplitPath(Path: string; sl: TStrings) ;
  begin
    sl.Delimiter := PathDelim;
    sl.StrictDelimiter := True;
    sl.DelimitedText := Path;
  end;
var
 sl1, sl2: TStrings;
 cnt: Integer;
begin
 Result := EmptyStr;

 sl1 := TStringList.Create;
 try
   SplitPath(Path1, sl1) ;

   sl2 := TStringList.Create;
   try
     SplitPath(Path2, sl2) ;

     for cnt := 0 to Min(sl1.Count, sl2.count) - 1 do
     begin
       if not AnsiSameText(sl1[cnt], sl2[cnt]) then Break;
       Result := Result + sl1[cnt] + PathDelim;
     end;
   finally
     sl2.Free;
   end;
 finally
   sl1.Free;
 end;
end;
方法八:Paul Bennett
function ExtractBasePath(const Path1, Path2: string): string;
var
  p1, p2, Matched: string;
  PathDelimiter: string[1];
  nStart, n1, n2, ctr: Integer;
begin
  p1 := ExtractFilePath(Path1) ;
  p2 := ExtractFilePath(Path2) ;

  if (Length(p1) = 0) or (Length(p2) = 0) then Exit;

  if CompareText(p1, p2) = 0 then
  begin
    Result:= p1;
    Exit;
  end;

  PathDelimiter := p1[Length(p1)];
  Matched := '';
  nStart := 1;

  repeat
    n1 := PosEx(PathDelimiter, p1, nStart) ;
    n2 := PosEx(PathDelimiter, p2, nStart) ;

    if (n1 = n2) And (n1 <> 0) then
    begin
      for ctr:= nStart to n1 do
      begin
        if p1[ctr] <> p2[ctr] then Break;
      end;

      if ctr > n1 then
      begin
        Matched:= Matched +Copy(p1, nStart, ctr -nStart) ;
        nStart := ctr;
      end;
    end;
  until (n1 <> n2) or (ctr < n1) ;

  if Length(Matched) > 2 then Matched := IncludeTrailingPathDelimiter(Matched) ;

  Result:= Matched;
end;
方法九:Caleb Hattingh
function ExtractBasePath(const path1, path2 : string) : string;
var
  tsl1, tsl2: TStringList;
  j: Integer;
begin
  Result := '';
  tsl1 := TStringList.Create;
  tsl2 := TStringList.Create;
  try
    tsl1.StrictDelimiter := True;
    tsl2.StrictDelimiter := True;
    tsl1.Delimiter := '\';
    tsl1.DelimitedText := path1;
    tsl2.Delimiter := '\';
    tsl2.DelimitedText := path2;
    for j := 0 to tsl1.Count - 1 do
    begin
      if tsl1[j] = tsl2[j] then
        Result := Result + tsl1[j] + '\'
      else
        Exit;
    end;
  finally
    FreeAndNil(tsl1) ;
    FreeAndNil(tsl2) ;
  end;
end;
方法十: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;
本文来自Delphi之窗,原文地址:http://www.52delphi.com
 

转载于:https://www.cnblogs.com/martian6125/archive/2009/07/22/9631286.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/299862.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

架构师讲解Java中websocket的应用

这篇文章主要来介绍一下在java项目中&#xff0c;特别是java web项目中websocket的应用。 场景&#xff1a;我做了一个商城系统&#xff0c;跟大多数商城系统&#xff0c;分为客户端和后台&#xff0c;客户端供客户浏览&#xff0c;下单&#xff0c;购买&#xff0c;后台主要管…

CocoStudio游戏发布后资源加密大致实现思路

截止到目前&#xff0c;CocoStudio版本已经发展到1.3.0.1&#xff0c;各个子工具间也已经发生了巨大变化。但是&#xff0c;无论是动画编辑器&#xff0c;UI编辑器&#xff0c;场景编辑器还是数据编辑器&#xff0c;它们生成&#xff08;导出&#xff09;的文件在商业开发中往往…

当你死后尸体如何处理?两种新玩法了解一下!

全世界只有3.14 %的人关注了青少年数学之旅据国外媒体报道&#xff0c;死亡是一个庄严肃穆的事情&#xff0c;但是依据不同的文化&#xff0c;人类死亡之后会被认为肉体与灵魂分离&#xff0c;采取的葬礼方式存在很大差异&#xff0c;你想过你的葬礼会是什么样吗&#xff1f;烟…

.NET Core开发实战(定义API的最佳实践)Source Generators版

前言极客时间上的《.NET Core开发实战》是一门非常好的课程&#xff0c;作者肖伟宇在第31课&#xff08;https://time.geekbang.org/course/detail/100044601-201165&#xff09;介绍了定义API的最佳实践。大意如下&#xff1a;Controller这一层负责与前端用户的交互&#xff0…

【转】2007高校BBS上20个睿智的冷笑话

1.真正的浪漫求婚应该是这样的&#xff1a;一位风度翩翩的男子请了10位同事吃饭&#xff0c;其中就有他心仪的mm。吃到一半时&#xff0c;他忽然站起来走到mm身旁&#xff0c;然后把mm坐的椅子整个搬了个90度面朝自己&#xff0c;而此刻mm嘴里塞满了各种食物……这时&#xff0…

多年经验的程序员迷失了自己,该怎么办?

多年的程序员迷失了自己&#xff0c;该怎么办&#xff1f; 本文选自《我也能做CTO之程序员职业规划 》一书 我应该朝哪个方向发展&#xff1f;我不做这行还能做什么&#xff1f;当现实情况与理想目标之间的差距越拉越大时&#xff0c;大多数刚入行的IT人员都会提出这样的问题&a…

大神讲解Java for循环的几种用法

本文非常适合初学Java的程序员&#xff0c;主要是来了解一下Java中的几种for循环用法&#xff0c;分析得十分详细&#xff0c;一起来看看。 J2SE 1.5提供了另一种形式的for循环。借助这种形式的for循环&#xff0c;可以用更简单地方式来遍历数组和Collection等类型的对象。本文…

苍天饶过谁?| 今日最佳

全世界只有3.14 % 的人关注了青少年数学之旅&#xff08;图源人民日报&#xff0c;侵权删&#xff09;

快速选择实例

功能&#xff1a;查找集合S中第k个最小元。 快速选择算法修改自快速排序算法&#xff0c;当算法终止时&#xff0c;第k个最小元就在位置k上。这破坏了原来的排序&#xff1b;如果不希望这样&#xff0c;那么需要做一份拷贝。 快速选择函数&#xff1a; /* quick_select.h */#if…

设计模式之桥接

桥接模式的介绍桥接模式就是通过将抽象部分与实现部分分离&#xff0c;把多种可匹配的使用进行组合。其实就是在A类中含有B类接口&#xff0c;通过构造函数传递B类的实现&#xff0c;这个B类就是设计的桥。它是一种结构型设计模式&#xff0c;可将一个大类或一系列紧密相关的类…

WinAPI: midiOutGetNumDevs - 获取 MIDI 输出设备的数目

//声明: midiOutGetNumDevs: UINT; {无参数; 返回 MIDI 输出设备的数目}//举例:

去除HTML标签--SQL写法

----Author: Derry--Create date: 2009-07-27--Description: 去除HTML标签--ALTERFUNCTION[dbo].[StripAllTags]( inputVARCHAR(8000))RETURNSVARCHAR(8000)ASBEGINdeclareResultvarchar(8000), startint, endint, lenintsetinputinput<>setResult…

手把手教你用Java的swing制作计算器

其实学到Java这一块很多人会觉得很复杂实际上学会使用方法其实很简单 话不多说直接贴源码,如下&#xff1a; package cn.sjy.calculator;import javax.swing.*; import java.awt.*;/*** 简易计算器* author 石俊熠* 2020.7.13 11:24* 注&#xff1a;仿照某Java大佬的源码改之*…

你们都被电视剧版的 《西游记》给骗了!| 今日趣图

全世界只有3.14 % 的人关注了青少年数学之旅你们都被电视剧版的《西游记》给骗了&#xff01;电视剧里挑担子的是沙僧但其实《西游记》原著中大部分都是二师兄挑担子沙僧也就是打个下手&#xff08;图源名场面All&#xff0c;侵权删&#xff09;如果再有人嘲笑你胖你就把这条涨…

lua工具库penlight--08额外的库(二)

执行一系列的参数 类型说明符也可以 是 (MIN .. MAX) 的形式。 local lapp require pl.lapp local args lapp [[ Setting ranges <x> (1..10) A number from 1 to 10 <y> (-5..1e6) Bigger range ]] print(args.x,args.y) 下面的意思是大与或等于 MIN 和小于或等…

.NET 6 Preview 6 Released

宣布 .NET 6 Preview 6Richard 2021 年 7 月 14 日我们很高兴发布 .NET 6 Preview 6。Preview 6 是我们进入 RC 时期之前的倒数第二个预览版。将有两个 RC 版本。此版本本身相对较小&#xff0c;而 Preview 7 会更大一些。在那之后&#xff0c;我们将进行质量修复&#xff0c;直…

SQL2K数据库开发十五之表操作查看表中的数据

1.可以使用SELECT语句查询表中的数据。如在查询分析器中执行SELECT * FROM Products语句就可以查询Products表中的数据&#xff0c;如下图&#xff1a;2.如在企业管理器中查询表中数据&#xff0c;则要展开sample数据库&#xff0c;在Products表上右击鼠标&#xff0c;在弹出的…

转:VC6.0与VC.net的具体区别

原文&#xff1a;http://www.itzhe.cn/article/20080302/98267.html 对于VC6.0和VC.net本人有几点疑问。 1.首先&#xff0c;vc.net开发的程序是否依然需要.net框架的支持&#xff1f;是否可以独立运行&#xff1f; 2.对与VC.net开发出来的程序在运行效率上和VC6.0开发的差距…

中考新大纲:初中数学无非就这26个考点!孩子吃透,再笨也能考115分!

全世界只有3.14 % 的人关注了青少年数学之旅升入初二、初三后数学难度急速上升&#xff0c;您的孩子是否学得吃力成绩却无法提高&#xff1f;1.总说自己上课都能听懂&#xff0c;可题目稍微一变就不会做&#xff1b;2.连课下时间都在刷题&#xff0c;到头来做的全是无用功&…

程序员(工作2年)立flag,面四家,成三家,最后进了蚂蚁.....

作为一个毕业2年的coder, 最近一直在寻找一个合适的机会能够换一个环境&#xff0c;一是寻找一个更加宽阔的舞台不断的提升自己&#xff0c;二是让自己走出现在的舒适区域&#xff0c;迎接更多的挑战和认识更多的人。当然还有为了获得更加好的一份收入。 这一个月&#xff0c;…