Character Escaping

The character escaping facility allows escape sequences to occur within strings and quoted atoms, so that programmers can put non-printable characters in atoms and strings and still be able to see what they are doing. This facility can be switched on and off by the commands:

     | ?- prolog_flag(character_escapes, _, on).
     | ?- prolog_flag(character_escapes, _, off).

See ref-lps-ove, for a description of prolog_flag/3. Character escaping is off by default.

Strings or quoted atoms containing the following escape sequences can occur in terms obtained by read/[1,2], compile/1, and so on.

The 0' notation for the integer code of a character is also affected by character escaping.

With character escaping turned on, the only things that can occur in a string or quoted atom are the characters with ASCII codes 9 (horizontal tab), 32 (space), 33 through 126 (non-layout characters), or one of the following escape sequences:

The escape sequence:
Is converted to:
backspace (ASCII 8)
horizontal tab (ASCII 9)
new line (ASCII 10)

vertical tab (ASCII 11)
form feed (ASCII 12)
carriage return (ASCII 13)
escape (ASCII 27)
delete (ASCII 127)
alarm = BEL (ASCII 7)
the ASCII character with code CD (hexadecimal number)
\octal string
the ASCII character with code octal string base 8, where <octal string> is either 1, 2, or 3 octal digits
\^<control char>
the ASCII character whose code is the ASCII code of control char mod 32. \^? is another name for \d.
\layout char
no character, where layout char is a character with ASCII code =< 32 or ASCII code >= 127 (thus a newline or form-feed in a string or quoted atom can be ignored by immediately preceding it with a backslash)
no character; also, all characters up to, but not including, the next non-layout character are ignored
the character other, where other is any character not predefined here; thus \\ should be used to insert one backslash

It is an error if an escape sequence or ASCII character that is not defined above occurs in a string or quoted atom. For instance, an ordinary newline in an atom or string is regarded as an error when character escapes are on. This allows the syntax error of a missing closing quote to be caught much earlier, but it has the problem that some old programs will break (which is why character_escapses are off by default).

With character escaping turned on, the escape sequence \' represents the same character as the sequence '' within a quoted atom, namely one single quote. Similarly, with character escaping turned on, the escape sequence \" represents the same character as the sequence "" within a string, namely one double quote.

The escape sequence \c (c for continue) is useful when formatting a string for readability. For example, the atom (A), is equivalent to (B):

                '!Ruth \c (A)
                  Gehrig \c
                  Cobb \c
          '!Ruth Gehrig Cobb Williams!' (B)

The following sequence denotes the integer 9: