rtl: added overload TryStrToFloat with type extended

This commit is contained in:
mattias 2019-03-05 10:59:04 +00:00
parent 834ef2de26
commit e3a287b9b3

View File

@ -273,7 +273,8 @@ Type
Function FloatToDecimal(Value : double; Precision, Decimals : integer) : TFloatRec; Function FloatToDecimal(Value : double; Precision, Decimals : integer) : TFloatRec;
Function FloatToStr(Value: Double): String; Function FloatToStr(Value: Double): String;
Function FloatToStrF(const Value : double; format: TFloatFormat; Precision, Digits: Integer): String; Function FloatToStrF(const Value : double; format: TFloatFormat; Precision, Digits: Integer): String;
Function TryStrToFloat(const S : String; Out res : Double) : Boolean; Function TryStrToFloat(const S : String; Out res : Extended) : Boolean; overload;
Function TryStrToFloat(const S : String; Out res : Double) : Boolean; overload;
Function StrToFloatDef(const S : String; Const aDef : Double) : Double; Function StrToFloatDef(const S : String; Const aDef : Double) : Double;
Function StrToFloat(const S : String) : Double; Function StrToFloat(const S : String) : Double;
Function FormatFloat (Fmt : String; aValue : Double) : String; Function FormatFloat (Fmt : String; aValue : Double) : String;
@ -659,7 +660,7 @@ begin
Result:=Ch in CSet; Result:=Ch in CSet;
end; end;
Function CharInSet(Ch: Char;Const CSet : array of char) : Boolean; function CharInSet(Ch: Char; const CSet: array of char): Boolean;
Var Var
I : integer; I : integer;
@ -705,7 +706,7 @@ begin
Result:=str(Value); Result:=str(Value);
end; end;
Function FloatToDecimal(Value : double; Precision, Decimals : integer) : TFloatRec; function FloatToDecimal(Value: double; Precision, Decimals: integer): TFloatRec;
Const Const
Rounds = '123456789:'; Rounds = '123456789:';
@ -852,7 +853,12 @@ begin
Result:=FloatToStrF(Value,ffGeneral,15,0); Result:=FloatToStrF(Value,ffGeneral,15,0);
end; end;
Function TryStrToFloat(const S : String; Out res : Double) : Boolean; function TryStrToFloat(const S: String; out res: Extended): Boolean;
begin
Result:=TryStrToFloat(S,double(res));
end;
function TryStrToFloat(const S: String; out res: Double): Boolean;
Var Var
J : JSValue; J : JSValue;
@ -871,14 +877,13 @@ begin
Res:=Double(J); Res:=Double(J);
end; end;
Function StrToFloatDef(const S : String; Const aDef : Double) : Double; function StrToFloatDef(const S: String; const aDef: Double): Double;
begin begin
if not TryStrToFloat(S,Result) then if not TryStrToFloat(S,Result) then
Result:=aDef; Result:=aDef;
end; end;
Function StrToFloat(const S : String) : Double; function StrToFloat(const S: String): Double;
begin begin
if not TryStrToFloat(S,Result) then if not TryStrToFloat(S,Result) then
Raise EConvertError.CreateFmt(SErrInvalidFloat,[S]); Raise EConvertError.CreateFmt(SErrInvalidFloat,[S]);
@ -1721,7 +1726,8 @@ Begin
end; end;
end; end;
Function FloatToStrF(const Value : double; format: TFloatFormat; Precision, Digits: Integer): String; function FloatToStrF(const Value: double; format: TFloatFormat; Precision,
Digits: Integer): String;
Var Var
DS: string; DS: string;
@ -1744,7 +1750,7 @@ Begin
RemoveLeadingNegativeSign(Result,DS); RemoveLeadingNegativeSign(Result,DS);
end; end;
function Format (const Fmt: String; const Args: array of jsvalue): String; function Format(const Fmt: String; const Args: array of JSValue): String;
Var ChPos,OldPos,ArgPos,DoArg,Len : SizeInt; Var ChPos,OldPos,ArgPos,DoArg,Len : SizeInt;
Hs,ToAdd : String; Hs,ToAdd : String;
@ -2131,7 +2137,8 @@ end;
Const Const
RESpecials = '([\[\]\(\)\\\.\*])'; RESpecials = '([\[\]\(\)\\\.\*])';
Function StringReplace(aOriginal, aSearch, aReplace : string; Flags : TStringReplaceFlags) : String; function StringReplace(aOriginal, aSearch, aReplace: string;
Flags: TStringReplaceFlags): String;
Var Var
REFlags : String; REFlags : String;
@ -2147,7 +2154,7 @@ begin
Result:=TJSString(aOriginal).replace(TJSRegexp.new(REString,REFlags),aReplace); Result:=TJSString(aOriginal).replace(TJSRegexp.new(REString,REFlags),aReplace);
end; end;
Function QuoteString(aOriginal : String; AQuote : Char) : String; function QuoteString(aOriginal: String; AQuote: Char): String;
begin begin
Result:=AQuote+StringReplace(aOriginal,aQuote,aQuote+aQuote,[rfReplaceAll])+AQuote; Result:=AQuote+StringReplace(aOriginal,aQuote,aQuote+aQuote,[rfReplaceAll])+AQuote;
@ -2236,7 +2243,8 @@ begin
Result:=Res; Result:=Res;
end; end;
function WrapText(const Line, BreakStr: string; const BreakChars: Array of char; MaxCol: Integer): string; function WrapText(const Line, BreakStr: string;
const BreakChars: array of char; MaxCol: Integer): string;
const const
Quotes = ['''', '"']; Quotes = ['''', '"'];
@ -2345,7 +2353,7 @@ begin
Result:=0; Result:=0;
end; end;
Function DateTimeToJSDate(aDateTime : TDateTime) : TJSDate; function DateTimeToJSDate(aDateTime: TDateTime): TJSDate;
Var Var
Y,M,D,h,n,s,z : Word; Y,M,D,h,n,s,z : Word;
@ -2356,7 +2364,7 @@ begin
Result:=TJSDate.New(Y,M,D,h,n,s,z); Result:=TJSDate.New(Y,M,D,h,n,s,z);
end; end;
Function JSDatetoDateTime(ADate: TJSDate) : TDateTime; function JSDateToDateTime(aDate: TJSDate): TDateTime;
begin begin
Result:=EncodeDate(ADate.FullYear,ADate.Month+1,ADate.Date) + Result:=EncodeDate(ADate.FullYear,ADate.Month+1,ADate.Date) +
@ -2374,7 +2382,7 @@ begin
Result := trunc(Date) + Abs(frac(Time)); Result := trunc(Date) + Abs(frac(Time));
end; end;
Function TryEncodeDate(Year,Month,Day : Word; Out Date : TDateTime) : Boolean; function TryEncodeDate(Year, Month, Day: Word; out Date: TDateTime): Boolean;
var var
c, ya: LongWord; c, ya: LongWord;
@ -2400,7 +2408,8 @@ begin
end end
end; end;
function TryEncodeTime(Hour, Min, Sec, MSec:word; Out Time : TDateTime) : boolean; function TryEncodeTime(Hour, Min, Sec, MSec: Word; out Time: TDateTime
): Boolean;
begin begin
Result:=(Hour<24) and (Min<60) and (Sec<60) and (MSec<1000); Result:=(Hour<24) and (Min<60) and (Sec<60) and (MSec<1000);
@ -3032,7 +3041,7 @@ begin
ErrorMsg:=Format(SErrInvalidTimeFormat,[S]); ErrorMsg:=Format(SErrInvalidTimeFormat,[S]);
end ; end ;
function StrToTime(const s: String; separator : char): TDateTime; function StrToTime(const S: String; separator: char): TDateTime;
Var Var
Msg : String; Msg : String;
@ -3043,7 +3052,7 @@ begin
Raise EConvertError.Create(Msg); Raise EConvertError.Create(Msg);
end; end;
function StrToTime(const s: String): TDateTime; function StrToTime(const S: String): TDateTime;
begin begin
result:= StrToTime(s, TimeSeparator); result:= StrToTime(s, TimeSeparator);
end; end;
@ -3125,7 +3134,7 @@ begin
end; end;
end; end;
function StrToDateTime(const s: String): TDateTime; function StrToDateTime(const S: String): TDateTime;
var var
TimeStr, DateStr: String; TimeStr, DateStr: String;
@ -3143,7 +3152,8 @@ begin
end; end;
end; end;
Function FormatDateTime(const FormatStr: string; const DateTime: TDateTime) : String; function FormatDateTime(const FormatStr: string; const DateTime: TDateTime
): string;
procedure StoreStr(APos,Len: Integer); procedure StoreStr(APos,Len: Integer);
begin begin
@ -3510,7 +3520,7 @@ begin
DateTime:=tmp; DateTime:=tmp;
end; end;
Function FloatToDateTime (Const Value : Extended) : TDateTime; function FloatToDateTime(const Value: Extended): TDateTime;
begin begin
If (Value<MinDateTime) or (Value>MaxDateTime) then If (Value<MinDateTime) or (Value>MaxDateTime) then
Raise EConvertError.CreateFmt (SInvalidDateTime,[FloatToStr(Value)]); Raise EConvertError.CreateFmt (SInvalidDateTime,[FloatToStr(Value)]);
@ -3603,14 +3613,13 @@ begin
and (TObject(Obj).InheritsFrom(AClass)); and (TObject(Obj).InheritsFrom(AClass));
end; end;
function Supports(const Instance: IInterface; const IID: TGUID; out Intf function Supports(const Instance: IInterface; const IID: TGuid; out Intf
): Boolean; ): Boolean;
begin begin
Result:=(Instance<>nil) and (Instance.QueryInterface(IID,Intf)=S_OK); Result:=(Instance<>nil) and (Instance.QueryInterface(IID,Intf)=S_OK);
end; end;
function Supports(const Instance: TObject; const IID: TGUID; out Intf function Supports(const Instance: TObject; const IID: TGuid; out Intf): Boolean;
): Boolean;
begin begin
Result:=(Instance<>nil) and Instance.GetInterface(IID,Intf); Result:=(Instance<>nil) and Instance.GetInterface(IID,Intf);
end; end;
@ -3628,14 +3637,14 @@ begin
Result:=Supports(Instance,AClass,Temp); Result:=Supports(Instance,AClass,Temp);
end; end;
function Supports(const Instance: IInterface; const IID: TGUID): Boolean; function Supports(const Instance: IInterface; const IID: TGuid): Boolean;
var var
Temp: IInterface; Temp: IInterface;
begin begin
Result:=Supports(Instance,IID,Temp); Result:=Supports(Instance,IID,Temp);
end; end;
function Supports(const Instance: TObject; const IID: TGUID): Boolean; function Supports(const Instance: TObject; const IID: TGuid): Boolean;
var var
Temp: TJSObject; Temp: TJSObject;
begin begin
@ -3655,7 +3664,7 @@ begin
end; end;
end; end;
function Supports(const AClass: TClass; const IID: TGUID): Boolean; function Supports(const AClass: TClass; const IID: TGuid): Boolean;
var var
maps: JSValue; maps: JSValue;
begin begin
@ -3677,7 +3686,7 @@ begin
Result:=false; Result:=false;
end; end;
function TryStringToGUID(const s: string; out Guid: TGUID): Boolean; function TryStringToGUID(const s: string; out Guid: TGuid): Boolean;
var var
re: TJSRegexp; re: TJSRegexp;
begin begin
@ -3695,18 +3704,18 @@ begin
Result:=true; Result:=true;
end; end;
function StringToGUID(const S: string): TGUID; function StringToGUID(const S: string): TGuid;
begin begin
if not TryStringToGUID(S, Result) then if not TryStringToGUID(S, Result) then
raise EConvertError.CreateFmt(SInvalidGUID, [S]); raise EConvertError.CreateFmt(SInvalidGUID, [S]);
end; end;
function GUIDToString(const guid: TGUID): string; function GUIDToString(const guid: TGuid): string;
begin begin
Result:=System.GUIDToString(guid); Result:=System.GUIDToString(guid);
end; end;
function IsEqualGUID(const guid1, guid2: TGUID): Boolean; function IsEqualGUID(const guid1, guid2: TGuid): Boolean;
var var
i: integer; i: integer;
begin begin
@ -3716,7 +3725,7 @@ begin
Result:=true; Result:=true;
end; end;
function GuidCase(const guid: TGUID; const List: array of TGuid): Integer; function GuidCase(const guid: TGuid; const List: array of TGuid): Integer;
begin begin
for Result := High(List) downto 0 do for Result := High(List) downto 0 do
if IsEqualGUID(guid, List[Result]) then if IsEqualGUID(guid, List[Result]) then
@ -3724,7 +3733,7 @@ begin
Result := -1; Result := -1;
end; end;
Function CreateGUID(out GUID : TGUID) : Integer; function CreateGUID(out GUID: TGUID): Integer;
Function R(B: Integer) : NativeInt; Function R(B: Integer) : NativeInt;
@ -3755,7 +3764,7 @@ end;
Integer/Ordinal related Integer/Ordinal related
---------------------------------------------------------------------} ---------------------------------------------------------------------}
Function TryStrToInt(const S : String; Out res : Integer) : Boolean; function TryStrToInt(const S: String; out res: Integer): Boolean;
Var Var
NI : NativeInt; NI : NativeInt;
@ -3766,7 +3775,7 @@ begin
res:=NI; res:=NI;
end; end;
Function TryStrToInt(const S : String; Out res : NativeInt) : Boolean; function TryStrToInt(const S: String; out res: NativeInt): Boolean;
Var Var
Radix : Integer = 10; Radix : Integer = 10;
@ -3788,7 +3797,7 @@ begin
res:=NativeInt(J); res:=NativeInt(J);
end; end;
Function StrToIntDef(const S : String; Const aDef : Integer) : Integer; function StrToIntDef(const S: String; const aDef: Integer): Integer;
Var Var
R : NativeInt; R : NativeInt;
@ -3800,7 +3809,7 @@ begin
Result:=aDef; Result:=aDef;
end; end;
Function StrToIntDef(const S : String; Const aDef : NativeInt) : NativeInt; function StrToIntDef(const S: String; const aDef: NativeInt): NativeInt;
Var Var
R : NativeInt; R : NativeInt;
@ -3812,7 +3821,7 @@ begin
Result:=aDef; Result:=aDef;
end; end;
Function StrToInt(const S : String) : Integer; function StrToInt(const S: String): Integer;
Var Var
R : NativeInt; R : NativeInt;
@ -3823,14 +3832,14 @@ begin
Result:=R; Result:=R;
end; end;
Function StrToNativeInt(const S : String) : NativeInt; function StrToNativeInt(const S: String): NativeInt;
begin begin
if not TryStrToInt(S,Result) then if not TryStrToInt(S,Result) then
Raise EConvertError.CreateFmt(SErrInvalidInteger,[S]); Raise EConvertError.CreateFmt(SErrInvalidInteger,[S]);
end; end;
Function StrToInt64(const S : String) : NativeLargeInt; function StrToInt64(const S: String): NativeLargeInt;
Var Var
N : NativeInt; N : NativeInt;
@ -3841,7 +3850,7 @@ begin
Result:=N; Result:=N;
end; end;
Function TryStrToInt64(const S : String; Out res : NativeLargeInt) : Boolean; function TryStrToInt64(const S: String; out res: NativeLargeInt): Boolean;
Var Var
R : nativeint; R : nativeint;
@ -3852,7 +3861,8 @@ begin
Res:=R; Res:=R;
end; end;
Function StrToInt64Def(const S : String; ADefault : NativeLargeInt) : NativeLargeInt; function StrToInt64Def(const S: String; ADefault: NativeLargeInt
): NativeLargeInt;
begin begin
@ -3860,7 +3870,7 @@ begin
Result:=ADefault; Result:=ADefault;
end; end;
Function StrToQWord(const S : String) : NativeLargeUInt; function StrToQWord(const S: String): NativeLargeUInt;
Var Var
N : NativeInt; N : NativeInt;
@ -3871,7 +3881,7 @@ begin
Result:=N; Result:=N;
end; end;
Function TryStrToQWord(const S : String; Out res : NativeLargeUInt) : Boolean; function TryStrToQWord(const S: String; out res: NativeLargeUInt): Boolean;
Var Var
R : nativeint; R : nativeint;
@ -3882,7 +3892,8 @@ begin
Res:=R; Res:=R;
end; end;
Function StrToQWordDef(const S : String; ADefault : NativeLargeUInt) : NativeLargeUInt; function StrToQWordDef(const S: String; ADefault: NativeLargeUInt
): NativeLargeUInt;
begin begin
if Not TryStrToQword(S,Result) then if Not TryStrToQword(S,Result) then
@ -3890,7 +3901,7 @@ begin
end; end;
Function StrToUInt64(const S : String) : NativeLargeUInt; function StrToUInt64(const S: String): NativeLargeUInt;
Var Var
N : NativeInt; N : NativeInt;
@ -3901,7 +3912,7 @@ begin
Result:=N; Result:=N;
end; end;
Function TryStrToUInt64(const S : String; Out res : NativeLargeUInt) : Boolean; function TryStrToUInt64(const S: String; out res: NativeLargeUInt): Boolean;
Var Var
R : nativeint; R : nativeint;
@ -3912,7 +3923,8 @@ begin
Res:=R; Res:=R;
end; end;
Function StrToUInt64Def(const S : String; ADefault : NativeLargeUInt) : NativeLargeUInt; function StrToUInt64Def(const S: String; ADefault: NativeLargeUInt
): NativeLargeUInt;
begin begin
@ -3920,7 +3932,7 @@ begin
Result:=ADefault; Result:=ADefault;
end; end;
Function TryStrToDWord(const S : String; Out res : DWord) : Boolean; function TryStrToDWord(const S: String; out res: DWord): Boolean;
Var Var
R : nativeint; R : nativeint;
@ -3931,7 +3943,7 @@ begin
Res:=R; Res:=R;
end; end;
Function StrToDWord(const S : String) : DWord; function StrToDWord(const S: String): DWord;
begin begin
if not TryStrToDWord(S,Result) then if not TryStrToDWord(S,Result) then
@ -3939,7 +3951,7 @@ begin
end; end;
Function StrToDWordDef(const S : String; ADefault : DWord) : DWord; function StrToDWordDef(const S: String; ADefault: DWord): DWord;
begin begin
if Not TryStrToDWord(S,Result) then if Not TryStrToDWord(S,Result) then
@ -4262,7 +4274,7 @@ begin
Result := ''; Result := '';
end; end;
function ExtractRelativepath (Const BaseName,DestName : PathStr): PathStr; function ExtractRelativepath(const BaseName, DestName: PathStr): PathStr;
Var Var
OneLevelBack,Source, Dest : PathStr; OneLevelBack,Source, Dest : PathStr;
@ -4297,7 +4309,7 @@ begin
Result:=Result+ExtractFileName(DestName); Result:=Result+ExtractFileName(DestName);
end; end;
Function SetDirSeparators (Const FileName : PathStr) : PathStr; function SetDirSeparators(const FileName: PathStr): PathStr;
Var Var
I : integer; I : integer;
@ -4309,7 +4321,7 @@ begin
Result[i]:=PathDelim; Result[i]:=PathDelim;
end; end;
Function GetDirs (DirName : PathStr) : TPathStrArray; function GetDirs(DirName: PathStr): TPathStrArray;
Var Var
I,J,L : Longint; I,J,L : Longint;
@ -4337,7 +4349,7 @@ begin
SetLength(Result,L); SetLength(Result,L);
end; end;
function IncludeTrailingPathDelimiter(Const Path : PathStr) : PathStr; function IncludeTrailingPathDelimiter(const Path: PathStr): PathStr;
Var Var
l : Integer; l : Integer;
@ -4349,7 +4361,7 @@ begin
Result:=Result+PathDelim; Result:=Result+PathDelim;
end; end;
function ExcludeTrailingPathDelimiter(Const Path: PathStr): PathStr; function ExcludeTrailingPathDelimiter(const Path: PathStr): PathStr;
Var Var
L : Integer; L : Integer;
@ -4361,7 +4373,7 @@ begin
Result:=Copy(Path,1,L); Result:=Copy(Path,1,L);
end; end;
function IncludeLeadingPathDelimiter(Const Path : PathStr) : PathStr; function IncludeLeadingPathDelimiter(const Path: PathStr): PathStr;
Var Var
l : Integer; l : Integer;
@ -4373,7 +4385,7 @@ begin
Result:=PathDelim+Result; Result:=PathDelim+Result;
end; end;
function ExcludeLeadingPathDelimiter(Const Path: PathStr): PathStr; function ExcludeLeadingPathDelimiter(const Path: PathStr): PathStr;
Var Var
L : Integer; L : Integer;
@ -4385,7 +4397,7 @@ begin
Delete(Result,1,1); Delete(Result,1,1);
end; end;
function IsPathDelimiter(Const Path: PathStr; Index: Integer): Boolean; function IsPathDelimiter(const Path: PathStr; Index: Integer): Boolean;
begin begin
Result:=(Index>0) and (Index<=Length(Path)) and CharInSet(Path[Index],AllowDirectorySeparators); Result:=(Index>0) and (Index<=Length(Path)) and CharInSet(Path[Index],AllowDirectorySeparators);