ASCII control characters: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added FreeBasic)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(14 intermediate revisions by 10 users not shown)
Line 1:
{{draft task}}
ASCII is the American Standard Code for Information Interchange. There are 128 ASCII characters.
ASCII is the American Standard Code for Information Interchange. There are 128 ASCII characters of which the first 32 and the last are 'control characters'.
<br>
 
;Task
Show how your language might treat control characters ''en bloc'' by using an enum, an enum like structure or other approach to associate their names with their ASCII values thereby enabling them to be retrieved or listed by name.
 
Technically, the 33rd character 'space' is a printable character, not a control character, though you may treat it as the latter for the purposes of this task.
<br>
;Reference
* [[wp:ASCII|Wikipedia: ASCII]]
<br>
 
=={{header|ALGOL 68}}==
Algol 68 doesn't have ENUMs but it is easy to create constants, this example shows how a facility similar to Go's iota can be implemented.
<br>Note space is a standard Algol 68 transput (I/O) routine, so spc is used for the spacce character.
<syntaxhighlight lang="algol68">
# create constants for the ASCII control characters (0-127) #
INT char value := -1;
# increments and returns the next value for a character #
PROC next char = CHAR: REPR ( char value +:= 1 );
CHAR nul = next char;
CHAR soh = next char;
CHAR stx = next char;
CHAR etx = next char;
CHAR eot = next char;
CHAR enq = next char;
CHAR ack = next char;
CHAR bel = next char;
CHAR bs = next char;
CHAR ht = next char;
CHAR lf = next char;
CHAR vt = next char;
CHAR ff = next char;
CHAR cr = next char;
CHAR so = next char;
CHAR si = next char;
CHAR dle = next char;
CHAR dc1 = next char;
CHAR dc2 = next char;
CHAR dc3 = next char;
CHAR dc4 = next char;
CHAR nak = next char;
CHAR syn = next char;
CHAR etb = next char;
CHAR can = next char;
CHAR em = next char;
CHAR sub = next char;
CHAR esc = next char;
CHAR fs = next char;
CHAR gs = next char;
CHAR rs = next char;
CHAR us = next char;
CHAR spc = " "; # using spc as space is a standard transput procedure #
CHAR del = REPR 127;
 
# e.g.: #
# print( ( nul, soh, ht, lf ) ); prints the characters themselve #
# print( ( ABS nul, ABS soh, ABS ht, ABS lf, lf ) ); prints the characters as #
# integers: +0 +1 +9 +10 #
</syntaxhighlight>
 
=={{Header|C}}==
<syntaxhighlight lang="C">
Line 90 ⟶ 151:
7F</pre>
 
=={{Headerheader|perlGo}}==
Go's support for enums is unconventional in that they are basically a bunch of constants with which a type name may be associated.
 
The pre-declared identifier 'iota' is typically used with enums and represents successive untyped integer constants, starting from zero though (as here) the sequence may be interrupted by assigning a new value.
<syntaxhighlight lang="go">package main
 
import "fmt"
 
type Ctrl int
 
const (
nul Ctrl = iota
soh
stx
etx
eot
enq
ack
bel
bs
ht
lf
vt
ff
cr
so
si
dle
dc1
dc2
dc3
dc4
nak
syn
etb
can
em
sub
esc
fs
gs
rs
us
space
del = 127
)
 
func main() {
// print some specimen values
fmt.Println(cr)
fmt.Println(del)
}</syntaxhighlight>
 
{{out}}
<pre>
13
127
</pre>
 
=={{header|jq}}==
<syntaxhighlight lang="jq">
def ascii_control_character_names: [
"nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
"bs", "ht", "lf", "vt", "ff", "cr", "so", "si",
"dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
"can", "em", "sub", "esc", "fs", "gs", "rs", "us",
"space", "del"
];
 
def Ctrl:
ascii_control_character_names as $a
| reduce range(0; $a|length) as $i ({}; .[$a[$i]] = $i)
| .["del"] = 127;
 
def examples:
Ctrl as $Ctrl
| "Ctrl.cr => \($Ctrl.cr)",
"Ctrl.del => \($Ctrl.del)",
"Ctrl.space => \($Ctrl.space)";
 
examples
</syntaxhighlight>
{{Output}}
<pre>
Ctrl.cr => 13
Ctrl.del => 127
Ctrl.space => 32
</pre>
 
=={{header|Julia}}==
Note that Julia recognizes many non-ASCII characters with values above 127 as control Char as well.
<syntaxhighlight lang="julia">
julia> filter(iscntrl, Char(0):Char(127))
33-element Vector{Char}:
'\0': ASCII/Unicode U+0000 (category Cc: Other, control)
'\x01': ASCII/Unicode U+0001 (category Cc: Other, control)
'\x02': ASCII/Unicode U+0002 (category Cc: Other, control)
'\x03': ASCII/Unicode U+0003 (category Cc: Other, control)
'\x04': ASCII/Unicode U+0004 (category Cc: Other, control)
'\x05': ASCII/Unicode U+0005 (category Cc: Other, control)
'\x06': ASCII/Unicode U+0006 (category Cc: Other, control)
'\a': ASCII/Unicode U+0007 (category Cc: Other, control)
'\b': ASCII/Unicode U+0008 (category Cc: Other, control)
'\t': ASCII/Unicode U+0009 (category Cc: Other, control)
'\n': ASCII/Unicode U+000A (category Cc: Other, control)
'\v': ASCII/Unicode U+000B (category Cc: Other, control)
'\f': ASCII/Unicode U+000C (category Cc: Other, control)
'\r': ASCII/Unicode U+000D (category Cc: Other, control)
'\x0e': ASCII/Unicode U+000E (category Cc: Other, control)
'\x0f': ASCII/Unicode U+000F (category Cc: Other, control)
'\x10': ASCII/Unicode U+0010 (category Cc: Other, control)
'\x11': ASCII/Unicode U+0011 (category Cc: Other, control)
'\x12': ASCII/Unicode U+0012 (category Cc: Other, control)
'\x13': ASCII/Unicode U+0013 (category Cc: Other, control)
'\x14': ASCII/Unicode U+0014 (category Cc: Other, control)
'\x15': ASCII/Unicode U+0015 (category Cc: Other, control)
'\x16': ASCII/Unicode U+0016 (category Cc: Other, control)
'\x17': ASCII/Unicode U+0017 (category Cc: Other, control)
'\x18': ASCII/Unicode U+0018 (category Cc: Other, control)
'\x19': ASCII/Unicode U+0019 (category Cc: Other, control)
'\x1a': ASCII/Unicode U+001A (category Cc: Other, control)
'\e': ASCII/Unicode U+001B (category Cc: Other, control)
'\x1c': ASCII/Unicode U+001C (category Cc: Other, control)
'\x1d': ASCII/Unicode U+001D (category Cc: Other, control)
'\x1e': ASCII/Unicode U+001E (category Cc: Other, control)
'\x1f': ASCII/Unicode U+001F (category Cc: Other, control)
'\x7f': ASCII/Unicode U+007F (category Cc: Other, control)
</syntaxhighlight>
An enum can be used for the task:
<syntaxhighlight lang="julia">@enum Control begin
nul = 0
soh = 1
stx = 2
etx = 3
eot = 4
enq = 5
ack = 6
bel = 7
bs = 8
ht = 9
lf = 10
vt = 11
ff = 12
cr = 13
so = 14
si = 15
dle = 16
dc1 = 17
dc2 = 18
dc3 = 19
dc4 = 20
nak = 21
syn = 22
etb = 23
can = 24
em = 25
sub = 26
esc = 27
fs = 28
gs = 29
rs = 30
us = 31
del = 127
end
display(nul), display(ht), display(us), display(del)
</syntaxhighlight>
A named tuple is a another way to reference such control Chars by name:
<syntaxhighlight lang="julia">const CNTRL = (
nul = 0,
soh = 1,
stx = 2,
etx = 3,
eot = 4,
enq = 5,
ack = 6,
bel = 7,
bs = 8,
ht = 9,
lf = 10,
vt = 11,
ff = 12,
cr = 13,
so = 14,
si = 15,
dle = 16,
dc1 = 17,
dc2 = 18,
dc3 = 19,
dc4 = 20,
nak = 21,
syn = 22,
etb = 23,
can = 24,
em = 25,
sub = 26,
esc = 27,
fs = 28,
gs = 29,
rs = 30,
us = 31,
del = 127,
)
@show CNTRL.nul, CNTRL.ht, CNTRL.us, CNTRL.del
</syntaxhighlight>{{out}}
<pre>
nul::Control = 0
ht::Control = 9
us::Control = 31
del::Control = 127
(CNTRL.nul, CNTRL.ht, CNTRL.us, CNTRL.del) = (0, 9, 31, 127)
</pre>
 
=={{header|Maxima}}==
Those character can be put in a list and can be found their ASCII numbers.
<syntaxhighlight lang="maxima">
/* List of characters that are not constituents (constituent is a graphic character but not a space character) */
block(makelist(ascii(i),i,0,127),sublist(%%,lambda([x],not constituent(x))));
/* [,"�","�","�","�","�","�","�","�","�","
","�","�","
","�","�","�","�","�","�","�","�","�","�","�","�","�","�","�","�","�","�"," ","�"] */
 
/* Ascii numbers of characters that are not constituents */
map(cint,%);
/* [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,127] */
</syntaxhighlight>
 
=={{header|Nim}}==
We define an enumeration type.
 
For each element, Nim allows to specify its integer value and its string representation. Here, the integer value is implicit (starting from zero) except for DEL whose value must be specified. As we use the standard names for the elements, we don’t have to specify the string representation.
 
The integer value is obtained with function <code>ord</code> and the string representation with function<code>$</code>.
 
<syntaxhighlight lang="Nim">import std/strutils
 
type AsciiControlChar {.pure.} = enum NUL, SOH, STX, ETX, EOT, ENQ, ACK, BEL,
BS, HT, LF, VT, FF, CR, SO, SI,
DLE, DC1, DC2, DC3, DC4, NAK, SYN, ETB,
CAN, EM, SUB, ESC, FS, GS, RS, US,
SP, DEL = 0x7F
 
echo ' ', CR, " = ", ord(CR).toHex(2)
echo DEL, " = ", ord(DEL).toHex(2)
</syntaxhighlight>
 
{{out}}
<pre> CR = 0D
DEL = 7F
</pre>
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">
use charnames ":loose";
Line 100 ⟶ 411:
charnames::string_vianame $_;
</syntaxhighlight>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">enum</span> <span style="color: #000000;">nul</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">soh</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">stx</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">etx</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">eot</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">enq</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ack</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bel</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bs</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ht</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">lf</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">vt</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ff</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cr</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">so</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">si</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">dle</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">dc1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">dc2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">dc3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">dc4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">nak</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">syn</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">etb</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">can</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">em</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">sub</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">esc</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">fs</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">gs</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rs</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">us</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">space</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">del</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">127</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">cr</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">del</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
13
127
</pre>
=={{header|Raku}}==
There doesn't seem to really be a point or a purpose to this task other than creating a enumeration...
 
'space' is absolutely '''NOT''' a control character. Pretending it is is just completely incorrect.
 
<syntaxhighlight lang="raku" line>enum C0 (|(^32).map({ (0x2400 + $_).chr => $_ }), '␡' => 127);
 
printf "Ord: %3d, Unicode: %s, Enum: %s\n", $_, .uniname, C0($_)
for (^128).grep: {.chr ~~ /<:Cc>/}</syntaxhighlight>
{{out}}
<pre style="font-size:140%;">Ord: 0, Unicode: <control-0000>, Enum: ␀
Ord: 1, Unicode: <control-0001>, Enum: ␁
Ord: 2, Unicode: <control-0002>, Enum: ␂
Ord: 3, Unicode: <control-0003>, Enum: ␃
Ord: 4, Unicode: <control-0004>, Enum: ␄
Ord: 5, Unicode: <control-0005>, Enum: ␅
Ord: 6, Unicode: <control-0006>, Enum: ␆
Ord: 7, Unicode: <control-0007>, Enum: ␇
Ord: 8, Unicode: <control-0008>, Enum: ␈
Ord: 9, Unicode: <control-0009>, Enum: ␉
Ord: 10, Unicode: <control-000A>, Enum: ␊
Ord: 11, Unicode: <control-000B>, Enum: ␋
Ord: 12, Unicode: <control-000C>, Enum: ␌
Ord: 13, Unicode: <control-000D>, Enum: ␍
Ord: 14, Unicode: <control-000E>, Enum: ␎
Ord: 15, Unicode: <control-000F>, Enum: ␏
Ord: 16, Unicode: <control-0010>, Enum: ␐
Ord: 17, Unicode: <control-0011>, Enum: ␑
Ord: 18, Unicode: <control-0012>, Enum: ␒
Ord: 19, Unicode: <control-0013>, Enum: ␓
Ord: 20, Unicode: <control-0014>, Enum: ␔
Ord: 21, Unicode: <control-0015>, Enum: ␕
Ord: 22, Unicode: <control-0016>, Enum: ␖
Ord: 23, Unicode: <control-0017>, Enum: ␗
Ord: 24, Unicode: <control-0018>, Enum: ␘
Ord: 25, Unicode: <control-0019>, Enum: ␙
Ord: 26, Unicode: <control-001A>, Enum: ␚
Ord: 27, Unicode: <control-001B>, Enum: ␛
Ord: 28, Unicode: <control-001C>, Enum: ␜
Ord: 29, Unicode: <control-001D>, Enum: ␝
Ord: 30, Unicode: <control-001E>, Enum: ␞
Ord: 31, Unicode: <control-001F>, Enum: ␟
Ord: 127, Unicode: <control-007F>, Enum: ␡
</pre>
 
=={{header|RPL}}==
There is no enum in RPL, but we can create global variables, each containing a specific control character, which are then available to all programs in the same directory tree.
≪ { NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US }
0 31 '''FOR''' ctrl
ctrl CHR OVER ctrl GET STO
'''NEXT''' DROP
127 CHR 'DEL' STO
≫ '<span style="color:blue">→ASCII</span>' STO
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="Rust">
enum Ctrl {
nul = 0
soh
stx
etx
eot
enq
ack
bel
bs
ht
lf
vt
ff
cr
so
si
dle
dc1
dc2
dc3
dc4
nak
syn
etb
can
em
sub
esc
fs
gs
rs
us
space
del = 127
}
 
fn main() {
println(int(Ctrl.cr))
println(int(Ctrl.del))
}
</syntaxhighlight>
 
{{out}}
<pre>
13
127
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-dynamic}}
I assume this isn't intended to be a task but simply a reference to how individual languages might treat ASCII control characters ''en bloc'' using enum like structures or otherwise. Note that technically 'space' is a printable character, not a control character.
 
Wren doesn't have enums built into the language but can create them dynamically at runtime. However, such enums need to have consecutive integer values.
 
Here, we create instead a ''Group'' which can contain any values in any order.
<syntaxhighlight lang="ecmascriptwren">import "./dynamic" for Group
 
var names = [
9,476

edits