comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <bauhaus@futureapps.invalid>
Subject: Re: trimming strings
Date: Mon, 04 Aug 2014 01:14:09 +0200
Date: 2014-08-04T01:14:09+02:00	[thread overview]
Message-ID: <lrmfo2$inf$1@dont-email.me> (raw)
In-Reply-To: <2satt9tc4b1c6ldmqnmec3181jnfsuj7hp@4ax.com>

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));





  reply	other threads:[~2014-08-03 23:14 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-02 13:10 trimming strings agent
2014-08-02 14:21 ` Pascal Obry
2014-08-02 15:27   ` G.B.
2014-08-02 17:00     ` Adam Beneschan
2014-08-02 17:22 ` mockturtle
2014-08-03 21:42   ` agent
2014-08-03 23:14     ` Georg Bauhaus [this message]
2014-08-04  0:17     ` Shark8
2014-08-04 10:06       ` mockturtle
2014-08-04 10:49         ` Pascal Obry
2014-08-04 11:51           ` Simon Clubley
2014-08-04 12:11             ` Pascal Obry
2014-08-04 16:50         ` Shark8
2014-08-04 23:28           ` Dennis Lee Bieber
2014-08-05 21:05             ` agent
replies disabled

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