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,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.224.3.131 with SMTP id 3mr4559952qan.5.1369611294627; Sun, 26 May 2013 16:34:54 -0700 (PDT) X-Received: by 10.50.108.104 with SMTP id hj8mr723182igb.13.1369611294592; Sun, 26 May 2013 16:34:54 -0700 (PDT) Path: border1.nntp.dca3.giganews.com!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!ch1no1273875qab.0!news-out.google.com!y6ni51517qax.0!nntp.google.com!ch1no1273866qab.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Sun, 26 May 2013 16:34:54 -0700 (PDT) Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=69.20.190.126; posting-account=lJ3JNwoAAAAQfH3VV9vttJLkThaxtTfC NNTP-Posting-Host: 69.20.190.126 User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <6bbc35cb-18ed-4efc-b1ec-6c41dcc0256c@googlegroups.com> Subject: Unchecked_Conversion vs Address-overlaying From: Shark8 Injection-Date: Sun, 26 May 2013 23:34:54 +0000 Content-Type: text/plain; charset=ISO-8859-1 X-Original-Bytes: 3793 Xref: number.nntp.dca.giganews.com comp.lang.ada:181743 Date: 2013-05-26T16:34:54-07:00 List-Id: I recently had to make a dump-function for a collection of bytes and was wondering if the method I chose (address-overlaying) was better than using Unchecked_Conversion or not. ------------------------------ -- The private type BLOB designates some collection of memory-data; -- it is actually an access to a storage-array [of Storage-Elements]. -- It's sized to the Cell_Size that all the other types are given so -- that they can be wrapped in a record and that pushed onto a stack. Type BLOB is Not Null Access All System.Storage_Elements.Storage_Array with Size => Cell_Size; -- Returns a hex-dump of the given BLOB. Function Image( Item : BLOB ) Return String is Use System, System.Storage_Elements; -- Our undervalued friend, the Nybble and the Byte, as well -- as a type that serves as a collection of bytes. Subtype Nybble is Interfaces.Unsigned_8 Range 16#0#..16#F#; Subtype Byte is Interfaces.Unsigned_8; Type Byte_String is Array(Positive Range <>) of Byte; -- Return a Hex Character for any given Nybble. Function Nybble_to_Hex( Input : Nybble ) Return Character is subtype Decimal is Nybble range 0..9; Base : Constant Interfaces.Unsigned_8:= ( if Input in Decimal then Character'Pos('0') else Character'Pos('A') - 10 ); begin Return Character'Val( Base + Input ); End Nybble_To_Hex; -- Return the string that is the hex-values for -- the high- and low-nybbles of the given byte. Function Image( Item : Byte ) Return String is begin Return ( 1 => Nybble_to_Hex(Item / 16), -- High 2 => Nybble_to_Hex(Item mod 16) ); -- Low End Image; -- Given a Byte_String return the string containing its Nybbles. Function Image( Item : Byte_String ) return String is begin Case Item'Length is when 0 => Return ""; when 1 => Return Image(Item(Item'First)); when others => declare Head : String Renames Image(Item(Item'First)); subtype Tail_Range is Positive Range Positive'Succ(Item'First)..Item'Last; Tail : String Renames Image(Item(Tail_Range)); begin Return Head & Tail; end; end case; End Image; -- Calculate the length of the BLOB, in bytes. Length : Constant Natural:= (Item'Length*Storage_Array'Component_Size) / Byte'Object_Size; Pragma Warnings(Off); -- Create an overlayed, non-initializing variable of the proper size. Image_String : Byte_String(1..Length) with Import, Convention => Ada, Address => Item.All'Address; Pragma Warnings(On); begin -- Return the image of that overlay. Return Image(Image_String); end Image;