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: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: Question on bounded / unbounded strings Date: Tue, 13 Sep 2016 10:41:12 -0700 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit Injection-Date: Tue, 13 Sep 2016 17:41:11 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="6df4b173985f7c5c043cea362c370ff7"; logging-data="31481"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1++Otb+IsScY+uhCSP8tIRJ23OY/CZFk7I=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 In-Reply-To: Cancel-Lock: sha1:kcPh2mhurpqWIt5DPzuaQ5FjnZY= Xref: news.eternal-september.org comp.lang.ada:31777 Date: 2016-09-13T10:41:12-07:00 List-Id: 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