C library functions may be optimized and significantly faster than a hand-coded iteration.
char* uuid; // = ...
// size_t uuid_len; // = ... length of uuid
char* ptr = strpbrk(uuid, "-24");
while (ptr)
{
switch(*ptr)
{
case '-':
*ptr = '_';
break;
case '2':
*ptr = 'f';
break;
case '4':
*ptr = 'x';
break;
}
// if (ptr-uuid == uuid_len) break;
ptr = strpbrk(ptr+1, "-24");
}
Edit: Took out the range checking, on the basis of the example here that doesn't seem necessary.
Edit: So I decided to test out the 3 algorithms here to see which is faster. I had a loop of 100000 strings, on a vintage 2006 Mac Pro, compiling with gcc, -O3. I took the average of 1000 runs and did 5 cycles.
AND THE WINNER IS...
@johnchen by a hair with an average time of 7.85ms.
@YongweiWu just behind with an average time with 7.89ms. The difference looks significant; going in and doing a proper statistical test is not going to happen tonight unfortunately. :)
...and strpbrk
a distant third at 32ms. (Glad I qualified all my optimization claims with 'might', 'may' etc....)
Edit: There is a big difference with Clang--j @ WY's algorithms take 10ms under Clang (looks tied between them), mine is unchanged.