File:  [gforth] / gforth / kernel / xchars.fs
Revision 1.10: download - view: text, annotated - select for diffs
Wed Oct 17 15:50:01 2007 UTC (15 years, 7 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Added xhold

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

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