13.17Escape characters. 4tH 4tH Forth compiler
تبلیغات
تبلیغات
CHAPTER 13. SPECIAL LIBRARIES
13.17
Escape characters
226
Sometimes you need a string with embedded control characters. You could try to patch them into your sourcecode, but that is usually not a good idea. You could also patch them into a variable, but that is cumbersome. 4tH offers a word that will allow you to use the control characters listed in table 13.4.
E
SCAPE CHARACTER
M
EANING
\f
\l
\n
\q
\a
\b
\e
\r
\t
\v
\z
Bell
Backspace
Escape
Formfeed
Linefeed
Linefeed
Quote (”)
Carriage return
Tab
Vertical tab
Null character
Table 13.4: Supported control characters
Using this facility is quite simple: include lib/escape.4th
s" \tThis is the \qC:\\4tH\q directory” s>escape type cr
Which will print:
This is the "C:\4tH" directory
Note that other escaped characters are printed ”as is”, like the backslash in this example.
If you need even more flexibility, we have another card up our sleeve. The only catch is you’ll have to know the ASCII code of the characters you want to insert, either in binary, octal, decimal or hex: include lib/embed.4th
s" %1001This is the&42C:\4tH#34$20directory" s>embed type cr
Which will also print:
This is the "C:\4tH" directory
• Every number prefixed with a ’%’ character is interpreted as a binary ASCII code;
• Every number prefixed with ’&’ character is interpreted as an octal ASCII code;
• Every number prefixed with a ’#’ character is interpreted as a decimal ASCII code;
• Every number prefixed with a ’$’ character is interpreted as a hexadecimal ASCII code.
How much flexibility do you need? Of course, all this flexibility comes at a price. The strings are not expanded at compile-time but at run-time, so it will cost you a little more space and a little more time compared to other string literals.
دانلود
تبلیغات