From ccf308d0221f41418a8d4c80217cb4ddd39962b6 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 14 Mar 2018 00:20:04 -0500 Subject: [PATCH] Fix issues with int to string conversion in ox string operations --- deps/ox/src/ox/std/string.hpp | 11 +++++++- deps/ox/src/ox/std/strops.cpp | 47 ++++++++++++++++++++--------------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/deps/ox/src/ox/std/string.hpp b/deps/ox/src/ox/std/string.hpp index a5a8b729..fd2d5951 100644 --- a/deps/ox/src/ox/std/string.hpp +++ b/deps/ox/src/ox/std/string.hpp @@ -35,6 +35,8 @@ class BString { const BString &operator+=(char *str); + const BString &operator+=(int64_t i); + bool operator==(const BString &other); char *data(); @@ -106,7 +108,14 @@ const BString &BString::operator+=(const char *str) { template const BString &BString::operator+=(char *str) { - return *this = (const char*) str; + return *this += (const char*) str; +} + +template +const BString &BString::operator+=(int64_t i) { + char str[65]; + ox_itoa(i, str); + return this->operator+=(str); } template diff --git a/deps/ox/src/ox/std/strops.cpp b/deps/ox/src/ox/std/strops.cpp index 9c2c4b9e..7675a1d0 100644 --- a/deps/ox/src/ox/std/strops.cpp +++ b/deps/ox/src/ox/std/strops.cpp @@ -81,28 +81,35 @@ int ox_atoi(const char *str) { } char *ox_itoa(int64_t v, char *str) { - auto mod = 1000000000000000000; - constexpr auto base = 10; - auto it = 0; - if (v < 0) { - str[it] = '-'; - it++; - } - while (mod) { - auto digit = v / mod; - v %= mod; - mod /= base; - if (it or digit) { - int start; - if (digit < 10) { - start = '0'; - } else { - start = 'a'; - digit -= 10; - } - str[it] = start + digit; + if (v) { + auto mod = 1000000000000000000; + constexpr auto base = 10; + auto it = 0; + if (v < 0) { + str[it] = '-'; it++; } + while (mod) { + auto digit = v / mod; + v %= mod; + mod /= base; + if (it or digit) { + int start; + if (digit < 10) { + start = '0'; + } else { + start = 'a'; + digit -= 10; + } + str[it] = start + digit; + it++; + } + } + str[it] = 0; + } else { + // 0 is a special case + str[0] = '0'; + str[1] = 0; } return str; }