Hello,
I have some C++ code which context I want to re-create in Nim. Briefly, there
is buffer of bytes. As it's just a pointer, I can easily designate an offset
(current). Do not care about unpack_int, it's just a function combining some
bytes from the buffer into an integer.
**The questions is how to make this buffer concept in Nim to be safe and
efficient?**
I wanted to use slices, but they holds an immutuable sequence.
const unsigned char* current, start, end;
void reset(const void* data, int size) {
start = (const unsigned char*) data;
end = start + size;
current = start;
}
int get_int() {
int i;
current = unpack_int(current, &i);
return i;
}
int unpack_int(const unsigned char *src, int *out) {
int sign = (*src >> 6) & 1;
*out = *src & 0x3F;
do {
if(!(*src & 0x80)) break;
src++;
*out |= (*src & (0x7F)) << (6);
if(!(*src & 0x80)) break;
src++;
*out |= (*src & (0x7F)) << (6+7);
if(!(*src & 0x80)) break;
src++;
*out |= (*src & (0x7F)) << (6+7+7);
if(!(*src & 0x80)) break;
src++;
*out |= (*src & (0x7F)) << (6+7+7+7);
} while(0);
src++;
*out ^= -sign; // if (sign) *i = ~(*i)
return src;
}