Hermes
0.7.0
Linux game streaming host
Toggle main menu visibility
Loading...
Searching...
No Matches
zwpad.h
1
// zero_width_pad.hpp
2
#pragma once
3
#include <string>
4
#include <bit>
// std::bit_width – C++20
5
#include <stdexcept>
6
7
namespace
zwpad
8
{
9
// Two distinct zero-width characters.
10
// U+200B ZERO WIDTH SPACE – “0”
11
// U+200C ZERO WIDTH NON-JOINER – “1”
12
inline
constexpr
char8_t
ZW0[] = u8
"\u200B"
;
13
inline
constexpr
char8_t
ZW1[] = u8
"\u200C"
;
14
29
inline
std::string
30
pad_for_ordering(std::string_view text,
31
std::size_t padBits,
32
std::size_t index)
33
{
34
if
(padBits == 0)
35
throw
std::invalid_argument(
"padBits must be > 0"
);
36
if
(index >= (std::size_t{1} << padBits))
37
throw
std::out_of_range(
"index does not fit into padBits"
);
38
39
std::string out;
40
out.reserve(padBits * 3 + text.size());
// each ZW char is 3-byte UTF-8
41
42
for
(std::size_t bit = 0; bit < padBits; ++bit)
43
{
44
// Emit the *most* significant bit first.
45
const
bool
one = (index >> (padBits - 1 - bit)) & 1;
46
out += one ?
reinterpret_cast<
const
char
*
>
(ZW1)
47
: reinterpret_cast<const char*>(ZW0);
48
}
49
out.append(text);
50
return
out;
51
}
52
54
[[nodiscard]]
55
inline
std::size_t pad_width_for_count(std::size_t count)
56
{
57
if
(count == 0)
58
throw
std::invalid_argument(
"count must be > 0"
);
59
return
std::bit_width(count - 1);
// e.g. count==8 → 3 bits
60
}
61
}
// namespace zwpad
src
zwpad.h
Generated by
1.18.0