scripts

random scripts
git clone https://git.parazyd.org/scripts
Log | Files | Refs

utf8.sh (856B)


      1 #!/bin/bash
      2 
      3 usage() {
      4 
      5     echo "`basename $0` [min max] (min, max: 0x0000..0xffff)"
      6 
      7 }
      8 
      9 fast_chr() {
     10     local __octal
     11     local __char
     12     printf -v __octal '%03o' $1
     13     printf -v __char \\$__octal
     14     REPLY=$__char
     15 }
     16 
     17 function unichr {
     18     local c=$1  # ordinal of char
     19     local l=0   # byte ctr
     20     local o=63  # ceiling
     21     local p=128 # accum. bits
     22     local s=''  # output string
     23 
     24     (( c < 0x80 )) && { fast_chr "$c"; echo -n "$REPLY"; return; }
     25 
     26     while (( c > o )); do
     27         fast_chr $(( t = 0x80 | c & 0x3f ))
     28         s="$REPLY$s"
     29         (( c >>= 6, l++, p += o+1, o>>=1 ))
     30     done
     31 
     32     fast_chr $(( t = p | c ))
     33     echo -n "$REPLY$s "
     34 }
     35 
     36 min=0xe000
     37 max=0xe1a0
     38 
     39 if test $# -eq 2; then
     40     min=$1
     41     max=$2
     42 elif test "$1" = "-h"; then
     43     usage
     44     exit 1
     45 fi
     46 
     47 ## test harness
     48 for (( i=$min; i<$max; i++ )); do
     49     unichr $i
     50 done
     51 echo