On Fri, Apr 29, 2011 at 1:08 PM, Raul Miller <[email protected]> wrote:
> subst6=: (8 u: [: u:@dfh 2 }. ]) ::] ^:(6&=@# *. ('\' = {.) *. 'uU'
> e.~ {.@}.)&.>

Watch out for split lines.

Here's that one again, with some whitespace removed to hopefully help
the code escape mutilation:

NB. backslash evaluator

subst2=:(_2<\'\"\\\/\b\f\n\r\t')&(i.{(34 92 47 8 12 10 13 9{&.><a.),])"0
hexchars=. 'ABCDEF0123456789abcdef'
dfh=: 16 #. 16 | _6 + hexchars i. ]
subst6=:(8 u:[:u:@dfh 2}.]) ::]^:(6&=@#*.('\'={.)*.'uU'e.~{.@}.)&.>

splitbs=: (0;(0 10#:10*".;._2]0 :0);(a.e.hexchars,'Uu')+2*a.='\')&;:
 1.1  1.1  2.1 NB. state 0 -- start
 1.0  1.0  2.2 NB. state 1 -- ordinary characters
 3.0  4.0  3.0 NB. state 2 -- \
 1.2  1.2  2.2 NB. state 3 -- \n or \u0000
 1.2  5.0  1.2 NB. state 4 -- \u
 1.2  6.0  1.2 NB. state 5 -- \u0
 1.2  7.0  1.2 NB. state 6 -- \u00
 1.2  3.0  1.2 NB. state 7 -- \u000
)
evalbs=: [:; [:subst2  [:subst6 splitbs


NB. test suite for backslash evaluator
testbs=: (7 u: evalbs@[) assert@-: 4 u: ]
NB. ordinary text should remain unchanged
'abcd' testbs 97+i.4
NB. unicode escapes should work
'\u005c\U00a5\U20a9' testbs 92 165 8361
NB. backslash escapes should work
'\"\\\/\b\f\n\r\t' testbs 34 92 47 8 12 10 13 9


-- 
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to