Skip to main content

Helper

As stated in an article about Object Pascal,

Helpers are a way to extend a class without using inheritance, which is also useful for records that do not allow inheritance at all.

FastPlaz provides several helpers that make writing Pascal programs easier. Using helpers makes your code easy to read, even close to everyday human language.

UnitDescription
string_helperHelper for the AnsiString/String data type
datetime_helperHelper for the TDateTime data type
integer_helperHelper for the Integer data type
array_helperHelper for the TStringArray data type
verbal_expressions_libVerbal Expression

Some of the functions in these helpers are already covered by fpc 3.2.0.

string_helper​

Given these variables:

d : TDateTime;
s : String;
i : integer;

This helper can do many things.

d := s.AsDateTime; //convert date string to datetime
i := s.AsInteger; //convert string to integer

// if s contains the word 'cari'
if s.Has('cari') then begin ... end;

// if s is a url/email
if s.IsEmail then ...
if s.IsURL then ...

s := s.UrlEncode;
s := s.Encode64;
s := s.Decode64;

s := 'saya makan nasi';
s := s.cut('saya','nasi').trim;
// s contains 'makan'

s.SaveToFile('/path/filename');

More examples are in the repository documentation.

For the full list, see the unit file string_helper.

datetime_helper​

Given these variables:

d : TDateTime;
s : String;

This helper can do many things.

d := d.FromString( '17-08-1945 09:59:00');
s := d.Format( 'yyyy-mm-dd HH:nn:ss');
s := d.AsString;

if d.IsAM then begin ... end;
if d.IsPM then begin ... end;
if d.IsToday then begin ... end;

if Tomorrow.IsSaturday then begin ... end;
if d.HourOf = 9 then ....

d := d.IncMinute(1);

if d.YearsDiff( Now) > 40 then begin ... end;

s := d.HumanReadable;
// 'more than 73 years ago'

// convert date to unixtime
i := d.AsUnixTime;

More examples are in the repository documentation.

For the full list, see the unit file datetime_helper.

integer_helper​

var
i: integer;

// if i is between 300 and 400
if i.InRange( 300, 400) then ...

// convert unixtime to tdatetime
d := i.AsDateTime;

For the full list, see the unit file integer_helper.

array_helper​

// check whether the string 'zero' is in the array
const
NUMBER_LIST: array[0..5] of string = ('zero', 'one', 'two', 'three', 'four', 'five');
var
i: integer;
whereAsArray: TStringArray;

.
.
if 'zero' in NUMBER_LIST then
begin
//
end;

// add a string to an array of string
whereAsArray.Add('isi string');

// get the length/number of items in the array
i := whereAsArray.Count;

For the full list, see the unit file array_helpers.

Verbal Expression​

Verbal Expression is a FastPlaz library that helps construct difficult regular expressions.

VE := TVerbalExpressions.Create;
VE.StartOfLine()
.Has('http')
.Maybe('s')
.Has('://')
.Maybe('www.')
.AnythingBut(' ')
.EndOfLine(false);

if VE.IsMatch('https://fastplaz.com') then
begin
// your code
end;


// expression: ^(http)(s)?(:\/\/)(www\.)?([^ ]*)

This code produces the regex: ^(http)(s)?(:\/\/)(www\.)?([^ ]*).

Replace String​

varString := 'Replace bird with a duck';
VE.Find('bird');
varString := VE.Replace(varString, 'duck');

or

varString := VE.Find('red').Replace('We have a red house', 'blue');

Methods list​

NameDescriptionUsage
Addadd values to the expressionAdd('abc')
startOfLinemark expression with ^startOfLine(false)
endoOfLinemark the expression with $endOfLine()
hasadd a string to the expressionhas('foo')
findalias for hasfind('foo')
maybedefine a string that might appear once or notmaybe('.com')
anythingaccept any stringanything()
anythingButaccept any string but the specified charanythingBut(',')
somethingaccept any non-empty stringsomething()
somethingButanything non-empty except for these charssomethingBut('a')
LineBreakmatch \r \nlineBreak()
brshorthand for lineBreakbr()
tabmatch tabs \ttab()
wordmatch \w+word()
anyOfany of the listed charsanyOf('abc')
anyshorthand for anyOfany('abc')
rangeadds a range to the expressionrange(a,z,0,9)

Note​

Inspiration from VerbalExpressions