From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Path: eternal-september.org!reader02.eternal-september.org!news.gegeweb.eu!gegeweb.org!usenet-fr.net!proxad.net!feeder1-2.proxad.net!cleanfeed3-a.proxad.net!nnrp1-1.free.fr!not-for-mail Newsgroups: comp.lang.ada References: <5126648c-215e-4d9e-bdd9-82fa5b510896n@googlegroups.com> <5ffa2a7b$0$19459$426a74cc@news.free.fr> From: DrPi <314@drpi.fr> Subject: Re: From_string(Arg : String) return Big_Integer Date: Sun, 10 Jan 2021 16:48:23 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: fr Content-Transfer-Encoding: 8bit Message-ID: <5ffb21c8$0$21590$426a74cc@news.free.fr> Organization: Guest of ProXad - France NNTP-Posting-Date: 10 Jan 2021 16:48:25 CET NNTP-Posting-Host: 82.65.30.55 X-Trace: 1610293705 news-3.free.fr 21590 82.65.30.55:50605 X-Complaints-To: abuse@proxad.net Xref: reader02.eternal-september.org comp.lang.ada:61088 List-Id: >> 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. >