comp.lang.ada
 help / color / mirror / Atom feed
* Strings and errors... (gnat)
@ 2006-11-29 17:30 tr00per
  2006-11-29 17:44 ` Robert A Duff
  2006-12-01  8:39 ` Martin Krischik
  0 siblings, 2 replies; 8+ messages in thread
From: tr00per @ 2006-11-29 17:30 UTC (permalink / raw)


Hello!
During compilation GNAT says:
pracownicy.adb:123:22: warning: too few elements for subtype of
"Standard.String" defined at line 107
pracownicy.adb:123:22: warning: "Constraint_Error" will be raised at
run time
(and it is raised at run time)

Those lines look like that:
...
104: 		buf:string(1..70);
...
107: 		tmp:string(1..30);
...
123: 		tmp:=buf(13..26);
...

123:22 is after :=
I don't get the point. Where is the problem? Please help!




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Strings and errors... (gnat)
  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-30  2:04   ` jimmaureenrogers
  2006-12-01  8:39 ` Martin Krischik
  1 sibling, 2 replies; 8+ messages in thread
From: Robert A Duff @ 2006-11-29 17:44 UTC (permalink / raw)


"tr00per" <tr00per@o2.pl> writes:

> Hello!
> During compilation GNAT says:
> pracownicy.adb:123:22: warning: too few elements for subtype of
> "Standard.String" defined at line 107
> pracownicy.adb:123:22: warning: "Constraint_Error" will be raised at
> run time
> (and it is raised at run time)
>
> Those lines look like that:
> ...
> 104: 		buf:string(1..70);
> ...
> 107: 		tmp:string(1..30);
> ...
> 123: 		tmp:=buf(13..26);
> ...
>
> 123:22 is after :=
> I don't get the point. Where is the problem? Please help!

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



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Strings and errors... (gnat)
  2006-11-29 17:44 ` Robert A Duff
@ 2006-11-29 17:57   ` tr00per
  2006-11-29 18:10     ` Ludovic Brenta
  2006-11-30  2:04   ` jimmaureenrogers
  1 sibling, 1 reply; 8+ messages in thread
From: tr00per @ 2006-11-29 17:57 UTC (permalink / raw)



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.




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Strings and errors... (gnat)
  2006-11-29 17:57   ` tr00per
@ 2006-11-29 18:10     ` Ludovic Brenta
  2006-11-30 11:36       ` Georg Bauhaus
  0 siblings, 1 reply; 8+ messages in thread
From: Ludovic Brenta @ 2006-11-29 18:10 UTC (permalink / raw)


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.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Strings and errors... (gnat)
  2006-11-29 17:44 ` Robert A Duff
  2006-11-29 17:57   ` tr00per
@ 2006-11-30  2:04   ` jimmaureenrogers
  1 sibling, 0 replies; 8+ messages in thread
From: jimmaureenrogers @ 2006-11-30  2:04 UTC (permalink / raw)


Robert A Duff wrote:
> You can't assign a shorter string into a longer one -- the lengths have
> to match.

However, you can use the predefined package Ada.Strings.Fixed to
move the shorter string into the longer string.

The Ada Reference Manual describes the move procedure as:

procedure Move (Source : in String;
Target : out String;
Drop : in Truncation := Error;
Justify : in Alignment := Left;
Pad : in Character := Space);

Note that the package Ada.Strings is defined as:

package Ada.Strings is
pragma Pure(Strings);

Space : constant Character := ' ';
Wide_Space : constant Wide_Character := ' ';

Length_Error, Pattern_Error, Index_Error, Translation_Error :
exception;

type Alignment is (Left, Right, Center);
type Truncation is (Left, Right, Error);
type Membership is (Inside, Outside);
type Direction is (Forward, Backward);
type Trim_End is (Left, Right, Both); 
end Ada.Strings; 


Jim Rogers




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Strings and errors... (gnat)
  2006-11-29 18:10     ` Ludovic Brenta
@ 2006-11-30 11:36       ` Georg Bauhaus
  0 siblings, 0 replies; 8+ messages in thread
From: Georg Bauhaus @ 2006-11-30 11:36 UTC (permalink / raw)


On Wed, 2006-11-29 at 19:10 +0100, Ludovic Brenta wrote:

> 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.

Returning 3 fixed strings will work if you use a suitable type
definition: We have
 "I have input in format String:String:String. I want to get rid of
  these colons and get three seperate strings."
Consider the colon separated input string an object. Hence,

procedure fs is

   type Threefixedstrings
      (af, al, bf, bl, cf, cl: Natural) is record
      a: String(af .. al);
      b: String(bf .. bl);
      c: String(cf .. cl);
   end record;

   function split(Input: String) return Threefixedstrings is separate;

begin
   declare
      result: constant Threefixedstrings := split("asdf:qwer:cxvbnm");
   begin
      -- do something with result
   end;
end fs;

Within the function split you can use Find_Token or Index etc.
and collect positions. Then return an aggregate of
type Threefixedstrings. There are several variations on this
subject. :-)


 -- Georg 





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Strings and errors... (gnat)
  2006-11-29 17:30 Strings and errors... (gnat) tr00per
  2006-11-29 17:44 ` Robert A Duff
@ 2006-12-01  8:39 ` Martin Krischik
  2006-12-01 19:39   ` [solved] tr00per
  1 sibling, 1 reply; 8+ messages in thread
From: Martin Krischik @ 2006-12-01  8:39 UTC (permalink / raw)


tr00per schrieb:
> Hello!
> During compilation GNAT says:
> pracownicy.adb:123:22: warning: too few elements for subtype of
> "Standard.String" defined at line 107
> pracownicy.adb:123:22: warning: "Constraint_Error" will be raised at
> run time
> (and it is raised at run time)
> 
> Those lines look like that:
> ...
> 104: 		buf:string(1..70);
> ...
> 107: 		tmp:string(1..30);
> ...
> 123: 		tmp:=buf(13..26);
> ...
> 
> 123:22 is after :=
> I don't get the point. Where is the problem? Please help!

You allready got some help, so here just a suggested reading:

http://en.wikibooks.org/wiki/Ada_Programming/Strings

Martin




^ permalink raw reply	[flat|nested] 8+ messages in thread

* [solved]
  2006-12-01  8:39 ` Martin Krischik
@ 2006-12-01 19:39   ` tr00per
  0 siblings, 0 replies; 8+ messages in thread
From: tr00per @ 2006-12-01 19:39 UTC (permalink / raw)


OK, thanks people! Problem solved! I used Ada.Strings.Unbounded and it
works.




^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2006-12-01 19:39 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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