From: Martin Dowie <martin@re.mo.ve.thedowies.com>
Subject: Re: Checking to see if a string is a letter
Date: Thu, 05 Apr 2012 12:24:05 -0500
Date: 2012-04-05T12:24:05-05:00 [thread overview]
Message-ID: <514763028355339029.041536martin-re.mo.ve.thedowies.com@news.btinternet.com> (raw)
In-Reply-To: jlkjqa$4fp$1@dont-email.me
deuteros <deuteros@xrs.net> wrote:
> On Tue 03 Apr 2012 04:26:40a, Simon Wright <simon@pushface.org> wrote in
> news:m2k41xi5kv.fsf@pushface.org:
>
>> deuteros <deuteros@xrs.net> writes:
>>
>>> On Tue 03 Apr 2012 01:15:27a, Jeffrey Carter
>>> <spam.jrcarter.not@spam.not.acm.org> wrote in
>>> news:jle13q$ale$1@tornado.tornevall.net:
>>>
>>>> What do you mean by "contains a single letter"?
>>>
>>> I mean the string contains a single letter and nothing more. For
>>> example:
>>>
>>> a - Legal
>>> A - Legal
>>> aa - Illegal
>>> a1 - Illegal
>>
>> Then for a start the length of the string needs to be 1.
>>
>> If it is, the first (and only!) character needs to be a lower- or
>> upper-case letter. There are (at least) three ways of doing this:
>>
>> * declare an array of Boolean indexed by Character, with the elements
>> indexed by letters set to True and the others to False, and index by
>> the character to be tested;
>>
>> * declare two subtypes of character ("Character range 'a' .. 'z'", for
>> instance) and check whether the character to be tested is 'in' either
>> of the subtypes;
>>
>> * use the standard library, Ada.Characters.Handling.Is_Letter (probably
>> the easiest for you!)
>
> 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"
See http://www.ada-auth.org/standards/12rm/html/RM-3-6-3.html to see how
String is defined.
From this you can see that token may not have a value at "token (1)",
perhaps it is indexed by 10..20.
If you know that all the strings you'll ever pass to this function will
start with an index = 1, then you could add a "pragma Assert (tokenFirst =
1);" at the start of the declarative part. An exception will then be raised
if this isn't true (assuming the correct compiler switches are selected).
NB: ParserException is declared locally, so isn't visible to any subprogram
that could attempt to catch it...you'd need to use "when others =>".
HTH
-- Martin
--
-- Sent from my iPad
next prev parent reply other threads:[~2012-04-05 17:24 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 [this message]
2012-04-05 18:04 ` deuteros
2012-04-05 18:18 ` Adam Beneschan
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