diff --git a/GeneralsMD/Code/CompatLib/Source/string_compat.cpp b/GeneralsMD/Code/CompatLib/Source/string_compat.cpp index ab4f49ae321..e73ed921597 100644 --- a/GeneralsMD/Code/CompatLib/Source/string_compat.cpp +++ b/GeneralsMD/Code/CompatLib/Source/string_compat.cpp @@ -1,16 +1,44 @@ #include "string_compat.h" -#include -#include -#include +#include +#include +#include char* itoa(int value, char* str, int base) { - // Create stringbuf from str - std::stringbuf buf; - buf.pubsetbuf(str, 33); - std::ostream os(&buf); - os << value << '\0'; + if (base < 2 || base > 36) + { + str[0] = '\0'; + return str; + } + + static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; + const bool is_negative = value < 0 && base == 10; + unsigned int magnitude = static_cast(value); + if (is_negative) + { + magnitude = 0U - magnitude; + } + + char* output = str; + do + { + *output++ = digits[magnitude % static_cast(base)]; + magnitude /= static_cast(base); + } while (magnitude != 0); + + if (is_negative) + { + *output++ = '-'; + } + *output = '\0'; + + for (char* left = str, *right = output - 1; left < right; ++left, --right) + { + const char temp = *left; + *left = *right; + *right = temp; + } return str; }