From 3cd215593ba05469bfac046f9ad113314cf3f124 Mon Sep 17 00:00:00 2001 From: Enrico Seiler Date: Thu, 5 Jan 2023 15:16:49 +0100 Subject: [PATCH] [REVIEW] Refactor read_sam_byte_vector --- include/seqan3/io/sam_file/format_sam.hpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/include/seqan3/io/sam_file/format_sam.hpp b/include/seqan3/io/sam_file/format_sam.hpp index 89640363b9..0249d3cff5 100644 --- a/include/seqan3/io/sam_file/format_sam.hpp +++ b/include/seqan3/io/sam_file/format_sam.hpp @@ -850,13 +850,14 @@ 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 SAM 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)