• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PeterCDMcLean / BitLib / 15696616722

17 Jun 2025 02:29AM UTC coverage: 54.11% (+0.6%) from 53.519%
15696616722

Pull #17

github

web-flow
Merge caf45e877 into 0f0b787a1
Pull Request #17: Truncation policy

10137 of 18710 branches covered (54.18%)

Branch coverage included in aggregate %.

212 of 251 new or added lines in 13 files covered. (84.46%)

214 existing lines in 11 files now uncovered.

6062 of 11227 relevant lines covered (53.99%)

7736453.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/include/bitlib/bit-algorithms/to_string.hpp
1
// ================================= BIT_ARRAY_REF =================================== //
2
// Project:     The Experimental Bit Algorithms Library
3
// \file        to_string.hpp
4
// Description: Implementation of bit_array_ref
5
// Creator:     Vincent Reverdy
6
// Contributor: Peter McLean [2025]
7
// License:     BSD 3-Clause License
8
// ========================================================================== //
9

10
#ifndef _BIT_TO_STRING_HPP_INCLUDED
11
#define _BIT_TO_STRING_HPP_INCLUDED
12

13
#include <bit>
14

15
#include "bitlib/bit-algorithms/accumulate.hpp"
16
#include "bitlib/bit-algorithms/count.hpp"
17
#include "bitlib/bit_concepts.hpp"
18

19
namespace bit {
20

21
namespace detail {
22

23
template <std::size_t Base>
NEW
24
constexpr auto make_digit_map() {
NEW
25
  static_assert((Base >= 2) && ((Base & (Base - 1)) == 0), "Base must be power of 2 >= 2");
NEW
26
  static_assert(Base <= 64, "Base too large for simple char mapping");
NEW
27

NEW
28
  std::array<char, Base> map = {};
NEW
29
  for (std::size_t i = 0; i < Base; ++i) {
NEW
30
    map[i] = (i < 10) ? ('0' + i) : ('A' + (i - 10));
NEW
31
  }
NEW
32
  return map;
NEW
33
}
34

35
}  // namespace detail
36

37
template <std::size_t base = 10, bool prepend_zeros = false, std::endian endian = std::endian::little, typename RandomAccessIt>
38
constexpr std::string to_string(const bit_iterator<RandomAccessIt>& first, const bit_iterator<RandomAccessIt>& last, std::string prefix = "") {
8✔
39
  if constexpr (std::has_single_bit(base)) {
4✔
40
    constexpr const auto base_bits = std::bit_width(base - 1);
8✔
41

42
    int skip_leading_bits = prepend_zeros ? 0 : count_msb(first, last, bit0);
8✔
43

44
    int str_len = (distance(first, last) - skip_leading_bits);
8✔
45
    str_len += (0 != (str_len % base_bits));
8✔
46
    std::string& str = prefix;
8✔
47
    str.resize(str.length() + str_len);
8✔
48

49
    static constexpr auto base_digits = detail::make_digit_map<base>();
4✔
50

51
    return accumulate(
12✔
52
        policy::AccumulateNoInitialSubword{},
4✔
53
        first, last - skip_leading_bits, (str.data() + str_len),
12✔
54
        [](char* acc, auto word, const size_t bits = bitsof<decltype(word)>()) {
8✔
55
          const int characters = ((bits + base_bits - 1) / base_bits);
8✔
56
          acc -= characters;
8✔
57
          for (int i = characters - 1; i >= 0; i--) {
72✔
58
            acc[i] = base_digits[word & (base - 1)];
64✔
59
            word >>= base_bits;
64✔
60
          }
32✔
61
          return acc;
8✔
62
        });
20✔
63
  }
4✔
64
}
4✔
65

66
template <std::size_t base = 10, bool prepend_zeros = false, std::endian endian = std::endian::little>
67
constexpr std::string to_string(const bit_sized_range auto& bits, std::string prefix = "") {
8✔
68
  return to_string<base, prepend_zeros, endian>(bits.begin(), bits.end(), prefix);
12✔
69
}
4✔
70

71
}  // namespace bit
72

73
#endif // _BIT_TO_STRING_HPP_INCLUDED
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc