Skip to content

Commit

Permalink
[FEATURE] cigar.assign_string from char pointers.
Browse files Browse the repository at this point in the history
  • Loading branch information
smehringer committed Apr 22, 2022
1 parent e5c63f9 commit 5194a37
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
29 changes: 29 additions & 0 deletions include/seqan3/alphabet/cigar/cigar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,35 @@ class cigar : public alphabet_tuple_base<cigar, uint32_t, exposition_only::cigar

return *this;
}

/*!\brief Assign from a contigous char array.
* \details
*
* In order to avoid unnecessary copies, you can initialise a seqan3::cigar from two char pointers that point to a
* contigous char array that stores the cigar string.
*
* \include test/snippet/alphabet/cigar/cigar_assign_string.cpp
*
* \experimentalapi{Experimental since version 3.2.}
*/
cigar & assign_string(char const * start, char const * end) noexcept
{
uint32_t num{};
auto [ ptr, errc ] = std::from_chars(start, end, num);

if ((errc != std::errc{}) || (!char_is_valid_for<operation>(*ptr)))
{
get<0>(*this) = 0;
assign_char_to('P', get<1>(*this));
}
else
{
get<0>(*this) = num;
assign_char_to(*ptr, get<1>(*this));
}

return *this;
}
//!\}

/*!\name Get functions
Expand Down
16 changes: 16 additions & 0 deletions test/snippet/alphabet/cigar/cigar_assign_string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <seqan3/alphabet/cigar/cigar.hpp>
#include <seqan3/core/debug_stream.hpp>

int main()
{
std::string cigar_str{"4S134M"};

seqan3::cigar letter1{};
seqan3::cigar letter2{};

letter1.assign_string(cigar_str.data(), cigar_str.data() + 2);
letter2.assign_string(cigar_str.data() + 3, cigar_str.data() + cigar_str.size());

seqan3::debug_stream << letter1 << '\n'; // prints 134M
seqan3::debug_stream << letter2 << '\n'; // prints 134M
}

0 comments on commit 5194a37

Please sign in to comment.