Compare a string input to an Enumerated type

北战南征 提交于 2019-12-01 01:39:35

First instantiate Enumeration_IO for StopLightColor:

package Color_IO is new Ada.Text_IO.Enumeration_IO(StopLightColor);

Then you can do either of the following:

  • Use Color_IO.Get to read the value, catching any Data_Error that arises, as shown here for a similar instance of Enumeration_IO.

  • Use Color_IO.Put to obtain a String for comparison to response.

As an aside, Stoplight_Color might be a more consistent style for the enumerated type's identifier.

Another possibility:

Get_Line (response, N);
declare
    Color : StopLightColor;
begin
    Color := StopLightColor'Value(response(1..N));
    -- if you get here, the string is a valid color
    Put_Line ("The stoplight is " & response(1..N));
exception
    when Constraint_Error =>
        -- if you get here, the string is not a valid color (also could
        -- be raised if N is out of range, which it won't be here)
        null;
end;

Answering your actual question:

Ada doesn't allow you to compare values of different types directly, but luckily there is a way to convert an enumerated type to a string, which always works.

For any enumerated type T there exists a function:

function T'Image (Item : in T) return String;

which returns a string representation of the enumerated object passed to it.

Using that, you can declare a function, which compares a string and an enumerated type:

function "=" (Left  : in String;
              Right : in Enumerated_Type) return Boolean is
begin
   return Left = Enumerated_Type'Image (Right);
end "=";

If you want to do a case-insensitive comparison, you could map both strings to lower case before comparing them:

function "=" (Left  : in String;
              Right : in Enumerated_Type) return Boolean is
   use Ada.Characters.Handling;
begin
   return To_Lower (Left) = To_Lower (Enumerated_Type'Image (Right));
end "=";
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!