comp.lang.ada
 help / color / mirror / Atom feed
From: Shark8 <OneWingedShark@gmail.com>
Subject: Re: comparing characters in string
Date: Sat, 04 Oct 2014 18:41:45 -0700
Date: 2014-10-04T18:41:45-07:00	[thread overview]
Message-ID: <lr0Yv.437953$O13.137119@fx17.iad> (raw)
In-Reply-To: <af79bb16-617e-46b6-b0b6-dae39a2089f8@googlegroups.com>

On 10/4/2014 4:03 PM, Stribor40 wrote:
>                    if a'(a'First) = b(b'First + 1) then

You're using qualification in that if, which is a semantic error.
Qualification is a method to explicitly tell the compiler what type 
something is, consider having the following functions:

    Function K return String;
    Function K return Character;

    Function J( I : Character ) return String;
    Function J( I : String ) return String;

and the following declaration:

    Data : String := J(K);

Which versions of J and K are used?
Well, it's ambiguous, so the compiler is to reject the compilation with 
an error; but if we had some way to explicitly tell the compiler what 
type K returns then we would resolve the ambiguity.

That is exactly what qualification is/does. Qualification is done with 
TYPE_NAME'( expression ), so we would resolve the above as follows:

    Data : String := J( Character'(K) );  -- Forces the K returning a 
character.

If you delete that tick after a, it should compile.
         if a(a'First) = b(b'First + 1) then

But there's a problem with your logic:
  a'(a'first) and b(b'first + 1) are both constant, so your loop is 
meaningless.

Try:
      function tt (a, b  : in String) return Boolean is
        Target    : Character renames a(a'first);
        Found     : Constant Boolean:= True;
        Substring : constant String:= b(b'first+1..b'last);
      begin

        for Index in substring'range loop
          if target = Substring(Index) then
            return Found;
          end if;
        end loop;

        return not Found;
      end tt;

Or, if you're doing Ada 2012:
      function tt (a, b  : in String) return Boolean is
        Target    : Character renames a(a'first);
        Found     : Constant Boolean:= True;
        Substring : constant String:= b(b'first+1..b'last);
      begin
        return (if (for some Ch of Substring => Ch = Target) then Found
                else not Found);
      end tt;

  reply	other threads:[~2014-10-05  1:41 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-04 23:03 comparing characters in string Stribor40
2014-10-05  1:41 ` Shark8 [this message]
2014-10-05  7:06   ` Simon Wright
2014-10-05 12:16     ` Shark8
2014-10-05 12:41       ` Simon Wright
replies disabled

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