comp.lang.ada
 help / color / mirror / Atom feed
* Checking to see is a string is a letter
@ 2012-04-03  2:11 deuteros
  2012-04-03  4:18 ` Leo Brewin
  2012-04-03  7:09 ` Checking to see is " Thomas Løcke
  0 siblings, 2 replies; 18+ messages in thread
From: deuteros @ 2012-04-03  2:11 UTC (permalink / raw)


Is there a way to check if a string variable contains a single letter?



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

* Re: Checking to see is a string is a letter
  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  7:09 ` Checking to see is " Thomas Løcke
  1 sibling, 1 reply; 18+ messages in thread
From: Leo Brewin @ 2012-04-03  4:18 UTC (permalink / raw)


On Tuesday, 3 April 2012 12:11:56 UTC+10, deuteros  wrote:
> Is there a way to check if a string variable contains a single letter?

Yes.



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

* Re: Checking to see if a string is a letter
  2012-04-03  4:18 ` Leo Brewin
@ 2012-04-03  4:52   ` deuteros
  2012-04-03  5:15     ` Jeffrey Carter
  0 siblings, 1 reply; 18+ messages in thread
From: deuteros @ 2012-04-03  4:52 UTC (permalink / raw)


Leo Brewin <leo.brewin@internode.on.net> wrote in 
news:2030859.0.1333426683573.JavaMail.geo-discussion-forums@pbbnv8:

> On Tuesday, 3 April 2012 12:11:56 UTC+10, deuteros  wrote:
>
>> Is there a way to check if a string variable contains a single letter?
> 
> Yes.

So how would one do that? In Java I would use regular expressions, and GNAT 
seems to have a library for regular expressions, but I'm looking for 
something a little more portable.

I'm very new to Ada.



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

* Re: Checking to see if a string is a letter
  2012-04-03  4:52   ` Checking to see if " deuteros
@ 2012-04-03  5:15     ` Jeffrey Carter
  2012-04-03  6:07       ` deuteros
  0 siblings, 1 reply; 18+ messages in thread
From: Jeffrey Carter @ 2012-04-03  5:15 UTC (permalink / raw)


On 04/02/2012 09:52 PM, deuteros wrote:
> Leo Brewin<leo.brewin@internode.on.net>  wrote in
> news:2030859.0.1333426683573.JavaMail.geo-discussion-forums@pbbnv8:
>
>> On Tuesday, 3 April 2012 12:11:56 UTC+10, deuteros  wrote:
>>
>>> Is there a way to check if a string variable contains a single letter?
>>
>> Yes.
>
> So how would one do that? In Java I would use regular expressions, and GNAT
> seems to have a library for regular expressions, but I'm looking for
> something a little more portable.

What do you mean by "contains a single letter"?

For example, given

S1 : String := "12345j%^&*(";
S2 : String := "A";

you have 2 String variables, both of which contain a single letter. S1 contains 
other characters as well, while S2 contains only a single letter.

It will probably also help to explain the problem that gives rise to this 
sub-problem, as context is always helpful in knowing how to answer.

-- 
Jeff Carter
"It is the German who is so uncourteous to his verbs."
A Scandal in Bohemia
122



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

* Re: Checking to see if a string is a letter
  2012-04-03  5:15     ` Jeffrey Carter
@ 2012-04-03  6:07       ` deuteros
  2012-04-03  6:47         ` Gautier write-only
                           ` (2 more replies)
  0 siblings, 3 replies; 18+ messages in thread
From: deuteros @ 2012-04-03  6:07 UTC (permalink / raw)


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

> It will probably also help to explain the problem that gives rise to
> this sub-problem, as context is always helpful in knowing how to answer.

I'm writing an interpreter for a very simple language grammar. The language 
only has an integer data type and the identifier is only a single letter (not 
case sensitive).

My program takes a text file, breakes it up into lexemes, and stores each 
lexeme in a vector. If the parser finds a single letter that hasn't been 
handled by some other function then it knows it needs to call a function to 
perform a variable assignment.



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

* Re: Checking to see if a string is a letter
  2012-04-03  6:07       ` deuteros
@ 2012-04-03  6:47         ` Gautier write-only
  2012-04-03 12:55           ` deuteros
  2012-04-03  8:26         ` Simon Wright
  2012-04-03 20:40         ` Jeffrey Carter
  2 siblings, 1 reply; 18+ messages in thread
From: Gautier write-only @ 2012-04-03  6:47 UTC (permalink / raw)


On 3 avr, 08:07, deuteros <deute...@xrs.net> wrote:

> I mean the string contains a single letter and nothing more. For example:
>
> a  - Legal
> A  - Legal
> aa - Illegal
> a1 - Illegal

Did you consider to use the string's length ?
Or is the problem more complicated (wildcards etc.) ?
_________________________
Gautier's Ada programming
http://gautiersblog.blogspot.com/search/label/Ada
NB: follow the above link for a valid e-mail address



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

* Re: Checking to see is a string is a letter
  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  7:09 ` Thomas Løcke
  1 sibling, 0 replies; 18+ messages in thread
From: Thomas Løcke @ 2012-04-03  7:09 UTC (permalink / raw)


On 04/03/2012 04:11 AM, deuteros wrote:
> Is there a way to check if a string variable contains a single letter?


A possible solution would be to take a look at Ada.Strings.Maps and
Ada.Strings.Maps.Constants.

You can then use Ada.Strings.Fixed.Count to count the number of matches
against your map, and if the result isn't exactly 1, then you know your
string contains either zero "single letter" or several "single letter".

But first of course you need to define exactly what "single letter"
means.

:o)

-- 
Thomas L�cke | thomas@12boo.net | http://12boo.net



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

* Re: Checking to see if a string is a letter
  2012-04-03  6:07       ` deuteros
  2012-04-03  6:47         ` Gautier write-only
@ 2012-04-03  8:26         ` Simon Wright
  2012-04-03 12:56           ` deuteros
                             ` (2 more replies)
  2012-04-03 20:40         ` Jeffrey Carter
  2 siblings, 3 replies; 18+ messages in thread
From: Simon Wright @ 2012-04-03  8:26 UTC (permalink / raw)


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!)



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

* Re: Checking to see if a string is a letter
  2012-04-03  6:47         ` Gautier write-only
@ 2012-04-03 12:55           ` deuteros
  2012-04-03 14:19             ` gautier_niouzes
  0 siblings, 1 reply; 18+ messages in thread
From: deuteros @ 2012-04-03 12:55 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 532 bytes --]

On Tue 03 Apr 2012 02:47:04a, Gautier write-only 
<gautier_niouzes@hotmail.com> wrote in news:4c3660d0-24bf-4cd8-92d0-
b0e8c3e03ce8@z38g2000vbu.googlegroups.com:

> On 3 avr, 08:07, deuteros <deute...@xrs.net> wrote:
> 
>> I mean the string contains a single letter and nothing more. For example:
>>
>> a �- Legal
>> A �- Legal
>> aa - Illegal
>> a1 - Illegal
> 
> Did you consider to use the string's length ?
> Or is the problem more complicated (wildcards etc.) ?

There are boolean operators which are also of length one.



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

* Re: Checking to see if a string is a letter
  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
  2 siblings, 0 replies; 18+ messages in thread
From: deuteros @ 2012-04-03 12:56 UTC (permalink / raw)


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!)

Thanks. I'll check out that library.



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

* Re: Checking to see if a string is a letter
  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
  2 siblings, 0 replies; 18+ messages in thread
From: Dmitry A. Kazakov @ 2012-04-03 13:46 UTC (permalink / raw)


On Tue, 03 Apr 2012 09:26:40 +0100, Simon Wright wrote:

> * use the standard library, Ada.Characters.Handling.Is_Letter (probably
>   the easiest for you!)

Ada.Wide_Wide_Characters.Handling.Is_Letter for Unicode.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Checking to see if a string is a letter
  2012-04-03 12:55           ` deuteros
@ 2012-04-03 14:19             ` gautier_niouzes
  0 siblings, 0 replies; 18+ messages in thread
From: gautier_niouzes @ 2012-04-03 14:19 UTC (permalink / raw)


> There are boolean operators which are also of length one.

So you check first the length, and then whether the first character is a letter.
(I almost wrote the Ada code :-) there!)
G.



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

* Re: Checking to see if a string is a letter
  2012-04-03  6:07       ` deuteros
  2012-04-03  6:47         ` Gautier write-only
  2012-04-03  8:26         ` Simon Wright
@ 2012-04-03 20:40         ` Jeffrey Carter
  2 siblings, 0 replies; 18+ messages in thread
From: Jeffrey Carter @ 2012-04-03 20:40 UTC (permalink / raw)


On 04/02/2012 11:07 PM, deuteros wrote:
>
> I mean the string contains a single letter and nothing more. For example:
>
> a  - Legal
> A  - Legal
> aa - Illegal
> a1 - Illegal

Then you'd probably start by checking 'Length for the String to be sure it's 1. 
Then you'd check that the single Character in the String is a letter, probably 
using an operation in Ada.Characters.Handling.

-- 
Jeff Carter
"I fart in your general direction."
Monty Python & the Holy Grail
05



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

* Re: Checking to see if a string is a letter
  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
                               ` (2 more replies)
  2 siblings, 3 replies; 18+ messages in thread
From: deuteros @ 2012-04-05 17:12 UTC (permalink / raw)


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"



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

* Re: Checking to see if a string is a letter
  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
  2012-04-05 18:56             ` Simon Wright
  2 siblings, 1 reply; 18+ messages in thread
From: Martin Dowie @ 2012-04-05 17:24 UTC (permalink / raw)


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



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

* Re: Checking to see if a string is a letter
  2012-04-05 17:24             ` Martin Dowie
@ 2012-04-05 18:04               ` deuteros
  0 siblings, 0 replies; 18+ messages in thread
From: deuteros @ 2012-04-05 18:04 UTC (permalink / raw)


On Thu 05 Apr 2012 01:24:05p, Martin Dowie <martin@re.mo.ve.thedowies.com> wrote in 
news:514763028355339029.041536martin-re.mo.ve.thedowies.com@news.btinternet.com:

> deuteros <deuteros@xrs.net> 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"
> 
> 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.

Yeah, I knew that much. I just didn't know how to make the compiler know
that it will always be 1. 

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

For the purposes of this particular project, the program is just supposed to abort
whenever there's an exception. Thanks though. 



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

* Re: Checking to see if a string is a letter
  2012-04-05 17:12           ` deuteros
  2012-04-05 17:24             ` Martin Dowie
@ 2012-04-05 18:18             ` Adam Beneschan
  2012-04-05 18:56             ` Simon Wright
  2 siblings, 0 replies; 18+ messages in thread
From: Adam Beneschan @ 2012-04-05 18:18 UTC (permalink / raw)


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



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

* Re: Checking to see if a string is a letter
  2012-04-05 17:12           ` deuteros
  2012-04-05 17:24             ` Martin Dowie
  2012-04-05 18:18             ` Adam Beneschan
@ 2012-04-05 18:56             ` Simon Wright
  2 siblings, 0 replies; 18+ messages in thread
From: Simon Wright @ 2012-04-05 18:56 UTC (permalink / raw)


deuteros <deuteros@xrs.net> writes:

> warning: index for "token" may assume lower bound of 1
> warning: suggested replacement: "token'First + -1"

Here (GCC 4.6.0, GCC-4.8.0 [trunk revision 185379] and GNAT GPL 2011 I
get

isvariable.adb:6:31: warning: index for "token" may assume lower bound of 1
isvariable.adb:6:31: warning: suggested replacement: "token'First"



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

end of thread, other threads:[~2012-04-05 18:56 UTC | newest]

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

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