From df0519bea5390ac8f3d9a0e168e2f799bd068985 Mon Sep 17 00:00:00 2001 From: YongDo-Hyun Date: Sat, 27 Dec 2025 01:06:26 +0300 Subject: feat: enhance error handling in ozlibstream and improve JSON escaping in json_formatter; add make_numeric_tag for cleaner tag creation Signed-off-by: YongDo-Hyun --- src/value.cpp | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) (limited to 'src/value.cpp') diff --git a/src/value.cpp b/src/value.cpp index 8c16da6e23..273049baf9 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -66,6 +66,21 @@ void value::set(tag&& t) //Primitive assignment namespace // helper functions local to this translation unit { + template + std::unique_ptr make_numeric_tag(tag_type type, T val) + { + switch(type) + { + case tag_type::Byte: return std::unique_ptr(new tag_byte(val)); + case tag_type::Short: return std::unique_ptr(new tag_short(val)); + case tag_type::Int: return std::unique_ptr(new tag_int(val)); + case tag_type::Long: return std::unique_ptr(new tag_long(val)); + case tag_type::Float: return std::unique_ptr(new tag_float(val)); + case tag_type::Double: return std::unique_ptr(new tag_double(val)); + default: return nullptr; + } + } + template void assign_numeric_impl(std::unique_ptr& tag_ptr, T val, tag_type default_type) @@ -73,16 +88,9 @@ namespace // helper functions local to this translation unit using nbt::tag_type; if(!tag_ptr) { - switch(default_type) - { - case tag_type::Byte: tag_ptr.reset(new tag_byte(static_cast(val))); break; - case tag_type::Short: tag_ptr.reset(new tag_short(static_cast(val))); break; - case tag_type::Int: tag_ptr.reset(new tag_int(static_cast(val))); break; - case tag_type::Long: tag_ptr.reset(new tag_long(static_cast(val))); break; - case tag_type::Float: tag_ptr.reset(new tag_float(static_cast(val))); break; - case tag_type::Double: tag_ptr.reset(new tag_double(static_cast(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(existing_type) < static_cast(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(val))); break; - case tag_type::Short: tag_ptr.reset(new tag_short(static_cast(val))); break; - case tag_type::Int: tag_ptr.reset(new tag_int(static_cast(val))); break; - case tag_type::Long: tag_ptr.reset(new tag_long(static_cast(val))); break; - case tag_type::Float: tag_ptr.reset(new tag_float(static_cast(val))); break; - case tag_type::Double: tag_ptr.reset(new tag_double(static_cast(val))); break; - default: throw std::bad_cast(); - } + tag_ptr = make_numeric_tag(incoming_type, val); + if(!tag_ptr) + throw std::bad_cast(); return; } -- cgit 0.0.5-2-1-g0f52