Browse Source

* Implement case-insensitive version of Equals & Contains. Fix issue #40029

Michaël Van Canneyt 2 years ago
parent
commit
13548088b7
2 changed files with 12 additions and 6 deletions
  1. 10 4
      rtl/objpas/sysutils/syshelp.inc
  2. 2 2
      rtl/objpas/sysutils/syshelph.inc

+ 10 - 4
rtl/objpas/sysutils/syshelp.inc

@@ -503,9 +503,12 @@ begin
     end;
     end;
 end;
 end;
 
 
-function TStringHelper.Contains(const AValue: string): Boolean;
+function TStringHelper.Contains(const AValue: string; IgnoreCase: Boolean): Boolean;
 begin
 begin
-  Result:=Pos(AValue,Self)>0;
+  if IgnoreCase then
+    Result:=Pos(LowerCase(AValue),LowerCase(Self))>0
+  else
+    Result:=Pos(AValue,Self)>0;
 end;
 end;
 
 
 
 
@@ -590,10 +593,13 @@ begin
 end;
 end;
 
 
 
 
-function TStringHelper.Equals(const AValue: string): Boolean;
+function TStringHelper.Equals(const AValue: string; IgnoreCase: Boolean = False): Boolean;
 
 
 begin
 begin
-  Result:=(Self=AValue);
+  if IgnoreCase then
+    Result:=SameText(Self,aValue)
+  else
+    Result:=(Self=AValue);
 end;
 end;
 
 
 
 

+ 2 - 2
rtl/objpas/sysutils/syshelph.inc

@@ -104,14 +104,14 @@ Type
     Class Function ToSingle(const S: string): Single; overload; static; inline;
     Class Function ToSingle(const S: string): Single; overload; static; inline;
     Class Function UpperCase(const S: string): string; overload; static; inline;
     Class Function UpperCase(const S: string): string; overload; static; inline;
     Function CompareTo(const B: string): Integer;
     Function CompareTo(const B: string): Integer;
-    Function Contains(const AValue: string): Boolean;
+    Function Contains(const AValue: string; IgnoreCase: Boolean = False): Boolean;
     procedure CopyTo(SourceIndex: SizeInt; var destination: array of Char; DestinationIndex: SizeInt; ACount: SizeInt);
     procedure CopyTo(SourceIndex: SizeInt; var destination: array of Char; DestinationIndex: SizeInt; ACount: SizeInt);
     Function CountChar(const C: Char): SizeInt;
     Function CountChar(const C: Char): SizeInt;
     Function DeQuotedString: string; overload;
     Function DeQuotedString: string; overload;
     Function DeQuotedString(const AQuoteChar: Char): string; overload;
     Function DeQuotedString(const AQuoteChar: Char): string; overload;
     Function EndsWith(const AValue: string): Boolean; overload; inline;
     Function EndsWith(const AValue: string): Boolean; overload; inline;
     Function EndsWith(const AValue: string; IgnoreCase: Boolean): Boolean; overload;
     Function EndsWith(const AValue: string; IgnoreCase: Boolean): Boolean; overload;
-    Function Equals(const AValue: string): Boolean; overload;
+    Function Equals(const AValue: string; IgnoreCase: Boolean = False): Boolean; overload;
     Function Format(const args: array of const): string; overload;
     Function Format(const args: array of const): string; overload;
     Function GetHashCode: Integer;
     Function GetHashCode: Integer;
     Function IndexOf(AValue: Char): SizeInt; overload; inline;
     Function IndexOf(AValue: Char): SizeInt; overload; inline;