code polishing following @gregmarr's feedback

This commit is contained in:
Qianqian Fang 2022-02-22 17:34:44 -05:00
parent ee9e078a5c
commit 6b9da3bf0a
3 changed files with 51 additions and 33 deletions

View File

@ -1914,20 +1914,25 @@ class binary_reader
} }
default: default:
{ break;
}
} }
auto last_token = get_token_string(); auto last_token = get_token_string();
if (input_format == input_format_t::bjdata) std::string message;
if (input_format != input_format_t::bjdata)
{ {
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, "expected length type specification (U, i, I, u, l, m, L, M); last byte: 0x" + last_token, "string"), nullptr)); message = "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token;
} }
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string"), BasicJsonType())); else
{
message = "expected length type specification (U, i, u, I, m, l, M, L); last byte: 0x" + last_token;
}
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "string"), BasicJsonType()));
} }
/*! /*!
@param[out] dim an integer vector storing the ND array dimensions @param[out] dim an integer vector storing the ND array dimensions
@return whether array creation completed @return whether reading ND array size vector is successful
*/ */
bool get_ubjson_ndarray_size(std::vector<size_t>& dim) bool get_ubjson_ndarray_size(std::vector<size_t>& dim)
{ {
@ -2106,7 +2111,7 @@ class binary_reader
{ {
return false; return false;
} }
result = 1; result = ( (dim.size() > 0) ? 1 : 0 );
for (auto i : dim) for (auto i : dim)
{ {
result *= i; result *= i;
@ -2115,15 +2120,20 @@ class binary_reader
} }
default: default:
{ break;
}
} }
auto last_token = get_token_string(); auto last_token = get_token_string();
if (input_format == input_format_t::bjdata) std::string message;
if (input_format != input_format_t::bjdata)
{ {
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, "expected length type specification (U, i, I, u, l, m, L, M) after '#'; last byte: 0x" + last_token, "size"), nullptr)); message = "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token;
} }
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size"), BasicJsonType())); else
{
message = "expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x" + last_token;
}
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "size"), BasicJsonType()));
} }
/*! /*!
@ -2352,8 +2362,7 @@ class binary_reader
return get_ubjson_object(); return get_ubjson_object();
default: // anything else default: // anything else
{ break;
}
} }
auto last_token = get_token_string(); auto last_token = get_token_string();
return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format, "invalid byte: 0x" + last_token, "value"), BasicJsonType())); return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format, "invalid byte: 0x" + last_token, "value"), BasicJsonType()));

View File

@ -10290,20 +10290,25 @@ class binary_reader
} }
default: default:
{ break;
}
} }
auto last_token = get_token_string(); auto last_token = get_token_string();
if (input_format == input_format_t::bjdata) std::string message;
if (input_format != input_format_t::bjdata)
{ {
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, "expected length type specification (U, i, I, u, l, m, L, M); last byte: 0x" + last_token, "string"), nullptr)); message = "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token;
} }
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string"), BasicJsonType())); else
{
message = "expected length type specification (U, i, u, I, m, l, M, L); last byte: 0x" + last_token;
}
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "string"), BasicJsonType()));
} }
/*! /*!
@param[out] dim an integer vector storing the ND array dimensions @param[out] dim an integer vector storing the ND array dimensions
@return whether array creation completed @return whether reading ND array size vector is successful
*/ */
bool get_ubjson_ndarray_size(std::vector<size_t>& dim) bool get_ubjson_ndarray_size(std::vector<size_t>& dim)
{ {
@ -10482,7 +10487,7 @@ class binary_reader
{ {
return false; return false;
} }
result = 1; result = ( (dim.size() > 0) ? 1 : 0 );
for (auto i : dim) for (auto i : dim)
{ {
result *= i; result *= i;
@ -10491,15 +10496,20 @@ class binary_reader
} }
default: default:
{ break;
}
} }
auto last_token = get_token_string(); auto last_token = get_token_string();
if (input_format == input_format_t::bjdata) std::string message;
if (input_format != input_format_t::bjdata)
{ {
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, "expected length type specification (U, i, I, u, l, m, L, M) after '#'; last byte: 0x" + last_token, "size"), nullptr)); message = "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token;
} }
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size"), BasicJsonType())); else
{
message = "expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x" + last_token;
}
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "size"), BasicJsonType()));
} }
/*! /*!
@ -10728,8 +10738,7 @@ class binary_reader
return get_ubjson_object(); return get_ubjson_object();
default: // anything else default: // anything else
{ break;
}
} }
auto last_token = get_token_string(); auto last_token = get_token_string();
return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format, "invalid byte: 0x" + last_token, "value"), BasicJsonType())); return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format, "invalid byte: 0x" + last_token, "value"), BasicJsonType()));

View File

@ -1184,7 +1184,7 @@ TEST_CASE("BJData")
std::vector<uint8_t> vec3 = {'H', 'i', 2, '1', '.'}; std::vector<uint8_t> vec3 = {'H', 'i', 2, '1', '.'};
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vec3), "[json.exception.parse_error.115] parse error at byte 5: syntax error while parsing BJData high-precision number: invalid number text: 1.", json::parse_error); CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vec3), "[json.exception.parse_error.115] parse error at byte 5: syntax error while parsing BJData high-precision number: invalid number text: 1.", json::parse_error);
std::vector<uint8_t> vec4 = {'H', 2, '1', '0'}; std::vector<uint8_t> vec4 = {'H', 2, '1', '0'};
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vec4), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing BJData size: expected length type specification (U, i, I, u, l, m, L, M) after '#'; last byte: 0x02", json::parse_error); CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vec4), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing BJData size: expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x02", json::parse_error);
} }
} }
} }
@ -2534,7 +2534,7 @@ TEST_CASE("BJData")
std::vector<uint8_t> v = {'S', '1', 'a'}; std::vector<uint8_t> v = {'S', '1', 'a'};
json _; json _;
CHECK_THROWS_AS(_ = json::from_bjdata(v), json::parse_error&); CHECK_THROWS_AS(_ = json::from_bjdata(v), json::parse_error&);
CHECK_THROWS_WITH(_ = json::from_bjdata(v), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing BJData string: expected length type specification (U, i, I, u, l, m, L, M); last byte: 0x31"); CHECK_THROWS_WITH(_ = json::from_bjdata(v), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing BJData string: expected length type specification (U, i, u, I, m, l, M, L); last byte: 0x31");
} }
SECTION("parse bjdata markers in ubjson") SECTION("parse bjdata markers in ubjson")
@ -2632,7 +2632,7 @@ TEST_CASE("BJData")
std::vector<uint8_t> v0 = {'[', '#', 'T', ']'}; std::vector<uint8_t> v0 = {'[', '#', 'T', ']'};
CHECK_THROWS_AS(_ = json::from_bjdata(v0), json::parse_error&); CHECK_THROWS_AS(_ = json::from_bjdata(v0), json::parse_error&);
CHECK_THROWS_WITH(_ = json::from_bjdata(v0), "[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing BJData size: expected length type specification (U, i, I, u, l, m, L, M) after '#'; last byte: 0x54"); CHECK_THROWS_WITH(_ = json::from_bjdata(v0), "[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing BJData size: expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x54");
CHECK(json::from_bjdata(v0, true, false).is_discarded()); CHECK(json::from_bjdata(v0, true, false).is_discarded());
} }
@ -2703,7 +2703,7 @@ TEST_CASE("BJData")
std::vector<uint8_t> vST = {'[', '$', 'i', '#', '[', '$', 'i', '#'}; std::vector<uint8_t> vST = {'[', '$', 'i', '#', '[', '$', 'i', '#'};
json _; json _;
CHECK_THROWS_AS(_ = json::from_bjdata(vST), json::parse_error&); CHECK_THROWS_AS(_ = json::from_bjdata(vST), json::parse_error&);
CHECK_THROWS_WITH(_ = json::from_bjdata(vST), "[json.exception.parse_error.113] parse error at byte 9: syntax error while parsing BJData size: expected length type specification (U, i, I, u, l, m, L, M) after '#'; last byte: 0xFF"); CHECK_THROWS_WITH(_ = json::from_bjdata(vST), "[json.exception.parse_error.113] parse error at byte 9: syntax error while parsing BJData size: expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0xFF");
CHECK(json::from_bjdata(vST, true, false).is_discarded()); CHECK(json::from_bjdata(vST, true, false).is_discarded());
std::vector<uint8_t> v = {'[', '$', 'i', '#', '[', '$', 'i', '#', 'i', 2, 1, 2}; std::vector<uint8_t> v = {'[', '$', 'i', '#', '[', '$', 'i', '#', 'i', 2, 1, 2};
@ -2718,7 +2718,7 @@ TEST_CASE("BJData")
std::vector<uint8_t> vS = {'[', '$', 'i', '#', '[', '#', 'i', 2, 1, 2, 1}; std::vector<uint8_t> vS = {'[', '$', 'i', '#', '[', '#', 'i', 2, 1, 2, 1};
CHECK_THROWS_AS(_ = json::from_bjdata(vS), json::parse_error&); CHECK_THROWS_AS(_ = json::from_bjdata(vS), json::parse_error&);
CHECK_THROWS_WITH(_ = json::from_bjdata(vS), "[json.exception.parse_error.113] parse error at byte 9: syntax error while parsing BJData size: expected length type specification (U, i, I, u, l, m, L, M) after '#'; last byte: 0x01"); CHECK_THROWS_WITH(_ = json::from_bjdata(vS), "[json.exception.parse_error.113] parse error at byte 9: syntax error while parsing BJData size: expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x01");
CHECK(json::from_bjdata(vS, true, false).is_discarded()); CHECK(json::from_bjdata(vS, true, false).is_discarded());
std::vector<uint8_t> vT = {'[', '$', 'i', '#', '[', 'i', 2, 'i'}; std::vector<uint8_t> vT = {'[', '$', 'i', '#', '[', 'i', 2, 'i'};