Looks right to me (and runs correctly when I step through examples including ], ]] and ]]]in the middle and at the end of CDATA sections).