home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.forth      Forth programmers eat a lot of Bratwurst      117,927 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 117,767 of 117,927   
   dxf to Hans Bezemer   
   Re: Idiomatic way to read a word of text   
   23 Nov 25 00:28:38   
   
   From: dxforth@gmail.com   
      
   On 22/11/2025 4:57 am, Hans Bezemer wrote:   
   > On 18-11-2025 00:25, Paul Rubin wrote:   
   >> ...   
   >> I guess I could use the FILE word set to write something like getc()   
   >> with its own buffering, but that seems messy.  I'm wondering if this is   
   >> a common situation and there's an idiomatic solution.   
   >   
   > I don't know if it's "idiomatic", but it works. In essence, it reads the   
   file binary. If there is something left at the end of the buffer, it copies   
   that to the start, adjusts the buffer address and size and continues. It   
   doesn't return a word per call,   
    you open the file and it applies a quotation to each word parsed (a n --).   
   >   
   > No, it's not beautiful, but it works. BTW, if you happen to be German and   
   your prose contains words that exceed 256 characters, you're on your own.   
   > ...   
      
   I know it's cheating but then that's what libraries are for ;-)   
      
   1 fload bfile   
   \ readch ( -- c true | false )   
      
   256 constant /word  \ max word length   
      
   /word reserve constant wordbuf   
      
   : eow? ( c -- f )   
     bl of true end   
      9 of true end   
     10 of true end   
     13 of true end   
     drop false   
      
      
   : parse-file ( xt -- )   
     >r  0  begin  readch while   
       dup eow? if   
         drop  wordbuf swap r@ execute  0   
       else   
         over wordbuf + c!  1+   
       then   
     repeat drop rdrop ;   
      
   : .word ( a u -- )  ( -leading -trailing) type cr ;   
      
   : run ( a u -- )   
     r/o openin  ['] .word  parse-file  closein ;   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca