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-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,4b4c3319d9998f6b X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2004-03-27 00:28:41 PST Path: archiver1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!t-online.de!kibo.news.demon.net!news.demon.co.uk!demon!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: Stream_Element_Array 2 String Date: 27 Mar 2004 08:24:38 +0000 Organization: Pushface Sender: simon@smaug.pushface.org Message-ID: References: NNTP-Posting-Host: pogner.demon.co.uk Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: news.demon.co.uk 1080376119 20503 62.49.19.209 (27 Mar 2004 08:28:39 GMT) X-Complaints-To: abuse@demon.net NNTP-Posting-Date: Sat, 27 Mar 2004 08:28:39 +0000 (UTC) User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.1 Xref: archiver1.google.com comp.lang.ada:6592 Date: 2004-03-27T08:24:38+00:00 List-Id: Marius Amado Alves writes: > > Im using Ada.Streams.Stream_IO for read block of files and i can convert > > a Stream_Element_Array to String type. > > how can i do? > > Ada.Unchecked_Conversion is your friend. > > But make sure you need that, instead of basic stream I/O e.g. > > with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO; > > procedure Strstr is > F : File_Type; > S3 : String (1 .. 3); > begin > Create (F); > String'Write (Stream (F), "abc"); > Reset (F); > S3 := "***"; > Set_Mode (F, In_File); > String'Read (Stream (F), S3); > pragma Assert (S3 = "abc"); > end; Also, it depends whether performance or clarity is what you need! There is a third way, using address overlays, which is possibly rather easier to get wrong .. I always feel uncomfortable with it, but here goes: with Ada.Text_IO; use Ada.Text_IO; with Ada.Unchecked_Conversion; with Ada.Streams; with BC.Support.High_Resolution_Time; procedure String_To_Stream_Element_Array is function To_Stream_Element_Array_Using_Unchecked_Conversion (S : String) return Ada.Streams.Stream_Element_Array; function To_Stream_Element_Array_Using_Overlay (S : String) return Ada.Streams.Stream_Element_Array; function To_Stream_Element_Array_Using_Unchecked_Conversion (S : String) return Ada.Streams.Stream_Element_Array is subtype Source is String (S'Range); subtype Result is Ada.Streams.Stream_Element_Array (Ada.Streams.Stream_Element_Offset (S'First) .. Ada.Streams.Stream_Element_Offset (S'Last)); function To_Array is new Ada.Unchecked_Conversion (Source, Result); begin return To_Array (S); end To_Stream_Element_Array_Using_Unchecked_Conversion; function To_Stream_Element_Array_Using_Overlay (S : String) return Ada.Streams.Stream_Element_Array is Array_View : Ada.Streams.Stream_Element_Array (Ada.Streams.Stream_Element_Offset (S'First) .. Ada.Streams.Stream_Element_Offset (S'Last)); for Array_View'Address use S'Address; begin return Array_View; end To_Stream_Element_Array_Using_Overlay; Start, Finished : BC.Support.High_Resolution_Time.Time; Took : Duration; use type BC.Support.High_Resolution_Time.Time; begin Start := BC.Support.High_Resolution_Time.Clock; declare A : constant Ada.Streams.Stream_Element_Array := To_Stream_Element_Array_Using_Unchecked_Conversion ("now is the time for all good men" & " to come to the aid of the country" & "now is the time for all good men" & " to come to the aid of the country" & "now is the time for all good men" & " to come to the aid of the country" & "now is the time for all good men" & " to come to the aid of the country" & "now is the time for all good men" & " to come to the aid of the country" & "now is the time for all good men" & " to come to the aid of the country"); begin Finished := BC.Support.High_Resolution_Time.Clock; Took := Finished - Start; end; Put_Line ("unchecked conversion took" & Took'Img); Start := BC.Support.High_Resolution_Time.Clock; declare A : constant Ada.Streams.Stream_Element_Array := To_Stream_Element_Array_Using_Overlay ("now is the time for all good men" & " to come to the aid of the country" & "now is the time for all good men" & " to come to the aid of the country" & "now is the time for all good men" & " to come to the aid of the country" & "now is the time for all good men" & " to come to the aid of the country" & "now is the time for all good men" & " to come to the aid of the country" & "now is the time for all good men" & " to come to the aid of the country"); begin Finished := BC.Support.High_Resolution_Time.Clock; Took := Finished - Start; end; Put_Line ("overlay took" & Took'Img); end String_To_Stream_Element_Array; Runing on this box (which is a bit antiquated now), I get (GNAT 3.15p) smaug.pushface.org[64]$ ./string_to_stream_element_array Clock rate is 0.400915179 GHz unchecked conversion took 0.000008465 overlay took 0.000002641 You can find the Booch Components at http://pushface.org/components/bc/. The high resolution time stuff is for GNAT and Intel (well, "must support the rdtsc instruction") only. Shouldn't that be ".. to the aid of the party"? oh well .. -- Simon Wright 100% Ada, no bugs.