comp.lang.ada
 help / color / mirror / Atom feed
From: "Robert I. Eachus" <rieachus@comcast.net>
Subject: Re: array of strings in a function
Date: Wed, 15 Oct 2003 02:55:25 GMT
Date: 2003-10-15T02:55:25+00:00	[thread overview]
Message-ID: <3F8CB712.2070808@comcast.net> (raw)
In-Reply-To: pan.2003.10.14.20.54.54.17088@atc.ugr.es

Antonio Martï¿œnez wrote:

> I'm trying to pass to a function an argument. The argument is an
> array range <> of Strings (unbounded array).

> number of string literals is unknown by the function. And the
> length of every string is unknown as well.

> How do I work with unbound strings?

You can create an array of Unbounded_String (found in 
Ada.Strings.Unbounded).  Or you can use the package below.  Note that in 
either case, you are going to have to create an array object, put the 
strings in it, and then pass that to the array.  With the package below 
your example above:

 > my_fun("Hello", ", I'm", " the function");

Becomes:
with Ada_Strings_Bounded_Array; use Ada_Strings_Bounded_Array;
...
   Foo: Bounded_Array(3,13); -- you could choose 20 instead of 13 or any 

                             -- number you felt like > 13.
begin
   Set(1, "Hello");
   Set(2, ", I'm");
   Set(3, " the function");
   Bar := my_fun(Foo);  -- you didn't say what my_fun returned.
   ...

alternatively:
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
...
    type My_Array is array (Integer range <>) of Unbounded_String;
    Foo: My_Array(1..3);

begin
   My_Array(1) := To_Unbounded_String("Hello");
   My_Array(2) := To_Unbounded_String(", I'm");
   My_Array(3) := To_Unbounded_String(" the function");
   Bar := my_fun(Foo);
   ...

This is not a normal Ada idiom, but it can be done.  The normal Ada 
idiom would be:

   Bar := my_fun("Hello" & ", I'm" & " the function");

That way you can declare my_fun to take a single string parameter, and 
use catenation (&) to put the pieces together.  The pieces you are 
putting together can be string literals, character literals, String and 
Character variables or constants, and functions returning Strings.  In 
fact, you will often see:

    Ada.Text_IO.Put_Line(" X is " & Integer'Image(X) & '.' );

or something similar.


-------------------------------------------------------------------------

package Ada_Strings_Bounded_Array is

    type Bounded_Array(Size, Max: Natural) is private;

    procedure Set(B: in out Bounded_Array;
                  Row: in Positive;
                  To: in String);

    procedure Append(B: in out Bounded_Array;
                     Row: in Positive;
                     Add: in String);

    function Get(From: Bounded_Array;
                 Row: Positive) return String;

    function Get_Length(From: Bounded_Array;
                        Row: Positive) return Natural;

private

    type Element_Array is
           array (Natural range <>, Natural range <>) of Character;

    type Length_Array is array(Natural range <>) of Natural;

    type Bounded_Array(Size, Max: Natural) is record
       Lengths: Length_Array(1..Size) := (others => 0);
       Contents: Element_Array(1..Size, 1..Max);
    end record;

end Ada_Strings_Bounded_Array;

----------------------------------------------------------------------

package body Ada_Strings_Bounded_Array is

    procedure Set(B: in out Bounded_Array;
                  Row: in Positive;
                  To: in String) is
      Temp: String(1..To'Length) := To; -- insure lower bound is 1.
    begin
      if To'Length > B.Max then raise Constraint_Error; end if;
      B.Lengths(Row) := Temp'Last;
      -- will raise C_E if Row out of bounds.
      for I in Temp'Range loop
        B.Contents(Row,I) := Temp(I);
      end loop;
    end Set;

    procedure Append(B: in out Bounded_Array;
                     Row: in Positive;
                     Add: in String) is
      Temp: String(B.Lengths(Row)+ 1..B.Lengths(Row)+Add'Length) := Add;
    begin
      if Temp'Last > B.Max then raise Constraint_Error; end if;
      B.Lengths(Row) := Temp'Last;
      for I in Temp'Range loop
        B.Contents(Row,I) := Temp(I);
      end loop;
    end Append;

    function Get(From: Bounded_Array;
                 Row: Positive) return String is
      Temp: String(1..From.Lengths(Row));
    begin
      for I in Temp'Range loop
        Temp(I) := From.Contents(Row,I);
      end loop;
      return Temp;
    end Get;


    function Get_Length(From: Bounded_Array;
                        Row: Positive) return Natural is
    begin return From.Lengths(Row); end Get_Length;

    pragma Inline(Set, Append, Get, Get_Length);

end Ada_Strings_Bounded_Array;

-------------------------------------------------------------------------

with Ada.Text_IO; use Ada.Text_IO;
with Ada_Strings_Bounded_Array; use Ada_Strings_Bounded_Array;
procedure Bounded_Array_Test is
   BA1: Bounded_Array(5, 30);
   BA2: Bounded_Array(2, 10);
begin
   Set(BA1,1, "Robert I. Eachus");
   Set(BA1,2, "Mortimer Q. Snerd");
   Set(BA2,1, "foo");
   Append(BA2,1, "bar");
   Put_Line(Get(BA1,1));
   Put_Line(Get(BA1,2));
   Put_Line(Get(BA2,1));
   if Get_Length(BA1,1) /= 16 then Put_Line("Oops! BA1,1"); end if;
   if Get_Length(BA2,1) /= 6 then Put_Line("Oops! BA2,1"); end if;
   if Get_Length(BA1,3) /= 0 then Put_Line("Oops! BA1,3"); end if;
end Bounded_Array_Test;


-- 
                                                     Robert I. Eachus

"Quality is the Buddha. Quality is scientific reality. Quality is the 
goal of Art. It remains to work these concepts into a practical, 
down-to-earth context, and for this there is nothing more practical or 
down-to-earth than what I have been talking about all along...the repair 
of an old motorcycle."  -- from Zen and the Art of Motorcycle 
Maintenance by Robert Pirsig




  reply	other threads:[~2003-10-15  2:55 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 [this message]
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
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