Right now, I'm working on a project which requires sequential text key generation. I need to seed the key generator with an integer corresponding to a certain key, which the constructor converts to a key.
My key generator overloads the increment operators so that the string is incremented directly, rather than what I had previously been doing, which is incrementing an index value, then converting the index to a key for every key that I wanted to generate.
My problem is that I have a limited character set I want to use when generating keys. I have to find the character in the key that I want to increment, find out where it is in my character set, find the next character in the set, then replace the character in the key with the next character in the set.
Here is my code:
// Not the full charset
std::string charset = "abcdefghijklmnopqrstuvwxyz0123456789";
std::string key;
key.push_back(charset[0]);
for(unsigned int place = 0; place < key.length(); place++)
{
if(key[place] == charset[charset.length() - 1])
{
// Overflow, reset char at place
key[place] = charset[0];
if((key.length() - 1) < (place + 1))
{
// Carry, no space, insert char
key.insert(key.begin(), charset[0]);
break;
}
else
{
// Space available, increment next char
continue;
}
}
else
{
// Increment char at place
key[place] = charset[charset.find(key[place]) + 1];
break;
}
}
In profiling, I found that the search operation is really slowing things down. Is there any faster way of doing this? I thought of creating a linked list out of the character set, but before I do that, I'd like some input on this.