src/edh/code39/lib.hpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "../../util/alias/others/lib.hpp" | ||
| 4 | |||
| 5 | namespace tifa_libs { | ||
| 6 | |||
| 7 | class code39 { | ||
| 8 | // NOLINTNEXTLINE(modernize-avoid-c-arrays) | ||
| 9 | static CEXP u32 symbol[44]{1341, 3351, 1815, 3861, 1335, 3381, 1845, 1311, 3357, 1821, 3399, 1863, 3909, 1479, 3525, 1989, 1359, 3405, 1869, 1485, 3411, 1875, 3921, 1491, 3537, 2001, 1395, 3441, 1905, 1521, 3159, 1239, 3285, 1143, 3189, 1269, 1119, 3165, 1245, 17477, 17489, 17681, 20753, 1149}; | ||
| 10 | // NOLINTNEXTLINE(modernize-avoid-c-arrays) | ||
| 11 | static CEXP chr fullmap[] = "%U$A$B$C$D$E$F$G$H$I$J$K$L$M$N$O$P$Q$R$S$T$U$V$W$X$Y$Z%A%B%C%D%E */A/B/C/D/E/F/G/H/I/J/K/L-*.*/O0*1*2*3*4*5*6*7*8*9*/Z%F%G%H%I%J%VA*B*C*D*E*F*G*H*I*J*K*L*M*N*O*P*Q*R*S*T*U*V*W*X*Y*Z*%K%L%M%N%O%W+A+B+C+D+E+F+G+H+I+J+K+L+M+N+O+P+Q+R+S+T+U+V+W+X+Y+Z%P%Q%R%S%T"; | ||
| 12 | |||
| 13 | 12 | static CEXP strn expand(strnv s) NE { | |
| 14 | 12 | strn ans; | |
| 15 | 12 | ans.reserve(s.size() * 2); | |
| 16 |
2/2✓ Branch 2 taken 9511110 times.
✓ Branch 3 taken 12 times.
|
9511122 | for (auto c : s) |
| 17 |
2/2✓ Branch 1 taken 6613943 times.
✓ Branch 2 taken 2897167 times.
|
9511110 | if (ans.push_back(fullmap[(u32)c * 2]); fullmap[(u32)c * 2 + 1] != '*') ans.push_back(fullmap[(u32)c * 2 + 1]); |
| 18 | 12 | ans.shrink_to_fit(); | |
| 19 | 12 | return ans; | |
| 20 | } | ||
| 21 | 12 | static CEXP strn parse(strnv s) NE { | |
| 22 | 12 | strn ans; | |
| 23 | 12 | ans.reserve(s.size()); | |
| 24 |
2/2✓ Branch 1 taken 9511110 times.
✓ Branch 2 taken 12 times.
|
9511122 | flt_ (u32, i, 0, (u32)s.size()) |
| 25 |
2/2✓ Branch 1 taken 1932755 times.
✓ Branch 2 taken 7578355 times.
|
9511110 | if (s[i] == '$') ans += chr(s[++i] - 'A' + 1); |
| 26 |
2/2✓ Branch 1 taken 1041154 times.
✓ Branch 2 taken 6537201 times.
|
7578355 | else if (s[i] == '/') ans += chr(s[++i] - 'A' + 33); |
| 27 |
2/2✓ Branch 1 taken 1931863 times.
✓ Branch 2 taken 4605338 times.
|
6537201 | else if (s[i] == '+') ans += (chr)tolower(s[++i]); |
| 28 |
2/2✓ Branch 1 taken 1708171 times.
✓ Branch 2 taken 2897167 times.
|
4605338 | else if (s[i] == '%') { |
| 29 |
2/2✓ Branch 1 taken 74224 times.
✓ Branch 2 taken 1633947 times.
|
1708171 | if (cchr c = s[++i]; c == 'U') ans += '\0'; |
| 30 |
2/2✓ Branch 0 taken 74174 times.
✓ Branch 1 taken 1559773 times.
|
1633947 | else if (c == 'V') ans += '\x40'; |
| 31 |
2/2✓ Branch 0 taken 74256 times.
✓ Branch 1 taken 1485517 times.
|
1559773 | else if (c == 'W') ans += '\x60'; |
| 32 |
2/2✓ Branch 0 taken 74323 times.
✓ Branch 1 taken 1411194 times.
|
1485517 | else if (c >= 'T') ans += '\x7f'; |
| 33 | 1411194 | else ans += chr((c - 'A') / 5 * 32 + (c - 'A') % 5 + 27); | |
| 34 | 2897167 | } else ans += s[i]; | |
| 35 | 12 | ans.shrink_to_fit(); | |
| 36 | 12 | return ans; | |
| 37 | } | ||
| 38 | |||
| 39 | public: | ||
| 40 | // NOLINTNEXTLINE(modernize-avoid-c-arrays) | ||
| 41 | static CEXP chr alphabet[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*"; | ||
| 42 | template <bool full = false> | ||
| 43 | 24 | static CEXP vecu encode(strnv s) NE { | |
| 44 | 24 | strn _; | |
| 45 | 12 | if CEXP (full) _ = expand(s), s = _; | |
| 46 | 24 | vecu ret; | |
| 47 | 24 | ret.reserve((s.size() + 1) / 2); | |
| 48 |
4/4std::vector<unsigned int, std::allocator<unsigned int> > tifa_libs::code39::encode<false>(std::basic_string_view<char, std::char_traits<char> >):
✓ Branch 6 taken 4755555 times.
✓ Branch 7 taken 12 times.
std::vector<unsigned int, std::allocator<unsigned int> > tifa_libs::code39::encode<true>(std::basic_string_view<char, std::char_traits<char> >):
✓ Branch 6 taken 8062524 times.
✓ Branch 7 taken 12 times.
|
12818103 | for (u32 i = 0; i + 1 < s.size(); i += 2) ret.push_back(symbol[std::find(alphabet, alphabet + 45, s[i]) - alphabet] << 16 | symbol[std::find(alphabet, alphabet + 45, s[i + 1]) - alphabet]); |
| 49 |
3/4std::vector<unsigned int, std::allocator<unsigned int> > tifa_libs::code39::encode<false>(std::basic_string_view<char, std::char_traits<char> >):
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
std::vector<unsigned int, std::allocator<unsigned int> > tifa_libs::code39::encode<true>(std::basic_string_view<char, std::char_traits<char> >):
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 7 times.
|
24 | if (s.size() & 1) ret.push_back(symbol[std::find(alphabet, alphabet + 45, s.back()) - alphabet]); |
| 50 | 24 | return ret; | |
| 51 | 24 | } | |
| 52 | template <bool mod10 = true> //== false: mod43 | ||
| 53 | 48 | static CEXP u32 check_digit(spnu s) NE { | |
| 54 | 48 | u64 ans = 0; | |
| 55 |
4/4unsigned int tifa_libs::code39::check_digit<false>(std::span<unsigned int const, 18446744073709551615ul>):
✓ Branch 7 taken 12818084 times.
✓ Branch 8 taken 24 times.
unsigned int tifa_libs::code39::check_digit<true>(std::span<unsigned int const, 18446744073709551615ul>):
✓ Branch 7 taken 12818084 times.
✓ Branch 8 taken 24 times.
|
25636216 | for (auto i : s) ans += u64((std::find(symbol, symbol + 44, i >> 16) - symbol) + (std::find(symbol, symbol + 44, i & 0xffff) - symbol)); |
| 56 | 24 | if CEXP (mod10) return u32(ans % 10); | |
| 57 | 24 | else return u32(ans % 43); | |
| 58 | } | ||
| 59 | template <bool full = false> | ||
| 60 | 24 | static CEXP strn decode(spnu s) NE { | |
| 61 | 24 | strn ans; | |
| 62 | 24 | ans.reserve(s.size() * 2); | |
| 63 | 12818084 | for (u32 i = 0; i + 1 < s.size(); ++i) (ans += alphabet[std::find(symbol, symbol + 44, s[i] >> 16) - symbol]) += alphabet[std::find(symbol, symbol + 44, s[i] & 0xffff) - symbol]; | |
| 64 | 24 | if (s.back() >> 16) ans += alphabet[std::find(symbol, symbol + 44, s.back() >> 16) - symbol]; | |
| 65 | 24 | ans += alphabet[std::find(symbol, symbol + 44, s.back() & 0xffff) - symbol]; | |
| 66 | 12 | if CEXP (full) ans = parse(ans); | |
| 67 | 24 | return ans; | |
| 68 | } | ||
| 69 | }; | ||
| 70 | |||
| 71 | } // namespace tifa_libs | ||
| 72 |