Correctly handle strings with escaped quotes and apostrophes.

Closes #1050. Fixes #962.