File:  [gforth] / gforth / kernel / xchars.fs
Revision 1.19: download - view: text, annotated - select for diffs
Mon Dec 31 15:25:19 2012 UTC (11 years, 3 months ago) by anton
Branches: MAIN
CVS tags: HEAD
updated copyright year

    1: \ extended characters (either 8bit or UTF-8, possibly other encodings)
    2: \ and their fixed-size variant
    3: 
    4: \ Copyright (C) 2005,2006,2007,2008,2011,2012 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 -- xc-addr2 ) \ xchar-ext	xc-store
   51: \G Stores the xchar @var{xc} at @var{xc-addr1}. @var{xc-addr2} is the next
   52: \G unused address in the buffer.
   53: Defer xc!+? ( xc xc-addr1 u1 -- xc-addr2 u2 f ) \ xchar-ext	xc-store-plus-query
   54: \G Stores the xchar @var{xc} into the buffer starting at address
   55: \G @var{xc-addr1}, @var{u1} pchars large. @var{xc-addr2} points to the
   56: \G first memory location after @var{xc}, @var{u2} is the remaining
   57: \G size of the buffer. If the xchar @var{xc} did fit into the buffer,
   58: \G @var{f} is true, otherwise @var{f} is false, and @var{xc-addr2}
   59: \G @var{u2} equal @var{xc-addr1} @var{u1}. XC!+?  is safe for buffer
   60: \G overflows, and therefore preferred over XC!+.
   61: Defer xc@+ ( xc-addr1 -- xc-addr2 xc ) \ xchar-ext	xc-fetch-plus
   62: \G Fetchs the xchar @var{xc} at @var{xc-addr1}. @var{xc-addr2} points
   63: \G to the first memory location after @var{xc}.
   64: Defer xc-size ( xc -- u ) \ xchar-ext
   65: \G Computes the memory size of the xchar @var{xc} in pchars.
   66: Defer x-size ( xc-addr u1 -- u2 ) \ xchar
   67: \G Computes the memory size of the first xchar stored at @var{xc-addr}
   68: \G in pchars.
   69: Defer x-width ( xc-addr u -- n ) \ xchar-ext
   70: \G @var{n} is the number of monospace ASCII pchars that take the same
   71: \G space to display as the the xchar string starting at @var{xc-addr},
   72: \G using @var{u} pchars; assuming a monospaced display font,
   73: \G i.e. pchar width is always an integer multiple of the width of an
   74: \G ASCII pchar.
   75: Defer -trailing-garbage ( xc-addr u1 -- addr u2 ) \ xchar-ext
   76: \G Examine the last XCHAR in the buffer @var{xc-addr} @var{u1}---if
   77: \G the encoding is correct and it repesents a full pchar, @var{u2}
   78: \G equals @var{u1}, otherwise, @var{u2} represents the string without
   79: \G the last (garbled) xchar.
   80: 
   81: \ derived words, faster implementations are probably possible
   82: 
   83: : x@+/string ( xc-addr1 u1 -- xc-addr2 u2 xc )
   84:     \ !! check for errors?
   85:     over >r +x/string
   86:     r> xc@ ;
   87: 
   88: : xhold ( xc -- )
   89:     \G Put xc into the pictured numeric output
   90:     dup xc-size negate chars holdptr +!
   91:     holdptr @ dup holdbuf u< -&17 and throw
   92:     8 xc!+? 2drop drop ;
   93: 
   94: : xc, ( xchar -- ) here unused xc!+? 2drop dp ! ;
   95: 
   96: \ fixed-size versions of these words
   97: 
   98: : char- ( c-addr1 -- c-addr2 )
   99:     [ 1 chars ] literal - ;
  100: 
  101: : +string ( c-addr1 u1 -- c-addr2 u2 )
  102:     1 /string ;
  103: : string- ( c-addr1 u1 -- c-addr1 u2 )
  104:     1- ;
  105: 
  106: : c!+ ( c c-addr1 -- c-addr2 )
  107:     dup 1+ >r c! r> ;
  108: 
  109: : c!+? ( c c-addr1 u1 -- c-addr2 u2 f )
  110:     dup 1 chars u< if \ or use < ?
  111: 	rot drop false
  112:     else
  113: 	>r dup >r c!
  114: 	r> r> 1 /string true
  115:     then ;
  116: 
  117: : c-size ( c -- 1 )
  118:     drop 1 ;
  119: 
  120: : set-encoding-fixed-width ( -- )
  121:     ['] emit is xemit
  122:     ['] key is xkey
  123:     ['] char+ is xchar+
  124:     ['] char- is xchar-
  125:     ['] +string is +x/string
  126:     ['] string- is x\string-
  127:     ['] c@ is xc@
  128:     ['] c!+ is xc!+
  129:     ['] c!+? is xc!+?
  130:     ['] count is xc@+
  131:     ['] c-size is xc-size
  132:     ['] c-size is x-size
  133:     ['] nip IS x-width
  134:     ['] noop is -trailing-garbage
  135: ;

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