This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,3 @@
Show literal specification of characters and strings. If supported, show how verbatim strings (quotes where escape sequences are quoted literally) and here-strings work. Also, discuss which quotes expand variables.
* Related tasks: [[Special characters]], [[Here document]]

View file

@ -0,0 +1,2 @@
---
note: Basic language learning

View file

@ -0,0 +1 @@
CHAR charx = "z";

View file

@ -0,0 +1,6 @@
INT pages=100, lines=25, characters=80;
FILE bookf; FLEX[pages]FLEX[lines]FLEX[characters]CHAR book;
associate(bookf, book);
# following putf inserts the string " Line 4 indented 5" on page 3 #
putf(bookf, $3p"Page 3"4l5x"Line 4 indented 5"$)

View file

@ -0,0 +1,3 @@
[]CHAR charxyz = "xyz";
STRING stringxyz = "xyz";
FORMAT twonewlines = $ll$, threenewpages=$ppp$, fourbackspaces=$bbbb$;

View file

@ -0,0 +1,2 @@
.PR QUOTE .PR
[]'CHAR' CHARXYZ = "XYZ";

View file

@ -0,0 +1 @@
MODE STRING = FLEX[1:0]CHAR;

View file

@ -0,0 +1 @@
BYTES bytesabc = bytes pack("abc");

View file

@ -0,0 +1 @@
STRING stringquote = """I'll be back."" - The Terminator";

View file

@ -0,0 +1,3 @@
STRING linexyz := "line X;" +
"line Y;" +
"line Z;";

View file

@ -0,0 +1,2 @@
FILE linef; STRING line;
associate(linef, line);

View file

@ -0,0 +1,3 @@
FORMAT my_symbol = $"SYMBOL"$;
FORMAT foo = $"prefix_"f(my_symbol)"_suffix"$;
putf(linef ,foo);

View file

@ -0,0 +1,6 @@
c = "x"
str= "hello"
s1 = "abcd" # simple string
s2 = "ab\"cd" # string containing a double quote, escaped with backslash
print s1
print s2

View file

@ -0,0 +1,2 @@
$ awk 'BEGIN{c="x"; s="hello";s1 = "abcd"; s2 = "ab\"cd"; s=s c; print s; print s1; print s2}'
hellox

View file

@ -0,0 +1 @@
ch : character := 'a';

View file

@ -0,0 +1,2 @@
msg : string := "hello world";
empty : string := ""; -- an empty string

View file

@ -0,0 +1,2 @@
integer c;
c = 'z';

View file

@ -0,0 +1,2 @@
text s;
s = "z";

View file

@ -0,0 +1,13 @@
"c" ; character
"text" ; string
hereString = ; with interpolation of %variables%
(
"<>"
the time is %A_Now%
\!
)
hereString2 = ; with same line comments allowed, without interpolation of variables
(Comments %
literal %A_Now% ; no interpolation here
)

View file

@ -0,0 +1,4 @@
10 LET Q$=CHR$(34): REM DOUBLEQUOTES
20 LET D$=Q$+Q$: REM A PAIR OF DOUBLEQUOTES
30 LET S$=Q$+"THIS IS A QUOTED STRING"+Q$
40 PRINT Q$;"HELLO";Q$:REM ADD QUOTES DURING OUTPUT

View file

@ -0,0 +1,5 @@
DIM c AS STRING * 1, s AS STRING
c = "char" 'everything after the first character is silently discarded
s = "string"
PRINT CHR$(34); s; " data "; c; CHR$(34)

View file

@ -0,0 +1,6 @@
10 REM Print some quotes
20 PRINT CHR$(34)
30 REM Print some more doublequotes
40 PRINT """"
50 REM Output the word hello enclosed in doublequotes
60 PRINT """Hello"""

View file

@ -0,0 +1 @@
PRINT "This is a ""quoted string"""

View file

@ -0,0 +1 @@
"gnirts">:#,_@

View file

@ -0,0 +1 @@
char ch = 'z';

View file

@ -0,0 +1 @@
char str[] = "z";

View file

@ -0,0 +1,3 @@
char lines[] = "line 1\n"
"line 2\n"
"line 3\n";

View file

@ -0,0 +1 @@
#define FOO "prefix_"##MY_SYMBOL##"_suffix"

View file

@ -0,0 +1,3 @@
[\h \e \l \l \o] ; a vector of characters
\uXXXX ; where XXXX is some hex Unicode code point
\\ ; the backslash character literal

View file

@ -0,0 +1,6 @@
\space
\newline
\tab
\formfeed
\return
\backspace

View file

@ -0,0 +1 @@
"hello world\r\n"

View file

@ -0,0 +1,3 @@
(let ((colon #\:)
(str "http://www.rosettacode.com/"))
(format t "colon found at position ~d~%" (position colon str)))

View file

@ -0,0 +1 @@
char c = 'a';

View file

@ -0,0 +1,4 @@
auto str = "hello"; // UTF-8
auto str2 = "hello"c; // UTF-8
auto str3 = "hello"w; // UTF-16
auto str4 = "hello"d; // UTF-32

View file

@ -0,0 +1,2 @@
auto str = `"Hello," he said.`;
auto str2 = r"\n is slash-n";

View file

@ -0,0 +1,4 @@
// Any character is allowed after the first quote;
// the string ends with that same character followed
// by a quote.
auto str = q"$"Hello?" he enquired.$";

View file

@ -0,0 +1,5 @@
// If you include a newline, you get a heredoc string:
auto otherStr = q"EOS
This is part of the string.
So is this.
EOS";

View file

@ -0,0 +1,4 @@
// The contents of a token string must be valid code fragments.
auto str = q{int i = 5;};
// The contents here isn't a legal token in D, so it's an error:
auto illegal = q{@?};

View file

@ -0,0 +1,2 @@
// assigns value 'hello' to str
auto str = x"68 65 6c 6c 6f";

View file

@ -0,0 +1,3 @@
const s1 := 'quoted "word" in string';
const s2 := "quoted ""word"" in string"; // sames as s1, shows the doubling of the delimiter
const s2 := 'first line'#13#10'second line'; // CR+LF in the middle

View file

@ -0,0 +1,8 @@
var
lChar: Char;
lLine: string;
lMultiLine: string;
begin
lChar := 'a';
lLine := 'some text';
lMultiLine := 'some text' + #13#10 + 'on two lines';

View file

@ -0,0 +1,4 @@
'T' # character
"The quick brown fox" # string
`The $kind brown fox` # "simple" quasiliteral
term`the($adjectives*, fox)` # "term" quasiliteral

View file

@ -0,0 +1,5 @@
? if ("<abc,def>" =~ `<@a,@b>`) { [a, b] } else { null }
# value: ["abc", "def"]
? if (" >abc, def< " =~ rx`\W*(@a\w+)\W+(@b\w+)\W*`) { [a, b] } else { null }
# value: ["abc", "def"]

View file

@ -0,0 +1,2 @@
"This is a string".
[$T,$h,$i,$s,$ ,$a,$ ,$s,$t,$r,$i,$n,$g,$,,$ ,$t,$o,$o].

View file

@ -0,0 +1 @@
97 == $a. % => true

View file

@ -0,0 +1 @@
"\"The quick brown fox jumps over the lazy dog.\"".

View file

@ -0,0 +1,2 @@
char c emit
s" string" type

View file

@ -0,0 +1,3 @@
: main
[char] c emit
s" string" type ;

View file

@ -0,0 +1,2 @@
'c emit
s\" hello\nthere!"

View file

@ -0,0 +1,8 @@
IsChar('a');
# true
IsString("abc");
# true
IsString('a');
# false
IsChar("a");
# false

View file

@ -0,0 +1 @@
ch := 'z'

View file

@ -0,0 +1,6 @@
ch := 'z' // by default, ch takes type int32
var r rune = 'z' // reflect.TypeOf(r) still returns int32
var b byte = 'z' // reflect.TypeOf(b) returns uint8
b2 := byte('z') // equivalent to b
const c byte = 'z' // c is now a "typed constant"
b3 := c // equivalent to b

View file

@ -0,0 +1 @@
str := "z"

View file

@ -0,0 +1 @@
str := "日本語"

View file

@ -0,0 +1 @@
`\n` == "\\n"

View file

@ -0,0 +1,2 @@
`abc
def` == "abc\ndef"

View file

@ -0,0 +1 @@
def string = 'Able was I'

View file

@ -0,0 +1,6 @@
def gString = "${string} ere I saw Elba"
println gString
//Outputs:
//Able was I ere I saw Elba

View file

@ -0,0 +1,5 @@
def gString2 = "1 + 1 = ${1 + 1}"
assert gString2 == '1 + 1 = 2'
def gString3 = "1 + 1 = \${1 + 1}"
assert gString3 == '1 + 1 = ${1 + 1}'

View file

@ -0,0 +1,20 @@
def multiLineString = '''
A man
A plan
A canal
'''
def multiLineGString = """
${multiLineString.trim()}:
Panama!
"""
println multiLineGString
//Outputs:
//
//A man
//A plan
//A canal:
//Panama!
//

View file

@ -0,0 +1,3 @@
def regexString = /(\[[Tt]itle\]|\[[Ss]ubject\])${10 * 5}/
assert regexString == '(\\[[Tt]itle\\]|\\[[Ss]ubject\\])50'

View file

@ -0,0 +1,8 @@
assert 'a' instanceof String
assert ('a' as char) instanceof Character
assert ((char)'a') instanceof Character
char x = 'a'
assert x instanceof Character
Character y = 'b'
assert y instanceof Character && (x+1 == y)

View file

@ -0,0 +1,2 @@
def quote = "\""
def apostrophe = '\''

View file

@ -0,0 +1,4 @@
def quote2 = '"'
def apostrophe2 = "'"
assert quote == quote2
assert apostrophe == apostrophe2

View file

@ -0,0 +1,5 @@
"abcdef" == "abc\
\def"
"abc\ndef" == "abc\n\
\def"

View file

@ -0,0 +1,4 @@
CHARACTER c1='A', c2="B", c3=&C&
CHARACTER str1='single quotes', str2="double quotes", str3*100
str3 = % delimit "Nested 'strings' " if needed %

View file

@ -0,0 +1 @@
str3 = 'a string' // CHAR(0) // "may contain" // $CRLF // ~ any character ~

View file

@ -0,0 +1,4 @@
$TAB == CHAR(9) ! evaluates to 1 (true)
$LF == CHAR(10)
$CR == CHAR(13)
$CRLF == CHAR(13) // CHAR(10) ! concatenation

View file

@ -0,0 +1,2 @@
a = " that's a string "
b = ' a "string" is this '

View file

@ -0,0 +1 @@
a = " that's a string

View file

@ -0,0 +1,3 @@
a = ' that<nowiki>''</nowiki>s a string
print,a
;==> that's a string

View file

@ -0,0 +1,4 @@
b = 'hello'
a = b+' world
print,a
;==> hello world

View file

@ -0,0 +1,6 @@
print,'777'x
;==> 1911
print,'777'o
;==> 511
print,'777'
;==> 777

View file

@ -0,0 +1,4 @@
print,"777
;==> 511
print,"877
;==> 877

View file

@ -0,0 +1,2 @@
a = "0"
;==> Syntax error.

View file

@ -0,0 +1 @@
crlf = string([13b,10b])

View file

@ -0,0 +1,14 @@
procedure main()
# strings are variable length are not NUL terminated
# at this time there is no support for unicode or multi-byte charactersets
c1 := 'aaab' # not a string - cset
s1 := "aaab" # string
s2 := "\"aaab\b\d\e\f\n\l\n\r\t\v\'\"\\\000\x00\^c" # with escapes and imbedded zero
# no native variable substitution, a printf library function is available in the IPL
every x := c1|s1|s2 do # show them
write(" size=",*x,", type=", type(x),", value=", image(x))
end

View file

@ -0,0 +1,3 @@
Home is a room. The description is "This is where you live...
...with your [number of animals in Home] pet[s]."

View file

@ -0,0 +1 @@
"'That's nice,' said the captain."

View file

@ -0,0 +1,3 @@
'x' NB. Scalar character
'string' NB. List of characters, i.e. a string
'can''t get simpler' NB. Embedded single-quote

View file

@ -0,0 +1,7 @@
'Here is line 1',LF,'and line two'
'On a mac, you need',CR,'a carriage return'
'And on windows, ',CRLF,'you need both'
TAB,TAB,TAB,'Everyone loves tabs!'

View file

@ -0,0 +1,4 @@
CR =: 13 { a.
LF =: 10 { a.
CRLF =: CR,LF NB. Or just 10 13 { a.
TAB =: 9 { a.

View file

@ -0,0 +1,2 @@
NAME =: 'John Q. Public'
'Hello, ',NAME,' you may have already won $1,000,000'

View file

@ -0,0 +1,9 @@
template =: noun define
Hello, NAME.
My name is SHYSTER, and I'm here to tell
you that you my have already won $AMOUNT!!
To collect your winnings, please send $PAYMENT
to ADDRESS.
)

View file

@ -0,0 +1,12 @@
load 'strings'
name =: 'John Q. Public'
shyster =: 'Ed McMahon'
amount =: 1e6
payment =: 2 * amount
address =: 'Publisher''s Clearing House'
targets =: ;: 'NAME SHYSTER AMOUNT PAYMENT ADDRESS'
sources =: ":&.> name;shyster;amount;payment;address
message =: template rplc targets,.sources

View file

@ -0,0 +1,5 @@
load 'printf'
'This should look %d%% familiar \nto programmers of %s.' sprintf 99;'C'
This should look 99% familiar
to programmers of C.

View file

@ -0,0 +1,6 @@
char a = 'a'; // prints as: a
String b = "abc"; // prints as: abc
char doubleQuote = '"'; // prints as: "
char singleQuote = '\''; // prints as: '
String singleQuotes = "''"; // prints as: ''
String doubleQuotes = "\"\""; // prints as: ""

View file

@ -0,0 +1 @@
`a' is for ``apple'''''''

View file

@ -0,0 +1,9 @@
'Liberty BASIC does not support escape characters within literal strings.
print "Quotation mark:"
print chr$(34)
print
'Print literal string
print "Hello, World."
'Print literal string displaying quotation marks.
print chr$(34);"Hello, World.";chr$(34)

View file

@ -0,0 +1,7 @@
c1 := 'a';
c2 := '\n'; // newline
c3 := '\''; // quote
c4 := '\101o'; // octal
c5 := '\10\'; // decimal
c6 := '\0Ah\'; // hexadecimal
c7 := '\10010110b\'; // binary

View file

@ -0,0 +1,4 @@
s1 := "this is a\nsample"; // newline
s2 := "\""; // double quote
s3 := "abc\
\xyz"; // "abcxyz", cut the gap

View file

@ -0,0 +1,2 @@
print "Hello\,\ world
print "|Hello, world|

View file

@ -0,0 +1,5 @@
singlequotestring = 'can contain "double quotes"'
doublequotestring = "can contain 'single quotes'"
longstring = [[can contain
newlines]]
longstring2 = [==[ can contain [[ other ]=] longstring " and ' string [===[ qualifiers]==]

View file

@ -0,0 +1 @@
`this is quoted string'

View file

@ -0,0 +1,2 @@
changequote(`[',`]')dnl
[this is a quoted string]

View file

@ -0,0 +1,2 @@
s1 = 'abcd' % simple string
s2 = 'ab''cd' % string containing a single quote

View file

@ -0,0 +1,11 @@
MCSKIP "WITH" NL
"" Literals/String
MCINS %.
MCSKIP MT,<>
"" Demonstration of literal string
MCDEF Bob AS Alice
"" The following two lines both mention Bob. The first line is
"" evaluated, but the second is surrounded by literal brackets and is not
"" evaluated.
This is the first mention of Bob
<and here we mention Bob again>

View file

@ -0,0 +1,2 @@
This is the first mention of Alice
and here we mention Bob again

View file

@ -0,0 +1,9 @@
> "foobar";
"foobar"
> "foo\nbar"; # string with a newline
"foo
bar"
> "c"; # a character
"c"

View file

@ -0,0 +1,2 @@
> "foo" "bar";
"foobar"

Some files were not shown because too many files have changed in this diff Show more