comp.lang.ada
 help / color / mirror / Atom feed
From: Brad Moore <brad.moore@shaw.ca>
Subject: Re: How to get this space away?
Date: Tue, 09 Jun 2015 08:38:46 -0600
Date: 2015-06-09T08:38:46-06:00	[thread overview]
Message-ID: <XTCdx.21799$OH7.17685@fx28.iad> (raw)
In-Reply-To: <ml4tl3$ln3$2@dont-email.me>

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

  reply	other threads:[~2015-06-09 14:38 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-02 20:32 How to get this space away? Laurent
2015-06-02 20:53 ` Simon Wright
2015-06-03 18:59   ` Laurent
2015-06-03 19:30     ` Simon Wright
2015-06-02 22:28 ` Jeffrey R. Carter
2015-06-03 19:07   ` Laurent
2015-06-03 20:50     ` J-P. Rosen
2015-06-03 23:00       ` Randy Brukardt
2015-06-05  9:26         ` Stephen Davies
2015-06-08 12:33         ` Brad Moore
2015-06-08 19:47           ` Randy Brukardt
2015-06-08 20:25             ` Jeffrey R. Carter
2015-06-09 14:38               ` Brad Moore [this message]
2015-06-04  9:37       ` Georg Bauhaus
2015-06-04 12:32         ` Dmitry A. Kazakov
2015-06-05 13:02       ` Laurent
2015-06-06  0:02         ` Dennis Lee Bieber
2015-06-03  7:28 ` Dmitry A. Kazakov
2015-06-03 19:19   ` Laurent
2015-06-08 22:43 ` wowwomenonwheels205
replies disabled

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