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=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,e037e74567eca23d X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!news1.google.com!news.glorb.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Nick Roberts Newsgroups: comp.lang.ada Subject: Re: Question about Ada.Unchecked_Conversion Date: Fri, 29 Oct 2004 16:15:07 +0100 Message-ID: <2uf53sF2b165eU1@uni-berlin.de> References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de V+cxHga5wAOZZdl6M0uORQ8v5/8C2mXpm+vfDdNdftpTjZZsE= User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040803 X-Accept-Language: en-us, en In-Reply-To: Xref: g2news1.google.com comp.lang.ada:5871 Date: 2004-10-29T16:15:07+01:00 List-Id: 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