소스 검색

Optimized code

* Get rids of costly and useless Copy() function here.
* Removes duplicate Length checks and reduces duplicated Compare calls.
* Explicitly assign Result before Exit for clarity.
jogo- 2 달 전
부모
커밋
08437fecc2
1개의 변경된 파일22개의 추가작업 그리고 17개의 파일을 삭제
  1. 22 17
      Projects/Src/ISPP.Funcs.pas

+ 22 - 17
Projects/Src/ISPP.Funcs.pas

@@ -526,33 +526,38 @@ var
   function Compare(const S1, S2: string; Sensitive: Boolean): Boolean;
   begin
     if Sensitive then
-      Result := AnsiCompareStr(S1, S2) = 0
+      Result := SameStr(S1, S2)
     else
-      Result := AnsiCompareText(S1, S2) = 0;
+      Result := SameText(S1, S2);
   end;
 
   function Contains(const Substr: string; Sensitive: Boolean): Boolean;
-  var
-    L, I: Integer;
   begin
-    Result := True;
-    L := Length(Substr);
-    for I := 1 to Length(Str) - L + 1 do
-      if Compare(Substr, Copy(Str, I, L), Sensitive) then Exit;
-    Result := False;
+  if Sensitive then
+    Result := AnsiPos(Substr, Str) > 0
+  else
+    Result := Pos(LowerCase(Substr), LowerCase(Str)) > 0;
   end;
 
   function Meets(const Substr: string; Sensitive: Boolean; Where: Integer): Boolean;
+  var
+    L, SL: Integer;
   begin
-    Result := False;
+    L := Length(Substr);
+    SL := Length(Str);
+
+    if (Where in [1, 2, 3]) and (L > SL) then
+    begin
+      Result := False;
+      Exit;
+    end;
+
     case Where of
-      1: if Length(Substr) <= Length(Str) then
-            Result := Compare(Substr, Copy(Str, 1, Length(Substr)), Sensitive);
-      2: if Length(Substr) <= Length(Str) then
-            Result := Compare(Substr, Copy(Str, Length(Str) - Length(Substr) + 1, Length(Substr)), Sensitive);
-      3: if Length(Substr) <= Length(Str) then
-            Result := Contains(Substr, Sensitive);
-      else Result := Compare(Substr, Str, Sensitive);
+      1: Result := Compare(Substr, Copy(Str, 1, L), Sensitive);
+      2: Result := Compare(Substr, Copy(Str, SL - L + 1, L), Sensitive);
+      3: Result := Contains(Substr, Sensitive);
+    else
+      Result := Compare(Substr, Str, Sensitive);
     end;
   end;