From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "Alejandro R. Mosteo" Newsgroups: comp.lang.ada Subject: Re: Question on bounded / unbounded strings Date: Tue, 13 Sep 2016 12:41:23 +0200 Organization: A noiseless patient Spider Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 13 Sep 2016 10:41:19 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="bc686c1890d5fda24d69bb608fea27b4"; logging-data="9488"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+33Ld2mVtIXKyDIqfP3fVp" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 In-Reply-To: Cancel-Lock: sha1:z1r4rBMTqk5LmGDB2o0lJdzwE0c= Xref: news.eternal-september.org comp.lang.ada:31767 Date: 2016-09-13T12:41:23+02:00 List-Id: 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