fix bug in StringToNumberWithBase; add string tokenizer example
FossilOrigin-Name: a5d4a3564e05d167ca72a87be254464c786f923048b262c645d8cb11b1a720a9
This commit is contained in:
parent
b07f8a82f6
commit
d09a03ddca
3 changed files with 19 additions and 1 deletions
|
@ -29,3 +29,5 @@
|
|||
| StringToNumberWithBase.forth | Numbers in misc. bases |
|
||||
| DictionaryStats.forth | Use of `d:for-each` to find info about |
|
||||
| | word names |
|
||||
| TokenizeString.forth | Tokenize a string into a set |
|
||||
| unicode.forth | Example showing Unicode (UTF-8) |
|
||||
|
|
|
@ -61,7 +61,7 @@ Going the other way, back to a string, follows a similar process.
|
|||
{{
|
||||
'String d:create #12 allot
|
||||
:check-sign (n-) n:negative? [ $- buffer:add ] if ;
|
||||
: n->digit (n-c) &DIGITS + fetch ;
|
||||
:n->digit (n-c) &DIGITS + fetch ;
|
||||
:convert (n-) [ @Base /mod swap n->digit buffer:add dup n:zero? ] until drop ;
|
||||
---reveal---
|
||||
:n:to-string<with-base> (n-s)
|
||||
|
|
16
example/TokenizeString.forth
Normal file
16
example/TokenizeString.forth
Normal file
|
@ -0,0 +1,16 @@
|
|||
If you want to tokenize a string into a set, this is one approach.
|
||||
|
||||
~~~
|
||||
{{
|
||||
'Split-On var
|
||||
:match? (c-f) @Split-On eq? ;
|
||||
:terminate (s-s) #0 over n:dec store ;
|
||||
:step (ss-s) [ n:inc ] dip match? [ dup , terminate ] if ;
|
||||
---reveal---
|
||||
:s:tokenize (sc-a)
|
||||
!Split-On s:keep
|
||||
here #0 , [ dup , dup [ step ] s:for-each drop ] dip
|
||||
here over - n:dec over store ;
|
||||
}}
|
||||
~~~
|
||||
|
Loading…
Reference in a new issue