首页 文章

如何在数组和记录中使用refs来表示常量字符串?

提问于
浏览
0

我打算将一些用C / C编写的程序转换为Ada . 这些通常作为不规则的数组大量使用常量字符文字:

const char * stringsA = { "Up", "Down", "Shutdown" };

或记录中的字符串引用,如:

typedef struct
   {
   int something;
   const char * regexp;
   const char * errormsg
   } ERRORDESCR;
ERRORDESCR edscrs [ ] =
   {
    { 1, "regexpression1", "Invalid char in person name" },
    { 2, "regexp2", "bad bad" }
   };

预设由C / C编译器计算,我希望Ada编译器也能够这样做 .

我使用谷歌搜索了不规则的数组,但只能找到两种预设字符串的方法 . 约翰巴恩斯为Ada 95提供基本原理,另一人为http://computer-programming-forum.com/44-ada/d4767ad6125feac7.htm . 这些在下面显示为stringsA和stringsB . StringsA分为两个阶段,如果要设置数百个字符串,这有点单调乏味 . StringsB仅使用一步,但依赖于编译器 .

问题1:还有其他方法吗?问题2:第二个字符串B是否可以与GNAT Ada一起使用?

我还没有开始转换 . 以下套餐仅供我自己进行实验和教学......

package ragged is
   type String_ptr is access constant String;
   procedure mydummy;
end ragged;

package body ragged is
   s1: aliased constant String := "Up";
   s2: aliased constant String := "Down";
   s3: aliased constant String := "Shutdown";

   stringsA: array (1 .. 3) of String_ptr :=
     (s1'Access, s2'Access, s3'Access); -- works

   stringsB: array (1 .. 3) of String_ptr :=
      (new String'("Up"), new String'("Down"),
      new String'("Shutdown"));  -- may work, compiler-dependent

   --   this would be convenient and clear...
   --stringsC: array (1 .. 3) of String_ptr :=
   -- ("Up", "Down", "Shutdown");  -- BUT Error, expected String_ptr values

   --stringsD: array (1 .. 3) of String_ptr :=
   --("Up"'Access, "Down"'Access, "Shutdown"'Access);  --Error - bad Access use

   --stringsE: array (1 .. 3) of String_ptr :=
   --(String_ptr("Up"), String_ptr("Down"),
   -- String_ptr("Shutdown"));  -- Error, invalid conversion

   procedure mydummy is
   begin
   null;
   end;
end ragged;

2 回答

  • 1

    稍微明智的运算符重载可以以较简洁的方式执行此操作:

    (在包体内)

    function New_String(S : String) return String_Ptr is
       begin
          return new String'(S);
       end New_String;
    
       function "+" (S : String) return String_Ptr renames New_String;
    

    现在你可以这样做:

    stringsC: array (1 .. 3) of String_ptr := (+"Up", +"Down", +"Shutdown");
    
  • 0

    没有足够的空间评论此测试程序

    package raggedtest is
       type String_ptr is access constant String;
       procedure mytest;
    end raggedtest;
    
    with ada.text_IO; use Ada.Text_IO;
    package body raggedtest is
       s1: aliased constant String := "Up";
       s2: aliased constant String := "Down";
       s3: aliased constant String := "Shutdown";
       stringsA: array (1 .. 3) of String_ptr :=
         (s1'Access, s2'Access, s3'Access);
    
       stringsB: array (1 .. 3) of String_ptr :=
          (new String'("UpB"), new String'("DownB"),
           new String'("ShutdownB"));
    
       function New_String(S : String) return String_Ptr is
       begin
          return new String'(S);
       end New_String;
       function "+" (S : String) return String_Ptr renames New_String;
       stringsC: array (1 .. 3) of String_ptr := (+"UpC", +"DownC", +"ShutdownC");
    
    
       procedure mytest is
       begin
          put ( "s1A: " ); put( stringsA(1).all ); New_line;
          put ( "s2A " ); put( stringsA(2).all ); New_line;
          put ( "s3A: " ); put( stringsA(3).all ); New_line;
    
          put ( "s1B: " ); put( stringsB(1).all ); New_line;
          put ( "s2B " ); put( stringsB(2).all ); New_line;
          put ( "s3B: " ); put( stringsB(3).all ); New_line;
    
          put ( "s1C: " ); put( stringsC(1).all ); New_line;
          put ( "s2C " ); put( stringsC(2).all ); New_line;
         put ( "s3C: " ); put( stringsC(3).all ); New_line;
       end;
    end raggedtest;
    
    with raggedtest; use raggedtest;
    procedure main is
    begin
       mytest;
    end main;
    

相关问题