Commit 1cd6dcfe authored by EXT Arnaud Clère's avatar EXT Arnaud Clère
Browse files

Fixed JsonWriter text escaping

parent f94d7234
......@@ -223,9 +223,9 @@ protected:
bool _sequence() { levels.push(Step{"","]"});
return io->write( "[" ); }
bool _null() { return io->write("null" ); }
bool _bind(const char* s) { return(io->write( "\"" ) &&
io->write( s ) && // TODO escape
io->write( "\"" )); }
bool _bind(const char* s) { io->write( "\"" );
escape ( s );
return io->write( "\"" ); }
// Natively supported overloads
bool _bind( bool& n) { return io->write( n? "true" : "false" ); }
......@@ -245,6 +245,32 @@ protected:
private:
struct Step { const char* sep; const char* end; };
void escape(const char* utf8)
{
for (; utf8 && *utf8 ; utf8++)
{
/**/ if (*utf8 == '\\') { io->putChar('\\'); io->putChar('\\'); }
else if (*utf8 == '"') { io->putChar('\\'); io->putChar( '"'); }
else if (*utf8 < 0|| // UTF-8 sequence bytes
*utf8 >= ' ') { io->putChar(*utf8); }
else {
io->putChar('\\'); switch (*utf8) {
case '\t' : io->putChar( 't'); break;
case '\b' : io->putChar( 'b'); break;
case '\f' : io->putChar( 'f'); break;
case '\n' : io->putChar( 'n'); break;
case '\r' : io->putChar( 'r'); break;
default : io->putChar( 'u'); {
char high=((*utf8) / 16), low=((*utf8) % 16);
io->putChar( '0' );
io->putChar( '0' );
io->putChar(high+(high<10?'0':'A'-10));
io->putChar( low+( low<10?'0':'A'-10)); } break;
}
}
}
}
QIODevice* io;
QStack<Step> levels = QStack<Step>(); //!< minimal dynamic context to implement out() and ensure well-formedness in case TResult is abandoned
};
......
......@@ -143,7 +143,7 @@ int main()
if (!samples) return -1;
// Read-only args
QString text("..ascii characters + U+A4 ¤ U+B0 ° U+D8 Ø U+FF ÿ..");
QString text("..ascii characters + U+01 \x01 + U+1F \x1F + U+A4 ¤ U+B0 ° U+D8 Ø U+FF ÿ..");
QVector<double> transform({1./3, 2./3, 1./3, 1.,
2./3, 1./3, 2./3, 1.,
1./3, 2./3, 1./3, 1.,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment