File:  [gforth] / gforth / kernel / xchars.fs
Revision 1.14: download - view: text, annotated - select for diffs
Sat Dec 6 16:28:13 2008 UTC (13 years, 10 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Use xc!+? for xc,

    1: \ extended characters (either 8bit or UTF-8, possibly other encodings)
    2: \ and their fixed-size variant
    3: 
    4: \ Copyright (C) 2005,2006,2007 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 3
   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, see http://www.gnu.org/licenses/.
   20: 
   21: \ We can do some of these (and possibly faster) by just using the
   22: \ utf-8 words with an appropriate setting of max-single-byte, but I
   23: \ like to see how an 8bit setting without UTF-8 stuff looks like.
   24: 
   25: Defer xemit ( xc -- ) \ xchar-ext
   26: \G Prints an xchar on the terminal.
   27: Defer xkey ( -- xc ) \ xchar-ext
   28: \G Reads an xchar from the terminal. This will discard all input
   29: \G events up to the completion of the xchar.
   30: Defer xchar+ ( xc-addr1 -- xc-addr2 ) \ xchar-ext
   31: \G Adds the size of the xchar stored at @var{xc-addr1} to this address,
   32: \G giving @var{xc-addr2}.
   33: Defer xchar- ( xc-addr1 -- xc-addr2 ) \ xchar-ext
   34: \G Goes backward from @var{xc_addr1} until it finds an xchar so that
   35: \G the size of this xchar added to @var{xc_addr2} gives
   36: \G @var{xc_addr1}.
   37: Defer +x/string ( xc-addr1 u1 -- xc-addr2 u2 ) \ xchar	plus-x-slash-string
   38: \G Step forward by one xchar in the buffer defined by address
   39: \G @var{xc-addr1}, size @var{u1} pchars. @var{xc-addr2} is the address
   40: \G and u2 the size in pchars of the remaining buffer after stepping
   41: \G over the first xchar in the buffer.
   42: Defer x\string- ( xc-addr1 u1 -- xc-addr1 u2 ) \ xchar	x-back-string-minus
   43: \G Step backward by one xchar in the buffer defined by address
   44: \G @var{xc-addr1} and size @var{u1} in pchars, starting at the end of
   45: \G the buffer. @var{xc-addr1} is the address and @var{u2} the size in
   46: \G pchars of the remaining buffer after stepping backward over the
   47: \G last xchar in the buffer.
   48: Defer xc@ ( xc-addr -- xc ) \ xchar-ext	xc-fetch
   49: \G Fetchs the xchar @var{xc} at @var{xc-addr1}.
   50: Defer xc!+? ( xc xc-addr1 u1 -- xc-addr2 u2 f ) \ xchar-ext	xc-store-plus-query
   51: \G Stores the xchar @var{xc} into the buffer starting at address
   52: \G @var{xc-addr1}, @var{u1} pchars large. @var{xc-addr2} points to the
   53: \G first memory location after @var{xc}, @var{u2} is the remaining
   54: \G size of the buffer. If the xchar @var{xc} did fit into the buffer,
   55: \G @var{f} is true, otherwise @var{f} is false, and @var{xc-addr2}
   56: \G @var{u2} equal @var{xc-addr1} @var{u1}. XC!+?  is safe for buffer
   57: \G overflows, and therefore preferred over XC!+.
   58: Defer xc@+ ( xc-addr1 -- xc-addr2 xc ) \ xchar-ext	xc-fetch-plus
   59: \G Fetchs the xchar @var{xc} at @var{xc-addr1}. @var{xc-addr2} points
   60: \G to the first memory location after @var{xc}.
   61: Defer xc-size ( xc -- u ) \ xchar-ext
   62: \G Computes the memory size of the xchar @var{xc} in pchars.
   63: Defer x-size ( xc-addr u1 -- u2 ) \ xchar
   64: \G Computes the memory size of the first xchar stored at @var{xc-addr}
   65: \G in pchars.
   66: Defer x-width ( xc-addr u -- n ) \ xchar-ext
   67: \G @var{n} is the number of monospace ASCII pchars that take the same
   68: \G space to display as the the xchar string starting at @var{xc-addr},
   69: \G using @var{u} pchars; assuming a monospaced display font,
   70: \G i.e. pchar width is always an integer multiple of the width of an
   71: \G ASCII pchar.
   72: Defer -trailing-garbage ( xc-addr u1 -- addr u2 ) \ xchar-ext
   73: \G Examine the last XCHAR in the buffer @var{xc-addr} @var{u1}---if
   74: \G the encoding is correct and it repesents a full pchar, @var{u2}
   75: \G equals @var{u1}, otherwise, @var{u2} represents the string without
   76: \G the last (garbled) xchar.
   77: 
   78: \ derived words, faster implementations are probably possible
   79: 
   80: : x@+/string ( xc-addr1 u1 -- xc-addr2 u2 xc )
   81:     \ !! check for errors?
   82:     over >r +x/string
   83:     r> xc@ ;
   84: 
   85: : xhold ( xc -- )
   86:     \G Put xc into the pictured numeric output
   87:     dup xc-size negate chars holdptr +!
   88:     holdptr @ dup holdbuf u< -&17 and throw
   89:     8 xc!+? 2drop drop ;
   90: 
   91: : xc, ( xchar -- ) here unused xc!+? 2drop dp ! ;
   92: 
   93: \ fixed-size versions of these words
   94: 
   95: : char- ( c-addr1 -- c-addr2 )
   96:     [ 1 chars ] literal - ;
   97: 
   98: : +string ( c-addr1 u1 -- c-addr2 u2 )
   99:     1 /string ;
  100: : string- ( c-addr1 u1 -- c-addr1 u2 )
  101:     1- ;
  102: 
  103: : c!+? ( c c-addr1 u1 -- c-addr2 u2 f )
  104:     dup 1 chars u< if \ or use < ?
  105: 	rot drop false
  106:     else
  107: 	>r dup >r c!
  108: 	r> r> 1 /string true
  109:     then ;
  110: 
  111: : c-size ( c -- 1 )
  112:     drop 1 ;
  113: 
  114: : set-encoding-fixed-width ( -- )
  115:     ['] emit is xemit
  116:     ['] key is xkey
  117:     ['] char+ is xchar+
  118:     ['] char- is xchar-
  119:     ['] +string is +x/string
  120:     ['] string- is x\string-
  121:     ['] c@ is xc@
  122:     ['] c!+? is xc!+?
  123:     ['] count is xc@+
  124:     ['] c-size is xc-size
  125:     ['] c-size is x-size
  126:     ['] nip IS x-width
  127:     ['] noop is -trailing-garbage
  128: ;

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