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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no 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!news.eternal-september.org!.POSTED!not-for-mail From: Georg Bauhaus Newsgroups: comp.lang.ada Subject: Re: trimming strings Date: Mon, 04 Aug 2014 01:14:09 +0200 Organization: A noiseless patient Spider Message-ID: References: <55c718c7-b4cd-43c7-ae66-fef3e514a47c@googlegroups.com> <2satt9tc4b1c6ldmqnmec3181jnfsuj7hp@4ax.com> Reply-To: nonlegitur@futureapps.de Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 3 Aug 2014 23:14:10 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="b96887e80893c84a90c3007226ca0d1c"; logging-data="19183"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX185ED36bKPOAxZIR8r5lNuYuA/PSEgeIRE=" User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: <2satt9tc4b1c6ldmqnmec3181jnfsuj7hp@4ax.com> Cancel-Lock: sha1:YGUMXkDIhgv6lg5gYIES7x+tLAs= Xref: news.eternal-september.org comp.lang.ada:21429 Date: 2014-08-04T01:14:09+02:00 List-Id: On 03.08.14 23:42, agent@drrob1.com wrote: > Procedure trimtest is > Subtype String255FixedType is String(1..255); > > str : String255Fixedtype; > STRLEN : Natural; > > BEGIN > Put(" Enter line: "); > Get_Line(Str,StrLen); > Str := TRIM(Str,Side => Both); > -- Str := Ada.Strings.Fixed.TRIM(str,side => Ada.Strings.both); > Put_Line(" Line is: " & Str(1..StrLen) & " with length of " & > Natural'image(StrLen) ); > End trimtest; > > This simple procedure compiles, but does give me an exception at > run-time after I call trim. I don't understand how to avoid this. I > am used to more flexible strings that are null terminated. These are Ada.Strings.Bounded and Ada.Strings.Unbounded. Or, if you want the greatest possible flexibility, wrap a pointer to String in a type derived from Finalization.Controlled. This, however, will likely turn into reinventing either of the former language defined packages. An object of type String255FixedType will always be what it is by declaration: an array of exactly 255 components, indexed by values between 1 and 255, inclusively. So, "fixed" is a well chosen part of the subtype's name. > How do I avoid a constraint_error exception at run-time? One more way is to declare the string object where you need it Get_Line (Str, StrLen); declare Trimmed : String := Trim (Str(1 .. StrLen), Side => Both); begin ... do something with Trimmed end; Or pass the trimmed result to some procedure that works on it: procedure Taking_Trimmed (S : String) is -- S is of any length, and of any indexing subtype begin -- note: S'Length, S'First, etc are known here end Taking_Trimmed; ... Get_Line (Str, StrLen); Taking_Trimmed (Trim (Str(1 .. StrLen), Side => Both));