File:  [gforth] / gforth / utf-8.fs
Revision 1.39: download - view: text, annotated - select for diffs
Mon May 11 08:07:44 2009 UTC (10 years, 7 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Added environment stuff

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

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