Here's another bugfix:

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)+(2*a.='\')+3*a.e.'Uu')&;:
 1.1  1.1  2.1 1.1 NB. state 0 -- start
 1.0  1.0  2.2 1.0 NB. state 1 -- ordinary characters
 3.0  3.0  3.0 4.0 NB. state 2 -- \
 1.2  1.2  2.2 1.2 NB. state 3 -- \n or \u0000
 1.2  5.0  2.2 1.2 NB. state 4 -- \u
 1.2  6.0  2.2 1.2 NB. state 5 -- \u0
 1.2  7.0  2.2 1.2 NB. state 6 -- \u00
 1.2  3.0  2.2 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
NB. empty strings should work
'' testbs ''
NB. backslash processing should not get confused about state
'\a\\\b000' testbs 92 97 92 8 48 48 48

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

Reply via email to