summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/io/ozlibstream.cpp2
-rw-r--r--src/text/json_formatter.cpp2
-rw-r--r--src/value.cpp41
3 files changed, 23 insertions, 22 deletions
diff --git a/src/io/ozlibstream.cpp b/src/io/ozlibstream.cpp
index b936219787..5f479f83f8 100644
--- a/src/io/ozlibstream.cpp
+++ b/src/io/ozlibstream.cpp
@@ -95,6 +95,7 @@ int deflate_streambuf::sync()
void ozlibstream::close()
{
+ std::ios_base::iostate old_ex = exceptions();
try
{
buf.close();
@@ -105,7 +106,6 @@ void ozlibstream::close()
// `setstate` to throw an `ios_base::failure` which would replace
// the original exception. Temporarily disable exceptions on this
// stream, set the `badbit`, then restore the exception mask.
- std::ios_base::iostate old_ex = exceptions();
try {
exceptions(std::ios_base::goodbit);
setstate(std::ios_base::badbit);
diff --git a/src/text/json_formatter.cpp b/src/text/json_formatter.cpp
index bd882c15d2..06a9c54f60 100644
--- a/src/text/json_formatter.cpp
+++ b/src/text/json_formatter.cpp
@@ -217,7 +217,7 @@ namespace //anonymous
default:
if (static_cast<unsigned char>(c) < 32 || c == 127) {
// Control characters, escape as \u00XX
- os << "\\u00" << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(static_cast<unsigned char>(c));
+ os << "\\u00" << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(c);
} else {
os << c;
}
diff --git a/src/value.cpp b/src/value.cpp
index 8c16da6e23..273049baf9 100644
--- a/src/value.cpp
+++ b/src/value.cpp
@@ -67,22 +67,30 @@ void value::set(tag&& t)
namespace // helper functions local to this translation unit
{
template<typename T>
+ std::unique_ptr<tag> make_numeric_tag(tag_type type, T val)
+ {
+ switch(type)
+ {
+ case tag_type::Byte: return std::unique_ptr<tag>(new tag_byte(val));
+ case tag_type::Short: return std::unique_ptr<tag>(new tag_short(val));
+ case tag_type::Int: return std::unique_ptr<tag>(new tag_int(val));
+ case tag_type::Long: return std::unique_ptr<tag>(new tag_long(val));
+ case tag_type::Float: return std::unique_ptr<tag>(new tag_float(val));
+ case tag_type::Double: return std::unique_ptr<tag>(new tag_double(val));
+ default: return nullptr;
+ }
+ }
+
+ template<typename T>
void assign_numeric_impl(std::unique_ptr<tag>& tag_ptr, T val,
tag_type default_type)
{
using nbt::tag_type;
if(!tag_ptr)
{
- switch(default_type)
- {
- case tag_type::Byte: tag_ptr.reset(new tag_byte(static_cast<int8_t>(val))); break;
- case tag_type::Short: tag_ptr.reset(new tag_short(static_cast<int16_t>(val))); break;
- case tag_type::Int: tag_ptr.reset(new tag_int(static_cast<int32_t>(val))); break;
- case tag_type::Long: tag_ptr.reset(new tag_long(static_cast<int64_t>(val))); break;
- case tag_type::Float: tag_ptr.reset(new tag_float(static_cast<float>(val))); break;
- case tag_type::Double: tag_ptr.reset(new tag_double(static_cast<double>(val))); break;
- default: throw std::invalid_argument("Invalid default_type");
- }
+ tag_ptr = make_numeric_tag(default_type, val);
+ if(!tag_ptr)
+ throw std::invalid_argument("Invalid default_type");
return;
}
@@ -97,16 +105,9 @@ namespace // helper functions local to this translation unit
if(static_cast<int>(existing_type) < static_cast<int>(incoming_type))
{
// replace with a new, wider tag that preserves the value
- switch(incoming_type)
- {
- case tag_type::Byte: tag_ptr.reset(new tag_byte(static_cast<int8_t>(val))); break;
- case tag_type::Short: tag_ptr.reset(new tag_short(static_cast<int16_t>(val))); break;
- case tag_type::Int: tag_ptr.reset(new tag_int(static_cast<int32_t>(val))); break;
- case tag_type::Long: tag_ptr.reset(new tag_long(static_cast<int64_t>(val))); break;
- case tag_type::Float: tag_ptr.reset(new tag_float(static_cast<float>(val))); break;
- case tag_type::Double: tag_ptr.reset(new tag_double(static_cast<double>(val))); break;
- default: throw std::bad_cast();
- }
+ tag_ptr = make_numeric_tag(incoming_type, val);
+ if(!tag_ptr)
+ throw std::bad_cast();
return;
}