diff --git a/json5_parser/json5_parser_reader_template.h b/json5_parser/json5_parser_reader_template.h index 865daf7..fa2bbd9 100644 --- a/json5_parser/json5_parser_reader_template.h +++ b/json5_parser/json5_parser_reader_template.h @@ -11,6 +11,7 @@ #endif #include +#include #include #include "json5_parser_error_position.h" @@ -139,28 +140,36 @@ template String_type substitute_esc_chars(typename String_type::const_iterator begin, typename String_type::const_iterator end) { typedef typename String_type::const_iterator Iter_type; - - if (end - begin < 2) return String_type(begin, end); - String_type result; - result.reserve(end - begin); + if (begin == end) return result; - const Iter_type end_minus_1(end - 1); + result.reserve(static_cast(std::distance(begin, end))); Iter_type substr_start = begin; Iter_type i = begin; - for (; i < end_minus_1; ++i) { - if (*i == '\\') { - result.append(substr_start, i); + while (i != end) { + if (*i != '\\') { + ++i; + continue; + } - ++i; // skip the '\' + result.append(substr_start, i); - append_esc_char_and_incr_iter(result, i, end); + ++i; // skip the '\\' - substr_start = i + 1; + // Preserve a trailing backslash literally. + if (i == end) { + result += '\\'; + substr_start = end; + break; } + + append_esc_char_and_incr_iter(result, i, end); + + ++i; + substr_start = i; } result.append(substr_start, end); diff --git a/json5_parser/json5_parser_value.h b/json5_parser/json5_parser_value.h index 6181cea..56b496c 100644 --- a/json5_parser/json5_parser_value.h +++ b/json5_parser/json5_parser_value.h @@ -119,6 +119,8 @@ class Value_impl { class Variant_converter_visitor : public boost::static_visitor { public: + Variant operator()(const String_type& value) const { return value; } + template class Cont> Variant operator()(const Cont& cont) const { return Array(cont.begin(), cont.end());