comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Question on bounded / unbounded strings
Date: Tue, 13 Sep 2016 10:41:12 -0700
Date: 2016-09-13T10:41:12-07:00	[thread overview]
Message-ID: <nr9dnl$unp$1@dont-email.me> (raw)
In-Reply-To: <nnd$12b05e33$1843543e@b46552de0b4781f4>

On 09/13/2016 01:46 AM, Arie van Wingerden wrote:
> 
> for a long time I've been interested in Ada but hadn't played with it.

Some terminology: Ada defines 3 kinds of strings of Character:

* Fixed strings: type String

* Variable strings with a fixed maximum length: Bounded_String (not very useful;
you might prefer something equivalent but easier to use, such as PragmARC.B_Strings)

* Variable strings with no maximum length: Unbounded_String

You seem to use "bounded String" to refer to String, which is confusing for
those of us who use it to refer to Bounded_String.

> So I had to convert the output of 2) to an unbounded string, because I could not
> know it's length in advance.

You may have chosen to convert this to Unbounded_String, and that may have been
a good decision, but you didn't have to do this. You never really need
Unbounded_String, but sometimes it's convenient.

> QUESTION: How can I convert an unbounded string to a standard (bounded) string?

If you don't know how to do this, then you haven't spent enough time reviewing
the definition of Ada.Strings.Unbounded, and shouldn't be using it until you have.

>                    A standard string must be defined with a fixed length in
> advance, which I do not know at the time ...

A String must be declared with fixed bounds, but as others have pointed out, you
don't need to know those bounds in advance (or in some cases, at all). Between
Strings with bounds determined from initialization, slices, block statements,
and unconstrained subprogram parameters, you can do whatever you like with
String without having to use Unbounded_String.

(Note that a String is declared with bounds, not a length. Most importantly, the
lower bound doesn't have to be 1. Remembering this will save you a lot of grief.
The 1st Ada error I saw was something like

   procedure P (S : in String) is
   begin
      if S (1) = ... -- Constraint_Error here
      ...
   end P;

   V     : String (1 .. 1000);
   Last  : Natural;
   Start : Positive;
begin
   Text_IO.Get_Line (Item => V, Last => Last);

   for I in 1 .. Last loop
      if V (I) = ... then
         Start := ...;

         exit;
      end if;
   end loop;

   P (S => V (Start .. Last) );

because Start /= 1.)

-- 
Jeff Carter
"C++ is vast and dangerous, a sort of Mordor of
programming languages."
Jason R. Fruit
120


  parent reply	other threads:[~2016-09-13 17:41 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-13  8:46 Question on bounded / unbounded strings Arie van Wingerden
2016-09-13  9:04 ` Dmitry A. Kazakov
2016-09-22  2:10   ` John Smith
2016-09-22  7:24     ` Dmitry A. Kazakov
2016-09-22  9:01       ` J-P. Rosen
2016-09-22  9:53         ` Dmitry A. Kazakov
2016-09-22 10:58           ` G.B.
2016-09-22 12:05             ` Dmitry A. Kazakov
2016-09-22 14:14               ` G.B.
2016-09-22 17:18                 ` Dmitry A. Kazakov
2016-09-22 11:08           ` J-P. Rosen
2016-09-22 12:05             ` Dmitry A. Kazakov
2016-09-22 13:18           ` Maciej Sobczak
2016-09-22 13:52             ` Dmitry A. Kazakov
2016-09-22 14:51               ` Maciej Sobczak
2016-09-22 17:13                 ` Dmitry A. Kazakov
2016-09-23  5:50                   ` Maciej Sobczak
2016-09-23  6:36                     ` Simon Wright
2016-09-23  7:48                       ` Dmitry A. Kazakov
2016-09-28 20:55                     ` Randy Brukardt
2016-09-23 23:58       ` John Smith
2016-09-24  7:52         ` Dmitry A. Kazakov
2016-09-24 16:25           ` John Smith
2016-09-24 17:44             ` Dmitry A. Kazakov
2016-09-24 18:33               ` John Smith
2016-09-24 18:37               ` John Smith
2016-09-24 18:59               ` John Smith
2016-09-25  8:50                 ` Dmitry A. Kazakov
2016-09-25 23:35                   ` brbarkstrom
2016-09-26  7:28                     ` Dmitry A. Kazakov
2016-09-26 12:39                       ` brbarkstrom
2016-09-28 21:09             ` Randy Brukardt
2016-09-30  7:59               ` Björn Lundin
2016-09-13  9:35 ` gautier_niouzes
2016-09-13 10:41 ` Alejandro R. Mosteo
2016-09-13 17:41 ` Jeffrey R. Carter [this message]
2016-09-13 17:59 ` Björn Lundin
2016-09-14 11:23 ` Arie van Wingerden
2016-09-14 12:26   ` Arie van Wingerden
2016-09-14 12:28   ` Arie van Wingerden
2016-09-14 12:57 ` Arie van Wingerden
2016-09-14 19:39   ` Jeffrey R. Carter
2016-09-17 16:35     ` Arie van Wingerden
2016-09-16 14:43 ` Olivier Henley
2016-09-17 16:35   ` Arie van Wingerden
replies disabled

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