comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: Strings and errors... (gnat)
Date: Wed, 29 Nov 2006 19:10:22 +0100
Date: 2006-11-29T19:10:22+01:00	[thread overview]
Message-ID: <87u00ip1ip.fsf@ludovic-brenta.org> (raw)
In-Reply-To: 1164823057.036164.238000@h54g2000cwb.googlegroups.com

tr00per writes:
> Robert A Duff napisal(a):
>> You can't assign a shorter string into a longer one -- the lengths have
>> to match.  You could do:
>>
>>     tmp: String := buf(13..26);
>>
>> or
>>
>>     tmp: constant String := buf(13..26);
>>
>> and it will take the bounds for tmp from buf(13..26).
>>
>> Or, you could use Unbounded_Strings.
>>
>> - Bob
>
> Actual length of the string is dependend on other data, range here is
> for example, so these declarations aren't solution for me, but thanks
> anyway. Maybe there is a ready-to-use function for splitting the string
> by template, so I don't have to reinvent the wheel?
> I have input in format String:String:String. I want to get rid of these
> colons and get three seperate strings.

Robert's advice still holds:

1) the bounds of a String can be dynamic, so you can also say
   tmp : constant String := buf (A .. B); -- A and B computed from input

2) You can use Ada.Strings.Unbounded.

Now for my own advice:

1) Look at Ada.Strings.Fixed.Find_Token.  It will help you find the
   colons.

2) Look at Ada.Strings.Bounded; it is more efficient than
   Unbounded_Strings, but you have know the maximum length in advance.

3) If you are writing a subprogram that splits a given string into 3
   strings of unknown length, you have basically 4 options:

   a) return 3 access values to dynamically allocated strings
   b) return 3 unbounded strings
   c) return 3 bounded strings
   d) return 3 fixed strings and 3 Last values, like
      Ada.Text_IO.Get_Line does.

But simply returning 3 fixed strings will not work.

-- 
Ludovic Brenta.



  reply	other threads:[~2006-11-29 18:10 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-11-29 17:30 Strings and errors... (gnat) tr00per
2006-11-29 17:44 ` Robert A Duff
2006-11-29 17:57   ` tr00per
2006-11-29 18:10     ` Ludovic Brenta [this message]
2006-11-30 11:36       ` Georg Bauhaus
2006-11-30  2:04   ` jimmaureenrogers
2006-12-01  8:39 ` Martin Krischik
2006-12-01 19:39   ` [solved] tr00per
  -- strict thread matches above, loose matches on Subject: below --
2006-11-29 17:57 Strings and errors... (gnat) Benjamin Place
2006-11-30  6:16 ` Jeffrey R. Carter
replies disabled

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