Skip to content

Instantly share code, notes, and snippets.

@d7samurai
Last active April 10, 2024 04:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d7samurai/1d778693ba33bbd2b9d709b209cc0aba to your computer and use it in GitHub Desktop.
Save d7samurai/1d778693ba33bbd2b9d709b209cc0aba to your computer and use it in GitHub Desktop.
immediate mode string concatenation
#include <Windows.h> // for OutputDebugString, max
///////////////////////////////////////////////////////////////////////////////////////////////
#define CONCAT_BUFFER_SIZE 256
///////////////////////////////////////////////////////////////////////////////////////////////
struct concat
{
concat(char* string) : length(0) { operator()(string); }
concat(int number) : length(0) { operator()(number); }
concat(float number, int decimals = 2) : length(0) { operator()(number, decimals); }
///////////////////////////////////////////////////////////////////////////////////////////
char buffer[CONCAT_BUFFER_SIZE];
int length;
///////////////////////////////////////////////////////////////////////////////////////////
concat& operator()(char* string)
{
while (buffer[length++] = *string++);
length--;
return *this;
}
concat& operator()(int number)
{
int neg = number < 0;
int num = neg ? -number : number;
length += log10(num) + neg;
int idx = length;
buffer[idx--] = 0;
do {
buffer[idx--] = '0' + (num % 10);
num /= 10;
} while (num);
if (neg) buffer[idx] = '-';
return *this;
}
concat& operator()(float number, int decimals = 2)
{
static double mul[] = { 1, 10, 100, 1000, 10000, 100000, 1000000 };
int neg = number < 0;
unsigned long long num = (unsigned long long)(mul[decimals] * (neg ? -number : number) + 0.5);
length += max(log10(num), decimals + 1) + neg + (decimals > 0);
int idx = length;
buffer[idx--] = 0;
do {
buffer[idx--] = '0' + (num % 10);
num /= 10;
if (idx == length - decimals - 1) buffer[idx--] = '.';
} while (num || length - idx <= (decimals ? decimals + 2 : 0));
if (neg) buffer[idx] = '-';
return *this;
}
operator char* ()
{
return buffer;
}
///////////////////////////////////////////////////////////////////////////////////////////
int log10(unsigned long long n)
{
if (n < 10000000000) return (n >= 1000000000) ? 10 : (n >= 100000000) ? 9 : (n >= 10000000) ? 8 : (n >= 1000000) ? 7 : (n >= 100000) ? 6 : (n >= 10000) ? 5 : (n >= 1000) ? 4 : (n >= 100) ? 3 : (n >= 10) ? 2 : 1;
else return (n >= 1000000000000000000) ? 19 : (n >= 100000000000000000) ? 18 : (n >= 10000000000000000) ? 17 : (n >= 1000000000000000) ? 16 : (n >= 100000000000000) ? 15 : (n >= 10000000000000) ? 14 : (n >= 1000000000000) ? 13 : (n >= 100000000000) ? 12 : 11;
}
};
///////////////////////////////////////////////////////////////////////////////////////////////
void main()
{
char* name = "slim shady";
int line = 1337;
float temp = -98.567f;
// attention, slim shady! there's an error in line 1337 : the error code is 666 and the temperature is -98.6 degrees
OutputDebugStringA(concat("attention, ")(name)("! there's an error in line ")(line)(" : the error code is ")(666)(" and the temperature is ")(temp, 1)(" degrees\n"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment