From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!feeder.eternal-september.org!news.glorb.com!peer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!fx28.iad.POSTED!not-for-mail From: Brad Moore User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: How to get this space away? References: <0b23f4af-9a55-4d1d-be90-f74b316a95cf@googlegroups.com> In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Message-ID: NNTP-Posting-Host: 68.145.219.148 X-Complaints-To: internet.abuse@sjrb.ca X-Trace: 1433860727 68.145.219.148 (Tue, 09 Jun 2015 14:38:47 UTC) NNTP-Posting-Date: Tue, 09 Jun 2015 14:38:47 UTC Date: Tue, 09 Jun 2015 08:38:46 -0600 X-Received-Bytes: 4202 X-Received-Body-CRC: 2215139283 Xref: news.eternal-september.org comp.lang.ada:26239 Date: 2015-06-09T08:38:46-06:00 List-Id: On 2015-06-08 02:25 PM, Jeffrey R. Carter wrote: > On 06/08/2015 12:47 PM, Randy Brukardt wrote: >> >> declare >> A_Img : constant String := Byte'Image(IP.A); >> B_Img : constant String := Byte'Image(IP.B); >> C_Img : constant String := Byte'Image(IP.C); >> D_Img : constant String := Byte'Image(IP.D); >> begin >> Put (A_Img(2..A_Img'Last) & '.' & B_Img(2..B_Img'Last) & '.' & >> C_Img(2..C_Img'Last) & '.' & C_Img(2..C_Img'Last); >> end; The version I was thinking of was Put (Strings.Fixed.Trim (Source => Byte'Image (IP(1)), Side => Strings.Left) & '.' & Strings.Fixed.Trim (Source => Byte'Image (IP(2)), Side => Strings.Left) & '.' & Strings.Fixed.Trim (Source => Byte'Image (IP(3)), Side => Strings.Left) & '.' & Strings.Fixed.Trim (Byte'Image (IP(4)), Side => Strings.Left)); Which is not that different in terms of readability. A difference is that Randy's version required 4 declarations, where this version can be written as an expression without any declarations. I agree that for an IP Address one would probably want to encapsulate the code in a library function. But for more general cases, sometimes you need to construct similar output on the fly, and it is awkward to have to write a function every time you want to do that. It would have been quite a bit nicer to be able to write: Put (Byte'Image (IP(1)) & '.' & Byte'Image (IP(2)) & '.' & Byte'Image (IP(3)) & '.' & Byte'Image (IP(4)); But then if one wants zero padding, perhaps extending Text_IO to add some sort of Text_IO control would be helpful, as in; Put (Byte'Image (IP(1)) & '.' & Text_IO.Pad('0') & Text_IO.Width(3) & Byte'Image (IP(2)) & '.' & Byte'Image (IP(3)) & '.' & Byte'Image (IP(4)); > > I think you have C_Img in there twice. > >> Now, if you prefer to have each part to take up three characters with >> leading zeros except for the first (so they're all the same length) -- >> that'll take a bit more than a handful of lines in Ada. Ada's formatting is >> primitive, except in the rarely thought of Annex F (does anyone use that?). > > Presuming that "except for the first" means that IP.A should take up the minimum > number of characters, as above: > > declare > function Image is new PragmARC.Images.Modular_Image (Number => Byte); > begin > Put (Image (IP.A) & '.' & > Image (IP.B, Width => 3, Zero_Filled => True) & '.' & > Image (IP.C, Width => 3, Zero_Filled => True) & '.' & > Image (IP.D, Width => 3, Zero_Filled => True) ); > end; > > And if you want IP.A to be 3 characters, zero filled, that's easy, too. > Yes, one could write their own routing or use a third party library such as this, but it strikes me that such a capability should be built into the language. Maybe the best answer is to standardize such a routine. I'm thinking it could fit nicely into Ada.Strings.Fixed. Brad