| 1 |
# ord.awk --- do ord and chr |
| 2 |
|
| 3 |
# Global identifiers: |
| 4 |
# _ord_: numerical values indexed by characters |
| 5 |
# _ord_init: function to initialize _ord_ |
| 6 |
# |
| 7 |
# Arnold Robbins, arnold@skeeve.com, Public Domain |
| 8 |
# 16 January, 1992 |
| 9 |
# 20 July, 1992, revised |
| 10 |
|
| 11 |
BEGIN { _ord_init() } |
| 12 |
|
| 13 |
function _ord_init( low, high, i, t) |
| 14 |
{ |
| 15 |
low = sprintf("%c", 7) # BEL is ascii 7 |
| 16 |
if (low == "\a") { # regular ascii |
| 17 |
low = 0 |
| 18 |
high = 127 |
| 19 |
} else if (sprintf("%c", 128 + 7) == "\a") { |
| 20 |
# ascii, mark parity |
| 21 |
low = 128 |
| 22 |
high = 255 |
| 23 |
} else { # ebcdic(!) |
| 24 |
low = 0 |
| 25 |
high = 255 |
| 26 |
} |
| 27 |
|
| 28 |
for (i = low; i <= high; i++) { |
| 29 |
t = sprintf("%c", i) |
| 30 |
_ord_[t] = i |
| 31 |
} |
| 32 |
} |
| 33 |
function ord(str, c) |
| 34 |
{ |
| 35 |
# only first character is of interest |
| 36 |
c = substr(str, 1, 1) |
| 37 |
return _ord_[c] |
| 38 |
} |
| 39 |
|
| 40 |
function chr(c) |
| 41 |
{ |
| 42 |
# force c to be numeric by adding 0 |
| 43 |
return sprintf("%c", c + 0) |
| 44 |
} |