metaborg / mb-rep

Apache License 2.0
2 stars 4 forks source link

String escape #18

Closed Virtlink closed 4 years ago

Virtlink commented 4 years ago

This PR adds a CStringEscaper class that escapes and unescapes strings in C-style (e.g., \', \", \r, \n).

The string escapes in StrategoString have been replaced by calls to this new class. The class does the same escapes (double quotes, not single quotes, and backslashes), but also escapes control codes that cannot be displayed. Most importantly, it works more efficiently as it walks over the string only once instead of however many times replace() was called in the original code. For writeAsString() the implementation directly writes to the Appendable whereas the original implementation created intermediate strings.

The implementation is verified by 1444 tests.

Apanatshka commented 4 years ago

Can you add a companion PR for SPT? I've found another instance of this kind of escaping in spt/org.metaborg.spt.core/src/main/java/org/metaborg/spt/core/SPTUtil.java.