Browse Source

PathFunc: Optimize PathSame function.

Jordan Russell 1 month ago
parent
commit
1b70ea867f
1 changed files with 10 additions and 6 deletions
  1. 10 6
      Components/PathFunc.pas

+ 10 - 6
Components/PathFunc.pas

@@ -19,7 +19,6 @@ function PathCharIsTrailByte(const S: String; const Index: Integer): Boolean;
 function PathCharLength(const S: String; const Index: Integer): Integer;
 function PathCombine(const Dir, Filename: String): String;
 function PathCompare(const S1, S2: String): Integer;
-function PathSame(const S1, S2: String): Boolean;
 function PathDrivePartLength(const Filename: String): Integer;
 function PathDrivePartLengthEx(const Filename: String;
   const IncludeSignificantSlash: Boolean): Integer;
@@ -41,6 +40,7 @@ function PathNormalizeSlashes(const S: String): String;
 function PathPathPartLength(const Filename: String;
   const IncludeSlashesAfterPath: Boolean): Integer;
 function PathPos(Ch: Char; const S: String): Integer;
+function PathSame(const S1, S2: String): Boolean;
 function PathStartsWith(const S, AStartsWith: String): Boolean;
 function PathStrNextChar(const S: PChar): PChar;
 function PathStrPrevChar(const Start, Current: PChar): PChar;
@@ -158,11 +158,6 @@ begin
   Result := CompareStr(PathLowercase(S1), PathLowercase(S2));
 end;
 
-function PathSame(const S1, S2: String): Boolean;
-begin
-  Result := PathCompare(S1, S2) = 0;
-end;
-
 function PathDrivePartLength(const Filename: String): Integer;
 begin
   Result := PathDrivePartLengthEx(Filename, False);
@@ -535,6 +530,15 @@ begin
   end;
 end;
 
+function PathSame(const S1, S2: String): Boolean;
+{ Returns True if the specified strings (typically filenames) are equal, using
+  a case-insensitive ordinal comparison.
+  Like PathCompare, but faster for checking equality as it returns False
+  immediately if the strings are different lengths. }
+begin
+  Result := (Length(S1) = Length(S2)) and (PathCompare(S1, S2) = 0);
+end;
+
 function PathStartsWith(const S, AStartsWith: String): Boolean;
 { Returns True if S starts with (or is equal to) AStartsWith. Uses path casing
   rules. }