From: Simon Wright <simon@pushface.org>
Subject: Re: Stream_Element_Array 2 String
Date: 27 Mar 2004 08:24:38 +0000
Date: 2004-03-27T08:24:38+00:00 [thread overview]
Message-ID: <x7vd66yzohl.fsf@smaug.pushface.org> (raw)
In-Reply-To: mailman.145.1080323447.327.comp.lang.ada@ada-france.org
Marius Amado Alves <maa@liacc.up.pt> 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.
next prev parent reply other threads:[~2004-03-27 8:24 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-03-26 17:30 Stream_Element_Array 2 String Shaddow
2004-03-26 18:04 ` Marius Amado Alves
2004-03-27 8:24 ` Simon Wright [this message]
2004-03-29 7:50 ` Duncan Sands
2004-03-29 19:00 ` Simon Wright
2004-03-31 9:32 ` Pascal Obry
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox