* Incorporate decimal conversion to conversion function

This commit is contained in:
Vovanium 2023-12-18 23:29:46 +03:00
parent 07c225238b
commit ba4359ae59
4 changed files with 22 additions and 35 deletions

View File

@ -32,6 +32,26 @@ is
(S'Length >= 4 and then S (S'First) in 'B' | 'b' | 'O' | 'o' | 'Z' | 'z' and then
S (S'First + 1) = S (S'Last) and then S (S'Last) in ''' | '"'); --"
-- Convert simple decimal number
function Decimal (S : String) return Exponent_Type is
N : Exponent_Type := 0;
D : Exponent_Type;
begin
for C of S loop
if C in '0' .. '9' then
D := Character'Pos (C) - Character'Pos ('0');
if N > (Exponent_Type'Last - D) / 10 then
return Exponent_Type'First; -- carefully treat overflow
end if; -- to have meaningfull exception
N := N * 10 + D;
elsif C not in NUL .. ' ' | '|' then
raise Constraint_Error with "Illegal character " & Character'Image (C);
end if;
end loop;
return N;
end Decimal;
-- Convert mantissa
procedure Mantissa (
S : in String;
R : in Radix_Type;
@ -173,7 +193,7 @@ begin
end loop Radix_Find;
if Based then
Radix := Decimal_to_Number (S (RF .. RL));
Radix := Integer (Decimal (S (RF .. RL)));
-- raise constraint error when not in range
elsif not Based then
Dec_Exp_Find : for J in MF + 1 .. ML - 1 loop
@ -202,7 +222,7 @@ begin
if Exp then
Sign (S, EF, EL, NE);
EE := Decimal_to_Number (S (EF .. EL));
EE := Decimal (S (EF .. EL));
if EE < 0 then
raise Constraint_Error with "Exponent value is out of range";
end if;

View File

@ -15,9 +15,6 @@ generic
with function To_Number (
A : Integer)
return Number_Type is <>; -- A function to convert a digit to a given type
with function Decimal_to_Number (
S : String)
return Exponent_Type is <>; -- A function to convert decimal digits to number
with function Multiply_by_Power (
A,
R : Number_Type;

View File

@ -1,24 +0,0 @@
with Ada.Characters.Latin_1;
use Ada.Characters.Latin_1;
package body VSSL.Conversions is
function Decimal_to_Number (S : String) return Integer is
N : Integer := 0;
D : Integer;
begin
for C of S loop
if C in '0' .. '9' then
D := Character'Pos (C) - Character'Pos ('0');
if N > (Integer'Last - D) / 10 then
return Integer'First; -- carefully treat overflow
end if; -- to have meaningfull exception
N := N * 10 + D;
elsif C not in NUL .. ' ' | '|' then
raise Constraint_Error with "Illegal character " & Character'Image (C);
end if;
end loop;
return N;
end Decimal_to_Number;
end VSSL.Conversions;

View File

@ -8,10 +8,4 @@ package VSSL.Conversions with Pure is
return Integer is (if N >= 0 then A * R**N else A / R**(-N));
-- used by conversion subprograms
function Decimal_to_Number (
S : String)
return Integer;
-- Convert decimal number (without a sign) to an integer
-- It is used by conversion subprograms
end VSSL.Conversions;