Sujet : Re: Reverse SCAN SPLIT
De : ruvim.pinka (at) *nospam* gmail.com (Ruvim)
Groupes : comp.lang.forthDate : 07. Oct 2024, 11:02:05
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <ve0bit$1lo0q$1@dont-email.me>
References : 1
User-Agent : Mozilla Thunderbird
On 2024-10-07 12:52, dxf wrote:
Earlier I mentioned scanning in reverse. Here's an implementation.
[undefined] dxforth [if]
: \CHAR ( a u -- a2 u2 c ) 1- 2dup + c@ ;
[then]
\ As for SCAN but scan from end
: SCAN< ( a u c -- a2 u2 | a 0 )
>r over swap begin dup while \char r@ = until 1+ then
rot drop rdrop ;
\ As for SPLIT but scan from end. Latter string is topmost.
: SPLIT< ( a u c -- a2 u2 a3 u3 )
>r 2dup r> scan< 2swap 2 pick /string ;
\ example
: /T ( a u -- hour min sec )
3 0 do
[char] : split< 0 0 2swap >number 2drop drop -rot
( u ... a u) dup if 1- then
loop 2drop swap rot ;
^^^^^^^^ (1)
: T /t cr rot . ." hr " swap . ." min " . ." sec " ;
^^^ ^^^^ (2)
Why do you prefer the order ( u.hour u.min u.sec ) rather than ( u.sec u.min u.hour ) ?
The later order makes code simpler in (1) and (2), also, it follows the order of parameters in `TIME&DATE`.
Moreover, if you want to convert three components to seconds, you need to reverse their order again:
: time3-to-seconds ( u.hour u.min u.sec -- u.sec-total )
swap rot 60 * + 60 * +
;
If the order of parameters is reversed, the above definition takes a simpler form:
: time3-to-seconds ( u.sec u.min u.hour -- u.sec-total )
60 * + 60 * +
;
-- Ruvim