2

How can I create a string in Ada containing newlines, whose definition also has those newlines?

I've tried with 0..2 backslashes at the end of the line, but none of that compiles:

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

In PHP this would be:

<<<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";
TamaMcGlinn
  • 2,840
  • 23
  • 34
  • If you can define "newline" as a Character or sequence of Characters then of course you can define a String value containing it. If all of the Characters are graphic characters then you can define a String literal contining it. I suspect that any such definition will be very non-portable. – Jeffrey R. Carter Oct 31 '19 at 17:10

2 Answers2

7

From what I know, Ada , like Java, does not support multiline literal. The only thing I see is something like this:

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

Of course, you need to with and use Ada.Characters.Latin_1 to make these constants visible.

Frédéric Praca
  • 1,620
  • 15
  • 29
  • 4
    See [ARM 2.6(7)](http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-2-6.html#p7), "An end of line cannot appear in a string_literal". – Simon Wright Oct 23 '19 at 08:39
  • @SimonWright Thanks for pointing out the small note on the ARM, now I'll be able to say without hesitation that this is not supported :) – Frédéric Praca Oct 23 '19 at 08:58
2

A complement to Frédéric Praca answer:

Depending on your needs, you can use ASCII package instead of Ada.Characters.* (such as Latin_1, Latin_9, Wide_Latin_.. etc.). ASCII can not be with'ed since it is not a package, so you'll have to prefix everything (or define "aliases" using 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;

One could define a custom & operator to use it as a new line insertion point. But ... how useful is it ?

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");
LoneWanderer
  • 3,058
  • 1
  • 23
  • 41
  • 1
    Well, there are still people who use and develop using older Ada versions in which ASCII may have some sort of sense or justification. The `&` operator part is still relevant. – LoneWanderer Oct 23 '19 at 19:29
  • 1
    Nice idea (about the operator)! Why not using another operator? ```with Ada.Text_IO; procedure LF is function "*" (Left, Right : String) return String is (Left & ASCII.LF & Right); begin Ada.Text_IO.Put_Line("Foo" * "bar"); end;``` – Zerte Oct 24 '19 at 01:04