comp.lang.ada
 help / color / mirror / Atom feed
From: DrPi <314@drpi.fr>
Subject: Re: From_string(Arg : String) return Big_Integer
Date: Sat, 9 Jan 2021 23:13:15 +0100	[thread overview]
Message-ID: <5ffa2a7b$0$19459$426a74cc@news.free.fr> (raw)
In-Reply-To: <5126648c-215e-4d9e-bdd9-82fa5b510896n@googlegroups.com>

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
          Num := (Num * 10) + Integer'Value((1 => C));
       end loop;

       return Num;
    end Int_From_String;

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).

  reply	other threads:[~2021-01-09 22:13 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 [this message]
2021-01-09 23:40       ` Jeffrey R. Carter
2021-01-10  9:46       ` Dmitry A. Kazakov
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