Fix issues with int to string conversion in ox string operations

This commit is contained in:
Gary Talent 2018-03-14 00:20:04 -05:00
parent 13a394e07f
commit ccf308d022
2 changed files with 37 additions and 21 deletions

View File

@ -35,6 +35,8 @@ class BString {
const BString &operator+=(char *str); const BString &operator+=(char *str);
const BString &operator+=(int64_t i);
bool operator==(const BString &other); bool operator==(const BString &other);
char *data(); char *data();
@ -106,7 +108,14 @@ const BString<size> &BString<size>::operator+=(const char *str) {
template<size_t size> template<size_t size>
const BString<size> &BString<size>::operator+=(char *str) { const BString<size> &BString<size>::operator+=(char *str) {
return *this = (const char*) str; return *this += (const char*) str;
}
template<size_t size>
const BString<size> &BString<size>::operator+=(int64_t i) {
char str[65];
ox_itoa(i, str);
return this->operator+=(str);
} }
template<size_t buffLen> template<size_t buffLen>

View File

@ -81,28 +81,35 @@ int ox_atoi(const char *str) {
} }
char *ox_itoa(int64_t v, char *str) { char *ox_itoa(int64_t v, char *str) {
auto mod = 1000000000000000000; if (v) {
constexpr auto base = 10; auto mod = 1000000000000000000;
auto it = 0; constexpr auto base = 10;
if (v < 0) { auto it = 0;
str[it] = '-'; if (v < 0) {
it++; str[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++; 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; return str;
} }