5.2.3 Adapting code for double quoted strings
The predicates in this section can help adapting your program to the new convention for handling double quoted strings. We have adapted a huge code base with which we were not familiar in about half a day.
- list_strings
- This predicate may be used to assess compatibility issues due to the
representation of double quoted text as string objects. See
section 5.2 and section
5.2.4. To use it, load your program into Prolog and run list_strings/0.
The predicate lists source locations of string objects encountered in
the program that are not considered safe. Such string need to be
examined manually, after which one of the actions below may be
appropriate:
- Rewrite the code. For example, change
[X] = "a"
intoX = 0'a
. - If a particular module relies heavily on representing strings as
lists of character code, consider adding the following directive to the
module. Note that this flag only applies to the module in which it
appears.
:- set_prolog_flag(double_quotes, codes).
- Use a back quoted string (e.g.,
`text`
). Note that this will not make your code run regardless of the --traditional command line option and code exploiting this mapping is also not portable to ISO compliant systems. - If the strings appear in facts and usage is safe, add a clause to the multifile predicate check:string_predicate/1 to silence list_strings/0 on all clauses of that predicate.
- If the strings appear as an argument to a predicate that can handle string objects, add a clause to the multifile predicate check:valid_string_goal/1 to silence list_strings/0.
- Rewrite the code. For example, change
- check:string_predicate(:PredicateIndicator)
- Declare that PredicateIndicator has clauses that contain
strings, but that this is safe. For example, if there is a predicate
help_info/2 , where the second argument contains a double quoted string
that is handled properly by the predicates of the applications' help
system, add the following declaration to stop
list_strings/0
from complaining:
:- multifile check:string_predicate/1. check:string_predicate(user:help_info/2).
- check:valid_string_goal(:Goal)
- Declare that calls to Goal are safe. The module qualification
is the actual module in which Goal is defined. For example, a
call to format/3
is resolved by the predicate system:format/3. and the code below
specifies that the second argument may be a string (system predicates
that accept strings are defined in the library).
:- multifile check:valid_string_goal/1. check:valid_string_goal(system:format(_,S,_)) :- string(S).