comp.lang.ada
 help / color / mirror / Atom feed
From: Marius Amado Alves <amado.alves@netcabo.pt>
To: comp.lang.ada@ada-france.org
Subject: Re: array of strings in a function
Date: Thu, 16 Oct 2003 21:44:16 +0000
Date: 2003-10-16T21:44:16+00:00	[thread overview]
Message-ID: <mailman.102.1066340699.25614.comp.lang.ada@ada-france.org> (raw)
In-Reply-To: <gpntovo7ckffd6cai057qpps4vm8iftdk4@4ax.com>

Craig Carey wrote: "Some of the other solutions seem worse."

Your array solution is probably the most "Adaist". But I liked Robert
Eachus's. I wrote a variant which allows a list of size 1 (something
Robert's doesn't), e.g.

   Show (+ "one");
   Show (+ "one" + "two");
   Show (+ "one" + "two" + "three");

Full source below. Vararg_Test outputs, as expected:

 1=>one
 
 1=>one
 2=>two
 
 1=>one
 2=>two
 3=>three
 
(Too much free time in our hands uh? :-)

with Vararg; use Vararg;
with Ada.Text_IO;

procedure Vararg_Test is
   L : List;
   procedure Show (L : List) is
      use Ada.Text_IO;
   begin
      for I in 1 .. Length (L) loop
         Put_Line (Integer'Image (I) & "=>" & Get (L, I));
      end loop;
      New_Line;
   end;
begin
   Show (+ "one");
   Show (+ "one" + "two");
   Show (+ "one" + "two" + "three");
end;


with Ada.Strings.Unbounded;

package Vararg is
   type List is private;
   function Length (L : List) return Natural;
   function Get (L : List; I : Positive) return String;
   function "+" (R : String) return List;
   function "+" (L : List; R : String) return List;
private
   type Store is array (1 ..1000) of
Ada.Strings.Unbounded.Unbounded_String;
   type List is record
      S : Store;
      N : Natural range 0 .. Store'Last := 0;
   end record;
end;

package body Vararg is

   function Length (L : List) return Natural is
   begin
      return L.N;
   end;

   use Ada.Strings.Unbounded;

   function Get (L : List; I : Positive) return String is
   begin
      return To_String (L.S (I));
   end;

   function "+" (R : String) return List is
      Y : List;
   begin
      Y.S (1) := To_Unbounded_String (R);
      Y.N := 1;
      return Y;
   end;

   function "+" (L : List; R : String) return List is
      Y : List;
   begin
      Y.S (1 .. Length (L)) := L.S (1 .. Length (L));
      Y.S (Length (L) + 1) := To_Unbounded_String (R);
      Y.N := Length (L) + 1;
      return Y;
   end;

end;




  reply	other threads:[~2003-10-16 21:44 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-14 20:54 array of strings in a function Antonio Martínez
2003-10-15  2:55 ` Robert I. Eachus
2003-10-15  3:29   ` Jeff C,
2003-10-15  3:08 ` Jeffrey Carter
2003-10-16  6:40   ` tmoran
2003-10-16  9:31     ` Craig Carey
2003-10-16 18:13       ` Craig Carey
2003-10-16 21:44         ` Marius Amado Alves [this message]
2003-10-17 19:48           ` Craig Carey
2003-10-18 10:05             ` Marius Amado Alves
2003-10-18 20:05               ` Craig Carey
2003-10-30  9:42                 ` Craig Carey
2003-10-16 17:58     ` Jeffrey Carter
2003-10-16 20:00       ` tmoran
2003-10-17  0:51         ` Jeffrey Carter
2003-10-15 11:49 ` Antonio Martínez Álvarez
2003-10-15 12:29   ` Preben Randhol
2003-10-15 14:19   ` Ole-Hjalmar Kristensen
2003-10-16 14:30   ` Robert I. Eachus
2003-10-16 17:53     ` Jeffrey Carter
2003-10-17  0:48       ` Robert I. Eachus
2003-10-17 18:41         ` Jeffrey Carter
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox