Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I iterate over a string in 128-bit chunks?

I have a function that operates on 128-bit blocks of data from an arbitrary-length string. If the string is not evenly divisible into chunks of 128 bits, it will be padded accordingly.

The purpose is to transform the data in the string that is fed into the function.

I initially thought of looping through the string a such:

//This might have stupid errors.  Hopefully it stillg gets the point across.
for (int i = 0; i < strn.size(); i += 16)
{
    string block = strn.substr(i, i + 15);
    strn.replace(i, i + 15, block);
}

I guess this would work, but I'm thinking there must be a more elegant way to do this. One idea that came to mind was to encapsulate strn in a class and implement my own iterator that can read its contents in 128-bit chunks. This is appealing because the constructor could handle padding and some of the functions I currently use could be made private, thus avoiding potential misuse. Does this seem like a fruitful approach? If so, how does one go about implementing his own iterator? Detailed explanations are most welcome, as I'm very inexperienced with C++.

Are there other, perhaps better, approaches?

Thanks!

like image 735
Louis Thibault Avatar asked Nov 06 '12 16:11

Louis Thibault


People also ask

Can you iterate through an integer?

Iterables in Python are objects and containers that could be stepped through one item at a time, usually using a for ... in loop. Not all objects can be iterated, for example - we cannot iterate an integer, it is a singular value.


2 Answers

There are many approaches. One of the more simple and straightforward ones would be like this: you can create your own type sized exactly 128 bit; simple struct will do the job.

typedef struct
{
    char buf[16];
} _128bit;

And use it to iterate over your string. Cast the beginning of your string to this struct type:_128bit* start = (_128bit*)buffer; and start iterating with it, using the integral pointer arithmetic. All operations on start will operate in terms of its size. E.g. ,start++ will move 128 bits forward; start-- will move 128 bit back. Once you're at the desired position, recast it to the desired type and perform your manipulations.

like image 199
SomeWittyUsername Avatar answered Sep 22 '22 14:09

SomeWittyUsername


I would probably do this with iterators rather than indexing, still using your own for loop:

const int NUM_BITS_IN_CHUNK = 128;
const int CHUNK_SIZE = NUM_BITS_IN_CHUNK / CHAR_BIT;

for(std::string::const_iterator iter = str.begin(); iter < str.end(); iter += CHUNK_SIZE)
{
    your_func(iter, iter + CHUNK_SIZE);
}

The boost::iterator_adaptor code would look something like this. Note that I'm only getting four bytes per chunk instead of 16 for simplicity in the example.:

#include <iostream>
#include <string>

#include <boost/iterator_adaptors.hpp>

struct string_chunk_iterator : public boost::iterator_adaptor<string_chunk_iterator, std::string::const_iterator>
{
    string_chunk_iterator(const std::string::const_iterator& base) : iterator_adaptor(base) { }

private:
    friend class boost::iterator_core_access;
    void increment() { this->base_reference() = this->base() + 4; }
    void advance(typename iterator_adaptor::difference_type n)
    {
        this->base_reference() = this->base() + (4 * n);
    }
};

int main()
{
    const std::string tester(20, 'A');

    string_chunk_iterator iter(tester.begin());
    string_chunk_iterator str_end(tester.end());

    for(; iter != str_end; ++iter)
    {
        std::string chunk(&*iter, &*(iter + 1));

        std::cout << chunk << std::endl;
    }

    return 0;
}
like image 22
Mark B Avatar answered Sep 19 '22 14:09

Mark B