comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <stephen_leake@stephe-leake.org>
Subject: Re: Hexadecimal and stream element arrays
Date: Wed, 21 Apr 2010 08:38:35 -0400
Date: 2010-04-21T08:38:35-04:00	[thread overview]
Message-ID: <821ve9nfp0.fsf@stephe-leake.org> (raw)
In-Reply-To: abd54875-ef6a-4d67-8235-2830f9d76a76@a39g2000prb.googlegroups.com

"Jeffrey R. Carter" <ggsub@pragmada.x10hosting.com> writes:

> On Apr 20, 12:25 am, Stephen Leake <stephen_le...@stephe-leake.org>
> wrote:
>>
>> generic
>>    Width : Natural;
>>    type Number_Type is mod <>;
>> function SAL.Generic_Hex_Image (Item : in Number_Type) return String;
>> --  Return a hexadecimal image of Item, padded with leading zeros to
>> --  Width. If Width is too small for Item, leading digits are silently
>> --  truncated.
>
> So if I sometimes want different widths for the same type, I have to
> have multiple instantiations? 

Yes. But in practice, the width you want is Number_Type'Width (but
assuming hexadecimal), so it's not a problem.

The only problem I've encountered is the "silently truncated" part. We
changed a number type to have more bits, but forgot to change the Width
parameter here.

There are times, such as building formatted dates, when I need a
specific number of characters, and want an exception if the actual value
can't be represented in that number. Then this function is not
appropriate.

> That doesn't seem very friendly to me. I don't see why Width couldn't
> be a parameter of the function, 

It could; in the body, it is only used as the size of the result image
string. It's just historical accident that it is a generic parameter
instead.

> probably defaulted to Number_Type'Width.

That assumes base 10, so it would be wrong. If I made a change like
this, I'd keep the generic parameter as the default width, or add a
function that computes a default width assuming base 16, and use that as
the default.

> What do I do for signed integer images, 

I never need to image signed integers in hex; that's confusing.

> for bases other than 16, 

sal-generic_binary_image.ads
sal-generic_decimal_image.ads

are the only ones I've ever needed. The binary image includes '_' every
four digits.

> and for signed integer images for bases other than 16? 

Never needed them.

> Do I have to roll my own for these cases?

Yes.

It is far easier to test a small, focused routine than a large, general
purpose one.

> In Ada.Text_IO, a Width of zero implies the minimum width needed to
> represent the value; here it's an expensive way to get a null String.
> I'd prefer consistency with the standard.

If this function were proposed as part of the Ada standard, that would
make sense. However, it is part of SAL, which often has a different
philosophy.

> I haven't used SAL, but these are the kind of comments you'd probably
> get from me if I did use this function. HTH.

Thanks for your comments.

-- 
-- Stephe



      reply	other threads:[~2010-04-21 12:38 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-19 15:44 Hexadecimal and stream element arrays tonyg
2010-04-19 15:58 ` tonyg
2010-04-19 16:24   ` Ludovic Brenta
2010-04-19 16:27     ` tonyg
2010-04-19 17:02       ` tonyg
2010-04-19 17:26         ` tonyg
2010-04-19 20:50           ` Ludovic Brenta
2010-04-20  9:00             ` tonyg
2010-04-20  9:25               ` Peter Hermann
2010-04-19 18:05     ` Warren
2010-04-19 19:21       ` Jeffrey R. Carter
2010-04-19 19:28         ` Warren
2010-04-19 23:21           ` John B. Matthews
2010-04-19 23:22           ` Adam Beneschan
2010-04-20 14:04             ` Warren
2010-04-20 14:46               ` J-P. Rosen
2010-04-20 15:52                 ` Warren
2010-04-19 19:17     ` Jeffrey R. Carter
2010-04-19 20:52       ` Ludovic Brenta
2010-04-19 23:34         ` Jeffrey R. Carter
2010-04-19 17:20   ` John B. Matthews
2010-04-19 17:27   ` Dmitry A. Kazakov
2010-04-19 17:55     ` tonyg
2010-04-20  7:25 ` Stephen Leake
2010-04-20 22:21   ` Jeffrey R. Carter
2010-04-21 12:38     ` Stephen Leake [this message]
replies disabled

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