[fpc-pascal] Feature "Multiline strings" added
Michael Van Canneyt
michael at freepascal.org
Sun Jul 27 14:23:18 CEST 2025
Hello,
I have added (long overdue) multiline string support to the FPC compiler.
It is activated by default in objfpc and delphi modes.
if you wish to use it (or disable it), there is a modeswitch:
{$modeswitch multilinestrings}
or, to disable
{$modeswitch multilinestrings-}
Due to historic reasons, it comes in 2 flavours.
Delphi style:
a multiline string starts with a odd number of quotes (3 at least),
followed by a newline, and ends with an equal amount of quotes on a
line preceded by only whitespace:
const s = '''
some
multiline
stuff
''';
In the resulting string, the first N whitespace characters will be trimmed,
where N is the number of whitespace characters before the terminating
quotes. The last CR/LF is also stripped. If a line contains less whitespaces
than the final line, it is considered an error.
So the above example would be equivalent to
const s = 'some '+sLineBreak+'multiline '+sLinebreak+'stuff';
(the "some " and "multiline " are followed by a space, your mail client may not
display that corectly in the source... )
The line-ending in the resulting string is by default the native line ending.
this can be influenced by the (delphi-compatible) directive:
{$TEXTBLOCK NATIVE/CR/LF/CRLF}
To include triple quotes in such a string, you can use 5 quotes to delimit
the string:
const s = '''''
some
'''multiline '''
stuff
''''';
All this is Delphi compatible.
The FPC bugtracker contains since a long time a proposal for multiline
strings using a backtick as the quote character:
https://gitlab.com/freepascal.org/fpc/source/-/issues/35827
This has since been implemented in pas2js (our pascal to Javascript transpiler).
In the interest of compatibility, that proposal has also been implemented in
FPC as well.
In fact, the implementation in the issue has served as the start of the Delphi-compatible
implementation.
This means you can also do
const S = `some
multiline
stuff`;
This form of multiline strings can be configured using 2 directives:
{$MULTILINESTRINGTRIMLEFT N}
where N is the number of whitespace characters to strip from the string.
The line ending can be set using
{$MULTILINESTRINGLINEENDING PLATFORM/CR/LF/CRLF/SOURCE}
SOURCE will use the line ending as used in the string definition, other than
that the effect is identical to $TEXTBLOCK.
Despite the large amount of tests for this new feature, there may still be bugs,
please report them in the bugtracker if you encounter them.
My thanks to user Akira1364 (real name?) for the initial implementation of
this feature.
Michael.
More information about the fpc-pascal
mailing list