6.1.1540 FILL CORE

( c-addr u char -- )

If u is greater than zero, store char in each of u consecutive characters of memory beginning at c-addr.

Testing:

T{ FBUF 0 20 FILL -> }T
T{ SEEBUF -> 00 00 00 }T

T{ FBUF 1 20 FILL -> }T
T{ SEEBUF -> 20 00 00 }T

T{ FBUF 3 20 FILL -> }T
T{ SEEBUF -> 20 20 20 }T

ContributeContributions

NieDzejkobavatar of NieDzejkob [37] Suggested reference implementationSuggested reference implementation2018-01-06 18:08:55

: FILL -ROT 0 ?DO 2DUP C! 1+ LOOP 2DROP ;

alextangentavatar of alextangent

-ROT is not in the standard. A reference implementation should (must?) use standard words.

NieDzejkobavatar of NieDzejkobNew Version: Suggested reference implementation

Hide differences

: FILL -ROT 0 ?DO 2DUP C! 1+ LOOP 2DROP ;

: FILL ( c-char u char -- ) ROT ROT 0 ?DO 2DUP C! CHAR+ LOOP 2DROP ;

The ROT ROT does not seem right to me, but finding anything better has proven not to be simple.

alextangentavatar of alextangent

Consider not using ?DO

: FILL ( c-addr u char -- )
    >R BEGIN DUP 0<> WHILE
      OVER R@ SWAP C!
      1 /STRING
    REPEAT R> DROP ;

ikysilavatar of ikysil

Implementation with BEGIN leaves c-addr u on the stack.

Corrected version:

: FILL ( c-addr u char -- )
   ( If u is greater than zero, store char in each of u consecutive characters of memory beginning at c-addr.)
   >R BEGIN
      DUP 0<>
   WHILE
      OVER R@ SWAP C!
      1 /STRING
   REPEAT
   2DROP
   R> DROP
;

superseeker13avatar of superseeker13

I prefer NieDzejkob's solution. Using a core-ext word, 0<>, in a core word seems to defeat the point of core-ext's optional nature.

Reply New Version