summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArseny Kapoulkine <arseny.kapoulkine@gmail.com>2014-01-15 04:28:10 +0000
committerArseny Kapoulkine <arseny.kapoulkine@gmail.com>2014-01-15 04:28:10 +0000
commit7f6b062e9f51c98e56713b1869c01573d7fe855f (patch)
treec372c947728a08361f2a5575d2f50cd9cf80cb08
parent783af7926420e17f10d80d74cf3459b2c87d2541 (diff)
Implement automatic hexadecimal decoding for xml_attribute::as_int and xml_text::as_int. This is effectively a form of strtol with base 0, but without octal support.
git-svn-id: http://pugixml.googlecode.com/svn/trunk@958 99668b35-9821-0410-8761-19e4c4f06640
-rw-r--r--docs/manual.qbk2
-rw-r--r--src/pugixml.cpp27
-rw-r--r--tests/test_dom_text.cpp40
-rw-r--r--tests/test_dom_traverse.cpp40
4 files changed, 99 insertions, 10 deletions
diff --git a/docs/manual.qbk b/docs/manual.qbk
index 587c2b4..820b9e7 100644
--- a/docs/manual.qbk
+++ b/docs/manual.qbk
@@ -882,7 +882,7 @@ In many cases attribute values have types that are not strings - i.e. an attribu
float xml_attribute::as_float(float def = 0) const;
bool xml_attribute::as_bool(bool def = false) const;
-`as_int`, `as_uint`, `as_double` and `as_float` convert attribute values to numbers. If attribute handle is null or attribute value is empty, `def` argument is returned (which is 0 by default). Otherwise, all leading whitespace characters are truncated, and the remaining string is parsed as a decimal number (`as_int` or `as_uint`) or as a floating point number in either decimal or scientific form (`as_double` or `as_float`). Any extra characters are silently discarded, i.e. `as_int` will return `1` for string `"1abc"`.
+`as_int`, `as_uint`, `as_double` and `as_float` convert attribute values to numbers. If attribute handle is null or attribute value is empty, `def` argument is returned (which is 0 by default). Otherwise, all leading whitespace characters are truncated, and the remaining string is parsed as an integer number in either decimal or hexadecimal form (`as_int` or `as_uint`; hexadecimal format is used if the number has `0x` or `0X` prefix) or as a floating point number in either decimal or scientific form (`as_double` or `as_float`). Any extra characters are silently discarded, i.e. `as_int` will return `1` for string `"1abc"`.
In case the input string contains a number that is out of the target numeric range, the result is undefined.
diff --git a/src/pugixml.cpp b/src/pugixml.cpp
index be8bcea..b637f27 100644
--- a/src/pugixml.cpp
+++ b/src/pugixml.cpp
@@ -3314,14 +3314,29 @@ PUGI__NS_BEGIN
}
// get value with conversion functions
+ PUGI__FN int get_integer_base(const char_t* value)
+ {
+ const char_t* s = value;
+
+ while (PUGI__IS_CHARTYPE(*s, ct_space))
+ s++;
+
+ if (*s == '-')
+ s++;
+
+ return (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? 16 : 10;
+ }
+
PUGI__FN int get_value_int(const char_t* value, int def)
{
if (!value) return def;
+ int base = get_integer_base(value);
+
#ifdef PUGIXML_WCHAR_MODE
- return static_cast<int>(wcstol(value, 0, 10));
+ return static_cast<int>(wcstol(value, 0, base));
#else
- return static_cast<int>(strtol(value, 0, 10));
+ return static_cast<int>(strtol(value, 0, base));
#endif
}
@@ -3329,10 +3344,12 @@ PUGI__NS_BEGIN
{
if (!value) return def;
+ int base = get_integer_base(value);
+
#ifdef PUGIXML_WCHAR_MODE
- return static_cast<unsigned int>(wcstoul(value, 0, 10));
+ return static_cast<unsigned int>(wcstoul(value, 0, base));
#else
- return static_cast<unsigned int>(strtoul(value, 0, 10));
+ return static_cast<unsigned int>(strtoul(value, 0, base));
#endif
}
@@ -9876,7 +9893,7 @@ namespace pugi
return error ? error : "No error";
}
- PUGI__FN xpath_variable::xpath_variable()
+ PUGI__FN xpath_variable::xpath_variable(): _type(xpath_type_none), _next(0)
{
}
diff --git a/tests/test_dom_text.cpp b/tests/test_dom_text.cpp
index 153c372..2e38f29 100644
--- a/tests/test_dom_text.cpp
+++ b/tests/test_dom_text.cpp
@@ -57,7 +57,7 @@ TEST_XML_FLAGS(dom_text_as_string, "<node><a>foo</a><b><node/><![CDATA[bar]]></b
CHECK_STRING(xml_node().text().as_string(), STR(""));
}
-TEST_XML(dom_text_as_int, "<node><text1>1</text1><text2>-1</text2><text3>-2147483648</text3><text4>2147483647</text4></node>")
+TEST_XML(dom_text_as_int, "<node><text1>1</text1><text2>-1</text2><text3>-2147483648</text3><text4>2147483647</text4><text5>0</text5></node>")
{
xml_node node = doc.child(STR("node"));
@@ -66,9 +66,22 @@ TEST_XML(dom_text_as_int, "<node><text1>1</text1><text2>-1</text2><text3>-214748
CHECK(node.child(STR("text2")).text().as_int() == -1);
CHECK(node.child(STR("text3")).text().as_int() == -2147483647 - 1);
CHECK(node.child(STR("text4")).text().as_int() == 2147483647);
+ CHECK(node.child(STR("text5")).text().as_int() == 0);
}
-TEST_XML(dom_text_as_uint, "<node><text1>0</text1><text2>1</text2><text3>2147483647</text3><text4>4294967295</text4></node>")
+TEST_XML(dom_text_as_int_hex, "<node><text1>0777</text1><text2>0x5ab</text2><text3>0XFf</text3><text4>-0x20</text4><text5>-0x80000000</text5><text6>0x</text6></node>")
+{
+ xml_node node = doc.child(STR("node"));
+
+ CHECK(node.child(STR("text1")).text().as_int() == 777); // no octal support! intentional
+ CHECK(node.child(STR("text2")).text().as_int() == 1451);
+ CHECK(node.child(STR("text3")).text().as_int() == 255);
+ CHECK(node.child(STR("text4")).text().as_int() == -32);
+ CHECK(node.child(STR("text5")).text().as_int() == -2147483647 - 1);
+ CHECK(node.child(STR("text6")).text().as_int() == 0);
+}
+
+TEST_XML(dom_text_as_uint, "<node><text1>0</text1><text2>1</text2><text3>2147483647</text3><text4>4294967295</text4><text5>0</text5></node>")
{
xml_node node = doc.child(STR("node"));
@@ -77,6 +90,29 @@ TEST_XML(dom_text_as_uint, "<node><text1>0</text1><text2>1</text2><text3>2147483
CHECK(node.child(STR("text2")).text().as_uint() == 1);
CHECK(node.child(STR("text3")).text().as_uint() == 2147483647);
CHECK(node.child(STR("text4")).text().as_uint() == 4294967295u);
+ CHECK(node.child(STR("text5")).text().as_uint() == 0);
+}
+
+TEST_XML(dom_text_as_uint_hex, "<node><text1>0777</text1><text2>0x5ab</text2><text3>0XFf</text3><text4>0x20</text4><text5>0xFFFFFFFF</text5><text6>0x</text6></node>")
+{
+ xml_node node = doc.child(STR("node"));
+
+ CHECK(node.child(STR("text1")).text().as_uint() == 777); // no octal support! intentional
+ CHECK(node.child(STR("text2")).text().as_uint() == 1451);
+ CHECK(node.child(STR("text3")).text().as_uint() == 255);
+ CHECK(node.child(STR("text4")).text().as_uint() == 32);
+ CHECK(node.child(STR("text5")).text().as_uint() == 4294967295u);
+ CHECK(node.child(STR("text6")).text().as_uint() == 0);
+}
+
+TEST_XML(dom_text_as_integer_space, "<node><text1> \t\n1234</text1><text2>\n\t 0x123</text2><text3>- 16</text3><text4>- 0x10</text4></node>")
+{
+ xml_node node = doc.child(STR("node"));
+
+ CHECK(node.child(STR("text1")).text().as_int() == 1234);
+ CHECK(node.child(STR("text2")).text().as_int() == 291);
+ CHECK(node.child(STR("text3")).text().as_int() == 0);
+ CHECK(node.child(STR("text4")).text().as_int() == 0);
}
TEST_XML(dom_text_as_float, "<node><text1>0</text1><text2>1</text2><text3>0.12</text3><text4>-5.1</text4><text5>3e-4</text5><text6>3.14159265358979323846</text6></node>")
diff --git a/tests/test_dom_traverse.cpp b/tests/test_dom_traverse.cpp
index d699c5e..1668174 100644
--- a/tests/test_dom_traverse.cpp
+++ b/tests/test_dom_traverse.cpp
@@ -87,7 +87,7 @@ TEST_XML(dom_attr_as_string, "<node attr='1'/>")
CHECK_STRING(xml_attribute().as_string(), STR(""));
}
-TEST_XML(dom_attr_as_int, "<node attr1='1' attr2='-1' attr3='-2147483648' attr4='2147483647'/>")
+TEST_XML(dom_attr_as_int, "<node attr1='1' attr2='-1' attr3='-2147483648' attr4='2147483647' attr5='0'/>")
{
xml_node node = doc.child(STR("node"));
@@ -96,9 +96,22 @@ TEST_XML(dom_attr_as_int, "<node attr1='1' attr2='-1' attr3='-2147483648' attr4=
CHECK(node.attribute(STR("attr2")).as_int() == -1);
CHECK(node.attribute(STR("attr3")).as_int() == -2147483647 - 1);
CHECK(node.attribute(STR("attr4")).as_int() == 2147483647);
+ CHECK(node.attribute(STR("attr5")).as_int() == 0);
}
-TEST_XML(dom_attr_as_uint, "<node attr1='0' attr2='1' attr3='2147483647' attr4='4294967295'/>")
+TEST_XML(dom_attr_as_int_hex, "<node attr1='0777' attr2='0x5ab' attr3='0XFf' attr4='-0x20' attr5='-0x80000000' attr6='0x'/>")
+{
+ xml_node node = doc.child(STR("node"));
+
+ CHECK(node.attribute(STR("attr1")).as_int() == 777); // no octal support! intentional
+ CHECK(node.attribute(STR("attr2")).as_int() == 1451);
+ CHECK(node.attribute(STR("attr3")).as_int() == 255);
+ CHECK(node.attribute(STR("attr4")).as_int() == -32);
+ CHECK(node.attribute(STR("attr5")).as_int() == -2147483647 - 1);
+ CHECK(node.attribute(STR("attr6")).as_int() == 0);
+}
+
+TEST_XML(dom_attr_as_uint, "<node attr1='0' attr2='1' attr3='2147483647' attr4='4294967295' attr5='0'/>")
{
xml_node node = doc.child(STR("node"));
@@ -107,6 +120,29 @@ TEST_XML(dom_attr_as_uint, "<node attr1='0' attr2='1' attr3='2147483647' attr4='
CHECK(node.attribute(STR("attr2")).as_uint() == 1);
CHECK(node.attribute(STR("attr3")).as_uint() == 2147483647);
CHECK(node.attribute(STR("attr4")).as_uint() == 4294967295u);
+ CHECK(node.attribute(STR("attr5")).as_uint() == 0);
+}
+
+TEST_XML(dom_attr_as_uint_hex, "<node attr1='0777' attr2='0x5ab' attr3='0XFf' attr4='0x20' attr5='0xFFFFFFFF' attr6='0x'/>")
+{
+ xml_node node = doc.child(STR("node"));
+
+ CHECK(node.attribute(STR("attr1")).as_uint() == 777); // no octal support! intentional
+ CHECK(node.attribute(STR("attr2")).as_uint() == 1451);
+ CHECK(node.attribute(STR("attr3")).as_uint() == 255);
+ CHECK(node.attribute(STR("attr4")).as_uint() == 32);
+ CHECK(node.attribute(STR("attr5")).as_uint() == 4294967295u);
+ CHECK(node.attribute(STR("attr6")).as_uint() == 0);
+}
+
+TEST_XML(dom_attr_as_integer_space, "<node attr1=' \t1234' attr2='\t 0x123' attr3='- 16' attr4='- 0x10'/>")
+{
+ xml_node node = doc.child(STR("node"));
+
+ CHECK(node.attribute(STR("attr1")).as_int() == 1234);
+ CHECK(node.attribute(STR("attr2")).as_int() == 291);
+ CHECK(node.attribute(STR("attr3")).as_int() == 0);
+ CHECK(node.attribute(STR("attr4")).as_int() == 0);
}
TEST_XML(dom_attr_as_float, "<node attr1='0' attr2='1' attr3='0.12' attr4='-5.1' attr5='3e-4' attr6='3.14159265358979323846'/>")