Skip to content(if available)orjump to list(if available)

ASCII Lookup Utility in Ada

ASCII Lookup Utility in Ada

7 comments

·April 12, 2025

illo

Standard ML program. I fear it does not contribute much to the conversation but I cannot abstain from calling attention to my favourite pragmatic programming language that is presently on what seems to be a neverending life support.

  structure I = Int
  structure S = String
  structure SU = Substring
  structure SC = StringCvt
  
  infix 5 >>
  fun (x >> y) f = if x = y then () else (f x; ((x + 1) >> y) f)
  
  val printRow =
  let
      val p0 = SC.padLeft #"0" val ps = SC.padLeft #" "
      val td = I.fmt SC.DEC val tx = I.fmt SC.HEX
      val to = I.fmt SC.OCT val tb = I.fmt SC.BIN
      val desc = fn
            0 => "NUL" | 1 => "SOH" | 2 => "STX" | 3 => "ETX" | 4 => "EOT" | 5 => "ENQ"
          | 6 => "ACK" | 7 => "BEL" | 8 => "BS" | 9 => "HT" | 10 => "LF" | 11 => "VT"
          | 12 => "FF" | 13 => "CR" | 14 => "SO" | 15 => "SI" | 16 => "DLE" | 17 => "DC1"
          | 18 => "DC2" | 19 => "DC3" | 20 => "DC4" | 21 => "NAK" | 22 => "SYN" | 23 => "ETB"
          | 24 => "CAN" | 25 => "EM" | 26 => "SUB" | 27 => "ESC" | 28 => "FS" | 29 => "GS"
          | 30 => "RS" | 31 => "US" | 32 => "SPC" | 127 => "DEL" | n => str (chr n)
  in
      fn n => print (S.concatWith " " [ps 3 (td n), p0 2 (tx n), p0 7 (tb n), p0 3 (to n), desc n] ^ "\n")
  end
  
  val spec =
  let
      val scan = fn rad => fn sl => SC.scanString (I.scan rad) (implode sl)
  in
      fn str => valOf (case explode str of
        #"0" :: #"x" :: hex => scan SC.HEX hex
      | #"0" :: #"o" :: oct => scan SC.OCT oct
      | #"0" :: #"b" :: bin => scan SC.BIN bin
      | dec => scan SC.DEC dec)
  end
  
  val () = case CommandLine.arguments () of
        [] => (0 >> 128) printRow
      | [req] => printRow (spec req)
      | _ => raise Fail "arguments"

kqr

> A full-scale Unicode lookup utility would be great, but I haven't found one, or even looked very hard for one.

Emacs! There's probably a quicker way but interactive insert-char takes codepoint numbers and then describe-char gets all sorts of useful information.

kazinator

TXR Lisp program:

  (defvarl ch-name (relate (rlist 0..32 127)
                           '#"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 SPC DEL"
                           nil))

  (defun print-row (ch)
    (format t "~,3d ~,02x ~,07b ~,03o ~a\n"
            ch ch ch ch (or [ch-name ch] (chr-num ch))))

  (match-case *args*
    (@(require @(or @(with (`0x@{dig #/[\dA-Fa-f]+/}`) base 16)
                    @(with (`0b@{dig #/[01]+/}`) base 2)
                    @(with (`0o@{dig #/[0-7]+/}`) base 8)
                    @(with (`@{dig #/\d+/}`) base 10))
               (let ((val (toint dig base)))
                 (if (<= 0 val 127)
                   (print-row val)))))
    (() [mapdo print-row 0..128])
    (@else (put-line "error in argument") (exit nil)))

kazinator

No need to use a word list literal in definition of ch-name; symbols will do:

  (defvarl ch-name (relate (rlist 0..32 127)
                           '(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 SPC DEL)
                           nil))

Mountain_Skies

Happy to see Ada still gets some interest outside of MIC. When I went off to university, I was a Perl snob but my school used Ada as its primary teaching language. It was a great way of expanding my knowledge across the horizon to understand the value in strong typing.

After nearly half a century, I still don't have ASCII memorized either but usually just use asciitable.com to do a quick lookup. Having a system utility like this one would be handy in situation without internet access. Plus like he noted, it was a good way to learn more about Ada.

bmn__

Obligatory Perl snobbery: Perl wins the day by being shorter and better output and already installed on your computer.

    perl -C -M5.032 -MUnicode::UCD=charprop -e \
    '# https://www.unicode.org/Public/MAPPINGS/VENDORS/MISC/IBMGRAPH.TXT
    my @cp437 = qw( 
    0 263a 263b 2665 2666 2663 2660 2022 25d8 25cb 25d9 2642 2640 266a 266b 263c
    25ba 25c4 2195 203c b6 a7 25ac 21a8 2191 2193 2192 2190 221f 2194 25b2 25bc
    );
    printf "%3u  %02x  %07b  %3o  %s  %s  %s\n",
    ($_) x 4,
    $_ == 127 ? chr(0x2421) : $_ <= 32 ? chr(0x2400+$_) : chr,
    $_ == 127 ? chr(0x2302) : 0 < $_ < 32 ? chr hex $cp437[$_] : chr,
    charprop($_, "Name") || charprop($_, "NameAlias") =~ s/: \w+//gr
    for @ARGV ? oct $ARGV[0] : 0..127'

ayastrebov2

man ascii