ASCII control characters: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{Header|Perl}}: Spell language name correctly. perl is the interpreter. Perl is the language.)
Line 208: Line 208:
13
13
127
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>
</pre>



Revision as of 03:58, 9 April 2023

ASCII control characters is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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'.

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.

Reference


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.
Note space is a standard Algol 68 transput (I/O) routine, so spc is used for the spacce character.

    # 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 #

C

enum: char {
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 = 127
};

D

import std.ascii.ControlChar;

FreeBASIC

Enum AsciiControlChar
    NUL = &H00  'Null
    SOH = &H01  'Star of Header
    STX = &H02  'Start of Text
    ETX = &H03  'End of Text
    EOT = &H04  'End of Transmission
    ENQ = &H05  'Enquiry
    ACK = &H06  'Acknowledge
    BEL = &H07  'Bell
    BS = &H08   'BackSpace
    HT = &H09   'Horizontal Tabulation
    LF = &H0A   'Line Feed
    VT = &H0B   'Vertical Tabulation
    FF = &H0C   'Form Feed
    CR = &H0D   'Carriage Return
    SO = &H0E   'Shift Out
    SI = &H0F   'Shift In
    DLE = &H10  'Data Link Escape
    DC1 = &H11  'Device Control 1 (XON)
    DC2 = &H12  'Device Control 2
    DC3 = &H13  'Device Control 3 (XOFF)
    DC4 = &H14  'Device Control 4
    NAK = &H15  'Negative acknowledge
    SYN = &H16  'Synchronous Idle
    ETB = &H17  'End of Transmission Block
    CAN = &H18  'Cancel
    EM = &H19   'End of Medium
    SUB_ = &H1A 'Substitute
    ESC = &H1B  'Escape
    FS = &H1C   'File Separator
    GS = &H1D   'Group Separator
    RS = &H1E   'Record Separator
    US = &H1F   'Unit Separator
    SP = &H20   'Space
    DEL = &H7F  'Delete
End Enum

Print(Hex(AsciiControlChar.CR))
Print(Hex(AsciiControlChar.DEL))
Sleep
Output:
 D
7F

Go

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.

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)
}
Output:
13
127

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
Output:
Ctrl.cr => 13
Ctrl.del => 127
Ctrl.space => 32

Perl

use charnames ":loose";
# There is no EM, use END OF MEDIUM.
# Do not confuse BEL with BELL. Starting in Perl 5.18, BELL refers to unicode emoji 0x1F514. ALERT is an alias for BEL.
# compile time literal
"\N{nul}\N{soh}\N{stx}\N{etx}\N{eot}\N{enq}\N{ack}\N{bel}\N{bs}\N{ht}\N{lf}\N{vt}\N{ff}\N{cr}\N{so}\N{si}\N{dle}\N{dc1}\N{dc2}\N{dc3}\N{dc4}\N{nak}\N{syn}\N{etb}\N{can}\N{end of medium}\N{sub}\N{esc}\N{fs}\N{gs}\N{rs}\N{us}\N{space}\N{delete}"
# run time
charnames::string_vianame $_;

Phix

enum 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
?cr
?del
Output:
13
127

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.

enum C0 (|(^32).map({ (0x2400 + $_).chr => $_ }), '␡' => 127);

printf "Ord: %3d, Unicode: %s, Enum: %s\n", $_, .uniname, C0($_)
   for (^128).grep: {.chr ~~ /<:Cc>/}
Output:
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: ␡

Wren

Library: Wren-dynamic

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.

import "./dynamic" for Group

var 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"
]

var values = (0..32).toList + [127]

var Ctrl = Group.create("Ctrl", names, values)

// print some specimen values
System.print(Ctrl.cr)
System.print(Ctrl.del)
Output:
13
127