Fixes #715

Walks characters forward counting open brackets so the code doesn't accidentally insert *inside* the for loop. Added test too.
Merge pull request #723 from remy/fix/loop-rewrite-with-brackets

Fixes #721 - loop with brackets inside conditional