Is it possible to convert a char* to uppercase without traversing character by character in a loop ?
Assumption:
1. Char pointer points to fixed size string array.
2. The array pointed to contains only lowercase characters
Is it possible to convert a char* to uppercase without traversing character by character in a loop ?
Assumption:
1. Char pointer points to fixed size string array.
2. The array pointed to contains only lowercase characters
In the ASCII encoding, converting lowercase to uppercase amounts to setting the bit of weight 32 (i.e. 20H, the space character).
With a bitwise operator,
Char|= 0x20;
You can process several characters at a time by mapping longer data types on the array. For instance, to convert an array of 11 characters,
int ToUpper= 0x20202020;
*(int*) &Char[0]|= ToUpper;
*(int*) &Char[4]|= ToUpper;
*(short*)&Char[8]|= ToUpper;
Char[10]|= ToUpper;
You can go to 64 bit ints and even larger (up to 512 bits = 64 characters at a time) with the SIMD intrinsics (SSE, AVX).
If your code allows it, it is better to extend the buffer length to the next larger data type so that all bytes can be updated in a single operation. But don't forget to restore the terminating null.