Literals/String: Difference between revisions

Add S-Lang
(→‎{{header|Inform 7}}: Describe linebreak and whitespace constraints)
(Add S-Lang)
Line 2,263:
This will not interpolate: #{a}
NON_INTERPOLATING</lang>
 
=={{header|S-lang}}==
S-Lang supports character literals with single-quote apostrophe. These are normally limited to byte sized 0 thru 255 (ASCII, extended into 8 bits).
Wide (unicode) character literals can be introduced with apostrophe <code>'\x{Hex}'</code>, for example <code>'\x{1D7BC}'</code> for math symbol sans-serif bold italic small sigma. Character literals are treated as Integer_Type data.
 
Double-quotes are used for strings, but these may not include literal newlines or NUL bytes without using backslash escapes.
S-Lang 2.2 introduced verbatim strings using backtick. These may include newline literals, but other platform concerns can make NUL byte literals in the source code a tricky business.
Both double-quote and back-quote allow a suffix:
 
* R for no backslash escapes (default for back-quoted strings)
* Q to force backslash escaping (default for double-quoted strings)
* B to produce a binary string, BString_Type
* $ to request dollar prefix variable substitutions within the given string.
 
<lang C>% String literals
variable c, ch, s, b, r, v;
 
c = 'A';
ch = '\x{1d7bc}';
 
printf("Double quotes\n");
s = "this is a single line string with\t\tbackslash substitutions";
() = fputs(s, stdout);
() = fputs("\n", stdout);
 
s = "this is a single line string without\t\tbackslash substitutions"R;
() = fputs(s, stdout);
() = fputs("\n", stdout);
 
s = "string with backslash escaped newline \
takes up two lines in source, but no newline is in the string";
() = fputs(s, stdout);
() = fputs("\n", stdout);
 
printf("\nBack quotes\n");
r = `this is a multi line string with
backslash substitutions and \t(tabs)\t`Q;
() = fputs(r, stdout);
() = fputs("\n", stdout);
 
r = `this is a multi line string without
backslash substitutions and \t(tabs)\t`;
() = fputs(r, stdout);
() = fputs("\n", stdout);
 
printf("\nvariable substitution\n");
v = "variable substitution with $$c as $c"$;
() = fputs(v, stdout);
() = fputs("\n", stdout);
 
v = "no variable substitutions, $$c as $c";
() = fputs(v, stdout);
() = fputs("\n", stdout);
 
printf("\nBString_Type\n");
b = "this is a binary string, NUL \0 \0 bytes allowed"B;
print(b);
% display of b will be stopped at the NUL byte using stdio streams
() = fputs(b, stdout);
() = fputs("\n", stdout);
printf("strlen(b) is %d, bstrlen(b) is %d\n", strlen(b), bstrlen(b));</lang>
 
{{out}}
<pre>
prompt$ slsh strlit.sl
Double quotes
this is a single line string with backslash substitutions
this is a single line string without\t\tbackslash substitutions
string with backslash escaped newline takes up two lines in source, but no newline is in the string
 
Back quotes
this is a multi line string with
backslash substitutions and (tabs)
this is a multi line string without
backslash substitutions and \t(tabs)\t
 
variable substitution
variable substitution with $c as 65
no variable substitutions, $$c as $c
 
BString_Type
"this is a binary string, NUL \000 \000 bytes allowed"
this is a binary string, NUL
strlen(b) is 29, bstrlen(b) is 46</pre>
 
With substitutions, if a literal dollar sign or backquote is required, the character needs to be doubled. "$$c" is the literal "$c" when using dollar suffixed strings.
 
Backslash escapes include:
 
* <code>\"</code> -- double quote
* <code>\'</code> -- single quote
* <code>\\</code> -- backslash
* <code>\0</code> -- NUL byte
* <code>\a</code> -- bell character (ASCII 7)
* <code>\t</code> -- tab character (ASCII 9)
* <code>\n</code> -- newline character (ASCII 10)
* <code>\e</code> -- escape character (ASCII 27)
* <code>\xhh</code> -- byte expressed in HEXADECIMAL notation
* <code>\ooo</code> -- byte expressed in OCTAL notation
* <code>\dnnn</code> -- byte expressed in DECIMAL
* <code>\u{h..h}</code> -- the Unicode character U+h..h
* <code>\x{h..h}</code> -- the Unicode character U+h..h [modal]
 
=={{header|Scala}}==
Anonymous user