Skip to content

Commit

Permalink
[REVIEW] Refactor read_sam_byte_vector
Browse files Browse the repository at this point in the history
  • Loading branch information
eseiler committed Jan 5, 2023
1 parent 3173b4f commit 349e168
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions include/seqan3/io/sam_file/format_sam.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -850,17 +850,18 @@ inline void format_sam::read_sam_byte_vector(seqan3::detail::sam_tag_variant & v
if (str.size() % 2 != 0)
throw format_error{"[CORRUPTED SAM FILE] Hexadecimal tag must have even number of digits."};

for (size_t i = 0; i < str.size(); i += 2)
// H encodes bytes in a hexadecimal format. Two hex values are stored for each byte as characters.
// E.g., '1' and 'A' need one byte each and are read as `\x1A`, which is 27 in decimal.
for (auto hex_begin = str.begin(), hex_end = str.begin() + 2; hex_begin != str.end(); hex_begin += 2, hex_end += 2)
{
auto res = std::from_chars(str.begin() + i, str.begin() + i + 2, dummy_byte, 16);
auto res = std::from_chars(hex_begin, hex_end, dummy_byte, 16);

if (res.ec == std::errc::invalid_argument)
throw format_error{std::string("[CORRUPTED SAM FILE] The string '")
+ std::string(str.begin() + i, str.begin() + i + 2)
throw format_error{std::string("[CORRUPTED BAM FILE] The string '") + std::string(hex_begin, hex_end)
+ "' could not be cast into type uint8_t."};

if (res.ec == std::errc::result_out_of_range)
throw format_error{std::string("[CORRUPTED SAM FILE] Casting '") + std::string(str)
throw format_error{std::string("[CORRUPTED BAM FILE] Casting '") + std::string(str)
+ "' into type uint8_t would cause an overflow."};

tmp_vector.push_back(std::byte{dummy_byte});
Expand Down

0 comments on commit 349e168

Please sign in to comment.