Document saving improvements - no escaping is done for ' character or for symbols in second half of ASCII table; format_utf8 is therefore gone

git-svn-id: http://pugixml.googlecode.com/svn/trunk@95 99668b35-9821-0410-8761-19e4c4f06640
This commit is contained in:
arseny.kapoulkine 2009-01-05 20:23:14 +00:00
parent e516c967f9
commit d2443e8948
2 changed files with 19 additions and 40 deletions

View File

@ -405,6 +405,13 @@ namespace
return reinterpret_cast<const char*>(str); return reinterpret_cast<const char*>(str);
} }
template <bool _1> struct opt1_to_type
{
static const bool o1;
};
template <bool _1> const bool opt1_to_type<_1>::o1 = _1;
template <bool _1, bool _2> struct opt2_to_type template <bool _1, bool _2> struct opt2_to_type
{ {
static const bool o1; static const bool o1;
@ -428,18 +435,17 @@ namespace
template <bool _1, bool _2, bool _3, bool _4> const bool opt4_to_type<_1, _2, _3, _4>::o4 = _4; template <bool _1, bool _2, bool _3, bool _4> const bool opt4_to_type<_1, _2, _3, _4>::o4 = _4;
#ifndef PUGIXML_NO_STL #ifndef PUGIXML_NO_STL
template <typename opt2> void text_output_escaped(std::ostream& os, const char* s, opt2) template <typename opt1> void text_output_escaped(std::ostream& os, const char* s, opt1)
{ {
const bool attribute = opt2::o1; const bool attribute = opt1::o1;
const bool utf8 = opt2::o2;
while (*s) while (*s)
{ {
const char* prev = s; const char* prev = s;
// While *s is a usual symbol // While *s is a usual symbol
while (*s && *s != '&' && *s != '<' && *s != '>' && ((*s != '"' && *s != '\'') || !attribute) while (*s && *s != '&' && *s != '<' && *s != '>' && (*s != '"' || !attribute)
&& (*s >= 32 || (*s == '\r' && !attribute) || (*s == '\n' && !attribute) || *s == '\t')) && (*s < 0 || *s >= 32 || (*s == '\r' && !attribute) || (*s == '\n' && !attribute) || *s == '\t'))
++s; ++s;
if (prev != s) os.write(prev, static_cast<std::streamsize>(s - prev)); if (prev != s) os.write(prev, static_cast<std::streamsize>(s - prev));
@ -463,10 +469,6 @@ namespace
os << "&quot;"; os << "&quot;";
++s; ++s;
break; break;
case '\'':
os << "&apos;";
++s;
break;
case '\r': case '\r':
os << "&#13;"; os << "&#13;";
++s; ++s;
@ -477,12 +479,7 @@ namespace
break; break;
default: // s is not a usual symbol default: // s is not a usual symbol
{ {
unsigned int ch; unsigned int ch = (unsigned char)*s++;
if (utf8)
s = strutf8_utf16(s, ch);
else
ch = (unsigned char)*s++;
os << "&#" << ch << ";"; os << "&#" << ch << ";";
} }
@ -2209,10 +2206,7 @@ namespace pugi
{ {
os << ' ' << a.name() << "=\""; os << ' ' << a.name() << "=\"";
if (flags & format_utf8) text_output_escaped(os, a.value(), opt1_to_type<1>());
text_output_escaped(os, a.value(), opt2_to_type<1, 1>());
else
text_output_escaped(os, a.value(), opt2_to_type<1, 0>());
os << "\""; os << "\"";
} }
@ -2235,10 +2229,7 @@ namespace pugi
{ {
os << ">"; os << ">";
if (flags & format_utf8) text_output_escaped(os, first_child().value(), opt1_to_type<0>());
text_output_escaped(os, first_child().value(), opt2_to_type<0, 1>());
else
text_output_escaped(os, first_child().value(), opt2_to_type<0, 0>());
os << "</" << name() << ">\n"; os << "</" << name() << ">\n";
} }
@ -2259,10 +2250,7 @@ namespace pugi
} }
case node_pcdata: case node_pcdata:
if (flags & format_utf8) text_output_escaped(os, value(), opt1_to_type<0>());
text_output_escaped(os, value(), opt2_to_type<0, 1>());
else
text_output_escaped(os, value(), opt2_to_type<0, 0>());
break; break;
case node_cdata: case node_cdata:

View File

@ -174,21 +174,12 @@ namespace pugi
*/ */
const unsigned int format_indent = 0x01; const unsigned int format_indent = 0x01;
/**
* This flag determines how the non-printable symbols are written to output stream - they are
* either considered UTF-8 and are written as UTF-8 character, escaped with &#...;, or they are
* considered to be ASCII and each ASCII character is escaped separately.
*
* This flag is on by default.
*/
const unsigned int format_utf8 = 0x02;
/** /**
* This flag determines if UTF-8 BOM is to be written to output stream. * This flag determines if UTF-8 BOM is to be written to output stream.
* *
* This flag is off by default. * This flag is off by default.
*/ */
const unsigned int format_write_bom = 0x04; const unsigned int format_write_bom = 0x02;
/** /**
* If this flag is on, no indentation is performed and no line breaks are written to output file. * If this flag is on, no indentation is performed and no line breaks are written to output file.
@ -196,13 +187,13 @@ namespace pugi
* *
* This flag is off by default. * This flag is off by default.
*/ */
const unsigned int format_raw = 0x08; const unsigned int format_raw = 0x04;
/** /**
* This is the default set of formatting flags. It includes indenting nodes depending on their * This is the default set of formatting flags. It includes indenting nodes depending on their
* depth in DOM tree and considering input data to be UTF-8. * depth in DOM tree.
*/ */
const unsigned int format_default = format_indent | format_utf8; const unsigned int format_default = format_indent;
// Forward declarations // Forward declarations
struct xml_attribute_struct; struct xml_attribute_struct;