Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dkounal
Created April 30, 2023 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkounal/137630c911a12b545f8dfebfe9efe32e to your computer and use it in GitHub Desktop.
Save dkounal/137630c911a12b545f8dfebfe9efe32e to your computer and use it in GitHub Desktop.
unit mormot.typ.helpers;
{$I mormot.defines.inc}
interface
uses SysUtils, mormot.core.base;
type
TRawUTF8Hlp = record helper for RawUTF8
public
procedure FromString(const Value: String); inline;
procedure FromInteger(Const Value: Integer); overload; inline;
procedure FromInt64(Const Value: Int64); overload; inline;
procedure FromDate(Const Value: TdateTime); inline;
procedure FromDateTime(Const Value: TdateTime; const Separator: AnsiChar = 'T'); overload; inline;
function ToInteger(const _Default: Integer = 0): Integer; inline;
function ToPtrInt(const _min, _max: PtrInt; const _Default: PtrInt = 0) : PtrInt; inline;
function ToInt64(const _Default: Int64 = 0): Int64; inline;
function ToVariant: Variant; inline;
function ToString: string; inline;
function ToUnicodeString: UnicodeString; inline;
function ToCurrency(const DecimalSeparator: Utf8Char): Currency; overload; inline;
function ToCurrency: Currency; overload; inline;
function RawToDateTime: TdateTime; inline;
function Trim: RawUTF8; inline;
function ToBoolean: Boolean; inline;
function ToLower: RawUTF8; inline;
function ToUpperCase: RawUTF8; inline;
function Contains(const Value: RawUTF8): Boolean; inline;
function IndexOf(const Value: RawUTF8): PtrInt; inline;
function Copy(const _Start, _Len: PtrInt): RawUTF8; inline;
function length: PtrInt; inline;
function Equals(const Value: RawUTF8): Boolean; inline;
function IsSame7bit(const Value: RawUTF8): Boolean; inline;
end;
Traw8=class
public
class function ToU8(Const Value: Integer): RawUTF8; overload; static; inline;
class function ToU8(Const Value: Int64): RawUTF8; overload; static; inline;
class function ToU8(Const Value: string): RawUTF8; overload; static; inline;
class function Date(Const Value: TDateTime): RawUTF8; static; inline;
class function DateTime(Const Value: TDateTime; const Separator: AnsiChar = 'T'): RawUTF8; static; inline;
end;
TvariantHlp = record helper for Variant
public
procedure ToDocVariant; inline;
function ToRawutf8: RawUTF8; inline;
function ToDateTime: TdateTime; inline;
function ToInteger: Integer; inline;
function ToInt64: Int64; inline;
function ToString: String; inline;
function ToBoolean: Boolean; inline;
function ToCurrency: Currency; inline;
end;
var datsets: Tformatsettings;
implementation
uses mormot.core.unicode, mormot.core.datetime, mormot.core.text, mormot.core.variants;
{ DRawUtf8Hhp }
function TRawUTF8Hlp.ToInteger(const _Default: Integer = 0): Integer;
begin
result := Utf8ToInteger(self, _Default);
end;
function TRawUTF8Hlp.Contains(const Value: RawUTF8): Boolean;
begin
result := PosEx(Value, self) > 0;
end;
function TRawUTF8Hlp.Copy(const _Start, _Len: PtrInt): RawUTF8;
var
s: string;
begin
s := utf8tostring(self);
result := stringtoutf8(system.Copy(s, _Start, _Len));
end;
function TRawUTF8Hlp.Equals(const Value: RawUTF8): Boolean;
begin
result := self = Value;
end;
procedure TRawUTF8Hlp.FromDate(const Value: TdateTime);
begin
if Value = 0 then
self := '0'
else
self := mormot.core.datetime.DateToIso8601(Value, False);
end;
procedure TRawUTF8Hlp.FromDateTime(const Value: TdateTime; const Separator: AnsiChar);
begin
if Value = 0 then
self := '0'
else
DateTimeToIso8601Var(Value, true, False, Separator, #0, self);
end;
procedure TRawUTF8Hlp.FromInt64(const Value: Int64);
begin
self := ToUtf8(Value);
end;
procedure TRawUTF8Hlp.FromInteger(const Value: Integer);
begin
self := ToUtf8(Value);
end;
procedure TRawUTF8Hlp.FromString(const Value: String);
begin
self := stringtoutf8(Value);
end;
function TRawUTF8Hlp.IndexOf(const Value: RawUTF8): PtrInt;
begin
result := PosEx(Value, self);
end;
function TRawUTF8Hlp.IsSame7bit(const Value: RawUTF8): Boolean;
begin
result := PropNameEquals(self, Value);
end;
function TRawUTF8Hlp.length: PtrInt;
begin
result := system.length(utf8tostring(self));
end;
function TRawUTF8Hlp.ToBoolean: Boolean;
begin
result := PropNameEquals(self, 'true') or (self = '1');
end;
function TRawUTF8Hlp.ToCurrency: Currency;
begin
result := ToCurrency(Utf8Char(datsets.DecimalSeparator));
end;
function TRawUTF8Hlp.ToCurrency(const DecimalSeparator: Utf8Char): Currency;
begin
try
if self = '' then
exit(0)
else
result := StrToCurrency(Putf8char(self), DecimalSeparator);
except
result := 0;
end;
end;
function TRawUTF8Hlp.RawToDateTime: TdateTime;
var
s: RawUTF8;
i: PtrInt;
begin
try
s := TrimU(self);
i := system.length(s);
if i = 14 then
begin
s := system.Copy(s, 1, 8) + 'T' + system.Copy(s, 9, 6);
inc(i);
end;
result := Iso8601ToDateTimePUtf8Char(Putf8char(s), i);
except
result := 0;
end;
end;
function TRawUTF8Hlp.ToInt64(const _Default: Int64): Int64;
begin
result := Utf8ToInt64(self, _Default);
end;
function TRawUTF8Hlp.ToPtrInt(const _min, _max, _Default: PtrInt): PtrInt;
begin
result := Utf8ToInteger(self, _min, _max, _Default);
end;
function TRawUTF8Hlp.ToLower: RawUTF8;
begin
result := LowerCaseU(self);
end;
function TRawUTF8Hlp.ToString: string;
begin
result := utf8tostring(self);
end;
function TRawUTF8Hlp.ToUnicodeString: UnicodeString;
begin
result := Utf8ToSynUnicode(self);
end;
function TRawUTF8Hlp.ToUpperCase: RawUTF8;
begin
result := UpperCaseU(self);
end;
function TRawUTF8Hlp.ToVariant: Variant;
begin
result := RawUtf8ToVariant(self);
end;
function TRawUTF8Hlp.Trim: RawUTF8;
begin
result:=TrimU(self);
end;
{ Traw8 }
class function Traw8.ToU8(const Value: string): RawUTF8;
begin
result := stringtoutf8(Value);
end;
class function Traw8.ToU8(const Value: Integer): RawUTF8;
begin
result := ToUtf8(Value);
end;
class function Traw8.ToU8(const Value: Int64): RawUTF8;
begin
result := ToUtf8(Value);
end;
class function Traw8.Date(const Value: TDateTime): RawUTF8;
begin
if Value = 0 then
Result := '0'
else
Result := mormot.core.datetime.DateToIso8601(Value, False);
end;
class function Traw8.DateTime(const Value: TDateTime; const Separator: AnsiChar): RawUTF8;
begin
if Value = 0 then
Result := '0'
else
DateTimeToIso8601Var(Value, true, False, Separator, #0, Result);
end;
{ TvariantHlp }
function TvariantHlp.ToBoolean: Boolean;
begin
if not VariantToboolean(self, result) then result := False;
end;
function TvariantHlp.ToCurrency: Currency;
begin
if not VariantToCurrency(self, result) then result := 0;
end;
function TvariantHlp.ToDateTime: TdateTime;
begin
if not VariantToDateTime(self, result) then result := 0;
end;
procedure TvariantHlp.ToDocVariant;
begin
TDocVariant.New(self);
end;
function TvariantHlp.ToInt64: Int64;
begin
if not VariantToInt64(self, result) then result := 0;
end;
function TvariantHlp.ToInteger: Integer;
begin
if not VariantToInteger(self, result) then result := 0;
end;
function TvariantHlp.ToRawutf8: RawUTF8;
begin
if not VariantToText(self, result) and (result = 'null') then result := '';
end;
function TvariantHlp.ToString: String;
begin
result := VariantToString(self);
end;
{$IFNDEF FPC}
initialization
datsets := Tformatsettings.Create;
{$ENDIF}
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment