comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Checking to see if a string is a letter
Date: Thu, 5 Apr 2012 11:18:16 -0700 (PDT)
Date: 2012-04-05T11:18:16-07:00	[thread overview]
Message-ID: <24830745.915.1333649896813.JavaMail.geo-discussion-forums@ynkf14> (raw)
In-Reply-To: <jlkjqa$4fp$1@dont-email.me>

On Thursday, April 5, 2012 10:12:42 AM UTC-7, deuteros wrote:
> 
> Alright, here's my function:
> 
>    function isVariable(token: in String) return Boolean is
>       ParserException : Exception;
>    begin
>       if(token'Length = 1) then
>          return (Is_Letter(token(1)));
>       end if;
> 
>       raise ParserException with ("Not a letter : " & token);
>      
>    end isVariable;
> 
> But I'm getting this warning:
> 
> warning: index for "token" may assume lower bound of 1
> warning: suggested replacement: "token'First + -1"

The warning hints at the problem; if something is declared as a "string" in Ada, the lower bound is not fixed at 1 but could be any positive integer.  You can get the lower bound with Token'First and the upper bound with Token'Last, so Is_Letter(Token(Token'First)) is what you want.  (The "suggested replacement" is wrong--it's off by one, in addition to being syntactically incorrect.  Someone needs to fix the compiler.)

Suppose that outside of isVariable, another procedure does this:

   Line : String (1..500);
...
   if isVariable (Line (362..362)) then blah-blah-blah...

then inside isVariable, Token'First and Token'Last will both be 362.  The caller is taking a *slice* of another string, and the bounds used for the slice become the bounds of the resulting string.
   
Also, note that in general, if S is a String, S(S'First) may raise an exception if S could be an empty (length 0) string.  In your example, you already ruled that out with the "if" statement.

Some other notes:
(1) You don't need parentheses around the expressions in the IF statement, the RETURN statement, or the RAISE statement, although they are harmless.  (2) The Ada fans on this newsgroup are going to roll their eyes at the names you picked (like "isVariable").  We tend to like putting underscores between words (or word-like parts) and capitalizing the first letter of each word, e.g. Is_Variable.  Also: you're probably already aware of this, but in case you aren't, Ada does *not* treat lower- and upper-case letters in identifiers as different, unlike C and some other languages.

                      -- Adam



  parent reply	other threads:[~2012-04-05 18:18 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-03  2:11 Checking to see is a string is a letter deuteros
2012-04-03  4:18 ` Leo Brewin
2012-04-03  4:52   ` Checking to see if " deuteros
2012-04-03  5:15     ` Jeffrey Carter
2012-04-03  6:07       ` deuteros
2012-04-03  6:47         ` Gautier write-only
2012-04-03 12:55           ` deuteros
2012-04-03 14:19             ` gautier_niouzes
2012-04-03  8:26         ` Simon Wright
2012-04-03 12:56           ` deuteros
2012-04-03 13:46           ` Dmitry A. Kazakov
2012-04-05 17:12           ` deuteros
2012-04-05 17:24             ` Martin Dowie
2012-04-05 18:04               ` deuteros
2012-04-05 18:18             ` Adam Beneschan [this message]
2012-04-05 18:56             ` Simon Wright
2012-04-03 20:40         ` Jeffrey Carter
2012-04-03  7:09 ` Checking to see is " Thomas Løcke
replies disabled

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