comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: From_string(Arg : String) return Big_Integer
Date: Sun, 10 Jan 2021 10:46:31 +0100	[thread overview]
Message-ID: <rteidn$1to4$1@gioia.aioe.org> (raw)
In-Reply-To: 5ffa2a7b$0$19459$426a74cc@news.free.fr

On 2021-01-09 23:13, DrPi wrote:
> Le 09/01/2021 à 21:11, soren rundgren a écrit :
>> lördag 9 januari 2021 kl. 18:00:36 UTC+1 skrev AdaMagica:
>>> Since Ada 202X is not yet released, this package is not yet fully 
>>> implemented. (I had a conversation with AdaCore about a similar case 
>>> a year ago.)
>>
>> I managed to make a very basic solution by creating my own:
>>
>>     function My_From_String(S : String) return Big_Integer is
>>        sum : Big_Integer := 0;
>>        Len : constant Integer := S'Length;
>>        i : Integer := 1;
>>     begin
>>        -- Ada.Text_IO.Put_Line("My_From_String.S = " & S);
>>        while i <= Len loop
>>           case S(i) is -- i = Len : Len - Len = 0,
>>                        -- i = 1 : Len - 1
>>              when '1' => sum := sum + 1*10**(Len - i);
>>              when '2' => sum := sum + 2*10**(Len - i);
>>              when '3' => sum := sum + 3*10**(Len - i);
>>              when '4' => sum := sum + 4*10**(Len - i);
>>              when '5' => sum := sum + 5*10**(Len - i);
>>              when '6' => sum := sum + 6*10**(Len - i);
>>              when '7' => sum := sum + 7*10**(Len - i);
>>              when '8' => sum := sum + 8*10**(Len - i);
>>              when '9' => sum := sum + 9*10**(Len - i);
>>              when others => null;
>>           end case;
>>           i := i + 1;
>>        end loop;
>>        return sum;
>>     end My_From_String;
>>
> A much simpler function for Integer :
> 
>     function Int_From_String (Str : String) return Integer is
>        Num : Integer := 0;
>     begin
>        for C of Str loop

for C in Str'Range loop

>           Num := (Num * 10) + Integer'Value((1 => C));

    Num := Num * 10 + Character'Pos (C) - Character'Pos'('0');

> Notes :
> - I don't have a 202x compiler, so I can't check if it works with big ints.
> - It does not manage the sign (like the function you designed).

- It does not handle overflow;
- It does not handle missing numbers in the input;
- It does not handle malformed input;
- It does not handle bases other than 10;
- It does not handle blank characters at the ends.

Functions mimicking S'Value (X) cover only half of cases. For the other 
half you need to get the number from a part of the string, so you have 
to recognize the end of the number and return the index or position 
where it ends or another token begins.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

  parent reply	other threads:[~2021-01-10  9:46 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-09 15:16 From_string(Arg : String) return Big_Integer soren rundgren
2021-01-09 17:00 ` AdaMagica
2021-01-09 20:09   ` soren rundgren
2021-01-09 20:09   ` soren rundgren
2021-01-09 20:11   ` soren rundgren
2021-01-09 22:13     ` DrPi
2021-01-09 23:40       ` Jeffrey R. Carter
2021-01-10  9:46       ` Dmitry A. Kazakov [this message]
2021-01-10 15:48         ` DrPi
2021-01-10 16:02           ` Dmitry A. Kazakov
2021-01-10 16:33             ` DrPi
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox