forked from malcolmgroves/FluentQuery
-
Notifications
You must be signed in to change notification settings - Fork 4
/
FluentQuery.Strings.MethodFactories.pas
122 lines (107 loc) · 4.37 KB
/
FluentQuery.Strings.MethodFactories.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
{****************************************************}
{ }
{ FluentQuery }
{ }
{ Copyright (C) 2013 Malcolm Groves }
{ }
{ http://www.malcolmgroves.com }
{ }
{****************************************************}
{ }
{ This Source Code Form is subject to the terms of }
{ the Mozilla Public License, v. 2.0. If a copy of }
{ the MPL was not distributed with this file, You }
{ can obtain one at }
{ }
{ http://mozilla.org/MPL/2.0/ }
{ }
{****************************************************}
unit FluentQuery.Strings.MethodFactories;
interface
uses
FluentQuery.Core.MethodFactories, System.SysUtils;
type
TStringMethodFactory = class(TMethodFactory<String>)
private
class function CaseCorrect(IgnoreCase : Boolean; const Value : String ) : string; inline;
public
// predicates
class function StartsWith(const Value : string; IgnoreCase : Boolean) : TPredicate<String>;
class function EndsWith(const Value : string; IgnoreCase : Boolean) : TPredicate<String>;
class function Contains(const Value : string; IgnoreCase : Boolean) : TPredicate<String>;
class function Matches(const Value : string; IgnoreCase : Boolean) : TPredicate<String>;
// transformers
class function SubString(StartIndex, Length : Integer) : TFunc<String, String>;
class function SubStringAfter(const AChar : Char; Length : Integer) : TFunc<String, String>;
end;
implementation
{ TStringPredicateFactory }
class function TStringMethodFactory.CaseCorrect(IgnoreCase: Boolean;
const Value: String): string;
begin
if IgnoreCase then
Result := UpperCase(Value)
else
Result := Value;
end;
class function TStringMethodFactory.Contains(const Value: string;
IgnoreCase: Boolean): TPredicate<String>;
begin
Result := function (CurrentValue : String) : Boolean
begin
Result := CaseCorrect(IgnoreCase,
CurrentValue).Contains(CaseCorrect(IgnoreCase,
Value));
end;
end;
class function TStringMethodFactory.EndsWith(const Value: string;
IgnoreCase: Boolean): TPredicate<String>;
begin
Result := function (CurrentValue : String) : Boolean
begin
Result := CaseCorrect(IgnoreCase,
CurrentValue).EndsWith(CaseCorrect(IgnoreCase,
Value));
end;
end;
class function TStringMethodFactory.Matches(const Value: string;
IgnoreCase: Boolean): TPredicate<String>;
begin
Result := function (CurrentValue : String) : Boolean
begin
Result := CurrentValue.Compare(CurrentValue, Value, IgnoreCase) = 0;
end;
end;
class function TStringMethodFactory.StartsWith(
const Value: string; IgnoreCase : Boolean): TPredicate<String>;
begin
Result := function (CurrentValue : String) : Boolean
begin
Result := CaseCorrect(IgnoreCase,
CurrentValue).StartsWith(CaseCorrect(IgnoreCase,
Value));
end;
end;
class function TStringMethodFactory.SubString(
StartIndex, Length: Integer): TFunc<String, String>;
begin
Result := function (Value : String) : String
begin
if Length < 0 then
Result := Value.Substring(StartIndex)
else
Result := Value.Substring(StartIndex, Length);
end;
end;
class function TStringMethodFactory.SubStringAfter(const AChar: Char;
Length: Integer): TFunc<String, String>;
begin
Result := function (Value : String) : String
begin
if Length < 0 then
Result := Value.Substring(Pos(AChar, Value))
else
Result := Value.Substring(Pos(AChar, Value), Length);
end;
end;
end.