Hosted by
|
package body Number_Strings is
function To_Number
(Value : in String)
return Number
is
Index : Positive := Value'First;
Result : Number := 0;
Negative : Boolean := Value(Index) = '-';
begin
if Negative then Index := Index + 1; end if;
loop
exit when Index > Value'Last;
exit when not (Value(Index) in '0' .. '9');
Result := Result * 10
+ Character'Pos(Value(Index))
- Character'Pos('0');
Index := Index + 1;
end loop;
if Negative then Result := -Result; end if;
return Result;
end To_Number;
function Fill_String
(Value : in String;
Fill : in Positive;
Char : in Character)
return String
is
Prefix_Length : Natural := Integer'Max(0, Fill - Value'Length);
Prefix : String(1 .. Prefix_Length) := (others => Char);
begin
return Prefix & Value;
end Fill_String;
function To_String
(Value : in Number;
Fill : in Natural;
Char : in Character := '0')
return String is
begin
if Fill /= 0 then
declare
Result : String := To_String(Value);
begin
return Fill_String(Result, Fill, Char);
end;
elsif Value < 0 then
return "-" & To_String(-Value);
elsif Value >= 10 then
return To_String(Value / 10) &
Character'Val(Character'Pos('0') + Value mod 10);
else
return "" & Character'Val(Character'Pos('0') + Value mod 10);
end if;
end To_String;
function To_String
(Value : in Number)
return String is
begin
return To_String(Value, 0);
end To_String;
end Number_Strings;
|