|
|
 | | From: | Jorge Acereda | | Subject: | Negative strings | | Date: | Mon, 24 Jan 2005 00:25:11 +0100 |
|
|
 | Hi all,
I'm considering adding support for negative strings to my toy system. What are negative strings? Lacking a better name, I use it for strings with a negative count value and pointing to the last char.
: sdir ( n -- -1 | 1, Calculate appropriate increment for strings ) 0< 2* 1+ ;
: snegate ( c-addr1 n1 -- c-addr2 n2, negate string) >r r@ + r@ sdir - r> negate ;
: /string ( c-addr1 n1 n -- c-addr2 n2, advance in string by n chars) over sdir * >r r@ under+ r> - ;
With some careful coding (i.e, always using /STRING when dealing with strings), you can get some interesting functions with little effort. Just some examples:
: -trailing ( a n1 -- a n2, remove trailing whitespace ) snegate bl skip snegate ;
: -extension ( c-addr n1 -- c-addr n2, remove filename extension) snegate [char] . scan 1 /string snegate ;
: palindrome ( c-addr1 n1 -- flag, simple and inneficient palindrome test) 2dup snegate compare 0= ;
There's an obvious disadvantage (besides speed) when implementing such behaviour. ANS compatibility would be dropped, since the standard specifies count values as positive integers. Is there any good rationale behind that decision?
Also, do you know any system that uses negative strings for this (or other) purpose?
TIA, Jorge Acereda
|
|
|