diff --git a/src/serialization.cpp b/src/serialization.cpp index a79363e..bf3e43d 100644 --- a/src/serialization.cpp +++ b/src/serialization.cpp @@ -2,6 +2,31 @@ #include "connection.h" #include "discord-rpc.h" +template +void NumberToString(char* dest, T number) +{ + if (!number) { + *dest++ = '0'; + *dest++ = 0; + return; + } + if (number < 0) { + *dest++ = '-'; + number = -number; + } + char temp[32]; + int place = 0; + while (number) { + auto digit = number % 10; + number = number / 10; + temp[place++] = '0' + (char)digit; + } + for (--place; place >= 0; --place) { + *dest++ = temp[place]; + } + *dest = 0; +} + // it's ever so slightly faster to not have to strlen the key template void WriteKey(JsonWriter& w, T& k) @@ -50,8 +75,8 @@ void WriteOptionalString(JsonWriter& w, T& k, const char* value) void JsonWriteNonce(JsonWriter& writer, int nonce) { WriteKey(writer, "nonce"); - char nonceBuffer[32]{}; - rapidjson::internal::i32toa(nonce, nonceBuffer); + char nonceBuffer[32]; + NumberToString(nonceBuffer, nonce); writer.String(nonceBuffer); } diff --git a/src/serialization.h b/src/serialization.h index f961d3c..4985c69 100644 --- a/src/serialization.h +++ b/src/serialization.h @@ -5,7 +5,6 @@ #include "rapidjson/document.h" #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" -#include "rapidjson/internal/itoa.h" // if only there was a standard library function for this template @@ -35,8 +34,7 @@ size_t JsonWriteRichPresenceObj(char* dest, size_t JsonWriteSubscribeCommand(char* dest, size_t maxLen, int nonce, const char* evtName); // I want to use as few allocations as I can get away with, and to do that with RapidJson, you need -// to supply some of -// your own allocators for stuff rather than use the defaults +// to supply some of your own allocators for stuff rather than use the defaults class LinearAllocator { public: