comp.lang.ada
 help / color / mirror / Atom feed
From: "Alejandro R. Mosteo" <alejandro@mosteo.com>
Subject: Re: Question on bounded / unbounded strings
Date: Tue, 13 Sep 2016 12:41:23 +0200
Date: 2016-09-13T12:41:23+02:00	[thread overview]
Message-ID: <nr8l4f$98g$1@dont-email.me> (raw)
In-Reply-To: <nnd$12b05e33$1843543e@b46552de0b4781f4>

On 13/09/16 10:46, Arie van Wingerden wrote:
> Hi,
>
> (...)
>                    A standard string must be defined with a fixed length
> in advance, which I do not know at the time ...

If you want to enjoy Ada, you will benefit from going in the direction 
Gautier points to. Basically, getting a good grasp of indefinite types 
and its implications. Perhaps the Wikibook can be of use too:

https://en.wikibooks.org/wiki/Ada_Programming/Type_System#Indefinite_subtype

Ada management of the stack for such types is one of its strong points 
IMO. Basically, although you will be using types with unknown size (at 
compile time), they have a known size at runtime and you don't need to 
care about what that size is (there are attributes to know, like 'First, 
'Last, 'Length).

That's what allows you to declare a function/procedure like:

function Tail (S : String; Len : Natural) return String;

Here, neither the input string S nor the result have a size known in 
advance (nor has to be the same), but you'll be able to use them without 
resorting to unbounded strings like this (which is very much the same 
Gautier gave):

declare
    Last_Three : String := Tail ("Hello", 3); -- Last_Three will be "llo"
    Last_One   : String := Tail (Last_Three, 1); -- Last_One will be "o"
begin
    -- whatever
end;

Basically, you have to perform declaration and initialization at the 
same time, so the indefinite type gets a bounded value. Otherwise, you 
can aim at not storing intermediate results whenever possible, for which 
the functional example in Gautier post is spot on.

You can check too this string splitting library of mine for ideas:

https://github.com/mosteo/agpl/blob/master/src/agpl-strings-fields.adb


  parent reply	other threads:[~2016-09-13 10: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 [this message]
2016-09-13 17:41 ` Jeffrey R. Carter
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