* String declaration and initialization
@ 2008-05-22 15:47 Sébastien
2008-05-22 16:05 ` Dmitry A. Kazakov
` (3 more replies)
0 siblings, 4 replies; 12+ messages in thread
From: Sébastien @ 2008-05-22 15:47 UTC (permalink / raw)
Hi,
Several times I had to do the following:
declare
buffer: String; -- Error at compile time
begin
case SomeTest is
case TEST1 =>
buffer := "Test1";
case TEST2 =>
buffer := "Test2";
end case;
MyTreatment1(buffer, buffer'Size);
MyTreatment2(buffer, buffer'Size);
MyTreatment3(buffer, buffer'Size);
end;
But How can I do this since I can't declare a string without constraint?
Sebastien
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: String declaration and initialization
2008-05-22 15:47 String declaration and initialization Sébastien
@ 2008-05-22 16:05 ` Dmitry A. Kazakov
2008-05-22 16:47 ` Sébastien
2008-05-22 16:16 ` Matthew Heaney
` (2 subsequent siblings)
3 siblings, 1 reply; 12+ messages in thread
From: Dmitry A. Kazakov @ 2008-05-22 16:05 UTC (permalink / raw)
On Thu, 22 May 2008 15:47:38 +0000, S�bastien wrote:
> Several times I had to do the following:
>
> declare
> buffer: String; -- Error at compile time
> begin
> case SomeTest is
> case TEST1 =>
> buffer := "Test1";
> case TEST2 =>
> buffer := "Test2";
> end case;
> MyTreatment1(buffer, buffer'Size);
You don't need buffer'Size, even if you incidentally meant buffer'Length...
> MyTreatment2(buffer, buffer'Size);
> MyTreatment3(buffer, buffer'Size);
> end;
>
> But How can I do this since I can't declare a string without constraint?
declare
function Test return String is -- Ada has scoped subprograms
begin
case Some_Test is
when Test_1 => return "Test1";
...
end case;
end Test;
Buffer: constant String := Test;
begin
My_Treatment_1 (Buffer);
...
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: String declaration and initialization
2008-05-22 16:05 ` Dmitry A. Kazakov
@ 2008-05-22 16:47 ` Sébastien
2008-05-23 8:18 ` Dmitry A. Kazakov
0 siblings, 1 reply; 12+ messages in thread
From: Sébastien @ 2008-05-22 16:47 UTC (permalink / raw)
> declare
> function Test return String is -- Ada has scoped subprograms
> begin
> case Some_Test is
> when Test_1 => return "Test1";
> ...
> end case;
> end Test;
> Buffer: constant String := Test;
> begin
> My_Treatment_1 (Buffer);
> ...
Good idea, but there are some other treatments in the case actually.
something like:
buffer := test1;
buffer_type := 3;
and so on.
So if I use another function, I have to implement 2 cases in order to do it.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: String declaration and initialization
2008-05-22 16:47 ` Sébastien
@ 2008-05-23 8:18 ` Dmitry A. Kazakov
2008-05-23 9:33 ` Sébastien
0 siblings, 1 reply; 12+ messages in thread
From: Dmitry A. Kazakov @ 2008-05-23 8:18 UTC (permalink / raw)
On Thu, 22 May 2008 16:47:44 +0000, S�bastien wrote:
>> declare
>> function Test return String is -- Ada has scoped subprograms
>> begin
>> case Some_Test is
>> when Test_1 => return "Test1";
>> ...
>> end case;
>> end Test;
>> Buffer: constant String := Test;
>> begin
>> My_Treatment_1 (Buffer);
>> ...
>
> Good idea, but there are some other treatments in the case actually.
>
> something like:
> buffer := test1;
> buffer_type := 3;
> and so on.
>
> So if I use another function, I have to implement 2 cases in order to do it.
No. Make a record of the test name and the test type:
declare
type Test_Type is range 1..20;
type Test_Case (Length : Positive) is record
Kind : Test_Type;
Name : String (1..Length);
end record;
function Test return Test_Case is -- Ada has scoped subprograms
begin
case Some_Test is
when Test_1 => return (Length => 5, Name => "Test1", Kind => 3);
...
end case;
end Test;
Buffer: constant Test_Case := Test;
begin
My_Treatment_1 (Buffer.Name);
[ Observe, that the function Test is nothing but a classical factory. ]
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: String declaration and initialization
2008-05-23 8:18 ` Dmitry A. Kazakov
@ 2008-05-23 9:33 ` Sébastien
0 siblings, 0 replies; 12+ messages in thread
From: Sébastien @ 2008-05-23 9:33 UTC (permalink / raw)
> No. Make a record of the test name and the test type:
>
> declare
> type Test_Type is range 1..20;
> type Test_Case (Length : Positive) is record
> Kind : Test_Type;
> Name : String (1..Length);
> end record;
> function Test return Test_Case is -- Ada has scoped subprograms
> begin
> case Some_Test is
> when Test_1 => return (Length => 5, Name => "Test1", Kind => 3);
> ...
> end case;
> end Test;
> Buffer: constant Test_Case := Test;
> begin
> My_Treatment_1 (Buffer.Name);
>
> [ Observe, that the function Test is nothing but a classical factory. ]
Ok got it, I like this one, nice and efficient.
Thanks for your help.
Sebastien
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: String declaration and initialization
2008-05-22 15:47 String declaration and initialization Sébastien
2008-05-22 16:05 ` Dmitry A. Kazakov
@ 2008-05-22 16:16 ` Matthew Heaney
2008-05-22 16:58 ` Sébastien
2008-05-22 16:40 ` Jean-Pierre Rosen
2008-05-22 18:09 ` Martin Krischik
3 siblings, 1 reply; 12+ messages in thread
From: Matthew Heaney @ 2008-05-22 16:16 UTC (permalink / raw)
On May 22, 11:47 am, Sébastien <seb.mor...@gmail.com> wrote:
>
> declare
> buffer: String; -- Error at compile time
> begin
> case SomeTest is
> case TEST1 =>
> buffer := "Test1";
> case TEST2 =>
> buffer := "Test2";
> end case;
> MyTreatment1(buffer, buffer'Size);
> MyTreatment2(buffer, buffer'Size);
> MyTreatment3(buffer, buffer'Size);
> end;
>
> But How can I do this since I can't declare a string without constraint?
The simplest way is to use a bounded or unbounded string:
declare
S : Unbounded_String;
begin
case Some_Test is
when T1 => S := To_Unbounded_String ("test1");
...
end;
Another way is to use a table like you can in C:
declare
Test1 : aliased constant String := "test1";
Test2 : aliased constant String := "test2";
type Table_Type is array (Test_Type) of
not null access constant String;
Table : constant Table_Type :=
(T1 => Test1'Access,
T2 => Test2'Access,
...);
S : String renames Table (Some_Test).all;
begin
...
end;
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: String declaration and initialization
2008-05-22 16:16 ` Matthew Heaney
@ 2008-05-22 16:58 ` Sébastien
2008-05-22 17:11 ` Robert A Duff
` (2 more replies)
0 siblings, 3 replies; 12+ messages in thread
From: Sébastien @ 2008-05-22 16:58 UTC (permalink / raw)
> The simplest way is to use a bounded or unbounded string:
>
> declare
> S : Unbounded_String;
> begin
> case Some_Test is
> when T1 => S := To_Unbounded_String ("test1");
> ...
> end;
Yep I think about it but in this case, I made several conversions
because I have stuff like that:
buffer := To_Unbouned_String(Trim(Integer'Image(my_int), Left));
And then treatment use String so:
MyTreatment(To_String(buffer));
So I don't actually know how fast is the conversion between String and
Unbounded_String but I think it's a waste of ressource.
> Another way is to use a table like you can in C:
>
> declare
> Test1 : aliased constant String := "test1";
> Test2 : aliased constant String := "test2";
>
> type Table_Type is array (Test_Type) of
> not null access constant String;
>
> Table : constant Table_Type :=
> (T1 => Test1'Access,
> T2 => Test2'Access,
> ...);
>
> S : String renames Table (Some_Test).all;
> begin
> ...
> end;
That's an idea but... Test1 are actually not a constant string, that was
an example :-) It's calculated sometime it's even a return of some
function and so on.
But you give me an Idea :
declare
buffer: access String;
Free_String is new Unchecked_Deallocation(String, access all String);
begin
case SomeTest is
case TEST1 =>
buffer := new String("Test1");
case TEST2 =>
buffer := new String("Test2");
end case;
MyTreatment1(buffer.all, buffer.all'Size);
MyTreatment2(buffer.all, buffer.all'Size);
MyTreatment3(buffer.all, buffer.all'Size);
end;
but it doesn't work of course since I can't create with new an
unconstrained object.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: String declaration and initialization
2008-05-22 16:58 ` Sébastien
@ 2008-05-22 17:11 ` Robert A Duff
2008-05-22 18:16 ` Matthew Heaney
2008-05-22 20:01 ` Jeffrey R. Carter
2 siblings, 0 replies; 12+ messages in thread
From: Robert A Duff @ 2008-05-22 17:11 UTC (permalink / raw)
S�bastien <seb.morand@gmail.com> writes:
> But you give me an Idea :
> declare
> buffer: access String;
> Free_String is new Unchecked_Deallocation(String, access all String);
> begin
> case SomeTest is
> case TEST1 =>
> buffer := new String("Test1");
> case TEST2 =>
> buffer := new String("Test2");
> end case;
> MyTreatment1(buffer.all, buffer.all'Size);
> MyTreatment2(buffer.all, buffer.all'Size);
> MyTreatment3(buffer.all, buffer.all'Size);
> end;
>
> but it doesn't work of course since I can't create with new an
> unconstrained object.
Sure you can:
buffer := new String'("Test1");
^
| Note tick mark!
<< new String("Test1"); >> does not make sense. You have to use a
qualified expression (as above) or a constrained subtype,
as in:
new String(1..10)
You didn't call Free_String, by the way. Think about whether you want
to call it in case of exception!
- Bob
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: String declaration and initialization
2008-05-22 16:58 ` Sébastien
2008-05-22 17:11 ` Robert A Duff
@ 2008-05-22 18:16 ` Matthew Heaney
2008-05-22 20:01 ` Jeffrey R. Carter
2 siblings, 0 replies; 12+ messages in thread
From: Matthew Heaney @ 2008-05-22 18:16 UTC (permalink / raw)
On May 22, 12:58 pm, Sébastien <seb.mor...@gmail.com> wrote:
>
> > declare
> > Test1 : aliased constant String := "test1";
> > Test2 : aliased constant String := "test2";
>
> > type Table_Type is array (Test_Type) of
> > not null access constant String;
>
> > Table : constant Table_Type :=
> > (T1 => Test1'Access,
> > T2 => Test2'Access,
> > ...);
>
> > S : String renames Table (Some_Test).all;
> > begin
> > ...
> > end;
>
> That's an idea but... Test1 are actually not a constant string, that was
> an example :-) It's calculated sometime it's even a return of some
> function and so on.
That doesn't really change anything. You could say:
declare
Test1 : aliased constant String := Result_Of_Function1;
Test2 : aliased constant String := Result_Of_Function2;
...
begin
> MyTreatment1(buffer.all, buffer.all'Size);
What are you trying to do here? Why can't you simply declare
MyTreatment1 like this:
procedure MyTreatment1 (S : String) is ...;
and then call it like this:
MyTreatment1 (buffer.all);
> end;
>
> but it doesn't work of course since I can't create with new an
> unconstrained object.
No, Bob showed you what to do:
... new String'("this is my string");
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: String declaration and initialization
2008-05-22 16:58 ` Sébastien
2008-05-22 17:11 ` Robert A Duff
2008-05-22 18:16 ` Matthew Heaney
@ 2008-05-22 20:01 ` Jeffrey R. Carter
2 siblings, 0 replies; 12+ messages in thread
From: Jeffrey R. Carter @ 2008-05-22 20:01 UTC (permalink / raw)
S�bastien wrote:
>
> So I don't actually know how fast is the conversion between String and
> Unbounded_String but I think it's a waste of ressource.
You've already wasted quite a bit of your most expensive resource, a developer's
time, worrying about this. Far more efficient is to do it the obvious way, with
Unbounded_String, and only worry about the time of the conversions if you are
unable to meet your timing requirements.
> But you give me an Idea :
> declare
> buffer: access String;
> Free_String is new Unchecked_Deallocation(String, access all String);
> begin
> case SomeTest is
> case TEST1 =>
> buffer := new String("Test1");
> case TEST2 =>
> buffer := new String("Test2");
> end case;
> MyTreatment1(buffer.all, buffer.all'Size);
> MyTreatment2(buffer.all, buffer.all'Size);
> MyTreatment3(buffer.all, buffer.all'Size);
> end;
>
> but it doesn't work of course since I can't create with new an
> unconstrained object.
You can if you get the syntax right, as has been pointed out elsewhere. But this
is probably your worst choice. Here you have written a memory leak. You should
avoid explicit access values whenever possible, since such errors are so easy to
make.
--
Jeff Carter
"You a big nose have it."
Never Give a Sucker an Even Break
107
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: String declaration and initialization
2008-05-22 15:47 String declaration and initialization Sébastien
2008-05-22 16:05 ` Dmitry A. Kazakov
2008-05-22 16:16 ` Matthew Heaney
@ 2008-05-22 16:40 ` Jean-Pierre Rosen
2008-05-22 18:09 ` Martin Krischik
3 siblings, 0 replies; 12+ messages in thread
From: Jean-Pierre Rosen @ 2008-05-22 16:40 UTC (permalink / raw)
S�bastien a �crit :
> Hi,
>
> Several times I had to do the following:
[...]
>
> But How can I do this since I can't declare a string without constraint?
>
declare
function choose_the_right_one return string is
begin
case SomeTest is
case TEST1 =>
return "Test1";
case TEST2 =>
return "Test2";
end case;
end choose_the_right_one;
buffer: String := Choose_The_Right_One;
begin
MyTreatment1(buffer, buffer'Size);
MyTreatment2(buffer, buffer'Size);
MyTreatment3(buffer, buffer'Size);
end;
--
---------------------------------------------------------
J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: String declaration and initialization
2008-05-22 15:47 String declaration and initialization Sébastien
` (2 preceding siblings ...)
2008-05-22 16:40 ` Jean-Pierre Rosen
@ 2008-05-22 18:09 ` Martin Krischik
3 siblings, 0 replies; 12+ messages in thread
From: Martin Krischik @ 2008-05-22 18:09 UTC (permalink / raw)
Sï¿œbastien wrote:
> But How can I do this since I can't declare a string without constraint?
By reading:
http://en.wikibooks.org/wiki/Ada_Programming/Strings
Ok, I am pulling your leg here - reading alone won't help - but reading the
link above will make you understand better.
Martin
--
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2008-05-23 9:33 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-05-22 15:47 String declaration and initialization Sébastien
2008-05-22 16:05 ` Dmitry A. Kazakov
2008-05-22 16:47 ` Sébastien
2008-05-23 8:18 ` Dmitry A. Kazakov
2008-05-23 9:33 ` Sébastien
2008-05-22 16:16 ` Matthew Heaney
2008-05-22 16:58 ` Sébastien
2008-05-22 17:11 ` Robert A Duff
2008-05-22 18:16 ` Matthew Heaney
2008-05-22 20:01 ` Jeffrey R. Carter
2008-05-22 16:40 ` Jean-Pierre Rosen
2008-05-22 18:09 ` Martin Krischik
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox