File:  [gforth] / gforth / kernel / xchars.fs
Revision 1.7: download - view: text, annotated - select for diffs
Sat Jul 14 19:57:16 2007 UTC (15 years, 2 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Changes to bring utf-8.fs up to current XCHAR proposal

    1: \ extended characters (either 8bit or UTF-8, possibly other encodings)
    2: \ and their fixed-size variant
    3: 
    4: \ Copyright (C) 2005,2006 Free Software Foundation, Inc.
    5: 
    6: \ This file is part of Gforth.
    7: 
    8: \ Gforth is free software; you can redistribute it and/or
    9: \ modify it under the terms of the GNU General Public License
   10: \ as published by the Free Software Foundation; either version 2
   11: \ of the License, or (at your option) any later version.
   12: 
   13: \ This program is distributed in the hope that it will be useful,
   14: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
   15: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16: \ GNU General Public License for more details.
   17: 
   18: \ You should have received a copy of the GNU General Public License
   19: \ along with this program; if not, write to the Free Software
   20: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   21: 
   22: \ We can do some of these (and possibly faster) by just using the
   23: \ utf-8 words with an appropriate setting of max-single-byte, but I
   24: \ like to see how an 8bit setting without UTF-8 stuff looks like.
   25: 
   26: DEFER XEMIT ( xc -- )
   27: DEFER XKEY ( -- xc )
   28: DEFER XCHAR+ ( xc-addr1 -- xc-addr2 )
   29: DEFER XCHAR- ( xc-addr1 -- xc-addr2 )
   30: DEFER +XSTRING ( xc-addr1 u1 -- xc-addr2 u2 )
   31: DEFER -XSTRING ( xc-addr1 u1 -- xc-addr2 u2 )
   32: DEFER XSTRING+ ( xc-addr1 u1 -- xc-addr1 u2 )
   33: DEFER XSTRING- ( xc-addr1 u1 -- xc-addr1 u2 )
   34: DEFER XC@ ( xc-addr -- xc )
   35: DEFER XC!+? ( xc xc-addr1 u1 -- xc-addr2 u2 f ) \ f if operation succeeded
   36: DEFER XC@+ ( xc-addr1 -- xc-addr2 xc )
   37: DEFER XC-SIZE ( xc -- u ) \ size in cs
   38: DEFER X-SIZE ( xc-addr u1 -- u2 ) \ size in cs
   39: DEFER X-WIDTH ( addr u -- n ) \ size in fixed chars
   40: DEFER -TRAILING-GARBAGE ( addr u1 -- addr u2 ) \ remove trailing incomplete xc
   41: 
   42: \ derived words, faster implementations are probably possible
   43: 
   44: : x@+/string ( xc-addr1 u1 -- xc-addr2 u2 xc )
   45:     \ !! check for errors?
   46:     over >r +xstring
   47:     r> xc@ ;
   48: 
   49: \ fixed-size versions of these words
   50: 
   51: : char- ( c-addr1 -- c-addr2 )
   52:     [ 1 chars ] literal - ;
   53: 
   54: : +string ( c-addr1 u1 -- c-addr2 u2 )
   55:     1 /string ;
   56: : -string ( c-addr1 u1 -- c-addr2 u2 )
   57:     -1 /string ;
   58: 
   59: : string+ ( c-addr1 u1 -- c-addr1 u2 )
   60:     1+ ;
   61: : string- ( c-addr1 u1 -- c-addr1 u2 )
   62:     1- ;
   63: 
   64: : c!+? ( c c-addr1 u1 -- c-addr2 u2 f )
   65:     dup 1 chars u< if \ or use < ?
   66: 	rot drop false
   67:     else
   68: 	>r dup >r c!
   69: 	r> r> 1 /string true
   70:     then ;
   71: 
   72: : c-size ( c -- 1 )
   73:     drop 1 ;
   74: 
   75: : set-encoding-fixed-width ( -- )
   76:     ['] emit is xemit
   77:     ['] key is xkey
   78:     ['] char+ is xchar+
   79:     ['] char- is xchar-
   80:     ['] +string is +xstring
   81:     ['] -string is -xstring
   82:     ['] string+ is xstring+
   83:     ['] string- is xstring-
   84:     ['] c@ is xc@
   85:     ['] c!+? is xc!+?
   86:     ['] count is xc@+
   87:     ['] c-size is xc-size
   88:     ['] c-size is x-size
   89:     ['] nip IS x-width
   90:     ['] noop is -trailing-garbage
   91: ;

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