Previous: Autostr input/output, Up: Automatic strings


10.13 Escape-sequence characters in automatic strings

Among the operations that are needed for a good human interface in text-oriented programs is a way of embedding `special' characters into user-supplied strings. The traditional Unix and C way of doing this is to reserve a character as an `escape-sequence prefix'; for example, a literal string in C contains zero or more characters, each of which may be either a printable character, or a sequence beginning with a backslash. Libretto provides a parser for similar escape-sequences.

— Function: int astr_esc_char (const Autostr *astr, ssize_t *offset)
— Function: int astr_esc_char_c (const Autostr *astr, ssize_t *offset, int c)

These functions parse an escape sequence at position *offset in astr. They return the character found (or a negative value on error), and increment *offset to index the next character to look at. If the character at position *offset in astr is not the escape sequence prefix, it is returned unchanged, and *offset is of course advanced past that character. The escape sequence prefix is '\\' for ‘astr_esc_char’, and c (converted to ‘char’) for ‘astr_esc_char_c’.

The following sequences are parsed:

\a, \b, \f, \n, \r, \t, \v
These sequences behave as they do in ANSI C.
\D
Represents an ASCII del (octal 0177) character.
\e, \E
Represents an ASCII esc (octal 033) character.
\ddd
ddd is a sequence of no more than three octal digits.
\xx
xx is a sequence of no more than two hexadecimal digits.
\M-char
Represents char with its meta bit (octal 0200) set. char may be either an ordinary character or another escape sequence.
\C-char, \^char
The resulting character is char bitwise-anded with octal 0237 (the meta bit and ASCII control bits). char may be either an ordinary character or another escape sequence. As a special case, if char is a question mark then ascii del (octal 0177, sometimes displayed as ‘^?’) is returned; but note that ‘\C-\M-?’ and ‘\M-\C-?’ are different.
\other
If other is the null character (that is, if the escape prefix is the last character in the string), then the escape prefix itself is returned. Otherwise, other is returned.