en:pfw:string_handling
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:pfw:string_handling [2024-04-17 15:55] – [The idea] willem | en:pfw:string_handling [2024-06-05 22:53] (current) – [Generic Forth] willem | ||
---|---|---|---|
Line 26: | Line 26: | ||
{{https:// | {{https:// | ||
- | However it more useful to use a construct of a cell containing a byte count followed by that many bytes. | + | |
- | You should not store a count in a character unless there is a dire need to conserve space. | + | |
- | Also a string in Chinese is some of the UTF representation is a sequence, | + | |
- | where an interpretation of a byte is dependant on previous bytes. | + | |
- | The abstraction presented here work equally well on this kind of data structure. | + | |
===== Pseudo code ===== | ===== Pseudo code ===== | ||
Line 84: | Line 80: | ||
: C+! ( n a -- ) >r r@ c@ + r> c! ; \ Incr. byte with n at a | : C+! ( n a -- ) >r r@ c@ + r> c! ; \ Incr. byte with n at a | ||
: $@ ( s -- c ) count ; \ Fetch string | : $@ ( s -- c ) count ; \ Fetch string | ||
- | : $+! ( c s -- ) >r tuck r@ $@ + swap cmove r> c+! ; \ Extend string | + | : $+! ( c s -- ) >r tuck r@ $@ + swap move r> c+! ; \ Extend string |
: $! ( c s -- ) 0 over c! $+! ; \ Store string | : $! ( c s -- ) 0 over c! $+! ; \ Store string | ||
: $. ( c -- ) type ; \ Print string | : $. ( c -- ) type ; \ Print string | ||
Line 90: | Line 86: | ||
</ | </ | ||
- | The version where the count is stored in a cell is hardly different, but simpler. | + | Here is a version where the count is stored in a cell, it is hardly different. |
Note that it uses the non Generic Forth word '' | Note that it uses the non Generic Forth word '' | ||
the [[https:// | the [[https:// | ||
Line 101: | Line 97: | ||
: $@ ( s -- c ) @+ ; \ Fetch string | : $@ ( s -- c ) @+ ; \ Fetch string | ||
- | : $+! ( c s -- ) >r tuck r@ $@ + swap cmove r> +! ; \ Extend string | + | : $+! ( c s -- ) >r tuck r@ $@ + swap move r> +! ; \ Extend string |
: $! ( c s -- ) 0 over ! $+! ; \ Store string | : $! ( c s -- ) 0 over ! $+! ; \ Store string | ||
: $. ( c -- ) type ; \ Print string | : $. ( c -- ) type ; \ Print string |
en/pfw/string_handling.1713362121.txt.gz · Last modified: 2024-04-17 15:55 by willem