Annotation of gforth/utf-8.fs, revision 1.35

1.1       pazsan      1: \ UTF-8 handling                                       12dec04py
                      2: 
1.33      anton       3: \ Copyright (C) 2004,2005,2006,2007 Free Software Foundation, Inc.
1.1       pazsan      4: 
                      5: \ This file is part of Gforth.
                      6: 
                      7: \ Gforth is free software; you can redistribute it and/or
                      8: \ modify it under the terms of the GNU General Public License
1.34      anton       9: \ as published by the Free Software Foundation, either version 3
1.1       pazsan     10: \ of the License, or (at your option) any later version.
                     11: 
                     12: \ This program is distributed in the hope that it will be useful,
                     13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
                     14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     15: \ GNU General Public License for more details.
                     16: 
                     17: \ You should have received a copy of the GNU General Public License
1.34      anton      18: \ along with this program. If not, see http://www.gnu.org/licenses/.
1.1       pazsan     19: 
                     20: \ short: u8 means utf-8 encoded address
                     21: 
1.7       pazsan     22: s" malformed UTF-8 character" exception Constant UTF-8-err
                     23: 
1.13      anton      24: $80 Value max-single-byte
1.10      pazsan     25: 
1.4       pazsan     26: : u8len ( u8 -- n )
1.13      anton      27:     dup      max-single-byte u< IF  drop 1  EXIT  THEN \ special case ASCII
1.1       pazsan     28:     $800  2 >r
1.17      pazsan     29:     BEGIN  2dup u>=  WHILE  5 lshift r> 1+ >r  dup 0= UNTIL  THEN
1.1       pazsan     30:     2drop r> ;
                     31: 
                     32: : u8@+ ( u8addr -- u8addr' u )
1.13      anton      33:     count  dup max-single-byte u< ?EXIT  \ special case ASCII
1.17      pazsan     34:     dup $C2 u< IF  UTF-8-err throw  THEN  \ malformed character
1.1       pazsan     35:     $7F and  $40 >r
                     36:     BEGIN  dup r@ and  WHILE  r@ xor
                     37:            6 lshift r> 5 lshift >r >r count
1.7       pazsan     38:            dup $C0 and $80 <> IF   UTF-8-err throw  THEN
1.1       pazsan     39:            $3F and r> or
                     40:     REPEAT  rdrop ;
                     41: 
                     42: : u8!+ ( u u8addr -- u8addr' )
1.13      anton      43:     over max-single-byte u< IF  tuck c! 1+  EXIT  THEN \ special case ASCII
1.1       pazsan     44:     >r 0 swap  $3F
                     45:     BEGIN  2dup u>  WHILE
                     46:            2/ >r  dup $3F and $80 or swap 6 rshift r>
                     47:     REPEAT  $7F xor 2* or  r>
                     48:     BEGIN   over $80 u>= WHILE  tuck c! 1+  REPEAT  nip ;
                     49: 
1.8       pazsan     50: \ plug-in so that char and '<char> work for UTF-8
                     51: 
1.9       anton      52: [ifundef] char@ \ !! bootstrapping help
                     53:     Defer char@ ( addr u -- char addr' u' )
                     54:     :noname  over c@ -rot 1 /string ; IS char@
                     55: [then]
                     56: 
1.8       pazsan     57: :noname  ( addr u -- char addr' u' )
1.9       anton      58:     \ !! the if here seems to work around some breakage, but not
1.24      pazsan     59:     \ entirely; e.g., try 'รง' with LANG=C.
1.9       anton      60:     dup 1 u<= IF defers char@ EXIT THEN
1.8       pazsan     61:     over + >r u8@+ swap r> over - ; IS char@
                     62: 
1.1       pazsan     63: \ scan to next/previous character
                     64: 
1.13      anton      65: \ alternative names: u8char+ u8char-
                     66: 
1.12      pazsan     67: : u8>> ( u8addr -- u8addr' )  u8@+ drop ;
1.1       pazsan     68: : u8<< ( u8addr -- u8addr' )
1.13      anton      69:     BEGIN  1- dup c@ $C0 and max-single-byte <>  UNTIL ;
1.1       pazsan     70: 
                     71: \ utf key and emit
                     72: 
1.27      pazsan     73: Defer check-xy  ' noop IS check-xy
                     74: 
1.1       pazsan     75: : u8key ( -- u )
1.13      anton      76:     defers key dup max-single-byte u< ?EXIT  \ special case ASCII
1.27      pazsan     77:     dup $FF = ?EXIT  \ special resize character
1.17      pazsan     78:     dup $C2 u< IF  UTF-8-err throw  THEN  \ malformed character
1.1       pazsan     79:     $7F and  $40 >r
                     80:     BEGIN  dup r@ and  WHILE  r@ xor
                     81:            6 lshift r> 5 lshift >r >r defers key
1.7       pazsan     82:            dup $C0 and $80 <> IF  UTF-8-err throw  THEN
1.1       pazsan     83:            $3F and r> or
                     84:     REPEAT  rdrop ;
                     85: 
                     86: : u8emit ( u -- )
1.13      anton      87:     dup max-single-byte u< IF  defers emit  EXIT  THEN \ special case ASCII
1.1       pazsan     88:     0 swap  $3F
                     89:     BEGIN  2dup u>  WHILE
                     90:            2/ >r  dup $3F and $80 or swap 6 rshift r>
                     91:     REPEAT  $7F xor 2* or
                     92:     BEGIN   dup $80 u>= WHILE  defers emit  REPEAT  drop ;
1.13      anton      93: 
                     94: \ utf-8 stuff for xchars
                     95: 
1.29      pazsan     96: : +u8/string ( xc-addr1 u1 -- xc-addr2 u2 )
                     97:     over dup u8>> swap - /string ;
                     98: : u8\string- ( xcaddr u -- xcaddr u' )
1.26      pazsan     99:     over + u8<< over - ;
                    100: 
1.13      anton     101: : u8@ ( c-addr -- u )
                    102:     u8@+ nip ;
                    103: 
                    104: : u8!+? ( xc xc-addr1 u1 -- xc-addr2 u2 f )
                    105:     >r over u8len r@ over u< if ( xc xc-addr1 len r: u1 )
                    106:        \ not enough space
                    107:        drop nip r> false
                    108:     else
                    109:        >r u8!+ r> r> swap - true
                    110:     then ;
                    111: 
1.28      pazsan    112: : u8addrlen ( u8-addr u -- u )  drop
1.13      anton     113:     \ length of UTF-8 char starting at u8-addr (accesses only u8-addr)
                    114:     c@
                    115:     dup $80 u< if drop 1 exit endif
                    116:     dup $c0 u< if UTF-8-err throw endif
                    117:     dup $e0 u< if drop 2 exit endif
                    118:     dup $f0 u< if drop 3 exit endif
                    119:     dup $f8 u< if drop 4 exit endif
                    120:     dup $fc u< if drop 5 exit endif
                    121:     dup $fe u< if drop 6 exit endif
                    122:     UTF-8-err throw ;
                    123: 
                    124: : -u8trailing-garbage ( addr u1 -- addr u2 )
                    125:     2dup + dup u8<< ( addr u1 end1 end2 )
1.28      pazsan    126:     2dup dup over over - u8addrlen + = if \ last character ok
1.13      anton     127:        2drop
                    128:     else
                    129:        nip nip over -
                    130:     then ;
                    131: 
1.22      anton     132: [IFUNDEF] wcwidth
1.24      pazsan    133: : wc,3 ( n low high -- )  1+ , , , ;
                    134: 
                    135: Create wc-table \ derived from wcwidth source code, for UCS32
1.35    ! anton     136: 0 $0300 $0357 wc,3
        !           137: 0 $035D $036F wc,3
        !           138: 0 $0483 $0486 wc,3
        !           139: 0 $0488 $0489 wc,3
        !           140: 0 $0591 $05A1 wc,3
        !           141: 0 $05A3 $05B9 wc,3
        !           142: 0 $05BB $05BD wc,3
        !           143: 0 $05BF $05BF wc,3
        !           144: 0 $05C1 $05C2 wc,3
        !           145: 0 $05C4 $05C4 wc,3
        !           146: 0 $0600 $0603 wc,3
        !           147: 0 $0610 $0615 wc,3
        !           148: 0 $064B $0658 wc,3
        !           149: 0 $0670 $0670 wc,3
        !           150: 0 $06D6 $06E4 wc,3
        !           151: 0 $06E7 $06E8 wc,3
        !           152: 0 $06EA $06ED wc,3
        !           153: 0 $070F $070F wc,3
        !           154: 0 $0711 $0711 wc,3
        !           155: 0 $0730 $074A wc,3
        !           156: 0 $07A6 $07B0 wc,3
        !           157: 0 $0901 $0902 wc,3
        !           158: 0 $093C $093C wc,3
        !           159: 0 $0941 $0948 wc,3
        !           160: 0 $094D $094D wc,3
        !           161: 0 $0951 $0954 wc,3
        !           162: 0 $0962 $0963 wc,3
        !           163: 0 $0981 $0981 wc,3
        !           164: 0 $09BC $09BC wc,3
        !           165: 0 $09C1 $09C4 wc,3
        !           166: 0 $09CD $09CD wc,3
        !           167: 0 $09E2 $09E3 wc,3
        !           168: 0 $0A01 $0A02 wc,3
        !           169: 0 $0A3C $0A3C wc,3
        !           170: 0 $0A41 $0A42 wc,3
        !           171: 0 $0A47 $0A48 wc,3
        !           172: 0 $0A4B $0A4D wc,3
        !           173: 0 $0A70 $0A71 wc,3
        !           174: 0 $0A81 $0A82 wc,3
        !           175: 0 $0ABC $0ABC wc,3
        !           176: 0 $0AC1 $0AC5 wc,3
        !           177: 0 $0AC7 $0AC8 wc,3
        !           178: 0 $0ACD $0ACD wc,3
        !           179: 0 $0AE2 $0AE3 wc,3
        !           180: 0 $0B01 $0B01 wc,3
        !           181: 0 $0B3C $0B3C wc,3
        !           182: 0 $0B3F $0B3F wc,3
        !           183: 0 $0B41 $0B43 wc,3
        !           184: 0 $0B4D $0B4D wc,3
        !           185: 0 $0B56 $0B56 wc,3
        !           186: 0 $0B82 $0B82 wc,3
        !           187: 0 $0BC0 $0BC0 wc,3
        !           188: 0 $0BCD $0BCD wc,3
        !           189: 0 $0C3E $0C40 wc,3
        !           190: 0 $0C46 $0C48 wc,3
        !           191: 0 $0C4A $0C4D wc,3
        !           192: 0 $0C55 $0C56 wc,3
        !           193: 0 $0CBC $0CBC wc,3
        !           194: 0 $0CBF $0CBF wc,3
        !           195: 0 $0CC6 $0CC6 wc,3
        !           196: 0 $0CCC $0CCD wc,3
        !           197: 0 $0D41 $0D43 wc,3
        !           198: 0 $0D4D $0D4D wc,3
        !           199: 0 $0DCA $0DCA wc,3
        !           200: 0 $0DD2 $0DD4 wc,3
        !           201: 0 $0DD6 $0DD6 wc,3
        !           202: 0 $0E31 $0E31 wc,3
        !           203: 0 $0E34 $0E3A wc,3
        !           204: 0 $0E47 $0E4E wc,3
        !           205: 0 $0EB1 $0EB1 wc,3
        !           206: 0 $0EB4 $0EB9 wc,3
        !           207: 0 $0EBB $0EBC wc,3
        !           208: 0 $0EC8 $0ECD wc,3
        !           209: 0 $0F18 $0F19 wc,3
        !           210: 0 $0F35 $0F35 wc,3
        !           211: 0 $0F37 $0F37 wc,3
        !           212: 0 $0F39 $0F39 wc,3
        !           213: 0 $0F71 $0F7E wc,3
        !           214: 0 $0F80 $0F84 wc,3
        !           215: 0 $0F86 $0F87 wc,3
        !           216: 0 $0F90 $0F97 wc,3
        !           217: 0 $0F99 $0FBC wc,3
        !           218: 0 $0FC6 $0FC6 wc,3
        !           219: 0 $102D $1030 wc,3
        !           220: 0 $1032 $1032 wc,3
        !           221: 0 $1036 $1037 wc,3
        !           222: 0 $1039 $1039 wc,3
        !           223: 0 $1058 $1059 wc,3
        !           224: 1 $0000 $1100 wc,3
        !           225: 2 $1100 $115f wc,3
        !           226: 0 $1160 $11FF wc,3
        !           227: 0 $1712 $1714 wc,3
        !           228: 0 $1732 $1734 wc,3
        !           229: 0 $1752 $1753 wc,3
        !           230: 0 $1772 $1773 wc,3
        !           231: 0 $17B4 $17B5 wc,3
        !           232: 0 $17B7 $17BD wc,3
        !           233: 0 $17C6 $17C6 wc,3
        !           234: 0 $17C9 $17D3 wc,3
        !           235: 0 $17DD $17DD wc,3
        !           236: 0 $180B $180D wc,3
        !           237: 0 $18A9 $18A9 wc,3
        !           238: 0 $1920 $1922 wc,3
        !           239: 0 $1927 $1928 wc,3
        !           240: 0 $1932 $1932 wc,3
        !           241: 0 $1939 $193B wc,3
        !           242: 0 $200B $200F wc,3
        !           243: 0 $202A $202E wc,3
        !           244: 0 $2060 $2063 wc,3
        !           245: 0 $206A $206F wc,3
        !           246: 0 $20D0 $20EA wc,3
        !           247: 2 $2329 $232A wc,3
        !           248: 0 $302A $302F wc,3
        !           249: 2 $2E80 $303E wc,3
        !           250: 0 $3099 $309A wc,3
        !           251: 2 $3040 $A4CF wc,3
        !           252: 2 $AC00 $D7A3 wc,3
        !           253: 2 $F900 $FAFF wc,3
        !           254: 0 $FB1E $FB1E wc,3
        !           255: 0 $FE00 $FE0F wc,3
        !           256: 0 $FE20 $FE23 wc,3
        !           257: 2 $FE30 $FE6F wc,3
        !           258: 0 $FEFF $FEFF wc,3
        !           259: 2 $FF00 $FF60 wc,3
        !           260: 2 $FFE0 $FFE6 wc,3
        !           261: 0 $FFF9 $FFFB wc,3
        !           262: 0 $1D167 $1D169 wc,3
        !           263: 0 $1D173 $1D182 wc,3
        !           264: 0 $1D185 $1D18B wc,3
        !           265: 0 $1D1AA $1D1AD wc,3
        !           266: 2 $20000 $2FFFD wc,3
        !           267: 2 $30000 $3FFFD wc,3
        !           268: 0 $E0001 $E0001 wc,3
        !           269: 0 $E0020 $E007F wc,3
        !           270: 0 $E0100 $E01EF wc,3
1.24      pazsan    271: here wc-table - Constant #wc-table
                    272: 
                    273: \ inefficient table walk:
                    274: 
                    275: : wcwidth ( xc -- n )
                    276:     wc-table #wc-table over + swap ?DO
                    277:        dup I 2@ within IF  I 2 cells + @  UNLOOP EXIT  THEN
                    278:     3 cells +LOOP  1 ;
1.22      anton     279: [THEN]
                    280:     
1.19      pazsan    281: : u8width ( xcaddr u -- n )
                    282:     0 rot rot over + swap ?DO
1.22      anton     283:         I xc@+ swap >r wcwidth +
1.19      pazsan    284:     r> I - +LOOP ;
                    285: 
1.13      anton     286: : set-encoding-utf-8 ( -- )
                    287:     ['] u8emit is xemit
                    288:     ['] u8key is xkey
                    289:     ['] u8>> is xchar+
                    290:     ['] u8<< is xchar-
1.26      pazsan    291: [ [IFDEF] xstring+ ]
1.29      pazsan    292:     ['] u8\string- is xstring-
                    293:     ['] +u8/string is +xstring
                    294: [ [THEN] ]
1.32      pazsan    295: [ [IFDEF] +x/string ]
1.29      pazsan    296:     ['] u8\string- is x\string-
                    297:     ['] +u8/string is +x/string
1.26      pazsan    298: [ [THEN] ]
1.13      anton     299:     ['] u8@ is xc@
                    300:     ['] u8!+? is xc!+?
                    301:     ['] u8@+ is xc@+
                    302:     ['] u8len is xc-size
1.20      pazsan    303: [ [IFDEF] x-width ]
1.19      pazsan    304:     ['] u8width is x-width
1.20      pazsan    305: [ [THEN] ]
1.28      pazsan    306: [ [IFDEF] x-size ]
                    307:     ['] u8addrlen is x-size
                    308: [ [THEN] ]
1.13      anton     309:     ['] -u8trailing-garbage is -trailing-garbage
                    310: ;
1.1       pazsan    311: 
1.15      anton     312: : utf-8-cold ( -- )
1.16      pazsan    313:     s" LC_ALL" getenv 2dup d0= IF  2drop
                    314:        s" LC_CTYPE" getenv 2dup d0= IF  2drop
                    315:            s" LANG" getenv 2dup d0= IF  2drop
                    316:                s" C"  THEN THEN THEN
                    317:     s" UTF-8" search nip nip
1.15      anton     318:     IF  set-encoding-utf-8  ELSE  set-encoding-fixed-width  THEN ;
                    319: 
1.30      pazsan    320: environment-wordlist set-current
1.31      pazsan    321: : xchar-encoding ( -- addr u ) \ xchar-ext
                    322:     \G Returns a printable ASCII string that reperesents the encoding,
                    323:     \G and use the preferred MIME name (if any) or the name in
                    324:     \G @url{http://www.iana.org/assignments/character-sets} like
                    325:     \G ``ISO-LATIN-1'' or ``UTF-8'', with the exception of ``ASCII'', where
                    326:     \G we prefer the alias ``ASCII''.
                    327:     max-single-byte $80 = IF s" UTF-8" ELSE s" ISO-LATIN-1" THEN ;
1.30      pazsan    328: forth definitions
                    329: 
1.23      anton     330: :noname ( -- )
                    331:     defers 'cold
                    332:     utf-8-cold
                    333: ; is 'cold
1.15      anton     334: 
                    335: utf-8-cold

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>