+static void rewriteStringLiteral(AST::StringLiteral *ast, const QString *code, int startPosition, TextWriter *writer)
+{
+ const unsigned position = ast->firstSourceLocation().begin() - startPosition + 1;
+ const unsigned length = ast->literalToken.length - 2;
+ const QStringRef spell = code->midRef(position, length);
+ const int end = spell.size();
+ int index = 0;
+
+ while (index < end) {
+ const QChar ch = spell.at(index++);
+
+ if (index < end && ch == QLatin1Char('\\')) {
+ int pos = index;
+
+ // skip a possibly empty sequence of \r characters
+ while (pos < end && spell.at(pos) == QLatin1Char('\r'))
+ ++pos;
+
+ if (pos < end && spell.at(pos) == QLatin1Char('\n')) {
+ // This is a `\' followed by a newline terminator.
+ // In this case there's nothing to replace. We keep the code
+ // as it is and we resume the searching.
+ index = pos + 1; // refresh the index
+ }
+ } else if (ch == QLatin1Char('\r') || ch == QLatin1Char('\n')) {
+ const QString sep = ch == QLatin1Char('\r') ? QLatin1String("\\r") : QLatin1String("\\n");
+ const int pos = index - 1;
+ QString s = sep;
+
+ while (index < end && spell.at(index) == ch) {
+ s += sep;
+ ++index;
+ }
+
+ writer->replace(position + pos, index - pos, s);
+ }
+ }
+}
+