summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArseny Kapoulkine <arseny.kapoulkine@gmail.com>2015-01-16 20:54:58 -0800
committerArseny Kapoulkine <arseny.kapoulkine@gmail.com>2015-01-16 20:54:58 -0800
commit550ab4b6356ba203acb6aaebcd3d41acb9e1fa4a (patch)
treeff9bdb4553d7c7c23ecf3e6822d5bd92fdae15fc
parentf3e42969a5118247de548c059e9bed69cdf208bb (diff)
parentcf72c20ca173bbad227e8f70bd68a2dbfb5b2890 (diff)
Merge pull request #27 from sdoiel61/master
Make float/double round-trip This change also adds xml_text::set and xml_attribute::set_value overloads for float so that float is only printed using just enough digits to represent float, instead of enough digits to represent double.
-rw-r--r--src/pugixml.cpp36
-rw-r--r--src/pugixml.hpp4
-rw-r--r--tests/test_dom_modify.cpp4
3 files changed, 41 insertions, 3 deletions
diff --git a/src/pugixml.cpp b/src/pugixml.cpp
index dd3f427..9760e9f 100644
--- a/src/pugixml.cpp
+++ b/src/pugixml.cpp
@@ -3954,10 +3954,18 @@ PUGI__NS_BEGIN
return set_value_buffer(dest, header, header_mask, buf);
}
+ PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, float value)
+ {
+ char buf[128];
+ sprintf(buf, "%.9g", value);
+
+ return set_value_buffer(dest, header, header_mask, buf);
+ }
+
PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, double value)
{
char buf[128];
- sprintf(buf, "%g", value);
+ sprintf(buf, "%.17g", value);
return set_value_buffer(dest, header, header_mask, buf);
}
@@ -4545,6 +4553,12 @@ namespace pugi
return *this;
}
+ PUGI__FN xml_attribute& xml_attribute::operator=(float rhs)
+ {
+ set_value(rhs);
+ return *this;
+ }
+
PUGI__FN xml_attribute& xml_attribute::operator=(bool rhs)
{
set_value(rhs);
@@ -4600,6 +4614,13 @@ namespace pugi
return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs);
}
+ PUGI__FN bool xml_attribute::set_value(float rhs)
+ {
+ if (!_attr) return false;
+
+ return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs);
+ }
+
PUGI__FN bool xml_attribute::set_value(bool rhs)
{
if (!_attr) return false;
@@ -5603,6 +5624,13 @@ namespace pugi
return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
}
+ PUGI__FN bool xml_text::set(float rhs)
+ {
+ xml_node_struct* dn = _data_new();
+
+ return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
+ }
+
PUGI__FN bool xml_text::set(double rhs)
{
xml_node_struct* dn = _data_new();
@@ -5657,6 +5685,12 @@ namespace pugi
return *this;
}
+ PUGI__FN xml_text& xml_text::operator=(float rhs)
+ {
+ set(rhs);
+ return *this;
+ }
+
PUGI__FN xml_text& xml_text::operator=(bool rhs)
{
set(rhs);
diff --git a/src/pugixml.hpp b/src/pugixml.hpp
index 2432a3f..240b5aa 100644
--- a/src/pugixml.hpp
+++ b/src/pugixml.hpp
@@ -352,6 +352,7 @@ namespace pugi
bool set_value(int rhs);
bool set_value(unsigned int rhs);
bool set_value(double rhs);
+ bool set_value(float rhs);
bool set_value(bool rhs);
#ifdef PUGIXML_HAS_LONG_LONG
@@ -364,6 +365,7 @@ namespace pugi
xml_attribute& operator=(int rhs);
xml_attribute& operator=(unsigned int rhs);
xml_attribute& operator=(double rhs);
+ xml_attribute& operator=(float rhs);
xml_attribute& operator=(bool rhs);
#ifdef PUGIXML_HAS_LONG_LONG
@@ -693,6 +695,7 @@ namespace pugi
// Set text with type conversion (numbers are converted to strings, boolean is converted to "true"/"false")
bool set(int rhs);
bool set(unsigned int rhs);
+ bool set(float rhs);
bool set(double rhs);
bool set(bool rhs);
@@ -706,6 +709,7 @@ namespace pugi
xml_text& operator=(int rhs);
xml_text& operator=(unsigned int rhs);
xml_text& operator=(double rhs);
+ xml_text& operator=(float rhs);
xml_text& operator=(bool rhs);
#ifdef PUGIXML_HAS_LONG_LONG
diff --git a/tests/test_dom_modify.cpp b/tests/test_dom_modify.cpp
index 7863718..85c381e 100644
--- a/tests/test_dom_modify.cpp
+++ b/tests/test_dom_modify.cpp
@@ -753,8 +753,8 @@ TEST_XML(dom_attr_assign_large_number, "<node attr1='' attr2='' />")
node.attribute(STR("attr1")) = std::numeric_limits<float>::max();
node.attribute(STR("attr2")) = std::numeric_limits<double>::max();
- CHECK(test_node(node, STR("<node attr1=\"3.40282e+038\" attr2=\"1.79769e+308\" />"), STR(""), pugi::format_raw) ||
- test_node(node, STR("<node attr1=\"3.40282e+38\" attr2=\"1.79769e+308\" />"), STR(""), pugi::format_raw));
+ CHECK(test_node(node, STR("<node attr1=\"3.40282347e+038\" attr2=\"1.7976931348623157ee+308\" />"), STR(""), pugi::format_raw) ||
+ test_node(node, STR("<node attr1=\"3.40282347e+38\" attr2=\"1.7976931348623157e+308\" />"), STR(""), pugi::format_raw));
}
TEST(dom_node_declaration_name)