sistema_progs

Programas para customizar o meu entorno de traballo nos meus equipos persoais
Log | Files | Refs

D1522.test-awk-printf.sh (990B)


      1 #!/bin/bash
      2 
      3 x=($'\1' $'\2' $'\32' ' ' $'\a' $'\b' $'\t' $'\n' $'\v' $'\f' $'\r' $'\177' a \" \' \$ \! \` \~)
      4 declare -p x
      5 
      6 function test-awk-c2s() {
      7   {
      8     echo 43 # +
      9     echo 945 # α
     10     echo 12354 # あ
     11   } | LANG=C awk '
     12     BEGIN{
     13       # ENCODING: UTF-8
     14       if (sprintf("%c", 945) == "α") {
     15         PRINTF_C_UNICODE = 1;
     16       } else {
     17         for (i = 1; i <= 255; i++)
     18           byte2char[i] = sprintf("%c", i);
     19       }
     20     }
     21 
     22     # ENCODING: UTF-8
     23     function c2s(code, _, leadbyte_mark, leadbyte_sup, tail) {
     24       if (PRINTF_C_UNICODE)
     25         return sprintf("%c", code);
     26 
     27       leadbyte_sup = 0x80;
     28       leadbyte_mark = 0;
     29       tail = "";
     30       while (code >= leadbyte_sup) {
     31         leadbyte_sup /= 2;
     32         leadbyte_mark = leadbyte_mark ? leadbyte_mark / 2 : 0xFFC0;
     33         tail = byte2char[0x80 + int(code % 64)] tail;
     34         code = int(code / 64);
     35       }
     36       return byte2char[(leadbyte_mark + code) % 256] tail;
     37     }
     38     {print c2s($1);}
     39   '
     40 }
     41 test-awk-c2s