comp.lang.ada
 help / color / mirror / Atom feed
From: DrPi <314@drpi.fr>
Subject: Re: From_string(Arg : String) return Big_Integer
Date: Sun, 10 Jan 2021 16:48:23 +0100	[thread overview]
Message-ID: <5ffb21c8$0$21590$426a74cc@news.free.fr> (raw)
In-Reply-To: <rteidn$1to4$1@gioia.aioe.org>


>> 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
In this case, C is a universal integer, not a character...

> 
>>           Num := (Num * 10) + Integer'Value((1 => C));
> 
>     Num := Num * 10 + Character'Pos (C) - Character'Pos'('0');
... so you have to use :
Num := Num * 10 + Character'Pos (Str(C)) - Character'Pos'('0');

I prefer this construct but when I quickly tried it, it did'nt work. 
Surely a typo somewhere.

So either :

       for C of Str loop
          Num := (Num * 10) + Character'Pos (C) - Character'Pos ('0');
       end loop;

or :

       for Idx in Str'Range loop
          Num := (Num * 10) + Character'Pos (Str(Idx)) - Character'Pos 
('0');
       end loop;




> 
>> 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 handles 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.
Sure. A very simple implementation usable in a known and controlled context.
> 
> 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.
> 

  reply	other threads:[~2021-01-10 15:48 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
2021-01-10 15:48         ` DrPi [this message]
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