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

PeterCDMcLean / BitLib / 16544577083

26 Jul 2025 10:39PM UTC coverage: 76.997% (-1.5%) from 78.485%
16544577083

Pull #18

github

web-flow
Merge 4bac8f49d into 079daa142
Pull Request #18: From string

3414 of 5100 branches covered (66.94%)

Branch coverage included in aggregate %.

264 of 313 new or added lines in 12 files covered. (84.35%)

30 existing lines in 2 files now uncovered.

2494 of 2573 relevant lines covered (96.93%)

31115123.0 hits per line

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

79.53
/include/bitlib/bit-algorithms/to_from_string.hpp
1
// ================================= array_REF =================================== //
2
// Project:     The Experimental Bit Algorithms Library
3
// \file        to_string.hpp
4
// Description: Implementation of 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 <array>
14
#include <bit>
15
#include <string>
16

17
#include "bitlib/bit-algorithms/accumulate.hpp"
18
#include "bitlib/bit-algorithms/count.hpp"
19
#include "bitlib/bit-algorithms/division.hpp"
20
#include "bitlib/bit-algorithms/multiplication.hpp"
21
#include "bitlib/bit-containers/bit_policy.hpp"
22
#include "bitlib/bit_concepts.hpp"
23

24
namespace bit {
25

26
namespace string {
27

28
template <std::size_t Base>
UNCOV
29
constexpr auto make_digit_map() {
UNCOV
30
  static_assert((Base >= 2) && ((Base & (Base - 1)) == 0), "Base must be power of 2 >= 2");
UNCOV
31
  static_assert(Base <= 64, "Base too large for simple char mapping");
UNCOV
32

NEW
33
  ::std::array<char, Base> map{};
UNCOV
34
  for (std::size_t i = 0; i < Base; ++i) {
UNCOV
35
    map[i] = (i < 10) ? ('0' + i) : ('A' + (i - 10));
UNCOV
36
  }
UNCOV
37
  return map;
UNCOV
38
}
39

40
template <std::size_t Base>
UNCOV
41
constexpr auto make_from_digit_map() {
UNCOV
42
  static_assert((Base >= 2) && ((Base & (Base - 1)) == 0), "Base must be power of 2 >= 2");
UNCOV
43
  static_assert(Base <= 64, "Base too large for simple char mapping");
UNCOV
44

NEW
45
  ::std::array<char, 128> map{};
UNCOV
46
  for (std::size_t i = 0; i < 128; ++i) {
UNCOV
47
    map[i] = ~0;
UNCOV
48
    if (i >= '0' && i <= '9') {
UNCOV
49
      map[i] = i - '0';
UNCOV
50
    }
UNCOV
51
    if (i >= 'a' && i <= 'z') {
UNCOV
52
      map[i] = (i - 'a') + 10;
UNCOV
53
    }
UNCOV
54
    if (i >= 'A' && i <= 'Z') {
UNCOV
55
      map[i] = (i - 'A') + 10;
UNCOV
56
    }
UNCOV
57
  }
UNCOV
58
  return map;
UNCOV
59
}
60

61
struct metadata_t {
62
  size_t base;
63
  bool is_signed;
64
  std::endian endian;
65
  bool str_sign_extend_zeros;
66
};
67

68
constexpr metadata_t typical(size_t base = 10, bool str_sign_extend_zeros = false) {
69
  return {
70
      .base = base,
71
      .is_signed = false,
72
      .endian = std::endian::big,
73
      .str_sign_extend_zeros = str_sign_extend_zeros};
74
}
75

76
}  // namespace string
77

78
template <string::metadata_t meta = string::typical(), typename RandomAccessIt>
79
constexpr std::string to_string(const bit_iterator<RandomAccessIt>& first, const bit_iterator<RandomAccessIt>& last, std::string prefix = "") {
10✔
80
  static_assert(meta.endian == std::endian::big, "Only bit big endian support (MSB on the left)");
5✔
81
  if constexpr (std::has_single_bit(meta.base)) {
5✔
82
    constexpr const auto base_bits = std::bit_width(meta.base - 1);
8✔
83

84
    int skip_leading_bits = meta.str_sign_extend_zeros ? 0 : count_msb(first, last, bit0);
8✔
85

86
    int str_len = (distance(first, last) - skip_leading_bits);
8✔
87
    str_len = (str_len + base_bits - 1) / base_bits;  // Round up to nearest base digit
8✔
88
    if (0 == str_len) {
8!
UNCOV
89
      return prefix + "0";
×
UNCOV
90
    }
91
    std::string& str = prefix;
8✔
92
    str.resize(str.length() + str_len);
8✔
93

94
    static constexpr auto base_digits = string::make_digit_map<meta.base>();
4✔
95

96
    return accumulate(
12✔
97
        policy::AccumulateNoInitialSubword{},
4✔
98
        first, last - skip_leading_bits, (str.data() + str_len),
12✔
99
        [](char* acc, auto word, const size_t bits = bitsof<decltype(word)>()) {
8✔
100
          const int characters = ((bits + base_bits - 1) / base_bits);
8✔
101
          acc -= characters;
8✔
102
          for (int i = characters - 1; i >= 0; i--) {
72✔
103
            acc[i] = base_digits[word & (meta.base - 1)];
64✔
104
            word >>= base_bits;
64✔
105
          }
32✔
106
          return acc;
8✔
107
        });
16✔
108
  } else {
4✔
109
    if (meta.base > 10) {
2✔
NEW
110
      throw std::runtime_error("Base not implemented");
×
NEW
111
    }
112
    using word_type = typename bit_iterator<RandomAccessIt>::word_type;
1✔
113
    size_t store_bits = distance(first, last);
2✔
114
    std::vector<word_type> vec((store_bits + bitsof<word_type>() - 1) / bitsof<word_type>());
4✔
115
    vec.back() = 0;  // Ensure last word is zeroed
2✔
116
    bit_iterator<word_type*> bit_it(vec.data());
2✔
117

118
    constexpr unsigned char base = static_cast<unsigned char>(meta.base);
2✔
119
    auto remainder = ::bit::division(first, last, bit_it, base);
2✔
120
    std::string str;
2✔
121
    str.push_back(static_cast<char>(remainder + '0'));
2✔
122

123
    while (::bit::count(bit_it, bit_it + store_bits, bit1) > 0) {
6✔
124
      remainder = ::bit::division(bit_it, bit_it + store_bits, bit_it, base);
4✔
125
      str.push_back(static_cast<char>(remainder + '0'));
4✔
126
    }
2✔
127
    std::reverse(str.begin(), str.end());
2✔
128
    return prefix + str;
3✔
129
  }
2✔
130
}
5✔
131

132
template <string::metadata_t meta = string::typical()>
133
constexpr std::string to_string(const bit_sized_range auto& bits, std::string prefix = "") {
10✔
134
  return to_string<meta>(bits.begin(), bits.end(), prefix);
15✔
135
}
5✔
136

137
template <string::metadata_t meta = string::typical(), typename RandomAccessIt, typename Policy = policy::typical<typename RandomAccessIt::value_type>>
138
constexpr void from_string(
3✔
139
    const char* str_first, const char* str_last,
140
    const bit_iterator<RandomAccessIt>& bit_first, const bit_iterator<RandomAccessIt>& bit_last) {
3✔
141
  static_assert(meta.endian == std::endian::big, "Only bit big endian support (MSB on the left)");
3✔
142
  if constexpr (std::has_single_bit(meta.base)) {
3✔
143
    constexpr const auto base_bits = std::bit_width(meta.base - 1);
4✔
144
    static constexpr auto base_from_digits = string::make_from_digit_map<meta.base>();
2✔
145
    using word_type = uint64_t;
2✔
146
    std::vector<word_type> vec;
4✔
147
    size_t store_bits = distance(bit_first, bit_last);
4✔
148

149
    bit_iterator<RandomAccessIt> bit_it = bit_first;
4✔
150
    str_last--;
4✔
151
    while (str_last >= str_first && store_bits) {
6!
152
      word_type work = 0;
4✔
153
      size_t bits = 0;
4✔
154
      for (; (bits < bitsof<word_type>()) && (str_last >= str_first); str_last--) {
28!
155
        char c = *str_last;
24✔
156
        // TODO: This should be a policy
157
        if (c >= base_from_digits.size()) {
36!
NEW
158
          continue;
×
NEW
159
        }
160
        auto digit = base_from_digits[c];
24✔
161
        // TODO: This should be a policy
162
        if (~0 == digit) {
24!
NEW
163
          continue;
×
NEW
164
        }
165
        work |= (digit << bits);
24✔
166
        bits += base_bits;
24✔
167
      }
12✔
168
      if (store_bits < bits) {
4!
169
        Policy::truncation::template from_integral<word_type, std::dynamic_extent, RandomAccessIt>(
2✔
170
            bit_it, bit_last, work);
1✔
171
        return;
2✔
172
      } else if ((store_bits > bits) && (str_last < str_first)) {
2!
173
        const bit_iterator<word_type*> p_integral(&work);
2✔
174
        bit_it = ::bit::copy(p_integral, p_integral + bits, bit_it);
2✔
175
        Policy::extension::template from_integral<word_type, std::dynamic_extent, RandomAccessIt>(
2✔
176
            bit_it, bit_last, work);
1✔
177
      } else if (store_bits >= bits) {
1!
NEW
178
        const bit_iterator<word_type*> p_integral(&work);
×
NEW
179
        bit_it = ::bit::copy(p_integral, p_integral + bits, bit_it);
×
NEW
180
      }
181
    }
2✔
182
  } else {
4✔
183
    if (meta.base != 10) {
2✔
NEW
184
      throw std::runtime_error("Base not implemented");
×
NEW
185
    }
186
    using word_type = typename bit_iterator<RandomAccessIt>::word_type;
1✔
187
    std::vector<word_type> vec;
2✔
188
    size_t store_bits = distance(bit_first, bit_last);
2✔
189

190
    // TODO: template with uninitialized_t
191
    ::bit::fill(bit_first, bit_last, bit0);  // Clear the bits first
2✔
192

193
    while (str_first != str_last) {
8✔
194
      unsigned char c = (*str_first - '0');
6✔
195
      if (c <= 9) {
6!
196
        auto overflow_mult = ::bit::multiplication(bit_first, bit_last, word_type{10});
6✔
197
        auto overflow_add = ::bit::addition(bit_first, bit_last, c);
6✔
198
        if (overflow_mult || overflow_add) {
6!
199
          //Policy::truncation::template overflow(bit_first, bit_last);
NEW
200
          return;
×
NEW
201
        }
202
      }
3✔
203
      str_first++;
6✔
204
    }
3✔
205
    //Policy::extension::template extend(bit_first, bit_last);
206
  }
2✔
207
}
3✔
208

209
template <string::metadata_t meta = string::typical()>
210
constexpr std::vector<uintptr_t> from_string(
211
    const char* first, const char* last) {
212
  static_assert(meta.endian == std::endian::big, "Only bit big endian support (MSB on the left)");
213
  if constexpr (std::has_single_bit(meta.base)) {
214
    constexpr const auto base_bits = std::bit_width(meta.base - 1);
215
    static constexpr auto base_from_digits = string::make_from_digit_map<meta.base>();
216

217
    std::vector<uintptr_t> vec;
218

219
    last--;
220
    while (last >= first) {
221
      uintptr_t work = 0;
222
      size_t bits = 0;
223
      for (; (bits < bitsof<uintptr_t>()) && (last >= first); last--) {
224
        char c = *last;
225
        // TODO: This should be a policy
226
        if (c >= base_from_digits.size()) {
227
          continue;
228
        }
229
        auto digit = base_from_digits[c];
230
        // TODO: This should be a policy
231
        if (~0 == digit) {
232
          continue;
233
        }
234
        work |= (digit << bits);
235
        bits += base_bits;
236
      }
237
      if (bits) {
238
        vec.push_back(work);
239
      }
240
    }
241
    return vec;
242
  } else {
243
    //from_string base 10 not implemented yet;
244
    return {};
245
  }
246
}
247

248
template <string::metadata_t meta = string::typical(), typename RandomAccessIt, typename Policy = policy::typical<typename RandomAccessIt::value_type>>
249
constexpr void from_string(
250
    const std::string& str,
251
    const bit_iterator<RandomAccessIt>& bit_first, const bit_iterator<RandomAccessIt>& bit_last) {
252
  from_string<meta, RandomAccessIt, Policy>(str.c_str(), str.c_str() + str.length(), bit_first, bit_last);
253
}
254

255
template <string::metadata_t meta = string::typical(), bit_range RangeT, typename Policy = policy::typical<typename std::ranges::iterator_t<RangeT>::value_type>>
256
constexpr void from_string(
3✔
257
    const std::string& str,
258
    RangeT& bits) {
3✔
259
  using range_iterator_t = std::ranges::iterator_t<RangeT>;
3✔
260
  using RandomAccessIt = typename range_iterator_t::iterator_type;
3✔
261
  from_string<meta, RandomAccessIt, Policy>(str.c_str(), str.c_str() + str.length(), bits.begin(), bits.end());
6✔
262
}
6✔
263

264
}  // namespace bit
265

266
#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