From 62204df423eea909798045c834fc6aafa2ed17fe Mon Sep 17 00:00:00 2001
From: "arseny.kapoulkine@gmail.com"
 <arseny.kapoulkine@gmail.com@99668b35-9821-0410-8761-19e4c4f06640>
Date: Tue, 27 Mar 2012 04:23:05 +0000
Subject: Write BOM as U+FEFF to buffered writer; this makes sure we don't have
 a very small unbuffered write with custom writer implementations

git-svn-id: http://pugixml.googlecode.com/svn/trunk@887 99668b35-9821-0410-8761-19e4c4f06640
---
 src/pugixml.cpp | 45 +++++++++++----------------------------------
 1 file changed, 11 insertions(+), 34 deletions(-)

(limited to 'src')

diff --git a/src/pugixml.cpp b/src/pugixml.cpp
index 01eeaaf..bee8f4a 100644
--- a/src/pugixml.cpp
+++ b/src/pugixml.cpp
@@ -2989,38 +2989,6 @@ PUGI__NS_BEGIN
 		xml_encoding encoding;
 	};
 
-	PUGI__FN void write_bom(xml_writer& writer, xml_encoding encoding)
-	{
-		switch (encoding)
-		{
-		case encoding_utf8:
-			writer.write("\xef\xbb\xbf", 3);
-			break;
-
-		case encoding_utf16_be:
-			writer.write("\xfe\xff", 2);
-			break;
-
-		case encoding_utf16_le:
-			writer.write("\xff\xfe", 2);
-			break;
-
-		case encoding_utf32_be:
-			writer.write("\x00\x00\xfe\xff", 4);
-			break;
-
-		case encoding_utf32_le:
-			writer.write("\xff\xfe\x00\x00", 4);
-			break;
-
-        case encoding_latin1:
-            break;
-
-		default:
-			assert(!"Invalid encoding");
-		}
-	}
-
 	PUGI__FN void text_output_escaped(xml_buffered_writer& writer, const char_t* s, chartypex_t type)
 	{
 		while (*s)
@@ -5223,10 +5191,19 @@ namespace pugi
 
 	PUGI__FN void xml_document::save(xml_writer& writer, const char_t* indent, unsigned int flags, xml_encoding encoding) const
 	{
-		if (flags & format_write_bom) impl::write_bom(writer, impl::get_write_encoding(encoding));
-
 		impl::xml_buffered_writer buffered_writer(writer, encoding);
 
+		if ((flags & format_write_bom) && encoding != encoding_latin1)
+        {
+            // BOM always represents the codepoint U+FEFF, so just write it in native encoding
+        #ifdef PUGIXML_WCHAR_MODE
+            uint16_t bom = 0xfeff;
+            buffered_writer.write(static_cast<wchar_t>(bom));
+        #else
+            buffered_writer.write('\xef', '\xbb', '\xbf');
+        #endif
+        }
+
 		if (!(flags & format_no_declaration) && !impl::has_declaration(*this))
 		{
 			buffered_writer.write(PUGIXML_TEXT("<?xml version=\"1.0\""));
-- 
cgit v1.2.3