MCPcopy Create free account
hub / github.com/GlobalTechInfo/MEGA-MD / encode

Function encode

lib/dna.cpp:12–22  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10const std::map<char, int> BASE_VAL = {{'A',0},{'T',1},{'C',2},{'G',3}};
11
12std::string encode(const std::string& text) {
13 std::string result;
14 result.reserve(text.size() * 4);
15 for (unsigned char c : text) {
16 result += BASES[(c >> 6) & 3];
17 result += BASES[(c >> 4) & 3];
18 result += BASES[(c >> 2) & 3];
19 result += BASES[c & 3];
20 }
21 return result;
22}
23
24std::string decode(const std::string& dna) {
25 if (dna.size() % 4 != 0) throw std::runtime_error("Invalid DNA length");

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected