0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00

Only escape if utf8 character in basic ascii range

This commit is contained in:
Aaron 2008-12-30 18:20:57 -05:00
parent 6b9b6ffa77
commit e598a0cedd
2 changed files with 22 additions and 4 deletions

View File

@ -108,7 +108,13 @@ string BSONElement::toString() const {
string escape( string s ) {
stringstream ret;
for( string::iterator i = s.begin(); i != s.end(); ++i ) {
unsigned char last = '0';
for( string::iterator i = s.begin(); i != s.end(); last = *i, ++i ) {
// for now, assume utf-8 encoding.
if ( last & 0x80 ) {
ret << *i;
continue;
}
switch( *i ) {
case '"':
ret << "\\\"";
@ -135,7 +141,7 @@ string escape( string s ) {
ret << "\\t";
break;
default:
if ( ( *i >= 0 && *i <= 0x1f ) || *i == 0x7f ) {
if ( *i >= 0 && *i <= 0x1f ) {
ret << "\\u";
ret << hex;
ret.width( 4 );

View File

@ -126,12 +126,14 @@ namespace JsonStringTests {
}
};
// per http://www.ietf.org/rfc/rfc4627.txt, control characters are
// (U+0000 through U+001F). U+007F is not mentioned as a control character.
class AdditionalControlCharacters {
public:
void run() {
BSONObjBuilder b;
b.append( "a", "\x1 \x1f \x7f" );
ASSERT_EQUALS( "{ \"a\" : \"\\u0001 \\u001f \\u007f\" }", b.done().jsonString() );
b.append( "a", "\x1 \x1f" );
ASSERT_EQUALS( "{ \"a\" : \"\\u0001 \\u001f\" }", b.done().jsonString() );
}
};
@ -144,6 +146,15 @@ namespace JsonStringTests {
}
};
class AsciiControlCharacterButNotUtf8UnicodeControlCharacter {
public:
void run() {
BSONObjBuilder b;
b.append( "a", "\x80\x01\x80\b" );
ASSERT_EQUALS( "{ \"a\" : \"\x80\x01\x80\b\" }", b.done().jsonString() );
}
};
class SingleIntMember {
public:
void run() {
@ -333,6 +344,7 @@ public:
add< BSONObjTests::JsonStringTests::EscapedCharacters >();
add< BSONObjTests::JsonStringTests::AdditionalControlCharacters >();
add< BSONObjTests::JsonStringTests::ExtendedAscii >();
add< BSONObjTests::JsonStringTests::AsciiControlCharacterButNotUtf8UnicodeControlCharacter >();
add< BSONObjTests::JsonStringTests::SingleIntMember >();
add< BSONObjTests::JsonStringTests::SingleNumberMember >();
add< BSONObjTests::JsonStringTests::InvalidNumbers >();