table 8
is broken. I get the error, 'The requested URL /pldoc/strings.html was not found on this server'Did you know ... | Search Documentation: |
library(strings): String utilities |
library(lynx/format)
provides
primitive to wrap long strings.
This module provides string handling utilities, currently notably for
dealing with multi-line strings and interpolation. The library
provides a couple of primitives as well definitions for the string
quasi quotation syntax. The latter allows for constructing both
single line and multi-line long strings based on template interpolation.
Below is a simple example using the quasi quotation syntax.
test(To) :- write({|string(To)|| | Dear {To}, | | I'm happy to announce a string interpolation quasi quoter. |}.
Warning
The general purpose string interpolation implemented by this library
should not be used to create strings for a formal language such
as HTML, JavaScript, SQL, etc. because the result will be subject to
injection attacks, providing a serious security risc. The
core idea of quasi quotation is to know about the target language and
interpolate Prolog data into the template while respecting the syntax
of the target language, notable to escape certain characters
where needed. See also library(http/html_write)
and library(http/js_write)
which define quasi quotation rules for HTML and JavaScript.
string
. If the first
character of the content is a newline (i.e., there is a newline
immediately after the ||
token) this first uses
dedent_lines/3 to
the remove common white space prefix from the lines. This is called with
the option chars("\s\t|")
, i.e., also removing |
characters and tab(8)
.
If the quasi quotation syntax carries arguments (e.g., string(To)
),
the string is compiled into a function that produces the result of
interpolating the arguments into the template. See user functions on
dict objects. If there are no arguments, the result is simply the final
string.
Name=Value
, insert Value
using write/1. If Name
does not appear in Map, raise an existence error.
Name must satisfy the rules for a Prolog variable.current_output
) of Goal
here. For safety reasons only accepted if Options contains
goals(true)
\n
"). E.g.
?- string_lines("a\nb\n", L). L = ["a", "b"]. ?- string_lines(S, ["a", "b"]). S = "a\nb\n".
This predicate is a true relation if both arguments are in canonical form, i.e. all text is represented as strings and the first argument ends with a newline. The implementation tolerates non-canonical input: other types than strings are accepted and String does not need to end with a newline.
split_string(String, "\n", "", Lines)
on a string
that ends in a newline adds an additional empty string compared to string_lines/2.\n
" -- conversion to and from external forms (such as "\
r\n
")
are typically done by the I/O predicates. A final "\n
" is
preserved.
Options:
*
or‘|
‘.
Default is
" \t"
.\n
" -- conversion to and from external
forms (such as "\
r\n
") are typically done by
the I/O predicates. Lines that consist entirely of whitespace are left
as-is.call(Filter, Line)
succeeds.
table 8
is broken. I get the error, 'The requested URL /pldoc/strings.html was not found on this server'The given example has a syntax error - the final parenthesis is missing
% test(+To) test(To) :- write({|string(To)|| | Dear {To}, | | I'm happy to announce a string interpolation quasi quoter. |}).
Also, a typo: "security risc" --> "security risk".
TODO
The string
notation should allow to specify whether one wants to see linefeed, carriagereturn+linefeed or the system default at the end of each line.
This is a good way to implement Here Documents (i.e. text directly included in the source, possibly subject to "interpolation" of variables) in Prolog.
For quasi-quotation syntax as used here (defined for the keyword string
), see
Another attempt at test code in SWI-Prolog 8.3.9:
:- use_module(library(strings)). :- begin_tests(here_document). test("generate greeting",true(TXT == 'Dear Perl Developer,\n\nI\'m happy to announce a\nstring interpolation quasi quoter\nfor Prolog!\n')) :- [To,Lang] = ["Perl Developer","Prolog"], with_output_to(atom(TXT), write({|string(To,Lang)|| | Dear {To}, | | I'm happy to announce a | string interpolation quasi quoter | for {Lang}! |})), format("~w\n",[TXT]). :- end_tests(here_document).
read_labyrinth(Labyrinth) :- format(string(Labyrinth), {|string|| |+ +---+---+---+ || | | |+---+- -+ +- -+ || | | | | |+ + +---+- -+ || | | | |+ +- -+- -+ + || | |+---+---+---+---+ |},[]).
Now to apply a DCG to that...
I know that one can generate strings-of-spaces using format/3 (if one remembers the syntax) but here is a simpler way. =Don't spend brainpower on producing spaces in particularly cute Prologgy ways!=
?- string_of_spaces(10,Spaces). Spaces = " ". ?- string_of_spaces(N," "). N = 4. ?- string_of_spaces(N," hey "). false.
I have the same problem with @abathologist above. But I figured out that table 8 is 2 lines below the link.
The incorrect URL for table 8 is: http://eu.swi-prolog.org/pldoc/strings.html#tab:quote-mapping