[fpc-pascal] BoolToStr() with correct locale output

Graeme Geldenhuys graemeg at opensoft.homeip.net
Fri Jun 26 16:42:52 CEST 2009


Vincent Snijders wrote:
> 
> Please add the declaration, to make sure it is StrToBool

I'm not that blind. :-)  And the function in question in BoolToStr().
But now that you mention StrToBool(), that is even worse, it hard-codes
the 'FALSE' and 'TRUE' and doesn't even use the arrays.

How come nobody ever noticed this? Does everybody only write English
programs. :-)

==================================
function BoolToStr(B: Boolean;UseBoolStrs:Boolean=False): string;

procedure CheckStrs;
begin
    If Length(TrueBoolStrs)=0 then
      begin
        SetLength(TrueBoolStrs,1);
        TrueBoolStrs[0]:='True';
      end;
    If Length(FalseBoolStrs)=0 then
      begin
        SetLength(FalseBoolStrs,1);
        FalseBoolStrs[0]:='False';
      end;
end;

begin
 if UseBoolStrs Then
  begin
    CheckStrs;
    if B then
      Result:=TrueBoolStrs[0]
    else
      Result:=FalseBoolStrs[0];
  end
 else
  If B then
    Result:='-1'
  else
    Result:='0';
end;


==================================

function TryStrToBool(const S: string; out Value: Boolean): Boolean;
Var
  Temp : String;
  D : Double;
  Code: word;
begin
  Temp:=upcase(S);
  Val(temp,D,code);
  Result:=true;
  If Code=0 then
    Value:=(D<>0.0)
  else If Temp='TRUE' then
    Value:=true
  else if Temp='FALSE' then
    Value:=false
  else
    Result:=false;
end;

==================================



Regards,
  - Graeme -

_______________________________________________________
fpGUI - a cross-platform GUI toolkit using Free Pascal
http://opensoft.homeip.net/fpgui/




More information about the fpc-pascal mailing list