scf - sscanf scanner

Module Description

The scf module implements a simplified version of C's sscanf function. The words in this module expect a format string with specifiers (see below for the format). For every specifier (except %%) the string is scanned and converted to a stack item. The white space character in the specifier string will match repeatedly all white space in the source string. All other characters must be matched in the source string.

Format: %[double]specifier
   Double: l      = the argument is interpreted as a double
Specifier: c      = scan a character [char]
           d      = scan a signed number [n or d]
           o      = scan an unsigned octal [n or d]
           s      = scan a string [c-addr u]
           u      = scan an unsigned number [u or ud]
           x      = scan an unsigned hexadecimal number [u or ud]
           X      = scan an unsigned hexadecimal number [u or ud]
           e      = scan a float number [r]
           E      = scan a float number [r]
           q      = scan a quoted string (non sprintf) [c-addr u]
           %      = match a '%' []

Module Words

sscanf words

scf+scan ( c-addr1 u1 c-addr2 u2 -- i*x j*r n )
Scan the source string c-addr1 u1 with specifier string c-addr2 u2, resulting in n arguments i*x j*r
scf" ( "ccc<quote>" c-addr u -- i*x j*r n )
Scan the source string c-addr1 u1 with the specifier string c-addr2 u2, resulting in n arguments i*x and j*r

Examples

include ffl/scf.fs

\ Conversions
.( Scan the string : ) 
s" 10 a hello CE 55 20" 2dup type     \ the source string
.(  with the specifiers: ) 
s" %d %c %s %x %o %ld"  2dup type cr  \ the specifier string

scf+scan                              \ scan the source string with the specifier string

.( Number of conversions: ) . cr      \ the number of conversions done by scf+scan
.( Double: ) d. cr                    \ the results
.( Octal: ) . cr
.( Hex: ) . cr
.( String: ) type cr
.( Character: ) emit cr
.( Decimal: ) . cr

\ Float conversions
[DEFINED] >float [IF]
.( Scan floats ) cr
s" 10 20.1 -30.3E+5" s" %e %e %e" scf+scan
.( Number of floats: ) . 
.(  values: ) f. f. f. cr
[THEN]

\ Matching and conversion
.( Match and scan, with parsing word ) cr
s" %var = 20 (24)" scf" %%%s = %d [%o]"

.( Number of conversions: ) .          \ Only 2 conversion due to mismatch of '['
.(  values: ) . type cr

generated 03-Jun-2010 by ofcfrth-0.10.0