17.6.1.0245 /STRING slash-string STRING
Adjust the character string at c-addr1 by n characters. The resulting character string, specified by c-addr2 u2, begins at c-addr1 plus n characters and is u1 minus n characters long.
See:
Rationale:
S" ABC" 2 /STRING 2DUP TYPE \ outputs "C"
-1 /STRING TYPE \ outputs "BC"
Testing:
T{ s1 10 /STRING -4 /STRING -> s1 6 /STRING }T
T{ s1 0 /STRING -> s1 }T
ContributeContributions
JimPeterson [203] Possible Reference ImplementationSuggested reference implementation2021-05-21 17:58:29
Would this be a sufficient reference implementation?:
: /STRING DUP >R - SWAP R> CHARS + SWAP ;
Are there no checks for when u2 might go negative (or, really, wraps around, as it's considered unsigned)?
ruv [352] Unspecified ambiguous condition in /STRINGRequest for clarification2024-07-28 17:17:53
The specification says that u.2
is u.1
minus n
.
If n
is greater than u.1
, the result of subtraction is wrapped around, and the resulting ( c-addr.2 u.2 )
cannot not be interpreted as a character string. Is it correct?
I think, either behavior (or maybe meaning) should be specified, or an ambiguous condition should be explicitly declared for this case.
Thus, I see the following options:
- change the result stack parameter data type to ( c-addr.2 n.2 ) and describe what these parameters mean if n.2 is negative;
- then, the input stack parameter data type should be changed to
( c-addr.1 +n.1 n )
, and/string
will not be applicable to character strings longer thanmax-n
;
- then, the input stack parameter data type should be changed to
- specify that if n is greater than u.1, it is replaced by u.1;
- declare that an ambiguous condition exists if n is greater than u.1;
- specify that if n is greater than u.1, an exception is thrown.
It seems, the last option is preferable. What do you think?