Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

trimming strings

468 views
Skip to first unread message

ag...@drrob1.com

unread,
Aug 2, 2014, 9:10:16 AM8/2/14
to
I am having a very difficult time understanding something. I am
trying to do this using gnat on Ubuntu 14.04 system:

with Ada.Strings; use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;

subtype string255fixedtype is string (1.255);
inbuf : string255fixedtype;

BEGIN
inbuf := Get_Line;
inbuf := trim(inbuf,both);
-- this does not work, error is both is not visible

No combination of ada.strings.fixed.both, or ada.strings.both got it
to be visible.

However, I was able to get left and right to work, by using
ada.strings.left and ada.strings.right.

Trim_end is an enumeration type that says (left,right,both) in the
books I've looked at.

What am I missing?

Thanks

Pascal Obry

unread,
Aug 2, 2014, 10:21:38 AM8/2/14
to
Le samedi 02 août 2014 à 09:10 -0400, ag...@drrob1.com a écrit :
> I am having a very difficult time understanding something. I am
> trying to do this using gnat on Ubuntu 14.04 system:
>
> with Ada.Strings; use Ada.Strings;
> with Ada.Strings.Fixed; use Ada.Strings.Fixed;
>
> subtype string255fixedtype is string (1.255);
> inbuf : string255fixedtype;
>
> BEGIN
> inbuf := Get_Line;
> inbuf := trim(inbuf,both);
> -- this does not work, error is both is not visible

Please post a complete example, there is many errors above, far to many
to even pass the compilation.

--
Pascal Obry / Magny Les Hameaux (78)

The best way to travel is by means of imagination

http://v2p.fr.eu.org
http://www.obry.net

gpg --keyserver keys.gnupg.net --recv-key F949BD3B


G.B.

unread,
Aug 2, 2014, 11:27:42 AM8/2/14
to
>> subtype string255fixedtype is string (1.255);
>> inbuf : string255fixedtype;
>>
>> BEGIN
>> inbuf := Get_Line;
>> inbuf := trim(inbuf,both);
>> -- this does not work, error is both is not visible
>
> Please post a complete example, there is many errors above, far to many
> to even pass the compilation.

one of these being likely that an array of 255 characters
is to receive a string object that may not have that many
due to trimming.

Adam Beneschan

unread,
Aug 2, 2014, 1:00:02 PM8/2/14
to
That's definitely an error (and assigning a string with a known length to Get_Line won't work either). But those won't cause errors at compile time. They will result in exceptions at run time.

-- Adam

mockturtle

unread,
Aug 2, 2014, 1:22:26 PM8/2/14
to
On Saturday, August 2, 2014 3:10:16 PM UTC+2, ag...@drrob1.com wrote:
> I am having a very difficult time understanding something. I am
>
> trying to do this using gnat on Ubuntu 14.04 system:
>
> with Ada.Strings; use Ada.Strings;
> with Ada.Strings.Fixed; use Ada.Strings.Fixed;
>
> subtype string255fixedtype is string (1.255);
> inbuf : string255fixedtype;
>
> BEGIN
>
> inbuf := Get_Line;
> inbuf := trim(inbuf,both);
>
> -- this does not work, error is both is not visible
>
> No combination of ada.strings.fixed.both, or ada.strings.both got it
> to be visible.


As others said, a complete example would help us to help you. Anyway, I am going to do a wild guessing: did you maybe declared another "both" in your code? I do not have a compiler at hand and I cannot check, but if I remember correctly in this case you get a "non visible" error because the two symbols hide each other.

Riccardo

ag...@drrob1.com

unread,
Aug 3, 2014, 5:42:24 PM8/3/14
to
I am, after all, a newbie in Ada. I was wondering what the
non-visible error meant, as I was getting that also. Now I know it
means that identifiers are clashing in different packages.

I'm guessing that it is the use statements that make the symbols
clash?

I also don't have string processing down.


> one of these being likely that an array of 255 characters
> is to receive a string object that may not have that many
> due to trimming.

> That's definitely an error (and assigning a string with a known length to Get_Line won't work either).
> But those won't cause errors at compile time. They will result in exceptions at run time.
> -- Adam


with Ada.Text_IO; use Ada.Text_IO;
with Ada.Characters; use Ada.Characters;
with Ada.Characters.Conversions; use Ada.Characters.Conversions;
with Ada.Characters.Handling; use Ada.Characters.Handling;
with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
---------------------------

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.

How do I avoid a constraint_error exception at run-time?

Thanks

Georg Bauhaus

unread,
Aug 3, 2014, 7:14:09 PM8/3/14
to
On 03.08.14 23:42, ag...@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));





Shark8

unread,
Aug 3, 2014, 8:17:28 PM8/3/14
to
On 03-Aug-14 15:42, ag...@drrob1.com wrote:
> I also don't have string processing down.

Ah, here's the big secret to Ada string processing: treat them as arrays.

Make liberal use of things like 'First, 'Last, 'Length and don't assume
your 'Fists = 1. Also, remember the difference between constrained and
unconstrained --
D : String; -- Unconstrained.
E : String := "This." -- Constrained by initialization.
F : String(1..255); -- Constrained by subtype.
G : String := Fn(X); -- Constrained by initialization.

-- the last thing to remember is that strings are perfectly sized for
their content. (This should be done at initialization.) TED explains it
quite well here: http://stackoverflow.com/a/15104158/608963

mockturtle

unread,
Aug 4, 2014, 6:06:29 AM8/4/14
to
On Monday, August 4, 2014 2:17:28 AM UTC+2, Shark8 wrote:
>
> Make liberal use of things like 'First, 'Last, 'Length and don't assume
> your 'Fists = 1.
^^^^^


This is what I would call "aggressive programming" :-) :-) :-)

Pascal Obry

unread,
Aug 4, 2014, 6:49:50 AM8/4/14
to
Not really, you don't want to assume a string will start to 1.

procedure Call (Str : in out String) is
begin
Str (1) := 'b'; -- assuming 'First is 1 is wrong
end Call;

Whatever : String := "une belle phrase!";

Then the following call will just fail:

Call (Whatever (4 .. 8));

Simon Clubley

unread,
Aug 4, 2014, 7:51:02 AM8/4/14
to
On 2014-08-04, Pascal Obry <pas...@obry.net> wrote:
> Le lundi 04 ao�t 2014 � 03:06 -0700, mockturtle a �crit :
>> On Monday, August 4, 2014 2:17:28 AM UTC+2, Shark8 wrote:
>> >
>> > Make liberal use of things like 'First, 'Last, 'Length and don't assume
>> > your 'Fists = 1.
>> ^^^^^
>>
>>
>> This is what I would call "aggressive programming" :-) :-) :-)
>
> Not really, you don't want to assume a string will start to 1.
>

mockturtle was making a comment on the typo of "'Fists" instead of
"'First". :-)

Simon.

--
Simon Clubley, clubley@remove_me.eisner.decus.org-Earth.UFP
Microsoft: Bringing you 1980s technology to a 21st century world

Pascal Obry

unread,
Aug 4, 2014, 8:11:59 AM8/4/14
to
Le lundi 04 août 2014 à 11:51 +0000, Simon Clubley a écrit :
> mockturtle was making a comment on the typo of "'Fists" instead of
> "'First". :-)

Missed the joke, sorry :)

Shark8

unread,
Aug 4, 2014, 12:50:16 PM8/4/14
to
On 04-Aug-14 04:06, mockturtle wrote:
>> Make liberal use of things like 'First, 'Last, 'Length and don't assume
>> >your 'Fists = 1.
> ^^^^^
>
>
> This is what I would call "aggressive programming"

Sometimes programming *JUST MAKES ME ANGRY!*
;)
Message has been deleted

ag...@drrob1.com

unread,
Aug 5, 2014, 5:05:54 PM8/5/14
to
On Mon, 04 Aug 2014 19:28:57 -0400, Dennis Lee Bieber
<wlf...@ix.netcom.com> wrote:

>On Mon, 04 Aug 2014 10:50:16 -0600, Shark8 <OneWing...@gmail.com>
>declaimed the following:
> Now repeat that in the voice of Marvin the Martian <G>

LOL.

And thanks for the help.
0 new messages