Ada中的多行字符串文字

问题描述 投票:1回答:2

如何在Ada中创建一个包含换行符的字符串,其定义也包含那些换行符?

我已经尝试过在行尾使用0..2反斜杠,但是没有一个可以编译:

   usage_info : String := "\
This should be the first line
and both the definition and the output
should contain newlines.";

在PHP中为:

<<<BLOCK
1
2
3
BLOCK;

In C++ this would be:

const std::string s = "\
1
2
3";

In C#, it would be:

const string s =
@"1
2
3";
string ada multiline literals
2个回答
3
投票

据我所知,Ada与Java一样,不支持多行文字。我唯一看到的就是这样:

usage_info : String := "This should be the first line" & CR & LF
                     & "and both the definition and the output" & CR & LF 
                     & "should contain newlines.";

当然,您需要withuse Ada.Characters.Latin_1使这些常量可见。


0
投票

对弗雷德里克·普拉卡(FrédéricPraca)的补充:

根据您的需要,您可以使用ASCII包代替Ada.Characters.*(例如Latin_1,Latin_9,Wide_Latin_ ..等)。由于ASCII不是包,因此无法进行with修改,因此您必须为所有内容加上前缀(或使用renames定义“别名”)

declare
    flex : constant String := "Foo" & ASCII.CR & "bar" & ASCII.LF;
    flux : constant String := "Foo" & ASCII.CR
                            & "bar" & ASCII.LF;
begin
    -- do stuff
    null;
end;

一个人可以定义一个自定义&运算符,以将其用作新的行插入点。但是...它有多有用?

function Foo (Left, Right : String) return String renames "&";
function Boo (Left : String; Right : Character) return String renames "&";

function "&" (Left, Right : String) return String is begin
   return Foo (
               Boo (Left, ASCII.LF),
               Right);
end "&";

Ada.Text_IO.Put_Line("Foo" &
                     "bar");
© www.soinside.com 2019 - 2024. All rights reserved.