Here's a C++ function that implements the algorithm described in your pseudocode:
cpp
#include <vector>
#include <cstdint>
std::vector<uint8_t> processVector(const std::vector<uint8_t>& inp) {
static size_t count = 0;
std::vector<uint8_t> output;
for (uint8_t current_byte : inp) {
if (count == 0) {
if ((current_byte & 0x80) == 0x80) {
// Extract count from lower 7 bits
count = current_byte & 0x7F;
// Skip this byte (continue)
} else {
// Output the byte
output.push_back(current_byte);
}
} else {
// Decrement count and output the byte
--count;
output.push_back(current_byte);
}
}
return output;
}
This function:
Takes a const reference to the input vector (since we're not modifying it)
Uses a static variable count to maintain state between calls
Iterates through each byte in the input vector
When count is 0 and a byte has its MSB set (0x80), it extracts the count from the lower 7 bits and skips that byte
Otherwise, it outputs bytes directly (either when count is 0 and MSB is not set, or when count > 0)
Note: The use of a static variable means this function maintains state between calls. If you need a stateless version or want to process multiple streams independently, you might want to encapsulate this in a class:
This way, each instance of ByteProcessor maintains its own state, allowing you to process multiple streams independently.
User
please remove these annoyinng comments from code and reduce overal text bloat you are generating. This noise creates too much cognitive load. Be laconic.