comp.lang.ada
 help / color / mirror / Atom feed
From: Nick Roberts <nick.roberts@acm.org>
Subject: Re: Question about Ada.Unchecked_Conversion
Date: Fri, 29 Oct 2004 16:15:07 +0100
Date: 2004-10-29T16:15:07+01:00	[thread overview]
Message-ID: <2uf53sF2b165eU1@uni-berlin.de> (raw)
In-Reply-To: <m2oeil3d4x.fsf@mac.scrogneugneu.org>

Eric Jacoboni wrote:

> subtype T_Phrase is String(1..Lg_Max);
>    
> type T_S�parateur is (' ', Ht, Lf, ',' ,';', ':', '.', '?', '!');
> for T_S�parateur'Size use Character'Size;
> 
> function Char_To_S�parateur is 
>       new Ada.Unchecked_Conversion(Character, T_S�parateur);
> 
> Ma_Phrase : T_Phrase;
> 
> What i want to do is simply a test like this, in order to find
> characters that are also separators:
> 
> if Char_To_S�parateur(Ma_Phrase(I)) in T_S�parateur then 
>   ...
> end if;
> 
> But this test always fails and i don't understand why. The logic seems
> correct so i suppose it's a misunderstanding of Unchecked_Conversion?

It certainly is a misunderstanding!

Unchecked_Conversion should be used only when you know for sure that
the bit representation of one type will have the meaning you intend
when interpreted as another type. In this case, the bit representation
for type T_S�parateur is likely to be totally different to that of
Standard.Character.

You don't need to use Unchecked_Conversion to convert between one
character type (T_S�parateur) and another (Standard.Character), and
you should not. You need only (and should) use predefined conversion
for this purpose.

However, in order to do the membership test, you do not want to
perform any conversion, because if a value of type Standard.Character
cannot be converted to T_S�parateur, Constraint_Error will be raised.

Instead, for this job, I recommend you use the facilities of the
standard package Ada.Strings.Maps. In this package, there is a type
Character_Set, with the operations you would expect, including
functions which make it easy (usually) to construct a set.

   with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
   with Ada.Strings.Maps; use Ada.Strings.Maps;
   ...
      S�parateurs: constant Character_Set := 
         To_Set( HT & LF & Space & ",;:.?!" );
   ...
      for i in Ma_Phrase'Range loop
         if Is_In( Ma_Phrase(i), S�parateurs ) then
            ...

There is another possible method, using an array that maps from
characters to a category (indicating some significance to each
character, in this case, whether it is a separator).

   with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
   ...
      Est_S�parateur: constant array (Character) of Boolean :=
         ( Space | HT | LF |
              ',' | ';' | ':' | '.' | '?' | '!' => True,
           others => False );
   ...
      for i in Ma_Phrase'Range loop
         if Est_S�parateur( Ma_Phrase(i) ) then
            ...

This method might be difficult for testing Wide_Characters, because
of the size of the array, and the type Character_Set can provide more
sophisticated functionality sometimes.

-- 
Nick Roberts



  parent reply	other threads:[~2004-10-29 15:15 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-10-29 12:46 Question about Ada.Unchecked_Conversion Eric Jacoboni
2004-10-29 14:22 ` Dmitry A. Kazakov
2004-10-29 14:26 ` Jean-Pierre Rosen
2004-10-29 15:15 ` Nick Roberts [this message]
2004-10-29 15:47   ` Eric Jacoboni
2004-10-30 10:23     ` skidmarks
2004-10-30 16:30       ` Nick Roberts
2004-10-30 17:18         ` Eric Jacoboni
replies disabled

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