Did you know ... | Search Documentation: |
strings.pl -- String utilities |
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)
?- 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.
Options:
*
or `|`. Default is
" \t"
.call(Filter, Line)
succeeds.